(t *testing.T)
| 77 | } |
| 78 | |
| 79 | func TestEntriesIteratorWithConcurrentUpdate(t *testing.T) { |
| 80 | t.Parallel() |
| 81 | |
| 82 | // given |
| 83 | cache, _ := New(context.Background(), Config{ |
| 84 | Shards: 1, |
| 85 | LifeWindow: time.Second, |
| 86 | MaxEntriesInWindow: 1, |
| 87 | MaxEntrySize: 256, |
| 88 | }) |
| 89 | |
| 90 | cache.Set("key", []byte("value")) |
| 91 | |
| 92 | // when |
| 93 | iterator := cache.Iterator() |
| 94 | |
| 95 | // then |
| 96 | if !iterator.SetNext() { |
| 97 | t.Errorf("Iterator should contain at least single element") |
| 98 | } |
| 99 | |
| 100 | getOldestEntry := func(s *cacheShard) ([]byte, error) { |
| 101 | s.lock.RLock() |
| 102 | defer s.lock.RUnlock() |
| 103 | return s.entries.Peek() |
| 104 | } |
| 105 | |
| 106 | // Quite ugly but works |
| 107 | for i := 0; i < cache.config.Shards; i++ { |
| 108 | if oldestEntry, err := getOldestEntry(cache.shards[i]); err == nil { |
| 109 | cache.onEvict(oldestEntry, 10, cache.shards[i].removeOldestEntry) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | current, err := iterator.Value() |
| 114 | assertEqual(t, nil, err) |
| 115 | assertEqual(t, []byte("value"), current.Value()) |
| 116 | |
| 117 | next := iterator.SetNext() |
| 118 | assertEqual(t, false, next) |
| 119 | } |
| 120 | |
| 121 | func TestEntriesIteratorWithAllShardsEmpty(t *testing.T) { |
| 122 | t.Parallel() |
nothing calls this directly
no test coverage detected
searching dependent graphs…