Skip to content

Commit

Permalink
test: fix flaky example test
Browse files Browse the repository at this point in the history
If we run the listener async, we might get expected events out of order.
  • Loading branch information
Stebalien committed Jul 22, 2021
1 parent 5d3b015 commit a060bf9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
9 changes: 4 additions & 5 deletions examples/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ func main() {
}

if *targetF == "" {
runListener(ctx, ha, *listenF, *insecureF)
startListener(ctx, ha, *listenF, *insecureF)
// Run until canceled.
<-ctx.Done()
} else {
runSender(ctx, ha, *targetF)
}
Expand Down Expand Up @@ -95,7 +97,7 @@ func getHostAddress(ha host.Host) string {
return addr.Encapsulate(hostAddr).String()
}

func runListener(ctx context.Context, ha host.Host, listenPort int, insecure bool) {
func startListener(ctx context.Context, ha host.Host, listenPort int, insecure bool) {
fullAddr := getHostAddress(ha)
log.Printf("I am %s\n", fullAddr)

Expand All @@ -118,9 +120,6 @@ func runListener(ctx context.Context, ha host.Host, listenPort int, insecure boo
} else {
log.Printf("Now run \"./echo -l %d -d %s\" on a different terminal\n", listenPort+1, fullAddr)
}

// Wait until canceled
<-ctx.Done()
}

func runSender(ctx context.Context, ha host.Host, targetPeer string) {
Expand Down
2 changes: 1 addition & 1 deletion examples/echo/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestMain(t *testing.T) {
return
}

go runListener(ctx, lh, lport, true)
startListener(ctx, lh, lport, true)

// Make sender
listenAddr := getHostAddress(lh)
Expand Down

0 comments on commit a060bf9

Please sign in to comment.