Skip to content

Commit

Permalink
reduce sensitivity of fncache cancellation test
Browse files Browse the repository at this point in the history
  • Loading branch information
fspmarshall committed Jul 21, 2022
1 parent 50f2145 commit ab02676
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions lib/utils/fncache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,35 +157,46 @@ func testFnCacheSimple(t *testing.T, ttl time.Duration, delay time.Duration) {
func TestFnCacheCancellation(t *testing.T) {
t.Parallel()

const timeout = time.Millisecond * 10
const longTimeout = time.Second * 10 // should never be hit

cache, err := NewFnCache(FnCacheConfig{TTL: time.Minute})
require.NoError(t, err)

ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

// used to artificially block the load function
blocker := make(chan struct{})

// set up a context that we can cancel from within the load function to
// simulate a scenario where the calling context is canceled or times out.
// if we actually hit the timeout, that is a bug.
ctx, cancel := context.WithTimeout(context.Background(), longTimeout)
defer cancel()

v, err := cache.Get(ctx, "key", func(context.Context) (interface{}, error) {
cancel()
<-blocker
return "val", nil
})

require.Nil(t, v)
require.Equal(t, context.DeadlineExceeded, trace.Unwrap(err))
require.Equal(t, context.Canceled, trace.Unwrap(err), "context should have been canceled immediately")

// unblock the loading operation which is still in progress
close(blocker)

ctx, cancel = context.WithTimeout(context.Background(), timeout)
// since we unblocked the loadfn, we expect the next Get to return almost
// immediately. we still use a fairly long timeout just to ensure that failure
// is due to an actual bug and not due to resource constraints in the test env.
ctx, cancel = context.WithTimeout(context.Background(), longTimeout)
defer cancel()

loadFnWasRun := atomic.NewBool(false)
v, err = cache.Get(ctx, "key", func(context.Context) (interface{}, error) {
t.Fatal("this should never run!")
loadFnWasRun.Store(true)
return nil, nil
})

require.False(t, loadFnWasRun.Load(), "loadfn should not have been run")

require.NoError(t, err)
require.Equal(t, "val", v.(string))
}
Expand Down

0 comments on commit ab02676

Please sign in to comment.