Skip to content
This repository was archived by the owner on May 11, 2022. It is now read-only.

Commit be72a49

Browse files
fix flaky tests
1 parent 12fc177 commit be72a49

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

autonat_test.go

+34-24
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,44 @@ import (
2121
// these are mock service implementations for testing
2222
func makeAutoNATServicePrivate(ctx context.Context, t *testing.T) host.Host {
2323
h := bhost.NewBlankHost(swarmt.GenSwarm(t))
24-
h.SetStreamHandler(AutoNATProto, sayAutoNATPrivate)
24+
h.SetStreamHandler(AutoNATProto, sayPrivateStreamHandler(t))
2525
return h
2626
}
2727

28-
func makeAutoNATServicePublic(ctx context.Context, t *testing.T) host.Host {
29-
h := bhost.NewBlankHost(swarmt.GenSwarm(t))
30-
h.SetStreamHandler(AutoNATProto, sayAutoNATPublic)
31-
return h
32-
}
33-
34-
func sayAutoNATPrivate(s network.Stream) {
35-
defer s.Close()
36-
w := protoio.NewDelimitedWriter(s)
37-
res := pb.Message{
38-
Type: pb.Message_DIAL_RESPONSE.Enum(),
39-
DialResponse: newDialResponseError(pb.Message_E_DIAL_ERROR, "no dialable addresses"),
28+
func sayPrivateStreamHandler(t *testing.T) network.StreamHandler {
29+
return func(s network.Stream) {
30+
defer s.Close()
31+
r := protoio.NewDelimitedReader(s, network.MessageSizeMax)
32+
if err := r.ReadMsg(&pb.Message{}); err != nil {
33+
t.Error(err)
34+
return
35+
}
36+
w := protoio.NewDelimitedWriter(s)
37+
res := pb.Message{
38+
Type: pb.Message_DIAL_RESPONSE.Enum(),
39+
DialResponse: newDialResponseError(pb.Message_E_DIAL_ERROR, "no dialable addresses"),
40+
}
41+
w.WriteMsg(&res)
4042
}
41-
w.WriteMsg(&res)
4243
}
4344

44-
func sayAutoNATPublic(s network.Stream) {
45-
defer s.Close()
46-
w := protoio.NewDelimitedWriter(s)
47-
res := pb.Message{
48-
Type: pb.Message_DIAL_RESPONSE.Enum(),
49-
DialResponse: newDialResponseOK(s.Conn().RemoteMultiaddr()),
50-
}
51-
w.WriteMsg(&res)
45+
func makeAutoNATServicePublic(ctx context.Context, t *testing.T) host.Host {
46+
h := bhost.NewBlankHost(swarmt.GenSwarm(t))
47+
h.SetStreamHandler(AutoNATProto, func(s network.Stream) {
48+
defer s.Close()
49+
r := protoio.NewDelimitedReader(s, network.MessageSizeMax)
50+
if err := r.ReadMsg(&pb.Message{}); err != nil {
51+
t.Error(err)
52+
return
53+
}
54+
w := protoio.NewDelimitedWriter(s)
55+
res := pb.Message{
56+
Type: pb.Message_DIAL_RESPONSE.Enum(),
57+
DialResponse: newDialResponseOK(s.Conn().RemoteMultiaddr()),
58+
}
59+
w.WriteMsg(&res)
60+
})
61+
return h
5262
}
5363

5464
func makeAutoNAT(ctx context.Context, t *testing.T, ash host.Host) (host.Host, AutoNAT) {
@@ -184,7 +194,7 @@ func TestAutoNATPublictoPrivate(t *testing.T) {
184194

185195
expectEvent(t, s, network.ReachabilityPublic)
186196

187-
hs.SetStreamHandler(AutoNATProto, sayAutoNATPrivate)
197+
hs.SetStreamHandler(AutoNATProto, sayPrivateStreamHandler(t))
188198
hps := makeAutoNATServicePrivate(ctx, t)
189199
connect(t, hps, hc)
190200
identifyAsServer(hps, hc)
@@ -252,7 +262,7 @@ func TestAutoNATObservationRecording(t *testing.T) {
252262
case <-s.Out():
253263
t.Fatal("not expecting a public reachability event")
254264
default:
255-
//expected
265+
// expected
256266
}
257267

258268
addr, _ := ma.NewMultiaddr("/ip4/127.0.0.1/udp/1234")

0 commit comments

Comments
 (0)