(t *testing.T)
| 35 | } |
| 36 | |
| 37 | func TestCacheTwo(t *testing.T) { |
| 38 | testResyncPeriod := time.Millisecond |
| 39 | rateLimiter := NewRateLimiter("mockLimiter", 100, 1) |
| 40 | |
| 41 | t.Run("normal operation", func(t *testing.T) { |
| 42 | // the size of the cache is at least as large as the number of items we're storing |
| 43 | cache, err := NewAutoRefreshCache(syncFakeItem, rateLimiter, testResyncPeriod, 10, nil) |
| 44 | assert.NoError(t, err) |
| 45 | |
| 46 | ctx, cancel := context.WithCancel(context.Background()) |
| 47 | cache.Start(ctx) |
| 48 | |
| 49 | // Create ten items in the cache |
| 50 | for i := 1; i <= 10; i++ { |
| 51 | _, err := cache.GetOrCreate(fakeCacheItem{ |
| 52 | id: fmt.Sprintf("%d", i), |
| 53 | val: 0, |
| 54 | }) |
| 55 | assert.NoError(t, err) |
| 56 | } |
| 57 | |
| 58 | // Wait half a second for all resync periods to complete |
| 59 | time.Sleep(500 * time.Millisecond) |
| 60 | for i := 1; i <= 10; i++ { |
| 61 | item := cache.Get(fmt.Sprintf("%d", i)) |
| 62 | assert.Equal(t, 10, item.(fakeCacheItem).val) |
| 63 | } |
| 64 | cancel() |
| 65 | }) |
| 66 | |
| 67 | t.Run("deleting objects from cache", func(t *testing.T) { |
| 68 | // the size of the cache is at least as large as the number of items we're storing |
| 69 | cache, err := NewAutoRefreshCache(syncFakeItemAlwaysDelete, rateLimiter, testResyncPeriod, 10, nil) |
| 70 | assert.NoError(t, err) |
| 71 | |
| 72 | ctx, cancel := context.WithCancel(context.Background()) |
| 73 | cache.Start(ctx) |
| 74 | |
| 75 | // Create ten items in the cache |
| 76 | for i := 1; i <= 10; i++ { |
| 77 | _, err = cache.GetOrCreate(fakeCacheItem{ |
| 78 | id: fmt.Sprintf("%d", i), |
| 79 | val: 0, |
| 80 | }) |
| 81 | assert.NoError(t, err) |
| 82 | } |
| 83 | |
| 84 | // Wait for all resync periods to complete |
| 85 | time.Sleep(50 * time.Millisecond) |
| 86 | for i := 1; i <= 10; i++ { |
| 87 | obj := cache.Get(fmt.Sprintf("%d", i)) |
| 88 | assert.Nil(t, obj) |
| 89 | } |
| 90 | cancel() |
| 91 | }) |
| 92 | } |
nothing calls this directly
no test coverage detected