From dbc419fc31bb0c6c89d7aac66c9697772399f4d0 Mon Sep 17 00:00:00 2001 From: Yahya Date: Wed, 28 Oct 2020 15:21:25 -0700 Subject: [PATCH] makes allocated ports concurrency safe --- network/gossip/libp2p/test/testUtil.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/network/gossip/libp2p/test/testUtil.go b/network/gossip/libp2p/test/testUtil.go index 86c89e9d7ec..c85176fdaba 100644 --- a/network/gossip/libp2p/test/testUtil.go +++ b/network/gossip/libp2p/test/testUtil.go @@ -5,6 +5,7 @@ import ( "reflect" "runtime" "strings" + "sync" "testing" "time" @@ -29,6 +30,9 @@ var rootBlockID string // allocatedPorts keeps track of ports allocated to different tests var allocatedPorts map[int]struct{} +// mu makes allocatedPorts access concurrency safe +var mu sync.Mutex + // init is a built-in golang function getting called first time this // package is imported. It initializes package-scoped variables. func init() { @@ -38,6 +42,9 @@ func init() { // getFreePorts finds `n` free ports on the machine and marks them as allocated. func getFreePorts(t *testing.T, n int) []int { + mu.Lock() + defer mu.Unlock() + ports := make([]int, n) // keeps track of discovered ports for count := 0; count < n; {