|
| 1 | +package boot |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/libp2p/go-libp2p" |
| 9 | + "github.com/libp2p/go-libp2p-kad-dht/dual" |
| 10 | + "github.com/libp2p/go-libp2p/core/host" |
| 11 | + "github.com/libp2p/go-libp2p/core/peer" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + "github.com/wetware/go/system" |
| 14 | + "golang.org/x/sync/errgroup" |
| 15 | +) |
| 16 | + |
| 17 | +// TestDHT verifies that the DHT service correctly handles peer discovery and shutdown. |
| 18 | +// It tests: |
| 19 | +// 1. Creation and bootstrapping of two DHT nodes |
| 20 | +// 2. Connection establishment between peers |
| 21 | +// 3. Service startup and peer discovery |
| 22 | +// 4. Clean shutdown on context cancellation |
| 23 | +// |
| 24 | +// The test creates two libp2p hosts with their own DHT instances, connects them, |
| 25 | +// and verifies they can discover each other through the DHT service. It then ensures |
| 26 | +// the services shut down cleanly when their context is cancelled. |
| 27 | +func TestDHT(t *testing.T) { |
| 28 | + t.Parallel() |
| 29 | + |
| 30 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 31 | + defer cancel() |
| 32 | + |
| 33 | + // Create two hosts |
| 34 | + h1, err := libp2p.New() |
| 35 | + require.NoError(t, err) |
| 36 | + defer h1.Close() |
| 37 | + |
| 38 | + h2, err := libp2p.New() |
| 39 | + require.NoError(t, err) |
| 40 | + defer h2.Close() |
| 41 | + |
| 42 | + // Create DHT instances for both hosts |
| 43 | + dht1, err := dual.New(ctx, h1) |
| 44 | + require.NoError(t, err) |
| 45 | + defer dht1.Close() |
| 46 | + |
| 47 | + dht2, err := dual.New(ctx, h2) |
| 48 | + require.NoError(t, err) |
| 49 | + defer dht2.Close() |
| 50 | + |
| 51 | + // Bootstrap the DHTs |
| 52 | + err = dht1.Bootstrap(ctx) |
| 53 | + require.NoError(t, err) |
| 54 | + |
| 55 | + err = dht2.Bootstrap(ctx) |
| 56 | + require.NoError(t, err) |
| 57 | + |
| 58 | + // Create environments for both hosts |
| 59 | + env1 := &system.Env{ |
| 60 | + Host: h1, |
| 61 | + DHT: dht1, |
| 62 | + } |
| 63 | + |
| 64 | + env2 := &system.Env{ |
| 65 | + Host: h2, |
| 66 | + DHT: dht2, |
| 67 | + } |
| 68 | + |
| 69 | + // Connect the hosts |
| 70 | + err = connectHosts(h1, h2) |
| 71 | + require.NoError(t, err) |
| 72 | + |
| 73 | + // Create DHT services |
| 74 | + d1 := &DHT{Env: env1} |
| 75 | + d2 := &DHT{Env: env2} |
| 76 | + |
| 77 | + // Create a sub-context for the services that we can cancel independently |
| 78 | + svcCtx, svcCancel := context.WithCancel(ctx) |
| 79 | + defer svcCancel() // Ensure services are cancelled even if test fails |
| 80 | + |
| 81 | + // Start DHT services with errgroup |
| 82 | + g := new(errgroup.Group) |
| 83 | + |
| 84 | + g.Go(func() error { |
| 85 | + return d1.Serve(svcCtx) |
| 86 | + }) |
| 87 | + |
| 88 | + g.Go(func() error { |
| 89 | + return d2.Serve(svcCtx) |
| 90 | + }) |
| 91 | + |
| 92 | + // Wait for peer discovery |
| 93 | + require.Eventually(t, func() bool { |
| 94 | + return len(h1.Network().Peers()) > 0 && len(h2.Network().Peers()) > 0 |
| 95 | + }, 3*time.Second, 100*time.Millisecond, "peers should discover each other") |
| 96 | + |
| 97 | + // Verify that peers are connected |
| 98 | + require.Contains(t, h1.Network().Peers(), h2.ID(), "h1 should be connected to h2") |
| 99 | + require.Contains(t, h2.Network().Peers(), h1.ID(), "h2 should be connected to h1") |
| 100 | + |
| 101 | + // Cancel service context |
| 102 | + svcCancel() |
| 103 | + |
| 104 | + // Wait for services to shut down with timeout |
| 105 | + done := make(chan error, 1) |
| 106 | + go func() { |
| 107 | + done <- g.Wait() |
| 108 | + }() |
| 109 | + |
| 110 | + select { |
| 111 | + case err := <-done: |
| 112 | + require.ErrorIs(t, err, context.Canceled) |
| 113 | + case <-time.After(2 * time.Second): |
| 114 | + t.Fatal("timeout waiting for services to shut down") |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// TestDHT_HandlePeerFound verifies that the DHT service correctly handles peer information |
| 119 | +// when a new peer is discovered. It tests: |
| 120 | +// 1. Proper storage of peer addresses in the peerstore |
| 121 | +// 2. Correct handling of multiaddress sets |
| 122 | +// 3. Address persistence after peer discovery |
| 123 | +// |
| 124 | +// The test creates a mock peer with a set of addresses and verifies that when HandlePeerFound |
| 125 | +// is called, those addresses are correctly stored in the peerstore. It uses string-based |
| 126 | +// comparison of address sets to handle variations in address ordering and representation. |
| 127 | +func TestDHT_HandlePeerFound(t *testing.T) { |
| 128 | + t.Parallel() |
| 129 | + |
| 130 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 131 | + defer cancel() |
| 132 | + |
| 133 | + // Create a host |
| 134 | + h, err := libp2p.New() |
| 135 | + require.NoError(t, err) |
| 136 | + defer h.Close() |
| 137 | + |
| 138 | + // Create DHT instance |
| 139 | + d, err := dual.New(ctx, h) |
| 140 | + require.NoError(t, err) |
| 141 | + defer d.Close() |
| 142 | + |
| 143 | + // Create environment |
| 144 | + env := &system.Env{ |
| 145 | + Host: h, |
| 146 | + DHT: d, |
| 147 | + } |
| 148 | + |
| 149 | + // Create a mock peer |
| 150 | + mockPeer, err := libp2p.New() |
| 151 | + require.NoError(t, err) |
| 152 | + defer mockPeer.Close() |
| 153 | + |
| 154 | + // Create peer info |
| 155 | + peerInfo := peer.AddrInfo{ |
| 156 | + ID: mockPeer.ID(), |
| 157 | + Addrs: mockPeer.Addrs(), |
| 158 | + } |
| 159 | + |
| 160 | + // Test HandlePeerFound |
| 161 | + env.HandlePeerFound(peerInfo) |
| 162 | + |
| 163 | + // Verify peer is in peerstore |
| 164 | + storedAddrs := h.Peerstore().Addrs(mockPeer.ID()) |
| 165 | + require.NotEmpty(t, storedAddrs, "peer should be added to peerstore") |
| 166 | + |
| 167 | + // Convert addresses to strings for easier comparison |
| 168 | + expectedAddrs := make(map[string]struct{}) |
| 169 | + for _, addr := range peerInfo.Addrs { |
| 170 | + expectedAddrs[addr.String()] = struct{}{} |
| 171 | + } |
| 172 | + |
| 173 | + storedAddrStrings := make(map[string]struct{}) |
| 174 | + for _, addr := range storedAddrs { |
| 175 | + storedAddrStrings[addr.String()] = struct{}{} |
| 176 | + } |
| 177 | + |
| 178 | + // Compare address sets |
| 179 | + require.Equal(t, expectedAddrs, storedAddrStrings, "stored addresses should match expected addresses") |
| 180 | +} |
| 181 | + |
| 182 | +// connectHosts establishes a direct connection between two libp2p hosts. |
| 183 | +// It takes the source (h1) and target (h2) hosts and attempts to connect them |
| 184 | +// using h2's peer info (ID and addresses). This is a helper function used |
| 185 | +// to establish initial connectivity in tests. |
| 186 | +func connectHosts(h1, h2 host.Host) error { |
| 187 | + h2Info := peer.AddrInfo{ |
| 188 | + ID: h2.ID(), |
| 189 | + Addrs: h2.Addrs(), |
| 190 | + } |
| 191 | + return h1.Connect(context.Background(), h2Info) |
| 192 | +} |
0 commit comments