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
|
package config
import (
"fmt"
"net"
"testing"
)
func TestNodePriority(t *testing.T) {
t.Parallel()
conf, _ := New(WithNodes(
Node{Hostname: "localhost", Port: 1234, Priority: 200},
Node{Hostname: "hamburger", Port: 4321, Priority: 100}))
num, _ := conf.NodePriority("localhost")
if num != 200 {
t.Errorf("localhost should have priority 200 but has %d", num)
}
num, _ = conf.NodePriority("hamburger")
if num != 100 {
t.Errorf("hamburger should have priority 100 but has %d", num)
}
}
func TestIsNode(t *testing.T) {
t.Parallel()
conf, _ := New(WithNodes(
Node{Hostname: "localhost", Port: 1234, Priority: 200},
Node{Hostname: "hamburger", Port: 4321, Priority: 100}))
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(WithNodes(
Node{Hostname: "localhost", Port: 1234, Priority: 200},
Node{Hostname: "hamburger", Port: 4321, Priority: 100}))
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)
}
}
|