Skip to content

Commit

Permalink
Fix failed tests using Gossip protocol in Github Actions (0xPolygon#116)
Browse files Browse the repository at this point in the history
* fix: add sleep before using gossip to build gossip network in test

* Add reference to comment for more information
  • Loading branch information
Kourin1996 authored Aug 5, 2021
1 parent 0bb9857 commit 24bd6ab
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions e2e/broadcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func TestBroadcast(t *testing.T) {
}
}

// wait until gossip protocol build mesh network (https://github.com/libp2p/specs/blob/master/pubsub/gossipsub/gossipsub-v1.0.md)
time.Sleep(time.Second * 2)

tx, err := signer.SignTx(&types.Transaction{
Nonce: 0,
From: senderAddr,
Expand Down
26 changes: 26 additions & 0 deletions network/gossip_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
package network

import (
"context"
"errors"
"testing"
"time"

testproto "github.com/0xPolygon/minimal/network/proto/test"
"github.com/stretchr/testify/assert"
)

func NumSubscribers(srv *Server, topic string) int {
return len(srv.ps.ListPeers(topic))
}

func WaitForSubscribers(ctx context.Context, srv *Server, topic string, expectedNumPeers int) error {
for {
if n := NumSubscribers(srv, topic); n >= expectedNumPeers {
return nil
}
select {
case <-ctx.Done():
return errors.New("canceled")
case <-time.After(100 * time.Millisecond):
continue
}
}

}

func TestGossip(t *testing.T) {
srv0 := CreateServer(t, nil)
srv1 := CreateServer(t, nil)
Expand All @@ -28,6 +49,11 @@ func TestGossip(t *testing.T) {
msgCh <- obj.(*testproto.AReq)
})

// wait until build mesh
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
WaitForSubscribers(ctx, srv0, topicName, 1)

// publish in topic0
assert.NoError(t, topic0.Publish(&testproto.AReq{Msg: "a"}))

Expand Down

0 comments on commit 24bd6ab

Please sign in to comment.