Skip to content

Commit 53127b2

Browse files
committed
fix registry grpc unit test intermittent failure
Signed-off-by: akihikokuroda <[email protected]>
1 parent 82fbb1a commit 53127b2

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

pkg/controller/registry/grpc/source_test.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ type FakeSourceSyncer struct {
5151
History map[registry.CatalogKey][]connectivity.State
5252

5353
sync.Mutex
54-
expectedEvents int
55-
done chan struct{}
54+
expectedReadies int
55+
done chan struct{}
5656
}
5757

5858
func (f *FakeSourceSyncer) sync(state SourceState) {
@@ -61,18 +61,20 @@ func (f *FakeSourceSyncer) sync(state SourceState) {
6161
f.History[state.Key] = []connectivity.State{}
6262
}
6363
f.History[state.Key] = append(f.History[state.Key], state.State)
64-
f.expectedEvents -= 1
65-
if f.expectedEvents == 0 {
64+
if state.State == connectivity.Ready {
65+
f.expectedReadies -= 1
66+
}
67+
if f.expectedReadies == 0 {
6668
f.done <- struct{}{}
6769
}
6870
f.Unlock()
6971
}
7072

71-
func NewFakeSourceSyncer(expectedEvents int) *FakeSourceSyncer {
73+
func NewFakeSourceSyncer(expectedReadies int) *FakeSourceSyncer {
7274
return &FakeSourceSyncer{
73-
History: map[registry.CatalogKey][]connectivity.State{},
74-
expectedEvents: expectedEvents,
75-
done: make(chan struct{}),
75+
History: map[registry.CatalogKey][]connectivity.State{},
76+
expectedReadies: expectedReadies,
77+
done: make(chan struct{}),
7678
}
7779
}
7880

@@ -85,21 +87,19 @@ func TestConnectionEvents(t *testing.T) {
8587
test := func(tt testcase) func(t *testing.T) {
8688
return func(t *testing.T) {
8789
// start server for each catalog
88-
totalEvents := 0
8990
addresses := map[registry.CatalogKey]string{}
9091

91-
for catalog, events := range tt.expectedHistory {
92-
totalEvents += len(events)
92+
for catalog, _ := range tt.expectedHistory {
9393
serve, address, stop := server(&fakes.FakeQuery{})
9494
addresses[catalog] = address
9595
go serve()
9696
defer stop()
9797
}
9898

9999
// start source manager
100-
syncer := NewFakeSourceSyncer(totalEvents)
100+
syncer := NewFakeSourceSyncer(len(tt.expectedHistory))
101101
sources := NewSourceStore(logrus.New(), 1*time.Second, 5*time.Second, syncer.sync)
102-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
102+
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
103103
defer cancel()
104104
sources.Start(ctx)
105105

@@ -116,7 +116,13 @@ func TestConnectionEvents(t *testing.T) {
116116
for catalog, events := range tt.expectedHistory {
117117
recordedEvents := syncer.History[catalog]
118118
for i := 0; i < len(recordedEvents); i++ {
119-
require.Equal(t, (events[i]).String(), (recordedEvents[i]).String())
119+
found := false
120+
for _, event := range events {
121+
if event.String() == recordedEvents[i].String() {
122+
found = true
123+
}
124+
}
125+
require.True(t, found)
120126
}
121127
}
122128
}

0 commit comments

Comments
 (0)