From 3862b6ef045037ad2a3701fed97c50543d16406b Mon Sep 17 00:00:00 2001 From: Sukun Date: Mon, 27 Mar 2023 14:25:15 +0530 Subject: [PATCH] autonat: clean up dialPolicy tests properly --- p2p/host/autonat/dialpolicy_test.go | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/p2p/host/autonat/dialpolicy_test.go b/p2p/host/autonat/dialpolicy_test.go index 75731ae9cf..4bcf91c977 100644 --- a/p2p/host/autonat/dialpolicy_test.go +++ b/p2p/host/autonat/dialpolicy_test.go @@ -23,8 +23,9 @@ func makeMA(a string) multiaddr.Multiaddr { } type mockT struct { - ctx context.Context - addr multiaddr.Multiaddr + ctx context.Context + cancel context.CancelFunc + addr multiaddr.Multiaddr } func (m *mockT) Dial(ctx context.Context, a multiaddr.Multiaddr, p peer.ID) (transport.CapableConn, error) { @@ -32,22 +33,23 @@ func (m *mockT) Dial(ctx context.Context, a multiaddr.Multiaddr, p peer.ID) (tra } func (m *mockT) CanDial(_ multiaddr.Multiaddr) bool { return true } func (m *mockT) Listen(a multiaddr.Multiaddr) (transport.Listener, error) { - return &mockL{m.ctx, m.addr}, nil + return &mockL{m.ctx, m.cancel, m.addr}, nil } func (m *mockT) Protocols() []int { return []int{multiaddr.P_IP4} } func (m *mockT) Proxy() bool { return false } func (m *mockT) String() string { return "mock-tcp-ipv4" } type mockL struct { - ctx context.Context - addr multiaddr.Multiaddr + ctx context.Context + cancel context.CancelFunc + addr multiaddr.Multiaddr } func (l *mockL) Accept() (transport.CapableConn, error) { <-l.ctx.Done() return nil, errors.New("expected in mocked test") } -func (l *mockL) Close() error { return nil } +func (l *mockL) Close() error { l.cancel(); return nil } func (l *mockL) Addr() net.Addr { return nil } func (l *mockL) Multiaddr() multiaddr.Multiaddr { return l.addr } @@ -56,6 +58,7 @@ func TestSkipDial(t *testing.T) { defer cancel() s := swarmt.GenSwarm(t) + defer s.Close() d := dialPolicy{host: blankhost.NewBlankHost(s)} if d.skipDial(makeMA("/ip4/8.8.8.8")) != false { t.Fatal("failed dialing a valid public addr") @@ -69,7 +72,7 @@ func TestSkipDial(t *testing.T) { t.Fatal("didn't skip dialing an internal addr") } - s.AddTransport(&mockT{ctx, makeMA("/ip4/8.8.8.8")}) + s.AddTransport(&mockT{ctx, cancel, makeMA("/ip4/8.8.8.8")}) err := s.AddListenAddr(makeMA("/ip4/8.8.8.8")) if err != nil { t.Fatal(err) @@ -84,6 +87,8 @@ func TestSkipPeer(t *testing.T) { defer cancel() s := swarmt.GenSwarm(t) + defer s.Close() + d := dialPolicy{host: blankhost.NewBlankHost(s)} if d.skipPeer([]multiaddr.Multiaddr{makeMA("/ip4/8.8.8.8")}) != false { t.Fatal("failed dialing a valid public addr") @@ -95,7 +100,7 @@ func TestSkipPeer(t *testing.T) { t.Fatal("succeeded with no public addr") } - s.AddTransport(&mockT{ctx, makeMA("/ip4/8.8.8.8")}) + s.AddTransport(&mockT{ctx, cancel, makeMA("/ip4/8.8.8.8")}) err := s.AddListenAddr(makeMA("/ip4/8.8.8.8")) if err != nil { t.Fatal(err) @@ -117,8 +122,10 @@ func TestSkipLocalPeer(t *testing.T) { defer cancel() s := swarmt.GenSwarm(t) + defer s.Close() + d := dialPolicy{host: blankhost.NewBlankHost(s)} - s.AddTransport(&mockT{ctx, makeMA("/ip4/192.168.0.1")}) + s.AddTransport(&mockT{ctx, cancel, makeMA("/ip4/192.168.0.1")}) err := s.AddListenAddr(makeMA("/ip4/192.168.0.1")) if err != nil { t.Fatal(err)