diff --git a/internal/utils.go b/internal/utils.go index e7ef6081..3ca7ee8f 100644 --- a/internal/utils.go +++ b/internal/utils.go @@ -410,3 +410,20 @@ func CompareNestedStringArrays(got [][]string, want [][]string) bool { } return true } + +func GetFreePort() (int, error) { + addr, err := net.ResolveTCPAddr("tcp", "localhost:0") + if err != nil { + return 0, err + } + + l, err := net.ListenTCP("tcp", addr) + if err != nil { + return 0, err + } + defer func() { + _ = l.Close() + }() + + return l.Addr().(*net.TCPAddr).Port, nil +} diff --git a/test/modules/acl/commands_test.go b/test/modules/acl/commands_test.go index bb0d9b82..bc1348f6 100644 --- a/test/modules/acl/commands_test.go +++ b/test/modules/acl/commands_test.go @@ -18,6 +18,7 @@ import ( "crypto/sha256" "fmt" "github.com/echovault/echovault/echovault" + "github.com/echovault/echovault/internal" "github.com/echovault/echovault/internal/config" "github.com/echovault/echovault/internal/constants" "github.com/echovault/echovault/internal/modules/acl" @@ -37,7 +38,9 @@ var mockServer *echovault.EchoVault func init() { bindAddr = "localhost" - port = 7496 + + p, _ := internal.GetFreePort() + port = uint16(p) mockServer = setUpServer(bindAddr, port, true, "") @@ -406,8 +409,8 @@ func Test_HandleCat(t *testing.T) { } func Test_HandleUsers(t *testing.T) { - var port uint16 = 7491 - mockServer := setUpServer(bindAddr, port, false, "") + port, _ := internal.GetFreePort() + mockServer := setUpServer(bindAddr, uint16(port), false, "") wg := sync.WaitGroup{} wg.Add(1) go func() { @@ -461,8 +464,8 @@ func Test_HandleUsers(t *testing.T) { } func Test_HandleSetUser(t *testing.T) { - var port uint16 = 7492 - mockServer := setUpServer(bindAddr, port, false, "") + port, _ := internal.GetFreePort() + mockServer := setUpServer(bindAddr, uint16(port), false, "") wg := sync.WaitGroup{} wg.Add(1) go func() { @@ -1054,8 +1057,8 @@ func Test_HandleSetUser(t *testing.T) { } func Test_HandleGetUser(t *testing.T) { - var port uint16 = 7493 - mockServer := setUpServer(bindAddr, port, false, "") + port, _ := internal.GetFreePort() + mockServer := setUpServer(bindAddr, uint16(port), false, "") wg := sync.WaitGroup{} wg.Add(1) go func() { @@ -1207,8 +1210,8 @@ func Test_HandleGetUser(t *testing.T) { } func Test_HandleDelUser(t *testing.T) { - var port uint16 = 7494 - mockServer := setUpServer(bindAddr, port, false, "") + port, _ := internal.GetFreePort() + mockServer := setUpServer(bindAddr, uint16(port), false, "") wg := sync.WaitGroup{} wg.Add(1) go func() { @@ -1357,8 +1360,8 @@ func Test_HandleWhoAmI(t *testing.T) { } func Test_HandleList(t *testing.T) { - var port uint16 = 7495 - mockServer := setUpServer(bindAddr, port, false, "") + port, _ := internal.GetFreePort() + mockServer := setUpServer(bindAddr, uint16(port), false, "") wg := sync.WaitGroup{} wg.Add(1) go func() { diff --git a/test/modules/pubsub/commands_test.go b/test/modules/pubsub/commands_test.go index 72febf19..1e7a97f3 100644 --- a/test/modules/pubsub/commands_test.go +++ b/test/modules/pubsub/commands_test.go @@ -38,9 +38,12 @@ var ps *pubsub.PubSub var mockServer *echovault.EchoVault var bindAddr = "localhost" -var port uint16 = 7490 +var port uint16 func init() { + p, _ := internal.GetFreePort() + port = uint16(p) + mockServer = setUpServer(bindAddr, port) getPubSub := getUnexportedField(reflect.ValueOf(mockServer).Elem().FieldByName("getPubSub")).(func() interface{}) @@ -547,8 +550,8 @@ func Test_HandlePubSubChannels(t *testing.T) { done := make(chan struct{}) go func() { // Create separate mock echovault for this test - var port uint16 = 7590 - mockServer := setUpServer(bindAddr, port) + port, _ := internal.GetFreePort() + mockServer := setUpServer(bindAddr, uint16(port)) ctx := context.WithValue(context.Background(), "test_name", "PUBSUB CHANNELS") @@ -682,8 +685,8 @@ func Test_HandleNumPat(t *testing.T) { done := make(chan struct{}) go func() { // Create separate mock echovault for this test - var port uint16 = 7591 - mockServer := setUpServer(bindAddr, port) + port, _ := internal.GetFreePort() + mockServer := setUpServer(bindAddr, uint16(port)) ctx := context.WithValue(context.Background(), "test_name", "PUBSUB NUMPAT") @@ -777,8 +780,8 @@ func Test_HandleNumSub(t *testing.T) { done := make(chan struct{}) go func() { // Create separate mock echovault for this test - var port uint16 = 7591 - mockServer := setUpServer(bindAddr, port) + port, _ := internal.GetFreePort() + mockServer := setUpServer(bindAddr, uint16(port)) ctx := context.WithValue(context.Background(), "test_name", "PUBSUB NUMSUB")