-
Notifications
You must be signed in to change notification settings - Fork 14
/
node_test.go
69 lines (63 loc) · 1.17 KB
/
node_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
59
60
61
62
63
64
65
66
67
68
69
package gorums
import (
"errors"
"fmt"
"testing"
"time"
)
func TestNodeSort(t *testing.T) {
nodes := []*RawNode{
{
id: 100,
channel: &channel{
lastError: nil,
latency: time.Second,
},
},
{
id: 101,
channel: &channel{
lastError: errors.New("some error"),
latency: 250 * time.Millisecond,
},
},
{
id: 42,
channel: &channel{
lastError: nil,
latency: 300 * time.Millisecond,
},
},
{
id: 99,
channel: &channel{
lastError: errors.New("some error"),
latency: 500 * time.Millisecond,
},
},
}
n := len(nodes)
OrderedBy(ID).Sort(nodes)
for i := n - 1; i > 0; i-- {
if nodes[i].id < nodes[i-1].id {
t.Error("by id: not sorted")
printNodes(t, nodes)
}
}
OrderedBy(LastNodeError).Sort(nodes)
for i := n - 1; i > 0; i-- {
if nodes[i].LastErr() == nil && nodes[i-1].LastErr() != nil {
t.Error("by error: not sorted")
printNodes(t, nodes)
}
}
}
func printNodes(t *testing.T, nodes []*RawNode) {
t.Helper()
for i, n := range nodes {
nodeStr := fmt.Sprintf(
"%d: node %d | addr: %s | latency: %v | err: %v",
i, n.id, n.addr, n.Latency(), n.LastErr())
t.Logf("%s", nodeStr)
}
}