From a060bf9eba7ccc1cae511eadc45ccbf18577a2da Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 22 Jul 2021 14:46:15 -0700 Subject: [PATCH] test: fix flaky example test If we run the listener async, we might get expected events out of order. --- examples/echo/main.go | 9 ++++----- examples/echo/main_test.go | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/examples/echo/main.go b/examples/echo/main.go index 396cf2cc1..b2e2bc2c2 100644 --- a/examples/echo/main.go +++ b/examples/echo/main.go @@ -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) } @@ -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) @@ -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) { diff --git a/examples/echo/main_test.go b/examples/echo/main_test.go index 2d04c7c81..b57f094c9 100644 --- a/examples/echo/main_test.go +++ b/examples/echo/main_test.go @@ -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)