Skip to content

Commit

Permalink
chore: testify fix got, expected transpositions (libp2p#2666)
Browse files Browse the repository at this point in the history
* chore: testify lint fixes

* chore: revert t.error replacements

* chore: revert errNotWebSocketAddress

---------

Co-authored-by: Sukun <[email protected]>
  • Loading branch information
dozyio and sukunrt authored Feb 14, 2024
1 parent 66a20a8 commit e3b0a86
Show file tree
Hide file tree
Showing 31 changed files with 117 additions and 132 deletions.
4 changes: 2 additions & 2 deletions core/network/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ func TestSimultaneousConnect(t *testing.T) {
ok, isClient, reason := GetSimultaneousConnect(serverCtx)
require.True(t, ok)
require.False(t, isClient)
require.Equal(t, reason, "foobar")
require.Equal(t, "foobar", reason)
})
t.Run("for the client", func(t *testing.T) {
serverCtx := WithSimultaneousConnect(context.Background(), true, "foo")
ok, isClient, reason := GetSimultaneousConnect(serverCtx)
require.True(t, ok)
require.True(t, isClient)
require.Equal(t, reason, "foo")
require.Equal(t, "foo", reason)
})
}
6 changes: 3 additions & 3 deletions libp2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ func TestTransportCustomAddressWebTransport(t *testing.T) {
require.NotEqual(t, addrs[0], customAddr)
restOfAddr, lastComp := ma.SplitLast(addrs[0])
restOfAddr, secondToLastComp := ma.SplitLast(restOfAddr)
require.Equal(t, lastComp.Protocol().Code, ma.P_CERTHASH)
require.Equal(t, secondToLastComp.Protocol().Code, ma.P_CERTHASH)
require.Equal(t, ma.P_CERTHASH, lastComp.Protocol().Code)
require.Equal(t, ma.P_CERTHASH, secondToLastComp.Protocol().Code)
require.True(t, restOfAddr.Equal(customAddr))
}

Expand All @@ -352,7 +352,7 @@ func TestTransportCustomAddressWebTransportDoesNotStall(t *testing.T) {
addrs := h.Addrs()
require.Len(t, addrs, 1)
_, lastComp := ma.SplitLast(addrs[0])
require.NotEqual(t, lastComp.Protocol().Code, ma.P_CERTHASH)
require.NotEqual(t, ma.P_CERTHASH, lastComp.Protocol().Code)
// We did not add the certhash to the multiaddr
require.Equal(t, addrs[0], customAddr)
}
8 changes: 4 additions & 4 deletions p2p/host/basic/basic_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ func TestHostProtoPreknowledge(t *testing.T) {

// This test implicitly relies on 1 connection. If a background identify
// completes after we set the stream handler below things break
require.Equal(t, 1, len(h1.Network().ConnsToPeer(h2.ID())))
require.Len(t, h1.Network().ConnsToPeer(h2.ID()), 1)

// wait for identify handshake to finish completely
select {
Expand Down Expand Up @@ -469,7 +469,7 @@ func TestNewStreamResolve(t *testing.T) {
break
}
}
assert.NotEqual(t, dialAddr, "")
assert.NotEqual(t, "", dialAddr)

// Add the DNS multiaddr to h1's peerstore.
maddr, err := ma.NewMultiaddr(dialAddr)
Expand Down Expand Up @@ -506,7 +506,7 @@ func TestProtoDowngrade(t *testing.T) {
defer s.Close()
result, err := io.ReadAll(s)
assert.NoError(t, err)
assert.Equal(t, string(result), "bar")
assert.Equal(t, "bar", string(result))
connectedOn <- s.Protocol()
})

Expand All @@ -527,7 +527,7 @@ func TestProtoDowngrade(t *testing.T) {
defer s.Close()
result, err := io.ReadAll(s)
assert.NoError(t, err)
assert.Equal(t, string(result), "foo")
assert.Equal(t, "foo", string(result))
connectedOn <- s.Protocol()
})

Expand Down
2 changes: 1 addition & 1 deletion p2p/host/peerstore/pstoremem/sorting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ func TestAddressSorting(t *testing.T) {

l := addrList{local, u1, u2l, norm}
sort.Sort(l)
require.Equal(t, l, addrList{u2l, u1, local, norm})
require.Equal(t, addrList{u2l, u1, local, norm}, l)
}
6 changes: 3 additions & 3 deletions p2p/host/peerstore/test/peerstore_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,11 @@ func testMetadata(ps pstore.Peerstore) func(t *testing.T) {
for _, p := range pids {
v, err := ps.Get(p, "AgentVersion")
require.NoError(t, err)
require.Equal(t, v, "string")
require.Equal(t, "string", v)

v, err = ps.Get(p, "bar")
require.NoError(t, err)
require.Equal(t, v, 1)
require.Equal(t, 1, v)
}
})

