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
|
package config
import (
"fmt"
"net"
"testing"
)
func TestNodeNumber(t *testing.T) {
t.Parallel()
conf, _ := New(Config{Nodes: []string{"localhost:1234", "hamburger:4321"}})
num := conf.NodeNumber("localhost")
if num != 0 {
t.Errorf("localhost should be node number 0 but is %d", num)
}
num = conf.NodeNumber("hamburger")
if num != 1 {
t.Errorf("hamburger should be node number 1 but is %d", num)
}
}
func TestIsNode(t *testing.T) {
t.Parallel()
conf, _ := New(Config{Nodes: []string{"localhost:1234", "hamburger:4321"}})
remoteAddr := "localhost:323232"
if !conf.IsNode(remoteAddr) {
t.Errorf("%s should be node of %v", remoteAddr, conf.Nodes)
}
remoteAddr = "foo.zone:2345"
if conf.IsNode(remoteAddr) {
t.Errorf("%s should not be node of %v", remoteAddr, conf.Nodes)
}
}
func TestIsNodeWithLookup(t *testing.T) {
t.Parallel()
conf, _ := New(Config{Nodes: []string{"localhost:1234", "hamburger:4321"}})
lookupIP := func(addr string) ([]net.IP, error) {
switch addr {
case "localhost":
return []net.IP{{127, 0, 0, 1}}, nil
case "hamburger":
return []net.IP{{8, 8, 8, 8}}, nil
default:
return []net.IP{}, fmt.Errorf("Can't resolve %s", addr)
}
}
remoteAddr := "127.0.0.1:323232"
if !conf.IsNodeWithLookup(remoteAddr, lookupIP) {
t.Errorf("%s should be node of %v", remoteAddr, conf.Nodes)
}
remoteAddr = "9.9.9.9:2345"
if conf.IsNodeWithLookup(remoteAddr, lookupIP) {
t.Errorf("%s should not be node of %v", remoteAddr, conf.Nodes)
}
}
|