Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented GetFreePort utility function to get free port for tests. … #35

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
25 changes: 14 additions & 11 deletions test/modules/acl/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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, "")

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down
17 changes: 10 additions & 7 deletions test/modules/pubsub/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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")

Expand Down
Loading