Skip to content

Commit

Permalink
test: add test for when remote peers switch between client and server…
Browse files Browse the repository at this point in the history
… modes
  • Loading branch information
aschmahmann committed Mar 3, 2020
1 parent 844aa70 commit 28fea97
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions dht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/binary"
"errors"
"fmt"
"github.com/libp2p/go-libp2p-core/protocol"
"math/rand"
"sort"
"strings"
Expand Down Expand Up @@ -1581,6 +1582,44 @@ func TestProvideDisabled(t *testing.T) {
}
}

func TestHandleRemotePeerProtocolChanges(t *testing.T) {
proto := protocol.ID("/v1/dht")
ctx := context.Background()
os := []opts.Option{
opts.Protocols(proto),
opts.Client(false),
opts.NamespacedValidator("v", blankValidator{}),
opts.DisableAutoRefresh(),
}

// start host 1 that speaks dht v1
dhtA, err := New(ctx, bhost.New(swarmt.GenSwarm(t, ctx, swarmt.OptDisableReuseport)), os...)
require.NoError(t, err)
defer dhtA.Close()

// start host 2 that also speaks dht v1
dhtB, err := New(ctx, bhost.New(swarmt.GenSwarm(t, ctx, swarmt.OptDisableReuseport)), os...)
require.NoError(t, err)
defer dhtB.Close()

connect(t, ctx, dhtA, dhtB)

// now assert both have each other in their RT
require.True(t, waitForWellFormedTables(t, []*IpfsDHT{dhtA, dhtB}, 1, 1, 10*time.Second), "both RT should have one peer each")

// dhtB becomes a client
require.NoError(t, dhtB.SetMode(ModeClient))

// which means that dhtA should evict it from it's RT
require.True(t, waitForWellFormedTables(t, []*IpfsDHT{dhtA}, 0, 0, 10*time.Second), "dHTA routing table should have 0 peers")

// dhtB becomes a server
require.NoError(t, dhtB.SetMode(ModeServer))

// which means dhtA should have it in the RT again
require.True(t, waitForWellFormedTables(t, []*IpfsDHT{dhtA}, 1, 1, 10*time.Second), "dHTA routing table should have 1 peers")
}

func TestGetSetPluggedProtocol(t *testing.T) {
t.Run("PutValue/GetValue - same protocol", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
Expand Down

0 comments on commit 28fea97

Please sign in to comment.