Skip to content

Commit

Permalink
Merge pull request #425 from libp2p/fix/refresh-wait
Browse files Browse the repository at this point in the history
fix: always send the result channel when triggering a refresh
  • Loading branch information
Stebalien committed Dec 13, 2019
2 parents 2e6adb8 + e512351 commit dd3d8fb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
5 changes: 1 addition & 4 deletions dht_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,6 @@ func (dht *IpfsDHT) Bootstrap(_ context.Context) error {
// error and close. The channel is buffered and safe to ignore.
func (dht *IpfsDHT) RefreshRoutingTable() <-chan error {
res := make(chan error, 1)
select {
case dht.triggerRtRefresh <- res:
default:
}
dht.triggerRtRefresh <- res
return res
}
42 changes: 41 additions & 1 deletion dht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ func connect(t *testing.T, ctx context.Context, a, b *IpfsDHT) {
}

func bootstrap(t *testing.T, ctx context.Context, dhts []*IpfsDHT) {

ctx, cancel := context.WithCancel(ctx)
defer cancel()

Expand All @@ -214,6 +213,47 @@ func bootstrap(t *testing.T, ctx context.Context, dhts []*IpfsDHT) {
}
}

// Check to make sure we always signal the RefreshRoutingTable channel.
func TestRefreshMultiple(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

dhts := setupDHTS(t, ctx, 5)
defer func() {
for _, dht := range dhts {
dht.Close()
defer dht.host.Close()
}
}()

for _, dht := range dhts[1:] {
connect(t, ctx, dhts[0], dht)
}

a := dhts[0].RefreshRoutingTable()
time.Sleep(time.Nanosecond)
b := dhts[0].RefreshRoutingTable()
time.Sleep(time.Nanosecond)
c := dhts[0].RefreshRoutingTable()

// make sure that all of these eventually return
select {
case <-a:
case <-ctx.Done():
t.Fatal("first channel didn't signal")
}
select {
case <-b:
case <-ctx.Done():
t.Fatal("second channel didn't signal")
}
select {
case <-c:
case <-ctx.Done():
t.Fatal("third channel didn't signal")
}
}

func TestValueGetSet(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down

0 comments on commit dd3d8fb

Please sign in to comment.