Skip to content

Commit cb1ad8a

Browse files
authored
Merge pull request #14 from zerodha/fix-flaky-test
fix: dynamically allocate port for flaky test
2 parents d6ab083 + 2bec868 commit cb1ad8a

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

tests/fastcache_test.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package tests
33
import (
44
"bytes"
55
"compress/gzip"
6+
"fmt"
67
"io"
78
"log"
9+
"net"
810
"net/http"
911
"os"
1012
"testing"
@@ -19,18 +21,33 @@ import (
1921
)
2022

2123
const (
22-
srvAddr = ":10200"
23-
srvRoot = "http://127.0.0.1:10200"
2424
namespaceKey = "req"
2525
group = "test"
2626
)
2727

2828
var (
29-
srv = fastglue.NewGlue()
29+
srv = fastglue.NewGlue()
30+
srvAddr = dummyServAddr()
31+
srvRoot = "http://127.0.0.1" + srvAddr
3032

3133
content = []byte("this is the reasonbly long test content that may be compressed")
3234
)
3335

36+
// dummyServeAddr returns a random port address.
37+
func dummyServAddr() string {
38+
// Dynamically allocate an available port
39+
listener, err := net.Listen("tcp", ":0")
40+
if err != nil {
41+
panic(err)
42+
}
43+
defer listener.Close()
44+
45+
// Get the actual port that was allocated
46+
port := listener.Addr().(*net.TCPAddr).Port
47+
48+
return fmt.Sprintf(":%d", port)
49+
}
50+
3451
func init() {
3552
// Setup fastcache.
3653
rd, err := miniredis.Run()

0 commit comments

Comments
 (0)