Skip to content

Commit d31eab9

Browse files
committed
Merge pull request bnb-chain#2671 from karalabe/randomize-console-tests
cmd/geth: truly randomize console test RPC endpoints
2 parents 553f08b + 32258af commit d31eab9

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

‎cmd/geth/consolecmd_test.go‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
package main
1818

1919
import (
20-
"math/rand"
20+
"crypto/rand"
21+
"math/big"
2122
"os"
2223
"path/filepath"
2324
"runtime"
@@ -73,7 +74,7 @@ func TestIPCAttachWelcome(t *testing.T) {
7374
coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
7475
var ipc string
7576
if runtime.GOOS == "windows" {
76-
ipc = `\\.\pipe\geth` + strconv.Itoa(rand.Int())
77+
ipc = `\\.\pipe\geth` + strconv.Itoa(trulyRandInt(100000, 999999))
7778
} else {
7879
ws := tmpdir(t)
7980
defer os.RemoveAll(ws)
@@ -94,7 +95,7 @@ func TestIPCAttachWelcome(t *testing.T) {
9495

9596
func TestHTTPAttachWelcome(t *testing.T) {
9697
coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
97-
port := strconv.Itoa(rand.Intn(65535-1024) + 1024) // Yeah, sometimes this will fail, sorry :P
98+
port := strconv.Itoa(trulyRandInt(1024, 65536)) // Yeah, sometimes this will fail, sorry :P
9899
geth := runGeth(t,
99100
"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
100101
"--etherbase", coinbase, "--rpc", "--rpcport", port)
@@ -108,7 +109,7 @@ func TestHTTPAttachWelcome(t *testing.T) {
108109

109110
func TestWSAttachWelcome(t *testing.T) {
110111
coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
111-
port := strconv.Itoa(rand.Intn(65535-1024) + 1024) // Yeah, sometimes this will fail, sorry :P
112+
port := strconv.Itoa(trulyRandInt(1024, 65536)) // Yeah, sometimes this will fail, sorry :P
112113

113114
geth := runGeth(t,
114115
"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
@@ -160,3 +161,10 @@ at block: 0 ({{niltime}}){{if ipc}}
160161
`)
161162
attach.expectExit()
162163
}
164+
165+
// trulyRandInt generates a crypto random integer used by the console tests to
166+
// not clash network ports with other tests running cocurrently.
167+
func trulyRandInt(lo, hi int) int {
168+
num, _ := rand.Int(rand.Reader, big.NewInt(int64(hi-lo)))
169+
return int(num.Int64()) + lo
170+
}

0 commit comments

Comments
 (0)