-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil_test.go
53 lines (48 loc) · 1.06 KB
/
util_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
package dinghy
import (
"testing"
"time"
)
func Test_hashToUint(t *testing.T) {
type args struct {
s string
max int
}
tests := []struct {
name string
args args
want int
}{
{"00", args{"", 150}, 61},
{"01", args{"abc.com", 150}, 132},
{"02", args{"bbc.com", 150}, 93},
{"03", args{"bbc.com:8100", 150}, 46},
{"04", args{"bbc1.com:8100", 150}, 51},
{"05", args{"bbc2.com:8100", 150}, 26},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
id, err := hashToInt(tt.args.s, tt.args.max)
if err != nil {
t.Fatal(err)
}
equals(t, id, tt.want)
})
}
}
func Test_randomTimeout(t *testing.T) {
// https://github.com/hashicorp/raft/blob/5f09c4ffdbcd2a53768e78c47717415de12b6728/util_test.go#L10
start := time.Now()
timeout := randomTimeout(time.Millisecond)
select {
case <-timeout:
diff := time.Since(start)
if diff < time.Millisecond {
t.Fatalf("fired early")
}
case <-time.After(3 * time.Millisecond):
t.Fatalf("timeout")
}
timeout = randomTimeout(0)
equals(t, (<-chan time.Time)(nil), timeout)
}