Implemented GetFreePort utility function to get free port for tests. Updated ACL and PubSub tests to use new GetFreePort functions instead of hardcoded ports

This commit is contained in:
Kelvin Clement Mwinuka
2024-05-03 14:13:16 +08:00
parent 851ca6d11c
commit 6a32f463d0
3 changed files with 41 additions and 18 deletions

View File

@@ -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
}