fix: race condition in rendezvous test code

This commit is contained in:
Prem Chaitanya Prathi 2023-08-04 16:37:45 +05:30
parent 34de2941c7
commit 42b2c0bc6d
No known key found for this signature in database
3 changed files with 11 additions and 2 deletions

View File

@ -50,6 +50,9 @@ all: build
deps: lint-install deps: lint-install
build-with-race:
${GOBIN} build -race -tags="${BUILD_TAGS}" $(BUILD_FLAGS) -o build/waku ./cmd/waku
build: build:
${GOBIN} build -tags="${BUILD_TAGS}" $(BUILD_FLAGS) -o build/waku ./cmd/waku ${GOBIN} build -tags="${BUILD_TAGS}" $(BUILD_FLAGS) -o build/waku ./cmd/waku
@ -70,7 +73,7 @@ lint:
@golangci-lint --exclude=SA1019 run ./... --deadline=5m @golangci-lint --exclude=SA1019 run ./... --deadline=5m
test: 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} cat ${GO_TEST_OUTFILE}.tmp | grep -v ".pb.go" > ${GO_TEST_OUTFILE}
${GOBIN} tool cover -html=${GO_TEST_OUTFILE} -o ${GO_HTML_COV} ${GOBIN} tool cover -html=${GO_TEST_OUTFILE} -o ${GO_HTML_COV}

View File

@ -91,7 +91,7 @@ func (r *Rendezvous) DiscoverShard(ctx context.Context, rp *RendezvousPoint, clu
r.DiscoverWithNamespace(ctx, namespace, rp, numPeers) 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) { func (r *Rendezvous) DiscoverWithNamespace(ctx context.Context, namespace string, rp *RendezvousPoint, numPeers int) {
rendezvousClient := rvs.NewRendezvousClient(r.host, rp.id) rendezvousClient := rvs.NewRendezvousClient(r.host, rp.id)

View File

@ -5,6 +5,7 @@ import (
"crypto/rand" "crypto/rand"
"database/sql" "database/sql"
"fmt" "fmt"
"sync"
"testing" "testing"
"time" "time"
@ -19,11 +20,14 @@ import (
) )
type PeerConn struct { type PeerConn struct {
sync.RWMutex
ch <-chan peermanager.PeerData ch <-chan peermanager.PeerData
} }
func (p *PeerConn) Subscribe(ctx context.Context, ch <-chan peermanager.PeerData) { func (p *PeerConn) Subscribe(ctx context.Context, ch <-chan peermanager.PeerData) {
p.Lock()
p.ch = ch p.ch = ch
p.Unlock()
} }
func NewPeerConn() *PeerConn { func NewPeerConn() *PeerConn {
@ -98,6 +102,8 @@ func TestRendezvous(t *testing.T) {
time.Sleep(500 * time.Millisecond) time.Sleep(500 * time.Millisecond)
timer := time.After(3 * time.Second) timer := time.After(3 * time.Second)
myPeerConnector.RLock()
defer myPeerConnector.RUnlock()
select { select {
case <-timer: case <-timer:
require.Fail(t, "no peer discovered") require.Fail(t, "no peer discovered")