Skip to content

Commit

Permalink
testing: fix flakiness (#987)
Browse files Browse the repository at this point in the history
* testing: fix flakiness
  • Loading branch information
vgonkivs committed Aug 10, 2022
1 parent 2cc8077 commit 6e4e694
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion fraud/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func TestService_BlackListPeer(t *testing.T) {
_, err = subsB.Proof(ctx)
require.NoError(t, err)

newCtx, cancel := context.WithTimeout(ctx, time.Millisecond*500)
newCtx, cancel := context.WithTimeout(ctx, time.Second*1)
t.Cleanup(cancel)

_, err = subsC.Proof(newCtx)
Expand Down
12 changes: 2 additions & 10 deletions node/tests/p2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ func TestBootstrapNodesFromBridgeNode(t *testing.T) {
assert.True(t, light.Host.Network().Connectedness(addrFull.ID) == network.Connected)
}

sw.Disconnect(t, light.Host.ID(), full.Host.ID())
require.NoError(t, full.Stop(ctx))
require.NoError(t, full.Host.Network().Close())
select {
case <-ctx.Done():
t.Fatal("peer was not disconnected")
Expand Down Expand Up @@ -212,15 +212,7 @@ func TestRestartNodeDiscovery(t *testing.T) {
connectSub, err := nodes[0].Host.EventBus().Subscribe(&event.EvtPeerConnectednessChanged{})
require.NoError(t, err)
defer connectSub.Close()

// disconnect and unlink peers in order to receive disconnected event
// NOTE: Order is very important here. We have to unlink peers, and only after that call disconnect,
// otherwise pubsub could re-establish the connection.
// For more information:
// https://github.com/libp2p/go-libp2p-pubsub/blob/60cf38003244a277084c6f3eec0e584ab6cc07bd/pubsub.go#L710
require.NoError(t, sw.Network.UnlinkPeers(nodes[0].Host.ID(), nodes[1].Host.ID()))
require.NoError(t, sw.Network.DisconnectPeers(nodes[0].Host.ID(), nodes[1].Host.ID()))

sw.Disconnect(t, nodes[0].Host.ID(), nodes[1].Host.ID())
require.NoError(t, node.Start(ctx))
for {
select {
Expand Down
18 changes: 18 additions & 0 deletions node/tests/swamp/swamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
ma "github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -296,3 +297,20 @@ func (s *Swamp) remove(rn *node.Node, sn []*node.Node) ([]*node.Node, error) {
}
return sn, nil
}

// Connect allows to connect peers after hard disconnection.
func (s *Swamp) Connect(t *testing.T, peerA, peerB peer.ID) {
_, err := s.Network.LinkPeers(peerA, peerB)
require.NoError(t, err)
_, err = s.Network.ConnectPeers(peerA, peerB)
require.NoError(t, err)
}

// Disconnect allows to break a connection between two peers without any possibility to re-establish it.
// Order is very important here. We have to unlink peers first, and only after that call disconnect.
// This is hard disconnect and peers will not be able to reconnect.
// In order to reconnect peers again, please use swamp.Connect
func (s *Swamp) Disconnect(t *testing.T, peerA, peerB peer.ID) {
require.NoError(t, s.Network.UnlinkPeers(peerA, peerB))
require.NoError(t, s.Network.DisconnectPeers(peerA, peerB))
}
4 changes: 2 additions & 2 deletions service/share/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ func (dn *dagNet) Connect(peerA, peerB peer.ID) {
// It does a hard disconnect, meaning that disconnected peers won't be able to reconnect on their own
// but only with dagNet.Connect or dagNet.ConnectAll.
func (dn *dagNet) Disconnect(peerA, peerB peer.ID) {
err := dn.net.DisconnectPeers(peerA, peerB)
err := dn.net.UnlinkPeers(peerA, peerB)
require.NoError(dn.t, err)
err = dn.net.UnlinkPeers(peerA, peerB)
err = dn.net.DisconnectPeers(peerA, peerB)
require.NoError(dn.t, err)
}

Expand Down

0 comments on commit 6e4e694

Please sign in to comment.