Expand All @@ -359,7 +359,7 @@ func testMetadata(ps pstore.Peerstore) func(t *testing.T) {
// make sure that entries for otherP were not deleted
val, err := ps.Get(otherP, "AgentVersion")
require.NoError(t, err)
require.Equal(t, val, "v1")
require.Equal(t, "v1", val)
})
}
}
Expand Down
5 changes: 2 additions & 3 deletions p2p/host/resource-manager/limit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ func TestScaling(t *testing.T) {
require.Equal(t, 20+4*2, scaled.service["B"].Streams)
require.Equal(t, int64(200+4*3), scaled.service["B"].Memory)
require.Equal(t, 400, scaled.service["B"].FD)

})
}

Expand Down Expand Up @@ -157,7 +156,7 @@ func TestJSONMarshalling(t *testing.T) {

jsonEncoded, err := json.Marshal(bl)
require.NoError(t, err)
require.Equal(t, string(jsonEncoded), `{"StreamsInbound":10,"StreamsOutbound":"blockAll","Conns":10,"ConnsOutbound":"unlimited","Memory":"unlimited"}`)
require.Equal(t, `{"StreamsInbound":10,"StreamsOutbound":"blockAll","Conns":10,"ConnsOutbound":"unlimited","Memory":"unlimited"}`, string(jsonEncoded))

// Roundtrip
var blDecoded ResourceLimits
Expand All @@ -175,7 +174,7 @@ func TestJSONRoundTripInt64(t *testing.T) {
jsonEncoded, err := json.Marshal(bl)
require.NoError(t, err)

require.Equal(t, string(jsonEncoded), `{"Memory":"9223372036854775807"}`)
require.Equal(t, `{"Memory":"9223372036854775807"}`, string(jsonEncoded))

// Roundtrip
var blDecoded ResourceLimits
Expand Down
5 changes: 2 additions & 3 deletions p2p/http/libp2phttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ func TestCustomServeMux(t *testing.T) {
defer server.Close()

addrs := server.Addrs()
require.Equal(t, len(addrs), 1)
require.Len(t, addrs, 1)
var clientHttpHost libp2phttp.Host
rt, err := clientHttpHost.NewConstrainedRoundTripper(peer.AddrInfo{Addrs: addrs}, libp2phttp.PreferHTTPTransport)
require.NoError(t, err)
Expand Down Expand Up @@ -466,7 +466,7 @@ func TestSetHandlerAtPath(t *testing.T) {
}()
defer server.Close()
addrs := server.Addrs()
require.Equal(t, 1, len(addrs))
require.Len(t, addrs, 1)
port, err := addrs[0].ValueForProtocol(ma.P_TCP)
require.NoError(t, err)
httpAddr := fmt.Sprintf("http://127.0.0.1:%s", port)
Expand All @@ -483,5 +483,4 @@ func TestSetHandlerAtPath(t *testing.T) {
}
})
}

}
11 changes: 5 additions & 6 deletions p2p/net/connmgr/connmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,6 @@ func TestPeerProtectionMultipleTags(t *testing.T) {
t.Error("protected connection was closed by connection manager")
}
}

}

func TestPeerProtectionIdempotent(t *testing.T) {
Expand Down Expand Up @@ -835,15 +834,15 @@ func TestPeerInfoSorting(t *testing.T) {
p2 := &peerInfo{id: peer.ID("peer2"), temp: true}
pis := peerInfos{p1, p2}
pis.SortByValueAndStreams(makeSegmentsWithPeerInfos(pis), false)
require.Equal(t, pis, peerInfos{p2, p1})
require.Equal(t, peerInfos{p2, p1}, pis)
})

t.Run("starts with low-value connections", func(t *testing.T) {
p1 := &peerInfo{id: peer.ID("peer1"), value: 40}
p2 := &peerInfo{id: peer.ID("peer2"), value: 20}
pis := peerInfos{p1, p2}
pis.SortByValueAndStreams(makeSegmentsWithPeerInfos(pis), false)
require.Equal(t, pis, peerInfos{p2, p1})
require.Equal(t, peerInfos{p2, p1}, pis)
})

