2018-04-24 18:43:43 +03:00
|
|
|
package rendezvous
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
2022-11-08 16:57:44 +01:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2022-11-08 15:59:16 +01:00
|
|
|
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
|
|
|
|
|
|
2022-11-15 10:50:42 +01:00
|
|
|
"github.com/berty/go-libp2p-rendezvous/test_utils"
|
2022-11-08 16:57:44 +01:00
|
|
|
"github.com/libp2p/go-libp2p/core/host"
|
2018-04-24 18:43:43 +03:00
|
|
|
)
|
|
|
|
|
|
2018-04-28 12:05:21 +03:00
|
|
|
func getRendezvousClients(t *testing.T, hosts []host.Host) []RendezvousClient {
|
|
|
|
|
clients := make([]RendezvousClient, len(hosts)-1)
|
|
|
|
|
for i, host := range hosts[1:] {
|
|
|
|
|
clients[i] = NewRendezvousClient(host, hosts[0].ID())
|
|
|
|
|
}
|
|
|
|
|
return clients
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-02 11:01:09 +02:00
|
|
|
func checkPeerInfo(t *testing.T, pi peer.AddrInfo, host host.Host) bool {
|
|
|
|
|
return test_utils.CheckPeerInfo(t, pi, host, true)
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-24 18:43:43 +03:00
|
|
|
func TestClientRegistrationAndDiscovery(t *testing.T) {
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
2022-11-08 15:59:16 +01:00
|
|
|
m := mocknet.New()
|
|
|
|
|
defer m.Close()
|
|
|
|
|
|
|
|
|
|
hosts := getRendezvousHosts(t, ctx, m, 5)
|
2018-04-24 18:43:43 +03:00
|
|
|
|
2018-04-26 14:06:58 +03:00
|
|
|
svc, err := makeRendezvousService(ctx, hosts[0], ":memory:")
|
2018-04-24 18:43:43 +03:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer svc.DB.Close()
|
|
|
|
|
|
|
|
|
|
clients := getRendezvousClients(t, hosts)
|
|
|
|
|
|
2019-06-05 13:44:44 -04:00
|
|
|
recordTTL, err := clients[0].Register(ctx, "foo1", DefaultTTL)
|
2018-04-24 18:43:43 +03:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2019-06-05 13:44:44 -04:00
|
|
|
if recordTTL != DefaultTTL*time.Second {
|
|
|
|
|
t.Fatalf("Expected record TTL to be %d seconds", DefaultTTL)
|
|
|
|
|
}
|
2018-04-24 18:43:43 +03:00
|
|
|
|
2018-04-28 12:05:21 +03:00
|
|
|
pi, cookie, err := clients[0].Discover(ctx, "foo1", 0, nil)
|
2018-04-24 18:43:43 +03:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if len(pi) != 1 {
|
|
|
|
|
t.Fatal("Expected 1 peer")
|
|
|
|
|
}
|
|
|
|
|
checkPeerInfo(t, pi[0], hosts[1])
|
|
|
|
|
|
|
|
|
|
for i, client := range clients[1:] {
|
2019-06-05 13:44:44 -04:00
|
|
|
recordTTL, err = client.Register(ctx, "foo1", DefaultTTL)
|
2018-04-24 18:43:43 +03:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2019-06-05 13:44:44 -04:00
|
|
|
if recordTTL != DefaultTTL*time.Second {
|
|
|
|
|
t.Fatalf("Expected record TTL to be %d seconds", DefaultTTL)
|
|
|
|
|
}
|
2018-04-24 18:43:43 +03:00
|
|
|
|
2018-04-28 12:05:21 +03:00
|
|
|
pi, cookie, err = clients[0].Discover(ctx, "foo1", 10, cookie)
|
2018-04-24 18:43:43 +03:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if len(pi) != 1 {
|
|
|
|
|
t.Fatal("Expected 1 peer")
|
|
|
|
|
}
|
|
|
|
|
checkPeerInfo(t, pi[0], hosts[2+i])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, client := range clients[1:] {
|
2018-04-28 12:05:21 +03:00
|
|
|
pi, _, err = client.Discover(ctx, "foo1", 10, nil)
|
2018-04-24 18:43:43 +03:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if len(pi) != 4 {
|
|
|
|
|
t.Fatal("Expected 4 registrations")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for j, p := range pi {
|
|
|
|
|
checkPeerInfo(t, p, hosts[1+j])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestClientRegistrationAndDiscoveryAsync(t *testing.T) {
|
2022-11-08 19:05:27 +01:00
|
|
|
DiscoverAsyncInterval = 1 * time.Second
|
|
|
|
|
|
2018-04-24 18:43:43 +03:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
2022-11-08 15:59:16 +01:00
|
|
|
m := mocknet.New()
|
|
|
|
|
defer m.Close()
|
|
|
|
|
|
|
|
|
|
hosts := getRendezvousHosts(t, ctx, m, 5)
|
2018-04-24 18:43:43 +03:00
|
|
|
|
2018-04-26 14:06:58 +03:00
|
|
|
svc, err := makeRendezvousService(ctx, hosts[0], ":memory:")
|
2018-04-24 18:43:43 +03:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer svc.DB.Close()
|
|
|
|
|
|
|
|
|
|
clients := getRendezvousClients(t, hosts)
|
|
|
|
|
|
2018-04-28 12:05:21 +03:00
|
|
|
ch, err := clients[0].DiscoverAsync(ctx, "foo1")
|
2018-04-24 18:43:43 +03:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i, client := range clients[0:] {
|
2019-06-05 13:44:44 -04:00
|
|
|
recordTTL, err := client.Register(ctx, "foo1", DefaultTTL)
|
2018-04-24 18:43:43 +03:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2019-06-05 13:44:44 -04:00
|
|
|
if recordTTL != DefaultTTL*time.Second {
|
|
|
|
|
t.Fatalf("Expected record TTL to be %d seconds", DefaultTTL)
|
|
|
|
|
}
|
2018-04-24 18:43:43 +03:00
|
|
|
|
|
|
|
|
pi := <-ch
|
|
|
|
|
checkPeerInfo(t, pi, hosts[1+i])
|
|
|
|
|
}
|
|
|
|
|
}
|