2017-07-26 23:47:23 -07:00
|
|
|
package dht
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-02-16 18:55:42 -08:00
|
|
|
"fmt"
|
2017-07-26 23:47:23 -07:00
|
|
|
"testing"
|
2018-02-16 18:55:42 -08:00
|
|
|
"time"
|
|
|
|
|
2019-05-26 23:33:15 +01:00
|
|
|
tu "github.com/libp2p/go-libp2p-testing/etc"
|
2020-03-06 18:27:36 +05:30
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2017-07-26 23:47:23 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNotifieeMultipleConn(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2020-03-06 18:27:36 +05:30
|
|
|
d1 := setupDHT(ctx, t, false, RoutingTableCheckInterval(50*time.Millisecond))
|
|
|
|
d2 := setupDHT(ctx, t, false, RoutingTableCheckInterval(50*time.Millisecond))
|
2017-07-26 23:47:23 -07:00
|
|
|
|
2020-03-06 14:56:03 -05:00
|
|
|
nn1, err := newSubscriberNotifiee(d1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
nn2, err := newSubscriberNotifiee(d2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-26 23:47:23 -07:00
|
|
|
|
|
|
|
connect(t, ctx, d1, d2)
|
|
|
|
c12 := d1.host.Network().ConnsToPeer(d2.self)[0]
|
|
|
|
c21 := d2.host.Network().ConnsToPeer(d1.self)[0]
|
|
|
|
|
|
|
|
// Pretend to reestablish/re-kill connection
|
|
|
|
nn1.Connected(d1.host.Network(), c12)
|
|
|
|
nn2.Connected(d2.host.Network(), c21)
|
|
|
|
|
|
|
|
if !checkRoutingTable(d1, d2) {
|
|
|
|
t.Fatal("no routes")
|
|
|
|
}
|
2020-03-06 18:27:36 +05:30
|
|
|
|
|
|
|
// we are still connected, so the disconnect notification should be a No-op
|
2017-07-26 23:47:23 -07:00
|
|
|
nn1.Disconnected(d1.host.Network(), c12)
|
|
|
|
nn2.Disconnected(d2.host.Network(), c21)
|
|
|
|
|
|
|
|
if !checkRoutingTable(d1, d2) {
|
|
|
|
t.Fatal("no routes")
|
|
|
|
}
|
|
|
|
|
2020-03-06 18:27:36 +05:30
|
|
|
// the connection close should now mark the peer as missing in the RT for both peers
|
|
|
|
// because of the disconnect notification
|
2017-07-26 23:47:23 -07:00
|
|
|
for _, conn := range d1.host.Network().ConnsToPeer(d2.self) {
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
for _, conn := range d2.host.Network().ConnsToPeer(d1.self) {
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
|
2020-03-06 18:27:36 +05:30
|
|
|
// close both the hosts so all connection attempts to them by RT Peer validation fail
|
|
|
|
d1.host.Close()
|
|
|
|
d2.host.Close()
|
|
|
|
|
|
|
|
// wait context will ensure that the RT cleanup completes
|
|
|
|
waitCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
require.NoError(t, tu.WaitFor(waitCtx, func() error {
|
2018-02-16 18:55:42 -08:00
|
|
|
if checkRoutingTable(d1, d2) {
|
|
|
|
return fmt.Errorf("should not have routes")
|
|
|
|
}
|
|
|
|
return nil
|
2020-03-06 18:27:36 +05:30
|
|
|
}))
|
2017-07-26 23:47:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNotifieeFuzz(t *testing.T) {
|
2018-02-16 18:55:42 -08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
|
2017-07-26 23:47:23 -07:00
|
|
|
defer cancel()
|
|
|
|
|
2020-03-06 18:27:36 +05:30
|
|
|
d1 := setupDHT(ctx, t, false, RoutingTableCheckInterval(50*time.Millisecond))
|
|
|
|
d2 := setupDHT(ctx, t, false, RoutingTableCheckInterval(50*time.Millisecond))
|
2017-07-26 23:47:23 -07:00
|
|
|
|
2019-01-25 12:05:08 +11:00
|
|
|
for i := 0; i < 10; i++ {
|
2017-07-26 23:47:23 -07:00
|
|
|
connectNoSync(t, ctx, d1, d2)
|
|
|
|
for _, conn := range d1.host.Network().ConnsToPeer(d2.self) {
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
}
|
2020-03-06 18:27:36 +05:30
|
|
|
|
|
|
|
// close both hosts so peer validation reconnect fails
|
|
|
|
d1.host.Close()
|
|
|
|
d2.host.Close()
|
|
|
|
require.NoError(t, tu.WaitFor(ctx, func() error {
|
2018-02-16 18:55:42 -08:00
|
|
|
if checkRoutingTable(d1, d2) {
|
|
|
|
return fmt.Errorf("should not have routes")
|
|
|
|
}
|
|
|
|
return nil
|
2020-03-06 18:27:36 +05:30
|
|
|
}))
|
2017-07-26 23:47:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkRoutingTable(a, b *IpfsDHT) bool {
|
|
|
|
// loop until connection notification has been received.
|
|
|
|
// under high load, this may not happen as immediately as we would like.
|
|
|
|
return a.routingTable.Find(b.self) != "" && b.routingTable.Find(a.self) != ""
|
|
|
|
}
|