|
| 1 | +package dht |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/libp2p/go-libp2p-core/event" |
| 9 | + kb "github.com/libp2p/go-libp2p-kbucket" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestSelfWalkOnAddressChange(t *testing.T) { |
| 15 | + ctx := context.Background() |
| 16 | + // create three DHT instances with auto refresh disabled |
| 17 | + d1 := setupDHT(ctx, t, false, DisableAutoRefresh()) |
| 18 | + d2 := setupDHT(ctx, t, false, DisableAutoRefresh()) |
| 19 | + d3 := setupDHT(ctx, t, false, DisableAutoRefresh()) |
| 20 | + |
| 21 | + var connectedTo *IpfsDHT |
| 22 | + // connect d1 to whoever is "further" |
| 23 | + if kb.CommonPrefixLen(kb.ConvertPeerID(d1.self), kb.ConvertPeerID(d2.self)) <= |
| 24 | + kb.CommonPrefixLen(kb.ConvertPeerID(d1.self), kb.ConvertPeerID(d3.self)) { |
| 25 | + connect(t, ctx, d1, d3) |
| 26 | + connectedTo = d3 |
| 27 | + } else { |
| 28 | + connect(t, ctx, d1, d2) |
| 29 | + connectedTo = d2 |
| 30 | + } |
| 31 | + |
| 32 | + // then connect d2 AND d3 |
| 33 | + connect(t, ctx, d2, d3) |
| 34 | + |
| 35 | + // d1 should have ONLY 1 peer in it's RT |
| 36 | + waitForWellFormedTables(t, []*IpfsDHT{d1}, 1, 1, 2*time.Second) |
| 37 | + require.Equal(t, connectedTo.self, d1.routingTable.ListPeers()[0]) |
| 38 | + |
| 39 | + // now emit the address change event |
| 40 | + em, err := d1.host.EventBus().Emitter(&event.EvtLocalAddressesUpdated{}) |
| 41 | + require.NoError(t, err) |
| 42 | + require.NoError(t, em.Emit(event.EvtLocalAddressesUpdated{})) |
| 43 | + waitForWellFormedTables(t, []*IpfsDHT{d1}, 2, 2, 2*time.Second) |
| 44 | + // it should now have both peers in the RT |
| 45 | + ps := d1.routingTable.ListPeers() |
| 46 | + require.Contains(t, ps, d2.self) |
| 47 | + require.Contains(t, ps, d3.self) |
| 48 | +} |
0 commit comments