mirror of
https://github.com/libp2p/go-libp2p.git
synced 2025-10-06 08:37:29 +08:00
test: fix flaky example test
If we run the listener async, we might get expected events out of order.
This commit is contained in:
@@ -49,7 +49,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if *targetF == "" {
|
if *targetF == "" {
|
||||||
runListener(ctx, ha, *listenF, *insecureF)
|
startListener(ctx, ha, *listenF, *insecureF)
|
||||||
|
// Run until canceled.
|
||||||
|
<-ctx.Done()
|
||||||
} else {
|
} else {
|
||||||
runSender(ctx, ha, *targetF)
|
runSender(ctx, ha, *targetF)
|
||||||
}
|
}
|
||||||
@@ -95,7 +97,7 @@ func getHostAddress(ha host.Host) string {
|
|||||||
return addr.Encapsulate(hostAddr).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)
|
fullAddr := getHostAddress(ha)
|
||||||
log.Printf("I am %s\n", fullAddr)
|
log.Printf("I am %s\n", fullAddr)
|
||||||
|
|
||||||
@@ -118,9 +120,6 @@ func runListener(ctx context.Context, ha host.Host, listenPort int, insecure boo
|
|||||||
} else {
|
} else {
|
||||||
log.Printf("Now run \"./echo -l %d -d %s\" on a different terminal\n", listenPort+1, fullAddr)
|
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) {
|
func runSender(ctx context.Context, ha host.Host, targetPeer string) {
|
||||||
|
@@ -43,7 +43,7 @@ func TestMain(t *testing.T) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
go runListener(ctx, lh, lport, true)
|
startListener(ctx, lh, lport, true)
|
||||||
|
|
||||||
// Make sender
|
// Make sender
|
||||||
listenAddr := getHostAddress(lh)
|
listenAddr := getHostAddress(lh)
|
||||||
|
Reference in New Issue
Block a user