2018-10-02 10:27:10 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-04-25 09:11:07 +00:00
|
|
|
"errors"
|
2018-10-02 10:27:10 +00:00
|
|
|
"fmt"
|
2023-01-03 16:11:56 +00:00
|
|
|
"reflect"
|
2018-10-02 10:27:10 +00:00
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-01-03 16:11:56 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
2018-10-02 10:27:10 +00:00
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-01-03 16:11:56 +00:00
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
2018-10-02 10:27:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Test that a type registered with a periodic refresh can be watched.
|
|
|
|
func TestCacheNotify(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
typ := TestType(t)
|
2020-04-14 22:29:30 +00:00
|
|
|
typ.On("RegisterOptions").Return(RegisterOptions{})
|
2018-10-02 10:27:10 +00:00
|
|
|
defer typ.AssertExpectations(t)
|
2020-07-28 16:24:30 +00:00
|
|
|
c := New(Options{})
|
2020-04-14 22:29:30 +00:00
|
|
|
c.RegisterType("t", typ)
|
2018-10-02 10:27:10 +00:00
|
|
|
|
|
|
|
// Setup triggers to control when "updates" should be delivered
|
2019-04-25 09:11:07 +00:00
|
|
|
trigger := make([]chan time.Time, 5)
|
2018-10-02 10:27:10 +00:00
|
|
|
for i := range trigger {
|
|
|
|
trigger[i] = make(chan time.Time)
|
|
|
|
}
|
|
|
|
|
2019-04-25 09:11:07 +00:00
|
|
|
// Send an error to fake a situation where the servers aren't reachable
|
|
|
|
// initially.
|
|
|
|
typ.Static(FetchResult{Value: nil, Index: 0}, errors.New("no servers available")).Once()
|
|
|
|
|
2019-09-26 15:42:17 +00:00
|
|
|
// Configure the type. The first time we use the fake index of "1" to verify we
|
|
|
|
// don't regress on https://github.com/hashicorp/consul/issues/6521 .
|
|
|
|
typ.Static(FetchResult{Value: 1, Index: 1}, nil).Once().Run(func(args mock.Arguments) {
|
2018-10-02 10:27:10 +00:00
|
|
|
// Assert the right request type - all real Fetch implementations do this so
|
|
|
|
// it keeps us honest that Watch doesn't require type mangling which will
|
|
|
|
// break in real life (hint: it did on the first attempt)
|
|
|
|
_, ok := args.Get(1).(*MockRequest)
|
|
|
|
require.True(t, ok)
|
2019-04-25 09:11:07 +00:00
|
|
|
}).WaitUntil(trigger[0])
|
2018-10-02 10:27:10 +00:00
|
|
|
typ.Static(FetchResult{Value: 12, Index: 5}, nil).Once().WaitUntil(trigger[1])
|
2019-04-25 09:11:07 +00:00
|
|
|
typ.Static(FetchResult{Value: 12, Index: 5}, nil).Once().WaitUntil(trigger[2])
|
|
|
|
typ.Static(FetchResult{Value: 42, Index: 7}, nil).Once().WaitUntil(trigger[3])
|
2018-10-02 10:27:10 +00:00
|
|
|
// It's timing dependent whether the blocking loop manages to make another
|
|
|
|
// call before we cancel so don't require it. We need to have a higher index
|
|
|
|
// here because if the index is the same then the cache Get will not return
|
|
|
|
// until the full 10 min timeout expires. This causes the last fetch to return
|
|
|
|
// after cancellation as if it had timed out.
|
2019-04-25 09:11:07 +00:00
|
|
|
typ.Static(FetchResult{Value: 42, Index: 8}, nil).WaitUntil(trigger[4])
|
2018-10-02 10:27:10 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
ch := make(chan UpdateEvent)
|
|
|
|
|
|
|
|
err := c.Notify(ctx, "t", TestRequest(t, RequestInfo{Key: "hello"}), "test", ch)
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NoError(t, err)
|
2018-10-02 10:27:10 +00:00
|
|
|
|
2019-04-25 09:11:07 +00:00
|
|
|
// Should receive the error with index == 0 first.
|
2018-10-02 10:27:10 +00:00
|
|
|
TestCacheNotifyChResult(t, ch, UpdateEvent{
|
|
|
|
CorrelationID: "test",
|
2019-04-25 09:11:07 +00:00
|
|
|
Result: nil,
|
|
|
|
Meta: ResultMeta{Hit: false, Index: 0},
|
|
|
|
Err: errors.New("no servers available"),
|
2018-10-02 10:27:10 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// There should be no more updates delivered yet
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.Len(t, ch, 0)
|
2018-10-02 10:27:10 +00:00
|
|
|
|
|
|
|
// Trigger blocking query to return a "change"
|
|
|
|
close(trigger[0])
|
|
|
|
|
2019-04-25 09:11:07 +00:00
|
|
|
// Should receive the first real update next.
|
|
|
|
TestCacheNotifyChResult(t, ch, UpdateEvent{
|
|
|
|
CorrelationID: "test",
|
|
|
|
Result: 1,
|
2019-09-26 15:42:17 +00:00
|
|
|
Meta: ResultMeta{Hit: false, Index: 1},
|
2019-04-25 09:11:07 +00:00
|
|
|
Err: nil,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Trigger blocking query to return a "change"
|
|
|
|
close(trigger[1])
|
|
|
|
|
2018-10-02 10:27:10 +00:00
|
|
|
// Should receive the next result pretty soon
|
|
|
|
TestCacheNotifyChResult(t, ch, UpdateEvent{
|
|
|
|
CorrelationID: "test",
|
|
|
|
Result: 12,
|
|
|
|
// Note these are never cache "hits" because blocking will wait until there
|
|
|
|
// is a new value at which point it's not considered a hit.
|
|
|
|
Meta: ResultMeta{Hit: false, Index: 5},
|
|
|
|
Err: nil,
|
|
|
|
})
|
|
|
|
|
2019-01-18 17:44:04 +00:00
|
|
|
// Register a second observer using same chan and request. Note that this is
|
2018-10-02 10:27:10 +00:00
|
|
|
// testing a few things implicitly:
|
|
|
|
// - that multiple watchers on the same cache entity are de-duped in their
|
|
|
|
// requests to the "backend"
|
|
|
|
// - that multiple watchers can distinguish their results using correlationID
|
|
|
|
err = c.Notify(ctx, "t", TestRequest(t, RequestInfo{Key: "hello"}), "test2", ch)
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NoError(t, err)
|
2018-10-02 10:27:10 +00:00
|
|
|
|
|
|
|
// Should get test2 notify immediately, and it should be a cache hit
|
|
|
|
TestCacheNotifyChResult(t, ch, UpdateEvent{
|
|
|
|
CorrelationID: "test2",
|
|
|
|
Result: 12,
|
|
|
|
Meta: ResultMeta{Hit: true, Index: 5},
|
|
|
|
Err: nil,
|
|
|
|
})
|
|
|
|
|
|
|
|
// We could wait for a full timeout but we can't directly observe it so
|
2019-03-06 17:13:28 +00:00
|
|
|
// simulate the behavior by triggering a response with the same value and
|
2018-10-02 10:27:10 +00:00
|
|
|
// index as the last one.
|
2019-04-25 09:11:07 +00:00
|
|
|
close(trigger[2])
|
2018-10-02 10:27:10 +00:00
|
|
|
|
|
|
|
// We should NOT be notified about that. Note this is timing dependent but
|
|
|
|
// it's only a sanity check, if we somehow _do_ get the change delivered later
|
|
|
|
// than 10ms the next value assertion will fail anyway.
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.Len(t, ch, 0)
|
2018-10-02 10:27:10 +00:00
|
|
|
|
|
|
|
// Trigger final update
|
2019-04-25 09:11:07 +00:00
|
|
|
close(trigger[3])
|
2018-10-02 10:27:10 +00:00
|
|
|
|
|
|
|
TestCacheNotifyChResult(t, ch, UpdateEvent{
|
|
|
|
CorrelationID: "test",
|
|
|
|
Result: 42,
|
|
|
|
Meta: ResultMeta{Hit: false, Index: 7},
|
|
|
|
Err: nil,
|
|
|
|
}, UpdateEvent{
|
|
|
|
CorrelationID: "test2",
|
|
|
|
Result: 42,
|
|
|
|
Meta: ResultMeta{Hit: false, Index: 7},
|
|
|
|
Err: nil,
|
|
|
|
})
|
|
|
|
|
2019-03-06 17:13:28 +00:00
|
|
|
// Sanity check closing chan before context is canceled doesn't panic
|
2018-10-02 10:27:10 +00:00
|
|
|
//close(ch)
|
|
|
|
|
|
|
|
// Close context
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
// It's likely but not certain that at least one of the watchers was blocked
|
|
|
|
// on the next cache Get so trigger that to timeout so we can observe the
|
|
|
|
// watch goroutines being cleaned up. This is necessary since currently we
|
|
|
|
// have no way to interrupt a blocking query. In practice it's fine to know
|
|
|
|
// that after 10 mins max the blocking query will return and the resources
|
|
|
|
// will be cleaned.
|
2019-04-25 09:11:07 +00:00
|
|
|
close(trigger[4])
|
2018-10-02 10:27:10 +00:00
|
|
|
|
2019-03-06 17:13:28 +00:00
|
|
|
// I want to test that canceling the context cleans up goroutines (which it
|
2018-10-02 10:27:10 +00:00
|
|
|
// does from manual verification with debugger etc). I had a check based on a
|
|
|
|
// similar approach to https://golang.org/src/net/http/main_test.go#L60 but it
|
|
|
|
// was just too flaky because it relies on the timing of the error backoff
|
|
|
|
// timer goroutines and similar so I've given up for now as I have more
|
|
|
|
// important things to get working.
|
|
|
|
}
|
|
|
|
|
2019-01-18 17:44:04 +00:00
|
|
|
func TestCacheNotifyPolling(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2019-01-18 17:44:04 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
typ := TestTypeNonBlocking(t)
|
|
|
|
defer typ.AssertExpectations(t)
|
2020-07-28 16:24:30 +00:00
|
|
|
c := New(Options{})
|
2020-04-14 22:29:30 +00:00
|
|
|
c.RegisterType("t", typ)
|
2019-01-18 17:44:04 +00:00
|
|
|
|
|
|
|
// Configure the type
|
|
|
|
typ.Static(FetchResult{Value: 1, Index: 1}, nil).Once().Run(func(args mock.Arguments) {
|
|
|
|
// Assert the right request type - all real Fetch implementations do this so
|
|
|
|
// it keeps us honest that Watch doesn't require type mangling which will
|
|
|
|
// break in real life (hint: it did on the first attempt)
|
|
|
|
_, ok := args.Get(1).(*MockRequest)
|
|
|
|
require.True(t, ok)
|
|
|
|
})
|
|
|
|
typ.Static(FetchResult{Value: 12, Index: 1}, nil).Once()
|
|
|
|
typ.Static(FetchResult{Value: 42, Index: 1}, nil).Once()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
ch := make(chan UpdateEvent)
|
|
|
|
|
|
|
|
err := c.Notify(ctx, "t", TestRequest(t, RequestInfo{Key: "hello", MaxAge: 100 * time.Millisecond}), "test", ch)
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NoError(t, err)
|
2019-01-18 17:44:04 +00:00
|
|
|
|
|
|
|
// Should receive the first result pretty soon
|
|
|
|
TestCacheNotifyChResult(t, ch, UpdateEvent{
|
|
|
|
CorrelationID: "test",
|
|
|
|
Result: 1,
|
|
|
|
Meta: ResultMeta{Hit: false, Index: 1},
|
|
|
|
Err: nil,
|
|
|
|
})
|
|
|
|
|
|
|
|
// There should be no more updates delivered yet
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.Len(t, ch, 0)
|
2019-01-18 17:44:04 +00:00
|
|
|
|
|
|
|
// make sure the updates do not come too quickly
|
|
|
|
select {
|
|
|
|
case <-time.After(50 * time.Millisecond):
|
|
|
|
case <-ch:
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.Fail(t, "Received update too early")
|
2019-01-18 17:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we get the update not too far out.
|
|
|
|
select {
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.Fail(t, "Didn't receive the notification")
|
2019-01-18 17:44:04 +00:00
|
|
|
case result := <-ch:
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.Equal(t, result.Result, 12)
|
|
|
|
require.Equal(t, result.CorrelationID, "test")
|
|
|
|
require.Equal(t, result.Meta.Hit, false)
|
|
|
|
require.Equal(t, result.Meta.Index, uint64(1))
|
2019-01-18 17:44:04 +00:00
|
|
|
// pretty conservative check it should be even newer because without a second
|
|
|
|
// notifier each value returned will have been executed just then and not served
|
|
|
|
// from the cache.
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.True(t, result.Meta.Age < 50*time.Millisecond)
|
|
|
|
require.NoError(t, result.Err)
|
2019-01-18 17:44:04 +00:00
|
|
|
}
|
|
|
|
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.Len(t, ch, 0)
|
2019-01-18 17:44:04 +00:00
|
|
|
|
|
|
|
// Register a second observer using same chan and request. Note that this is
|
|
|
|
// testing a few things implicitly:
|
|
|
|
// - that multiple watchers on the same cache entity are de-duped in their
|
|
|
|
// requests to the "backend"
|
|
|
|
// - that multiple watchers can distinguish their results using correlationID
|
|
|
|
err = c.Notify(ctx, "t", TestRequest(t, RequestInfo{Key: "hello", MaxAge: 100 * time.Millisecond}), "test2", ch)
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NoError(t, err)
|
2019-01-18 17:44:04 +00:00
|
|
|
|
|
|
|
// Should get test2 notify immediately, and it should be a cache hit
|
|
|
|
TestCacheNotifyChResult(t, ch, UpdateEvent{
|
|
|
|
CorrelationID: "test2",
|
|
|
|
Result: 12,
|
|
|
|
Meta: ResultMeta{Hit: true, Index: 1},
|
|
|
|
Err: nil,
|
|
|
|
})
|
|
|
|
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.Len(t, ch, 0)
|
2019-01-18 17:44:04 +00:00
|
|
|
|
|
|
|
// wait for the next batch of responses
|
|
|
|
events := make([]UpdateEvent, 0)
|
2019-07-12 15:52:26 +00:00
|
|
|
// At least 110ms is needed to allow for the jitter
|
2020-04-03 08:29:38 +00:00
|
|
|
timeout := time.After(220 * time.Millisecond)
|
2019-01-18 17:44:04 +00:00
|
|
|
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.Fail(t, "UpdateEvent not received in time")
|
2019-01-18 17:44:04 +00:00
|
|
|
case eve := <-ch:
|
|
|
|
events = append(events, eve)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.Equal(t, events[0].Result, 42)
|
|
|
|
require.Equal(t, events[0].Meta.Hit && events[1].Meta.Hit, false)
|
|
|
|
require.Equal(t, events[0].Meta.Index, uint64(1))
|
|
|
|
require.True(t, events[0].Meta.Age < 50*time.Millisecond)
|
|
|
|
require.NoError(t, events[0].Err)
|
|
|
|
require.Equal(t, events[1].Result, 42)
|
2019-01-18 17:44:04 +00:00
|
|
|
// Sometimes this would be a hit and others not. It all depends on when the various getWithIndex calls got fired.
|
|
|
|
// If both are done concurrently then it will not be a cache hit but the request gets single flighted and both
|
|
|
|
// get notified at the same time.
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
// require.Equal(t,events[1].Meta.Hit, true)
|
|
|
|
require.Equal(t, events[1].Meta.Index, uint64(1))
|
|
|
|
require.True(t, events[1].Meta.Age < 100*time.Millisecond)
|
|
|
|
require.NoError(t, events[1].Err)
|
2019-01-18 17:44:04 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 10:27:10 +00:00
|
|
|
// Test that a refresh performs a backoff.
|
|
|
|
func TestCacheWatch_ErrorBackoff(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2018-10-02 10:27:10 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
typ := TestType(t)
|
2020-04-14 22:29:30 +00:00
|
|
|
typ.On("RegisterOptions").Return(RegisterOptions{})
|
2018-10-02 10:27:10 +00:00
|
|
|
defer typ.AssertExpectations(t)
|
2020-07-28 16:24:30 +00:00
|
|
|
c := New(Options{})
|
2020-04-14 22:29:30 +00:00
|
|
|
c.RegisterType("t", typ)
|
2018-10-02 10:27:10 +00:00
|
|
|
|
|
|
|
// Configure the type
|
|
|
|
var retries uint32
|
|
|
|
fetchErr := fmt.Errorf("test fetch error")
|
|
|
|
typ.Static(FetchResult{Value: 1, Index: 4}, nil).Once()
|
|
|
|
typ.Static(FetchResult{Value: nil, Index: 5}, fetchErr).Run(func(args mock.Arguments) {
|
|
|
|
atomic.AddUint32(&retries, 1)
|
|
|
|
})
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
ch := make(chan UpdateEvent)
|
|
|
|
|
|
|
|
err := c.Notify(ctx, "t", TestRequest(t, RequestInfo{Key: "hello"}), "test", ch)
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NoError(t, err)
|
2018-10-02 10:27:10 +00:00
|
|
|
|
|
|
|
// Should receive the first result pretty soon
|
|
|
|
TestCacheNotifyChResult(t, ch, UpdateEvent{
|
|
|
|
CorrelationID: "test",
|
|
|
|
Result: 1,
|
|
|
|
Meta: ResultMeta{Hit: false, Index: 4},
|
|
|
|
Err: nil,
|
|
|
|
})
|
|
|
|
|
|
|
|
numErrors := 0
|
|
|
|
// Loop for a little while and count how many errors we see reported. If this
|
|
|
|
// was running as fast as it could go we'd expect this to be huge. We have to
|
|
|
|
// be a little careful here because the watch chan ch doesn't have a large
|
|
|
|
// buffer so we could be artificially slowing down the loop without the
|
2019-01-18 17:44:04 +00:00
|
|
|
// backoff actually taking effect. We can validate that by ensuring this test
|
2018-10-02 10:27:10 +00:00
|
|
|
// fails without the backoff code reliably.
|
|
|
|
timeoutC := time.After(500 * time.Millisecond)
|
|
|
|
OUT:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timeoutC:
|
|
|
|
break OUT
|
|
|
|
case u := <-ch:
|
|
|
|
numErrors++
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.Error(t, u.Err)
|
2018-10-02 10:27:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Must be fewer than 10 failures in that time
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.True(t, numErrors < 10, fmt.Sprintf("numErrors: %d", numErrors))
|
2018-10-02 10:27:10 +00:00
|
|
|
|
|
|
|
// Check the number of RPCs as a sanity check too
|
|
|
|
actual := atomic.LoadUint32(&retries)
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.True(t, actual < 10, fmt.Sprintf("actual: %d", actual))
|
2018-10-02 10:27:10 +00:00
|
|
|
}
|
2019-01-18 17:44:04 +00:00
|
|
|
|
|
|
|
// Test that a refresh performs a backoff.
|
|
|
|
func TestCacheWatch_ErrorBackoffNonBlocking(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2019-01-18 17:44:04 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
typ := TestTypeNonBlocking(t)
|
|
|
|
defer typ.AssertExpectations(t)
|
2020-07-28 16:24:30 +00:00
|
|
|
c := New(Options{})
|
2020-04-14 22:29:30 +00:00
|
|
|
c.RegisterType("t", typ)
|
2019-01-18 17:44:04 +00:00
|
|
|
|
|
|
|
// Configure the type
|
|
|
|
var retries uint32
|
|
|
|
fetchErr := fmt.Errorf("test fetch error")
|
|
|
|
typ.Static(FetchResult{Value: 1, Index: 4}, nil).Once()
|
|
|
|
typ.Static(FetchResult{Value: nil, Index: 5}, fetchErr).Run(func(args mock.Arguments) {
|
|
|
|
atomic.AddUint32(&retries, 1)
|
|
|
|
})
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
ch := make(chan UpdateEvent)
|
|
|
|
|
|
|
|
err := c.Notify(ctx, "t", TestRequest(t, RequestInfo{Key: "hello", MaxAge: 100 * time.Millisecond}), "test", ch)
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NoError(t, err)
|
2019-01-18 17:44:04 +00:00
|
|
|
|
|
|
|
// Should receive the first result pretty soon
|
|
|
|
TestCacheNotifyChResult(t, ch, UpdateEvent{
|
|
|
|
CorrelationID: "test",
|
|
|
|
Result: 1,
|
|
|
|
Meta: ResultMeta{Hit: false, Index: 4},
|
|
|
|
Err: nil,
|
|
|
|
})
|
|
|
|
|
|
|
|
numErrors := 0
|
|
|
|
// Loop for a little while and count how many errors we see reported. If this
|
|
|
|
// was running as fast as it could go we'd expect this to be huge. We have to
|
|
|
|
// be a little careful here because the watch chan ch doesn't have a large
|
|
|
|
// buffer so we could be artificially slowing down the loop without the
|
|
|
|
// backoff actually taking effect. We can validate that by ensuring this test
|
|
|
|
// fails without the backoff code reliably.
|
|
|
|
//
|
|
|
|
// 100 + 500 milliseconds. 100 because the first retry will not happen until
|
|
|
|
// the 100 + jitter milliseconds have elapsed.
|
|
|
|
timeoutC := time.After(600 * time.Millisecond)
|
|
|
|
OUT:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timeoutC:
|
|
|
|
break OUT
|
|
|
|
case u := <-ch:
|
|
|
|
numErrors++
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.Error(t, u.Err)
|
2019-01-18 17:44:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Must be fewer than 10 failures in that time
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.True(t, numErrors < 10, fmt.Sprintf("numErrors: %d", numErrors))
|
2019-01-18 17:44:04 +00:00
|
|
|
|
|
|
|
// Check the number of RPCs as a sanity check too
|
|
|
|
actual := atomic.LoadUint32(&retries)
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.True(t, actual < 10, fmt.Sprintf("actual: %d", actual))
|
2019-01-18 17:44:04 +00:00
|
|
|
}
|
2023-01-03 16:11:56 +00:00
|
|
|
|
|
|
|
func Test_isEqual(t *testing.T) {
|
|
|
|
s1 := time.Now()
|
|
|
|
s2 := s1
|
|
|
|
|
|
|
|
var pb1 proto.Message = timestamppb.New(s1)
|
|
|
|
var pb2 proto.Message = ×tamppb.Timestamp{}
|
|
|
|
|
|
|
|
// Marshal and unmarshal the proto object
|
|
|
|
b, err := proto.Marshal(pb1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, proto.Unmarshal(b, pb2))
|
|
|
|
|
|
|
|
// Sanity-check that protobuf comparisons fail reflect.DeepEqual
|
|
|
|
require.False(t, reflect.DeepEqual(pb1, pb2))
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
equal bool
|
|
|
|
v1 interface{}
|
|
|
|
v2 interface{}
|
|
|
|
}{
|
|
|
|
// Test protobuf message comparisons work.
|
|
|
|
{true, nil, nil},
|
|
|
|
{true, pb1, pb2},
|
|
|
|
{false, nil, pb2},
|
|
|
|
{false, pb1, nil},
|
|
|
|
{false, pb1, timestamppb.New(s1.Add(time.Second))},
|
|
|
|
// Test normal struct comparisons
|
|
|
|
{true, s1, s2},
|
|
|
|
{true, &s1, &s2},
|
|
|
|
{false, s1, nil},
|
|
|
|
{false, &s1, nil},
|
|
|
|
{false, nil, s2},
|
|
|
|
{false, nil, &s2},
|
|
|
|
{false, s1, s1.Add(time.Second)},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
require.Equal(t, tc.equal, isEqual(tc.v1, tc.v2))
|
|
|
|
}
|
|
|
|
}
|