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

Commit

Permalink
fix flaky tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Dec 21, 2021
1 parent 1f200aa commit ed89216
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions autonat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,44 @@ import (
// these are mock service implementations for testing
func makeAutoNATServicePrivate(t *testing.T) host.Host {
h := bhost.NewBlankHost(swarmt.GenSwarm(t))
h.SetStreamHandler(AutoNATProto, sayAutoNATPrivate)
h.SetStreamHandler(AutoNATProto, sayPrivateStreamHandler(t))
return h
}

func makeAutoNATServicePublic(t *testing.T) host.Host {
h := bhost.NewBlankHost(swarmt.GenSwarm(t))
h.SetStreamHandler(AutoNATProto, sayAutoNATPublic)
return h
}

func sayAutoNATPrivate(s network.Stream) {
defer s.Close()
w := protoio.NewDelimitedWriter(s)
res := pb.Message{
Type: pb.Message_DIAL_RESPONSE.Enum(),
DialResponse: newDialResponseError(pb.Message_E_DIAL_ERROR, "no dialable addresses"),
func sayPrivateStreamHandler(t *testing.T) network.StreamHandler {
return func(s network.Stream) {
defer s.Close()
r := protoio.NewDelimitedReader(s, network.MessageSizeMax)
if err := r.ReadMsg(&pb.Message{}); err != nil {
t.Error(err)
return
}
w := protoio.NewDelimitedWriter(s)
res := pb.Message{
Type: pb.Message_DIAL_RESPONSE.Enum(),
DialResponse: newDialResponseError(pb.Message_E_DIAL_ERROR, "no dialable addresses"),
}
w.WriteMsg(&res)
}
w.WriteMsg(&res)
}

func sayAutoNATPublic(s network.Stream) {
defer s.Close()
w := protoio.NewDelimitedWriter(s)
res := pb.Message{
Type: pb.Message_DIAL_RESPONSE.Enum(),
DialResponse: newDialResponseOK(s.Conn().RemoteMultiaddr()),
}
w.WriteMsg(&res)
func makeAutoNATServicePublic(t *testing.T) host.Host {
h := bhost.NewBlankHost(swarmt.GenSwarm(t))
h.SetStreamHandler(AutoNATProto, func(s network.Stream) {
defer s.Close()
r := protoio.NewDelimitedReader(s, network.MessageSizeMax)
if err := r.ReadMsg(&pb.Message{}); err != nil {
t.Error(err)
return
}
w := protoio.NewDelimitedWriter(s)
res := pb.Message{
Type: pb.Message_DIAL_RESPONSE.Enum(),
DialResponse: newDialResponseOK(s.Conn().RemoteMultiaddr()),
}
w.WriteMsg(&res)
})
return h
}

func makeAutoNAT(t *testing.T, ash host.Host) (host.Host, AutoNAT) {
Expand Down Expand Up @@ -173,7 +183,7 @@ func TestAutoNATPublictoPrivate(t *testing.T) {
)
expectEvent(t, s, network.ReachabilityPublic)

hs.SetStreamHandler(AutoNATProto, sayAutoNATPrivate)
hs.SetStreamHandler(AutoNATProto, sayPrivateStreamHandler(t))
hps := makeAutoNATServicePrivate(t)
connect(t, hps, hc)
identifyAsServer(hps, hc)
Expand Down

0 comments on commit ed89216

Please sign in to comment.