Skip to content

Commit

Permalink
fix(test): retry MockBroker Listen for EADDRINUSE (#2721)
Browse files Browse the repository at this point in the history
Some of the FV tests involve shutting down a MockBroker and then
re-creating a new one on the same ip:port. Sometimes this fails because
the port shows as still being in-use. Add some rudimentary retry
attempts for this case to try and close the FV flakiness in that area.

Signed-off-by: Dominic Evans <dominic.evans@uk.ibm.com>
  • Loading branch information
dnwe authored Jan 31, 2024
1 parent d382099 commit b2999b4
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion mockbroker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"reflect"
"strconv"
"sync"
"syscall"
"time"

"github.com/davecgh/go-spew/spew"
Expand Down Expand Up @@ -410,10 +411,29 @@ func NewMockBroker(t TestReporter, brokerID int32) *MockBroker {
// NewMockBrokerAddr behaves like newMockBroker but listens on the address you give
// it rather than just some ephemeral port.
func NewMockBrokerAddr(t TestReporter, brokerID int32, addr string) *MockBroker {
listener, err := net.Listen("tcp", addr)
var (
listener net.Listener
err error
)

// retry up to 20 times if address already in use (e.g., if replacing broker which hasn't cleanly shutdown)
for i := 0; i < 20; i++ {
listener, err = net.Listen("tcp", addr)
if err != nil {
if errors.Is(err, syscall.EADDRINUSE) {
Logger.Printf("*** mockbroker/%d waiting for %s (address already in use)", brokerID, addr)
time.Sleep(time.Millisecond * 100)
continue
}
t.Fatal(err)
}
break
}

if err != nil {
t.Fatal(err)
}

return NewMockBrokerListener(t, brokerID, listener)
}

Expand Down

0 comments on commit b2999b4

Please sign in to comment.