From 42b2c0bc6d51e6c781387248840b9dc477249cc5 Mon Sep 17 00:00:00 2001 From: Prem Chaitanya Prathi Date: Fri, 4 Aug 2023 16:37:45 +0530 Subject: [PATCH] fix: race condition in rendezvous test code --- Makefile | 5 ++++- waku/v2/rendezvous/rendezvous.go | 2 +- waku/v2/rendezvous/rendezvous_test.go | 6 ++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 3d2f1867..9f1ae11d 100644 --- a/Makefile +++ b/Makefile @@ -50,6 +50,9 @@ all: build deps: lint-install +build-with-race: + ${GOBIN} build -race -tags="${BUILD_TAGS}" $(BUILD_FLAGS) -o build/waku ./cmd/waku + build: ${GOBIN} build -tags="${BUILD_TAGS}" $(BUILD_FLAGS) -o build/waku ./cmd/waku @@ -70,7 +73,7 @@ lint: @golangci-lint --exclude=SA1019 run ./... --deadline=5m test: - ${GOBIN} test -timeout 300s ./waku/... -coverprofile=${GO_TEST_OUTFILE}.tmp + ${GOBIN} test -race -timeout 300s ./waku/... -coverprofile=${GO_TEST_OUTFILE}.tmp cat ${GO_TEST_OUTFILE}.tmp | grep -v ".pb.go" > ${GO_TEST_OUTFILE} ${GOBIN} tool cover -html=${GO_TEST_OUTFILE} -o ${GO_HTML_COV} diff --git a/waku/v2/rendezvous/rendezvous.go b/waku/v2/rendezvous/rendezvous.go index 606cc6a7..4d4550b3 100644 --- a/waku/v2/rendezvous/rendezvous.go +++ b/waku/v2/rendezvous/rendezvous.go @@ -91,7 +91,7 @@ func (r *Rendezvous) DiscoverShard(ctx context.Context, rp *RendezvousPoint, clu r.DiscoverWithNamespace(ctx, namespace, rp, numPeers) } -// DiscoverWithNamespace is uded to find a number of peers using a custom namespace (usually a pubsub topic) +// DiscoverWithNamespace is used to find a number of peers using a custom namespace (usually a pubsub topic) func (r *Rendezvous) DiscoverWithNamespace(ctx context.Context, namespace string, rp *RendezvousPoint, numPeers int) { rendezvousClient := rvs.NewRendezvousClient(r.host, rp.id) diff --git a/waku/v2/rendezvous/rendezvous_test.go b/waku/v2/rendezvous/rendezvous_test.go index 8310be3f..671d5a09 100644 --- a/waku/v2/rendezvous/rendezvous_test.go +++ b/waku/v2/rendezvous/rendezvous_test.go @@ -5,6 +5,7 @@ import ( "crypto/rand" "database/sql" "fmt" + "sync" "testing" "time" @@ -19,11 +20,14 @@ import ( ) type PeerConn struct { + sync.RWMutex ch <-chan peermanager.PeerData } func (p *PeerConn) Subscribe(ctx context.Context, ch <-chan peermanager.PeerData) { + p.Lock() p.ch = ch + p.Unlock() } func NewPeerConn() *PeerConn { @@ -98,6 +102,8 @@ func TestRendezvous(t *testing.T) { time.Sleep(500 * time.Millisecond) timer := time.After(3 * time.Second) + myPeerConnector.RLock() + defer myPeerConnector.RUnlock() select { case <-timer: require.Fail(t, "no peer discovered")