t.Run("prefer peers with no streams", func(t *testing.T) {
Expand All @@ -859,7 +858,7 @@ func TestPeerInfoSorting(t *testing.T) {
}
pis := peerInfos{p2, p1}
pis.SortByValueAndStreams(makeSegmentsWithPeerInfos(pis), false)
require.Equal(t, pis, peerInfos{p1, p2})
require.Equal(t, peerInfos{p1, p2}, pis)
})

t.Run("in a memory emergency, starts with incoming connections and higher streams", func(t *testing.T) {
Expand Down Expand Up @@ -902,7 +901,7 @@ func TestPeerInfoSorting(t *testing.T) {
// p3 is first because it is inactive (no streams).
// p4 is second because it has the most streams and we priortize killing
// connections with the higher number of streams.
require.Equal(t, pis, peerInfos{p3, p4, p2, p1})
require.Equal(t, peerInfos{p3, p4, p2, p1}, pis)
})

t.Run("in a memory emergency, starts with connections that have many streams", func(t *testing.T) {
Expand All @@ -921,7 +920,7 @@ func TestPeerInfoSorting(t *testing.T) {
}
pis := peerInfos{p1, p2}
pis.SortByValueAndStreams(makeSegmentsWithPeerInfos(pis), true)
require.Equal(t, pis, peerInfos{p2, p1})
require.Equal(t, peerInfos{p2, p1}, pis)
})
}

