lab2 fix
This commit is contained in:
+63
-31
@@ -106,39 +106,11 @@ func TestDistributedDeduplicatesConcurrentMisses(t *testing.T) {
|
||||
require.Equal(t, int32(1), calls.Load(), "one instance must load once for concurrent misses")
|
||||
}
|
||||
|
||||
func TestDistributedTimeoutPolicyError(t *testing.T) {
|
||||
func TestDistributedFallsBackOnWaitTimeout(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
store := mocks.NewMockLockStore(t)
|
||||
dc := newDistributed(t, store, cache.DistributedConfig{
|
||||
WaitTimeout: 100 * time.Millisecond,
|
||||
TimeoutPolicy: cache.TimeoutError,
|
||||
})
|
||||
ctx := context.Background()
|
||||
|
||||
store.EXPECT().Get(mock.Anything, "key").Return(nil, cache.ErrCacheMiss).Maybe()
|
||||
store.EXPECT().AcquireLock(mock.Anything, "lock:key", mock.AnythingOfType("string"), mock.Anything).Return(false, nil).Once()
|
||||
store.EXPECT().LockHeld(mock.Anything, "lock:key").Return(true, nil).Maybe()
|
||||
|
||||
var calls atomic.Int32
|
||||
|
||||
_, err := dc.GetOrLoad(ctx, "key", time.Minute, func(context.Context) ([]byte, error) {
|
||||
calls.Add(1)
|
||||
|
||||
return []byte("value"), nil
|
||||
})
|
||||
require.ErrorIs(t, err, cache.ErrLoadInProgress)
|
||||
require.Zero(t, calls.Load(), "the error policy must not fall back to a direct load")
|
||||
}
|
||||
|
||||
func TestDistributedTimeoutPolicyLoad(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
store := mocks.NewMockLockStore(t)
|
||||
dc := newDistributed(t, store, cache.DistributedConfig{
|
||||
WaitTimeout: 100 * time.Millisecond,
|
||||
TimeoutPolicy: cache.TimeoutLoad,
|
||||
})
|
||||
dc := newDistributed(t, store, cache.DistributedConfig{WaitTimeout: 100 * time.Millisecond})
|
||||
ctx := context.Background()
|
||||
|
||||
store.EXPECT().Get(mock.Anything, "key").Return(nil, cache.ErrCacheMiss).Maybe()
|
||||
@@ -154,7 +126,7 @@ func TestDistributedTimeoutPolicyLoad(t *testing.T) {
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, []byte("fallback"), value)
|
||||
require.Equal(t, int32(1), calls.Load(), "the load policy must fall back to a direct load")
|
||||
require.Equal(t, int32(1), calls.Load(), "a wait timeout must fall back to a direct load")
|
||||
}
|
||||
|
||||
func TestDistributedSharedLoadSurvivesCallerCancellation(t *testing.T) {
|
||||
@@ -252,3 +224,63 @@ func TestDistributedRetriesWhenLockVanishes(t *testing.T) {
|
||||
require.Equal(t, []byte("recovered"), value)
|
||||
require.Equal(t, int32(1), calls.Load())
|
||||
}
|
||||
|
||||
func TestFillStoreCallsAreBoundedByDeadline(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
store := mocks.NewMockLockStore(t)
|
||||
dc := newDistributed(t, store, cache.DistributedConfig{})
|
||||
|
||||
var (
|
||||
mu sync.Mutex
|
||||
getBudgets []time.Duration
|
||||
setBudget time.Duration
|
||||
setHasDead bool
|
||||
)
|
||||
|
||||
store.EXPECT().Get(mock.Anything, "key").RunAndReturn(func(ctx context.Context, _ string) ([]byte, error) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
if deadline, ok := ctx.Deadline(); ok {
|
||||
getBudgets = append(getBudgets, time.Until(deadline))
|
||||
}
|
||||
|
||||
return nil, cache.ErrCacheMiss
|
||||
}).Twice()
|
||||
store.EXPECT().AcquireLock(mock.Anything, "lock:key", mock.AnythingOfType("string"), mock.Anything).Return(true, nil).Once()
|
||||
store.EXPECT().Set(mock.Anything, "key", mock.Anything, mock.Anything).
|
||||
RunAndReturn(func(ctx context.Context, _ string, _ []byte, _ time.Duration) error {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
deadline, ok := ctx.Deadline()
|
||||
setHasDead = ok
|
||||
if ok {
|
||||
setBudget = time.Until(deadline)
|
||||
}
|
||||
|
||||
return nil
|
||||
}).Once()
|
||||
store.EXPECT().ReleaseLock(mock.Anything, "lock:key", mock.AnythingOfType("string")).Return(true, nil).Once()
|
||||
|
||||
_, err := dc.GetOrLoad(context.Background(), "key", time.Minute, func(context.Context) ([]byte, error) {
|
||||
return []byte("value"), nil
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
require.True(t, setHasDead, "Set must receive a bounded context")
|
||||
require.LessOrEqual(t, setBudget, 300*time.Millisecond)
|
||||
|
||||
minGetBudget := time.Hour
|
||||
for _, budget := range getBudgets {
|
||||
if budget < minGetBudget {
|
||||
minGetBudget = budget
|
||||
}
|
||||
}
|
||||
|
||||
require.LessOrEqual(t, minGetBudget, 300*time.Millisecond, "fill's Get must receive a bounded context")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user