Skip to content

Commit 15e677a

Browse files
committed
Remove timing assumtions from gossip test
Change-Id: Ifde82d756c4bc13cdfa97900ee05eb7d35966814 Signed-off-by: Artem Barger <[email protected]>
1 parent d221a6b commit 15e677a

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

gossip/gossip/gossip_test.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -314,15 +314,17 @@ func TestDissemination(t *testing.T) {
314314
n := 20
315315
msgsCount2Send := 10
316316
boot := newGossipInstance(0, 100)
317-
time.Sleep(time.Duration(2) * time.Second)
317+
318318
peers := make([]Gossip, n)
319319
receivedMessages := make([]int, n)
320320
wg := sync.WaitGroup{}
321+
321322
for i := 1; i <= n; i++ {
322323
pI := newGossipInstance(i, 100, 0)
323324
peers[i - 1] = pI
325+
326+
wg.Add(1)
324327
go func(index int, ch <-chan *proto.GossipMessage) {
325-
wg.Add(1)
326328
defer wg.Done()
327329
for j := 0; j < msgsCount2Send; j++ {
328330
<-ch
@@ -331,7 +333,7 @@ func TestDissemination(t *testing.T) {
331333
}(i - 1, pI.Accept(acceptData))
332334
}
333335

334-
time.Sleep(time.Duration(10) * time.Second)
336+
waitUntilOrFail(t, checkPeersMembership(peers, n))
335337

336338
for i := 1; i <= msgsCount2Send; i++ {
337339
boot.Gossip(createDataMsg(uint64(i), []byte{}, ""))
@@ -618,12 +620,22 @@ func waitUntilOrFailBlocking(t *testing.T, f func()) {
618620
assert.Fail(t, "Timeout expired!")
619621
}
620622

621-
622623
func searchInStackTrace(searchTerm string, stack []string) bool {
623624
for _, ste := range stack {
624625
if strings.Index(ste, searchTerm) != -1 {
625626
return true
626627
}
627628
}
628629
return false
629-
}
630+
}
631+
632+
func checkPeersMembership(peers []Gossip, n int) func() bool {
633+
return func() bool {
634+
for _, peer := range peers {
635+
if len(peer.GetPeers()) != n {
636+
return false
637+
}
638+
}
639+
return true
640+
}
641+
}

0 commit comments

Comments
 (0)