Expand Down
4 changes: 2 additions & 2 deletions p2p/net/connmgr/decay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestMultipleBumps(t *testing.T) {
require.NoError(t, tag.Bump(id, 5))

waitForTag(t, mgr, id)
require.Equal(t, mgr.GetTagInfo(id).Value, 10)
require.Equal(t, 10, mgr.GetTagInfo(id).Value)

require.NoError(t, tag.Bump(id, 100))
require.Eventually(t, func() bool { return mgr.GetTagInfo(id).Value == 20 }, 100*time.Millisecond, 10*time.Millisecond, "expected tag value to decay to 20")
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestMultipleTagsNoDecay(t *testing.T) {

// all tags are upper-bounded, so the score must be 300
ti := mgr.GetTagInfo(id)
require.Equal(t, ti.Value, 300)
require.Equal(t, 300, ti.Value)

for _, s := range []string{"beep", "bop", "foo"} {
if v, ok := ti.Tags[s]; !ok || v != 100 {
Expand Down
4 changes: 2 additions & 2 deletions p2p/net/swarm/swarm_dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func TestDedupAddrsForDial(t *testing.T) {
mas, _, err := s.addrsForDial(ctx, otherPeer)
require.NoError(t, err)

require.Equal(t, 1, len(mas))
require.Len(t, mas, 1)
}

func newTestSwarmWithResolver(t *testing.T, resolver *madns.Resolver) *Swarm {
Expand Down Expand Up @@ -275,7 +275,7 @@ func TestAddrResolutionRecursiveTransportSpecific(t *testing.T) {
addrs, _, err := s.addrsForDial(tctx, p)
require.NoError(t, err)
require.Len(t, addrs, 1)
require.Equal(t, addrs[0].String(), "/ip4/1.2.3.4/tcp/443/tls/sni/sub.example.com/ws")
require.Equal(t, "/ip4/1.2.3.4/tcp/443/tls/sni/sub.example.com/ws", addrs[0].String())
}

func TestAddrsForDialFiltering(t *testing.T) {
Expand Down
12 changes: 6 additions & 6 deletions p2p/net/swarm/swarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,16 +445,16 @@ func TestStreamCount(t *testing.T) {
<-streamAccepted
}
require.Eventually(t, func() bool { return len(streams) == 10 }, 5*time.Second, 10*time.Millisecond)
require.Equal(t, countStreams(), 10)
require.Equal(t, 10, countStreams())
(<-streams).Reset()
(<-streams).Close()
require.Equal(t, countStreams(), 8)
require.Equal(t, 8, countStreams())

str, err := s1.NewStream(context.Background(), s2.LocalPeer())
require.NoError(t, err)
require.Equal(t, countStreams(), 9)
require.Equal(t, 9, countStreams())
str.Close()
require.Equal(t, countStreams(), 8)
require.Equal(t, 8, countStreams())
}

func TestResourceManager(t *testing.T) {
Expand Down Expand Up @@ -552,7 +552,7 @@ func TestListenCloseCount(t *testing.T) {
t.Fatal(err)
}
listenedAddrs := s.ListenAddresses()
require.Equal(t, 2, len(listenedAddrs))
require.Len(t, listenedAddrs, 2)
var addrToClose ma.Multiaddr
for _, addr := range listenedAddrs {
if _, err := addr.ValueForProtocol(ma.P_QUIC_V1); err == nil {
Expand All @@ -564,7 +564,7 @@ func TestListenCloseCount(t *testing.T) {
s.ListenClose(addrToClose)

remainingAddrs := s.ListenAddresses()
require.Equal(t, 1, len(remainingAddrs))
require.Len(t, remainingAddrs, 1)
_, err := remainingAddrs[0].ValueForProtocol(ma.P_TCP)
require.NoError(t, err, "expected the TCP address to still be present")
}
2 changes: 1 addition & 1 deletion p2p/net/upgrader/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func TestConnectionsClosedIfNotAccepted(t *testing.T) {
}

time.Sleep(timeout)
require.Nil(<-errCh)
require.NoError(<-errCh)
}

func TestFailedUpgradeOnListen(t *testing.T) {
Expand Down
10 changes: 4 additions & 6 deletions p2p/protocol/holepunch/holepunch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ func TestNoHolePunchIfDirectConnExists(t *testing.T) {
require.GreaterOrEqual(t, nc2, 1)

require.NoError(t, hps.DirectConnect(h2.ID()))
require.Equal(t, len(h1.Network().ConnsToPeer(h2.ID())), nc1)
require.Equal(t, len(h2.Network().ConnsToPeer(h1.ID())), nc2)
require.Len(t, h1.Network().ConnsToPeer(h2.ID()), nc1)
require.Len(t, h2.Network().ConnsToPeer(h1.ID()), nc2)
require.Empty(t, tr.getEvents())
}

Expand All @@ -120,13 +120,13 @@ func TestDirectDialWorks(t *testing.T) {
h1.Peerstore().AddAddrs(h2.ID(), h2.Addrs(), peerstore.ConnectedAddrTTL)

// try to hole punch without any connection and streams, if it works -> it's a direct connection
require.Len(t, h1.Network().ConnsToPeer(h2.ID()), 0)
require.Empty(t, h1.Network().ConnsToPeer(h2.ID()))
require.NoError(t, h1ps.DirectConnect(h2.ID()))
require.GreaterOrEqual(t, len(h1.Network().ConnsToPeer(h2.ID())), 1)
require.GreaterOrEqual(t, len(h2.Network().ConnsToPeer(h1.ID())), 1)
events := tr.getEvents()
require.Len(t, events, 1)
require.Equal(t, events[0].Type, holepunch.DirectDialEvtT)
require.Equal(t, holepunch.DirectDialEvtT, events[0].Type)
}

func TestEndToEndSimConnect(t *testing.T) {
Expand Down Expand Up @@ -247,7 +247,6 @@ func TestFailuresOnInitiator(t *testing.T) {
require.Contains(t, err.Error(), tc.errMsg)
}
})

}
}

Expand Down Expand Up @@ -361,7 +360,6 @@ func TestFailuresOnResponder(t *testing.T) {
require.Len(t, errs, 1)
require.Contains(t, errs[0], tc.errMsg)
})

}
}

Expand Down
2 changes: 1 addition & 1 deletion p2p/protocol/identify/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ func TestLargeIdentifyMessage(t *testing.T) {
require.NoError(t, h1.Connect(context.Background(), h2pi))

h1t2c := h1.Network().ConnsToPeer(h2p)
require.Equal(t, 1, len(h1t2c), "should have a conn here")
require.Len(t, h1t2c, 1, "should have a conn here")

ids1.IdentifyConn(h1t2c[0])

Expand Down
3 changes: 1 addition & 2 deletions p2p/protocol/identify/obsaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,6 @@ func TestEmitNATDeviceTypeSymmetric(t *testing.T) {
case <-time.After(5 * time.Second):
t.Fatal("did not get Symmetric NAT event")
}

}

func TestEmitNATDeviceTypeCone(t *testing.T) {
Expand Down Expand Up @@ -431,6 +430,6 @@ func TestObserveWebtransport(t *testing.T) {
harness.observe(observedAddr, pb4)
harness.observe(observedAddr, pb5)

require.Equal(t, 1, len(harness.oas.Addrs()))
require.Len(t, harness.oas.Addrs(), 1)
require.Equal(t, "/ip4/1.2.3.4/udp/1231/quic-v1/webtransport", harness.oas.Addrs()[0].String())
}
Loading

0 comments on commit e3b0a86

Please sign in to comment.