Skip to content

Commit

Permalink
Add unit test for panic scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
Sovietaced committed Aug 2, 2024
1 parent 6a58014 commit 960030a
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions flytestdlib/cache/auto_refresh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ func syncTerminalItem(_ context.Context, batch Batch) ([]ItemSyncResponse, error
panic("This should never be called")
}

type panickingSyncer struct {
callCount atomic.Int32
}

func (p *panickingSyncer) sync(_ context.Context, _ Batch) ([]ItemSyncResponse, error) {
p.callCount.Inc()
panic("testing")
}

func TestCacheFour(t *testing.T) {
testResyncPeriod := 10 * time.Millisecond
rateLimiter := workqueue.DefaultControllerRateLimiter()
Expand Down Expand Up @@ -172,6 +181,34 @@ func TestCacheFour(t *testing.T) {

cancel()
})

t.Run("Test panic on sync and shutdown", func(t *testing.T) {
syncer := &panickingSyncer{}
cache, err := NewAutoRefreshCache("fake3", syncer.sync, rateLimiter, testResyncPeriod, 10, 2, promutils.NewTestScope())
assert.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())
assert.NoError(t, cache.Start(ctx))

itemID := "dummy_id"
_, err = cache.GetOrCreate(itemID, fakeCacheItem{
val: 0,
})
assert.NoError(t, err)

// wait for all workers to run
assert.Eventually(t, func() bool {
return syncer.callCount.Load() == int32(10)
}, time.Second, time.Millisecond)

// wait some more time
time.Sleep(500 * time.Millisecond)

// all workers should have shut down.
assert.Equal(t, int32(10), syncer.callCount.Load())

cancel()
})
}

func TestQueueBuildUp(t *testing.T) {
Expand Down

0 comments on commit 960030a

Please sign in to comment.