-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
announce_test.go
58 lines (52 loc) · 1.42 KB
/
announce_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package dht
import (
"context"
"crypto/rand"
"net"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/time/rate"
)
func TestAnnounceNoStartingNodes(t *testing.T) {
s, err := NewServer(&ServerConfig{
Conn: mustListen(":0"),
NoSecurity: true,
})
require.NoError(t, err)
defer s.Close()
var ih [20]byte
copy(ih[:], "blah")
_, err = s.Announce(ih, 0, true)
require.EqualError(t, err, "no initial nodes")
}
func randomInfohash() (ih [20]byte) {
rand.Read(ih[:])
return
}
func TestAnnounceStopsNoPending(t *testing.T) {
s, err := NewServer(&ServerConfig{
Conn: mustListen(":0"),
StartingNodes: func() ([]Addr, error) {
return []Addr{NewAddr(&net.TCPAddr{})}, nil
},
})
require.NoError(t, err)
a, err := s.Announce(randomInfohash(), 0, true)
require.NoError(t, err)
defer a.Close()
<-a.Peers
}
// Assert that rate.Limiter won't wake-up waiters once they have determined a
// delay. This means we can't use it to cancel reservations for queries that
// are successful.
func TestRateLimiterInadequate(t *testing.T) {
rl := rate.NewLimiter(rate.Every(time.Hour), 1)
assert.NoError(t, rl.Wait(context.Background()))
time.AfterFunc(time.Millisecond, func() { rl.AllowN(time.Now(), -1) })
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
time.AfterFunc(2*time.Millisecond, cancel)
assert.EqualValues(t, context.Canceled, rl.Wait(ctx))
}