go-waku/waku/v2/node/keepalive_test.go

49 lines
1.2 KiB
Go
Raw Normal View History

2021-10-29 15:24:31 +00:00
package node
import (
"context"
"sync"
2021-10-29 15:24:31 +00:00
"testing"
"time"
"github.com/libp2p/go-libp2p"
2022-10-19 19:39:32 +00:00
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/peerstore"
2021-10-29 15:24:31 +00:00
"github.com/stretchr/testify/require"
"github.com/waku-org/go-waku/waku/v2/utils"
2021-10-29 15:24:31 +00:00
)
func TestKeepAlive(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
2022-03-22 13:12:58 +00:00
host1, err := libp2p.New(libp2p.DefaultTransports, libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0"))
2021-10-29 15:24:31 +00:00
require.NoError(t, err)
2022-03-22 13:12:58 +00:00
host2, err := libp2p.New(libp2p.DefaultTransports, libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0"))
2021-10-29 15:24:31 +00:00
require.NoError(t, err)
host1.Peerstore().AddAddrs(host2.ID(), host2.Addrs(), peerstore.PermanentAddrTTL)
err = host1.Connect(ctx, host1.Peerstore().PeerInfo(host2.ID()))
require.NoError(t, err)
require.Len(t, host1.Network().Peers(), 1)
ctx2, cancel2 := context.WithTimeout(ctx, 3*time.Second)
defer cancel2()
wg := &sync.WaitGroup{}
w := &WakuNode{
host: host1,
wg: wg,
2022-05-06 19:29:31 +00:00
log: utils.Logger(),
keepAliveMutex: sync.Mutex{},
keepAliveFails: make(map[peer.ID]int),
}
w.wg.Add(1)
2023-01-06 22:37:57 +00:00
w.pingPeer(ctx2, host2.ID())
2021-10-29 15:24:31 +00:00
require.NoError(t, ctx.Err())
}