mirror of
https://github.com/libp2p/go-libp2p.git
synced 2025-10-05 16:17:12 +08:00
54 lines
1.0 KiB
Go
54 lines
1.0 KiB
Go
package ping
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
netutil "github.com/libp2p/go-libp2p-netutil"
|
|
peer "github.com/libp2p/go-libp2p-peer"
|
|
pstore "github.com/libp2p/go-libp2p-peerstore"
|
|
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
|
|
)
|
|
|
|
func TestPing(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
h1 := bhost.New(netutil.GenSwarmNetwork(t, ctx))
|
|
h2 := bhost.New(netutil.GenSwarmNetwork(t, ctx))
|
|
|
|
err := h1.Connect(ctx, pstore.PeerInfo{
|
|
ID: h2.ID(),
|
|
Addrs: h2.Addrs(),
|
|
})
|
|
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ps1 := NewPingService(h1)
|
|
ps2 := NewPingService(h2)
|
|
|
|
testPing(t, ps1, h2.ID())
|
|
testPing(t, ps2, h1.ID())
|
|
}
|
|
|
|
func testPing(t *testing.T, ps *PingService, p peer.ID) {
|
|
pctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
ts, err := ps.Ping(pctx, p)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
for i := 0; i < 5; i++ {
|
|
select {
|
|
case took := <-ts:
|
|
t.Log("ping took: ", took)
|
|
case <-time.After(time.Second * 4):
|
|
t.Fatal("failed to receive ping")
|
|
}
|
|
}
|
|
|
|
}
|