Skip to content

Commit d32054f

Browse files
Fix flaky agent LRU cache test (#3746)
Signed-off-by: Prasad Borole <[email protected]>
1 parent 6b8b9d1 commit d32054f

File tree

1 file changed

+42
-33
lines changed

1 file changed

+42
-33
lines changed

pkg/agent/manager/cache/lru_cache_test.go

+42-33
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ import (
88
"testing"
99
"time"
1010

11-
"github.com/andres-erbsen/clock"
1211
"github.com/sirupsen/logrus/hooks/test"
1312
"github.com/spiffe/go-spiffe/v2/spiffeid"
1413
"github.com/spiffe/spire/pkg/common/bundleutil"
1514
"github.com/spiffe/spire/pkg/common/telemetry"
1615
"github.com/spiffe/spire/proto/spire/common"
16+
"github.com/spiffe/spire/test/clock"
1717
"github.com/stretchr/testify/assert"
1818
"github.com/stretchr/testify/require"
1919
)
2020

2121
func TestLRUCacheFetchWorkloadUpdate(t *testing.T) {
22-
cache := newTestLRUCache()
22+
cache := newTestLRUCache(t)
2323
// populate the cache with FOO and BAR without SVIDS
2424
foo := makeRegistrationEntry("FOO", "A")
2525
bar := makeRegistrationEntry("BAR", "B")
@@ -50,7 +50,7 @@ func TestLRUCacheFetchWorkloadUpdate(t *testing.T) {
5050
}
5151

5252
func TestLRUCacheMatchingRegistrationIdentities(t *testing.T) {
53-
cache := newTestLRUCache()
53+
cache := newTestLRUCache(t)
5454

5555
// populate the cache with FOO and BAR without SVIDS
5656
foo := makeRegistrationEntry("FOO", "A")
@@ -79,7 +79,7 @@ func TestLRUCacheMatchingRegistrationIdentities(t *testing.T) {
7979
}
8080

8181
func TestLRUCacheCountSVIDs(t *testing.T) {
82-
cache := newTestLRUCache()
82+
cache := newTestLRUCache(t)
8383

8484
// populate the cache with FOO and BAR without SVIDS
8585
foo := makeRegistrationEntry("FOO", "A")
@@ -103,7 +103,7 @@ func TestLRUCacheCountSVIDs(t *testing.T) {
103103
}
104104

105105
func TestLRUCacheBundleChanges(t *testing.T) {
106-
cache := newTestLRUCache()
106+
cache := newTestLRUCache(t)
107107

108108
bundleStream := cache.SubscribeToBundleChanges()
109109
assert.Equal(t, makeBundles(bundleV1), bundleStream.Value())
@@ -127,7 +127,7 @@ func TestLRUCacheBundleChanges(t *testing.T) {
127127
}
128128

129129
func TestLRUCacheAllSubscribersNotifiedOnBundleChange(t *testing.T) {
130-
cache := newTestLRUCache()
130+
cache := newTestLRUCache(t)
131131

132132
// create some subscribers and assert they get the initial bundle
133133
subA := subscribeToWorkloadUpdates(t, cache, makeSelectors("A"))
@@ -147,7 +147,7 @@ func TestLRUCacheAllSubscribersNotifiedOnBundleChange(t *testing.T) {
147147
}
148148

149149
func TestLRUCacheSomeSubscribersNotifiedOnFederatedBundleChange(t *testing.T) {
150-
cache := newTestLRUCache()
150+
cache := newTestLRUCache(t)
151151

152152
// initialize the cache with an entry FOO that has a valid SVID and
153153
// selector "A"
@@ -221,7 +221,7 @@ func TestLRUCacheSomeSubscribersNotifiedOnFederatedBundleChange(t *testing.T) {
221221
}
222222

223223
func TestLRUCacheSubscribersGetEntriesWithSelectorSubsets(t *testing.T) {
224-
cache := newTestLRUCache()
224+
cache := newTestLRUCache(t)
225225

226226
// create subscribers for each combination of selectors
227227
subA := subscribeToWorkloadUpdates(t, cache, makeSelectors("A"))
@@ -270,7 +270,7 @@ func TestLRUCacheSubscribersGetEntriesWithSelectorSubsets(t *testing.T) {
270270
}
271271

272272
func TestLRUCacheSubscriberIsNotNotifiedIfNothingChanges(t *testing.T) {
273-
cache := newTestLRUCache()
273+
cache := newTestLRUCache(t)
274274

275275
foo := makeRegistrationEntry("FOO", "A")
276276
cache.UpdateEntries(&UpdateEntries{
@@ -296,7 +296,7 @@ func TestLRUCacheSubscriberIsNotNotifiedIfNothingChanges(t *testing.T) {
296296
}
297297

298298
func TestLRUCacheSubscriberNotifiedOnSVIDChanges(t *testing.T) {
299-
cache := newTestLRUCache()
299+
cache := newTestLRUCache(t)
300300

301301
foo := makeRegistrationEntry("FOO", "A")
302302
cache.UpdateEntries(&UpdateEntries{
@@ -323,7 +323,7 @@ func TestLRUCacheSubscriberNotifiedOnSVIDChanges(t *testing.T) {
323323
}
324324

325325
func TestLRUCacheSubscriberNotificationsOnSelectorChanges(t *testing.T) {
326-
cache := newTestLRUCache()
326+
cache := newTestLRUCache(t)
327327

328328
// initialize the cache with a FOO entry with selector A and an SVID
329329
foo := makeRegistrationEntry("FOO", "A")
@@ -374,7 +374,7 @@ func TestLRUCacheSubscriberNotificationsOnSelectorChanges(t *testing.T) {
374374
}
375375

376376
func TestLRUCacheSubscriberNotifiedWhenEntryDropped(t *testing.T) {
377-
cache := newTestLRUCache()
377+
cache := newTestLRUCache(t)
378378

379379
subA := subscribeToWorkloadUpdates(t, cache, makeSelectors("A"))
380380
defer subA.Finish()
@@ -417,7 +417,7 @@ func TestLRUCacheSubscriberNotifiedWhenEntryDropped(t *testing.T) {
417417
}
418418

419419
func TestLRUCacheSubscriberOnlyGetsEntriesWithSVID(t *testing.T) {
420-
cache := newTestLRUCache()
420+
cache := newTestLRUCache(t)
421421

422422
foo := makeRegistrationEntry("FOO", "A")
423423
updateEntries := &UpdateEntries{
@@ -441,7 +441,7 @@ func TestLRUCacheSubscriberOnlyGetsEntriesWithSVID(t *testing.T) {
441441
}
442442

443443
func TestLRUCacheSubscribersDoNotBlockNotifications(t *testing.T) {
444-
cache := newTestLRUCache()
444+
cache := newTestLRUCache(t)
445445

446446
sub := subscribeToWorkloadUpdates(t, cache, makeSelectors("A"))
447447
defer sub.Finish()
@@ -460,7 +460,7 @@ func TestLRUCacheSubscribersDoNotBlockNotifications(t *testing.T) {
460460
}
461461

462462
func TestLRUCacheCheckSVIDCallback(t *testing.T) {
463-
cache := newTestLRUCache()
463+
cache := newTestLRUCache(t)
464464

465465
// no calls because there are no registration entries
466466
cache.UpdateEntries(&UpdateEntries{
@@ -507,7 +507,7 @@ func TestLRUCacheCheckSVIDCallback(t *testing.T) {
507507
}
508508

509509
func TestLRUCacheGetStaleEntries(t *testing.T) {
510-
cache := newTestLRUCache()
510+
cache := newTestLRUCache(t)
511511

512512
bar := makeRegistrationEntryWithTTL("BAR", 130, 140, "B")
513513

@@ -561,7 +561,7 @@ func TestLRUCacheGetStaleEntries(t *testing.T) {
561561
}
562562

563563
func TestLRUCacheSubscriberNotNotifiedOnDifferentSVIDChanges(t *testing.T) {
564-
cache := newTestLRUCache()
564+
cache := newTestLRUCache(t)
565565

566566
foo := makeRegistrationEntry("FOO", "A")
567567
bar := makeRegistrationEntry("BAR", "B")
@@ -586,7 +586,7 @@ func TestLRUCacheSubscriberNotNotifiedOnDifferentSVIDChanges(t *testing.T) {
586586
}
587587

588588
func TestLRUCacheSubscriberNotNotifiedOnOverlappingSVIDChanges(t *testing.T) {
589-
cache := newTestLRUCache()
589+
cache := newTestLRUCache(t)
590590

591591
foo := makeRegistrationEntry("FOO", "A", "C")
592592
bar := makeRegistrationEntry("FOO", "A", "B")
@@ -611,7 +611,7 @@ func TestLRUCacheSubscriberNotNotifiedOnOverlappingSVIDChanges(t *testing.T) {
611611
}
612612

613613
func TestLRUCacheSVIDCacheExpiry(t *testing.T) {
614-
clk := clock.NewMock()
614+
clk := clock.NewMock(t)
615615
cache := newTestLRUCacheWithConfig(10, clk)
616616

617617
clk.Add(1 * time.Second)
@@ -696,7 +696,7 @@ func TestLRUCacheSVIDCacheExpiry(t *testing.T) {
696696
}
697697

698698
func TestLRUCacheMaxSVIDCacheSize(t *testing.T) {
699-
clk := clock.NewMock()
699+
clk := clock.NewMock(t)
700700
cache := newTestLRUCacheWithConfig(10, clk)
701701

702702
// create entries more than maxSvidCacheSize
@@ -730,7 +730,7 @@ func TestLRUCacheMaxSVIDCacheSize(t *testing.T) {
730730
}
731731

732732
func TestSyncSVIDsWithSubscribers(t *testing.T) {
733-
clk := clock.NewMock()
733+
clk := clock.NewMock(t)
734734
cache := newTestLRUCacheWithConfig(5, clk)
735735

736736
updateEntries := createUpdateEntries(5, makeBundles(bundleV1))
@@ -760,7 +760,7 @@ func TestSyncSVIDsWithSubscribers(t *testing.T) {
760760
}
761761

762762
func TestNotify(t *testing.T) {
763-
cache := newTestLRUCache()
763+
cache := newTestLRUCache(t)
764764

765765
foo := makeRegistrationEntry("FOO", "A")
766766
cache.UpdateEntries(&UpdateEntries{
@@ -776,7 +776,7 @@ func TestNotify(t *testing.T) {
776776
}
777777

778778
func TestSubscribeToLRUCacheChanges(t *testing.T) {
779-
clk := clock.NewMock()
779+
clk := clock.NewMock(t)
780780
cache := newTestLRUCacheWithConfig(1, clk)
781781

782782
foo := makeRegistrationEntry("FOO", "A")
@@ -836,27 +836,36 @@ func TestSubscribeToLRUCacheChanges(t *testing.T) {
836836
})
837837
assert.Equal(t, 2, cache.CountSVIDs())
838838

839-
clk.Add(SVIDSyncInterval * 2)
839+
clk.WaitForAfter(time.Second, "waiting for after to get called")
840+
clk.Add(SVIDSyncInterval * 4)
840841

841-
sub1Err := <-sub1ErrCh
842-
assert.NoError(t, sub1Err, "subscriber 1 error")
842+
select {
843+
case sub1Err := <-sub1ErrCh:
844+
assert.NoError(t, sub1Err, "subscriber 1 error")
845+
case <-time.After(10 * time.Second):
846+
require.FailNow(t, "timed out waiting for SVID")
847+
}
843848

844-
sub2Err := <-sub2ErrCh
845-
assert.NoError(t, sub2Err, "subscriber 2 error")
849+
select {
850+
case sub2Err := <-sub2ErrCh:
851+
assert.NoError(t, sub2Err, "subscriber 2 error")
852+
case <-time.After(10 * time.Second):
853+
require.FailNow(t, "timed out waiting for SVID")
854+
}
846855
}
847856

848857
func TestNewLRUCache(t *testing.T) {
849858
// negative value
850-
cache := newTestLRUCacheWithConfig(-5, clock.NewMock())
859+
cache := newTestLRUCacheWithConfig(-5, clock.NewMock(t))
851860
require.Equal(t, DefaultSVIDCacheMaxSize, cache.svidCacheMaxSize)
852861

853862
// zero value
854-
cache = newTestLRUCacheWithConfig(0, clock.NewMock())
863+
cache = newTestLRUCacheWithConfig(0, clock.NewMock(t))
855864
require.Equal(t, DefaultSVIDCacheMaxSize, cache.svidCacheMaxSize)
856865
}
857866

858867
func BenchmarkLRUCacheGlobalNotification(b *testing.B) {
859-
cache := newTestLRUCache()
868+
cache := newTestLRUCache(b)
860869

861870
const numEntries = 1000
862871
const numWorkloads = 1000
@@ -900,10 +909,10 @@ func BenchmarkLRUCacheGlobalNotification(b *testing.B) {
900909
}
901910
}
902911

903-
func newTestLRUCache() *LRUCache {
912+
func newTestLRUCache(t testing.TB) *LRUCache {
904913
log, _ := test.NewNullLogger()
905914
return NewLRUCache(log, spiffeid.RequireTrustDomainFromString("domain.test"), bundleV1,
906-
telemetry.Blackhole{}, 0, clock.NewMock())
915+
telemetry.Blackhole{}, 0, clock.NewMock(t))
907916
}
908917

909918
func newTestLRUCacheWithConfig(svidCacheMaxSize int, clk clock.Clock) *LRUCache {

0 commit comments

Comments
 (0)