Skip to content

Commit

Permalink
makes allocated ports concurrency safe
Browse files Browse the repository at this point in the history
  • Loading branch information
yhassanzadeh13 committed Oct 28, 2020
1 parent 57aa665 commit dbc419f
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions network/gossip/libp2p/test/testUtil.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"reflect"
"runtime"
"strings"
"sync"
"testing"
"time"

Expand All @@ -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() {
Expand All @@ -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; {
Expand Down

0 comments on commit dbc419f

Please sign in to comment.