|
@ -135,20 +135,19 @@ func newBrokenRelay(t *testing.T, workAfter int) host.Host { |
|
|
return h |
|
|
return h |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func TestSingleRelay(t *testing.T) { |
|
|
func TestSingleCandidate(t *testing.T) { |
|
|
const numPeers = 5 |
|
|
var counter int |
|
|
peerChan := make(chan peer.AddrInfo) |
|
|
h := newPrivateNode(t, |
|
|
done := make(chan struct{}) |
|
|
autorelay.WithPeerSource(func(num int) <-chan peer.AddrInfo { |
|
|
go func() { |
|
|
counter++ |
|
|
defer close(done) |
|
|
require.Equal(t, 1, num) |
|
|
for i := 0; i < numPeers; i++ { |
|
|
peerChan := make(chan peer.AddrInfo, num) |
|
|
|
|
|
defer close(peerChan) |
|
|
r := newRelay(t) |
|
|
r := newRelay(t) |
|
|
t.Cleanup(func() { r.Close() }) |
|
|
t.Cleanup(func() { r.Close() }) |
|
|
peerChan <- peer.AddrInfo{ID: r.ID(), Addrs: r.Addrs()} |
|
|
peerChan <- peer.AddrInfo{ID: r.ID(), Addrs: r.Addrs()} |
|
|
} |
|
|
return peerChan |
|
|
}() |
|
|
}), |
|
|
h := newPrivateNode(t, |
|
|
|
|
|
autorelay.WithPeerSource(peerChan), |
|
|
|
|
|
autorelay.WithMaxCandidates(1), |
|
|
autorelay.WithMaxCandidates(1), |
|
|
autorelay.WithNumRelays(99999), |
|
|
autorelay.WithNumRelays(99999), |
|
|
autorelay.WithBootDelay(0), |
|
|
autorelay.WithBootDelay(0), |
|
@ -156,11 +155,39 @@ func TestSingleRelay(t *testing.T) { |
|
|
defer h.Close() |
|
|
defer h.Close() |
|
|
|
|
|
|
|
|
require.Eventually(t, func() bool { return numRelays(h) > 0 }, 3*time.Second, 100*time.Millisecond) |
|
|
require.Eventually(t, func() bool { return numRelays(h) > 0 }, 3*time.Second, 100*time.Millisecond) |
|
|
<-done |
|
|
|
|
|
// test that we don't add any more relays
|
|
|
// test that we don't add any more relays
|
|
|
require.Never(t, func() bool { return numRelays(h) != 1 }, 200*time.Millisecond, 50*time.Millisecond) |
|
|
require.Never(t, func() bool { return numRelays(h) > 1 }, 200*time.Millisecond, 50*time.Millisecond) |
|
|
|
|
|
require.Equal(t, 1, counter, "expected the peer source callback to only have been called once") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestSingleRelay(t *testing.T) { |
|
|
|
|
|
const numCandidates = 3 |
|
|
|
|
|
var called bool |
|
|
|
|
|
peerChan := make(chan peer.AddrInfo, numCandidates) |
|
|
|
|
|
for i := 0; i < numCandidates; i++ { |
|
|
|
|
|
r := newRelay(t) |
|
|
|
|
|
t.Cleanup(func() { r.Close() }) |
|
|
|
|
|
peerChan <- peer.AddrInfo{ID: r.ID(), Addrs: r.Addrs()} |
|
|
|
|
|
} |
|
|
|
|
|
close(peerChan) |
|
|
|
|
|
|
|
|
|
|
|
h := newPrivateNode(t, |
|
|
|
|
|
autorelay.WithPeerSource(func(num int) <-chan peer.AddrInfo { |
|
|
|
|
|
require.False(t, called, "expected the peer source callback to only have been called once") |
|
|
|
|
|
called = true |
|
|
|
|
|
require.Equal(t, numCandidates, num) |
|
|
|
|
|
return peerChan |
|
|
|
|
|
}), |
|
|
|
|
|
autorelay.WithMaxCandidates(numCandidates), |
|
|
|
|
|
autorelay.WithNumRelays(1), |
|
|
|
|
|
autorelay.WithBootDelay(0), |
|
|
|
|
|
) |
|
|
|
|
|
defer h.Close() |
|
|
|
|
|
|
|
|
|
|
|
require.Eventually(t, func() bool { return numRelays(h) > 0 }, 5*time.Second, 100*time.Millisecond) |
|
|
|
|
|
// test that we don't add any more relays
|
|
|
|
|
|
require.Never(t, func() bool { return numRelays(h) > 1 }, 200*time.Millisecond, 50*time.Millisecond) |
|
|
|
|
|
} |
|
|
func TestPreferRelayV2(t *testing.T) { |
|
|
func TestPreferRelayV2(t *testing.T) { |
|
|
r := newRelay(t) |
|
|
r := newRelay(t) |
|
|
defer r.Close() |
|
|
defer r.Close() |
|
@ -170,10 +197,14 @@ func TestPreferRelayV2(t *testing.T) { |
|
|
str.Reset() |
|
|
str.Reset() |
|
|
t.Fatal("used relay v1") |
|
|
t.Fatal("used relay v1") |
|
|
}) |
|
|
}) |
|
|
peerChan := make(chan peer.AddrInfo, 1) |
|
|
|
|
|
peerChan <- peer.AddrInfo{ID: r.ID(), Addrs: r.Addrs()} |
|
|
|
|
|
h := newPrivateNode(t, |
|
|
h := newPrivateNode(t, |
|
|
autorelay.WithPeerSource(peerChan), |
|
|
autorelay.WithPeerSource(func(int) <-chan peer.AddrInfo { |
|
|
|
|
|
peerChan := make(chan peer.AddrInfo, 1) |
|
|
|
|
|
defer close(peerChan) |
|
|
|
|
|
peerChan <- peer.AddrInfo{ID: r.ID(), Addrs: r.Addrs()} |
|
|
|
|
|
return peerChan |
|
|
|
|
|
}), |
|
|
autorelay.WithMaxCandidates(1), |
|
|
autorelay.WithMaxCandidates(1), |
|
|
autorelay.WithNumRelays(99999), |
|
|
autorelay.WithNumRelays(99999), |
|
|
autorelay.WithBootDelay(0), |
|
|
autorelay.WithBootDelay(0), |
|
@ -186,7 +217,7 @@ func TestPreferRelayV2(t *testing.T) { |
|
|
func TestWaitForCandidates(t *testing.T) { |
|
|
func TestWaitForCandidates(t *testing.T) { |
|
|
peerChan := make(chan peer.AddrInfo) |
|
|
peerChan := make(chan peer.AddrInfo) |
|
|
h := newPrivateNode(t, |
|
|
h := newPrivateNode(t, |
|
|
autorelay.WithPeerSource(peerChan), |
|
|
autorelay.WithPeerSource(func(int) <-chan peer.AddrInfo { return peerChan }), |
|
|
autorelay.WithMinCandidates(2), |
|
|
autorelay.WithMinCandidates(2), |
|
|
autorelay.WithNumRelays(1), |
|
|
autorelay.WithNumRelays(1), |
|
|
autorelay.WithBootDelay(time.Hour), |
|
|
autorelay.WithBootDelay(time.Hour), |
|
@ -212,7 +243,7 @@ func TestBackoff(t *testing.T) { |
|
|
peerChan := make(chan peer.AddrInfo) |
|
|
peerChan := make(chan peer.AddrInfo) |
|
|
cl := clock.NewMock() |
|
|
cl := clock.NewMock() |
|
|
h := newPrivateNode(t, |
|
|
h := newPrivateNode(t, |
|
|
autorelay.WithPeerSource(peerChan), |
|
|
autorelay.WithPeerSource(func(int) <-chan peer.AddrInfo { return peerChan }), |
|
|
autorelay.WithNumRelays(1), |
|
|
autorelay.WithNumRelays(1), |
|
|
autorelay.WithBootDelay(0), |
|
|
autorelay.WithBootDelay(0), |
|
|
autorelay.WithBackoff(backoff), |
|
|
autorelay.WithBackoff(backoff), |
|
@ -229,7 +260,7 @@ func TestBackoff(t *testing.T) { |
|
|
cl.Add(backoff * 2 / 3) |
|
|
cl.Add(backoff * 2 / 3) |
|
|
require.Never(t, func() bool { return numRelays(h) > 0 }, 100*time.Millisecond, 20*time.Millisecond) |
|
|
require.Never(t, func() bool { return numRelays(h) > 0 }, 100*time.Millisecond, 20*time.Millisecond) |
|
|
cl.Add(backoff * 2 / 3) |
|
|
cl.Add(backoff * 2 / 3) |
|
|
require.Eventually(t, func() bool { return numRelays(h) > 0 }, 500*time.Millisecond, 10*time.Millisecond) |
|
|
require.Eventually(t, func() bool { return numRelays(h) > 0 }, 3*time.Second, 100*time.Millisecond) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func TestMaxBackoffs(t *testing.T) { |
|
|
func TestMaxBackoffs(t *testing.T) { |
|
@ -237,7 +268,7 @@ func TestMaxBackoffs(t *testing.T) { |
|
|
cl := clock.NewMock() |
|
|
cl := clock.NewMock() |
|
|
peerChan := make(chan peer.AddrInfo) |
|
|
peerChan := make(chan peer.AddrInfo) |
|
|
h := newPrivateNode(t, |
|
|
h := newPrivateNode(t, |
|
|
autorelay.WithPeerSource(peerChan), |
|
|
autorelay.WithPeerSource(func(int) <-chan peer.AddrInfo { return peerChan }), |
|
|
autorelay.WithNumRelays(1), |
|
|
autorelay.WithNumRelays(1), |
|
|
autorelay.WithBootDelay(0), |
|
|
autorelay.WithBootDelay(0), |
|
|
autorelay.WithBackoff(backoff), |
|
|
autorelay.WithBackoff(backoff), |
|
@ -277,14 +308,14 @@ func TestStaticRelays(t *testing.T) { |
|
|
|
|
|
|
|
|
func TestRelayV1(t *testing.T) { |
|
|
func TestRelayV1(t *testing.T) { |
|
|
t.Run("relay v1 support disabled", func(t *testing.T) { |
|
|
t.Run("relay v1 support disabled", func(t *testing.T) { |
|
|
peerChan := make(chan peer.AddrInfo) |
|
|
peerChan := make(chan peer.AddrInfo, 1) |
|
|
go func() { |
|
|
r := newRelayV1(t) |
|
|
r := newRelayV1(t) |
|
|
t.Cleanup(func() { r.Close() }) |
|
|
peerChan <- peer.AddrInfo{ID: r.ID(), Addrs: r.Addrs()} |
|
|
peerChan <- peer.AddrInfo{ID: r.ID(), Addrs: r.Addrs()} |
|
|
t.Cleanup(func() { r.Close() }) |
|
|
close(peerChan) |
|
|
}() |
|
|
|
|
|
h := newPrivateNode(t, |
|
|
h := newPrivateNode(t, |
|
|
autorelay.WithPeerSource(peerChan), |
|
|
autorelay.WithPeerSource(func(int) <-chan peer.AddrInfo { return peerChan }), |
|
|
autorelay.WithBootDelay(0), |
|
|
autorelay.WithBootDelay(0), |
|
|
) |
|
|
) |
|
|
defer h.Close() |
|
|
defer h.Close() |
|
@ -293,14 +324,14 @@ func TestRelayV1(t *testing.T) { |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
t.Run("relay v1 support enabled", func(t *testing.T) { |
|
|
t.Run("relay v1 support enabled", func(t *testing.T) { |
|
|
peerChan := make(chan peer.AddrInfo) |
|
|
peerChan := make(chan peer.AddrInfo, 1) |
|
|
go func() { |
|
|
r := newRelayV1(t) |
|
|
r := newRelayV1(t) |
|
|
t.Cleanup(func() { r.Close() }) |
|
|
peerChan <- peer.AddrInfo{ID: r.ID(), Addrs: r.Addrs()} |
|
|
peerChan <- peer.AddrInfo{ID: r.ID(), Addrs: r.Addrs()} |
|
|
t.Cleanup(func() { r.Close() }) |
|
|
close(peerChan) |
|
|
}() |
|
|
|
|
|
h := newPrivateNode(t, |
|
|
h := newPrivateNode(t, |
|
|
autorelay.WithPeerSource(peerChan), |
|
|
autorelay.WithPeerSource(func(int) <-chan peer.AddrInfo { return peerChan }), |
|
|
autorelay.WithBootDelay(0), |
|
|
autorelay.WithBootDelay(0), |
|
|
autorelay.WithCircuitV1Support(), |
|
|
autorelay.WithCircuitV1Support(), |
|
|
) |
|
|
) |
|
|