-
Notifications
You must be signed in to change notification settings - Fork 9
/
strategy_test.go
89 lines (70 loc) · 1.82 KB
/
strategy_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"sort"
"testing"
"github.com/benschw/srv-lb/dns"
"github.com/benschw/srv-lb/lb"
"github.com/benschw/srv-lb/strategy/first"
"github.com/benschw/srv-lb/strategy/random"
"github.com/stretchr/testify/assert"
)
type ByConnectionString []dns.Address
func (a ByConnectionString) Len() int { return len(a) }
func (a ByConnectionString) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByConnectionString) Less(i, j int) bool {
if a[i].Address == a[j].Address {
return a[i].Port < a[j].Port
}
return a[i].Address < a[j].Address
}
func TestRoundRobinStrategy(t *testing.T) {
lib, err := dns.NewDefaultLookupLib()
assert.Nil(t, err)
//given
c := lb.NewGeneric(&lb.Config{
Dns: lib,
Strategy: lb.RoundRobinStrategy,
})
// when
srvName := "foo.service.fligl.io"
add1, err1 := c.Next(srvName)
add2, err2 := c.Next(srvName)
// then
assert.Nil(t, err1)
assert.Nil(t, err2)
adds := []dns.Address{add1, add2}
sort.Sort(ByConnectionString(adds))
expected := []dns.Address{dns.Address{Address: "0.1.2.3", Port: 8001}, dns.Address{Address: "4.5.6.7", Port: 8002}}
assert.Equal(t, expected, adds, "unexpected results")
}
func TestRandomStrategy(t *testing.T) {
lib, err := dns.NewDefaultLookupLib()
assert.Nil(t, err)
//given
c := lb.NewGeneric(&lb.Config{
Dns: lib,
Strategy: random.RandomStrategy,
})
// when
srvName := "foo.service.fligl.io"
_, err = c.Next(srvName)
// then
assert.Nil(t, err)
}
func TestFirstStrategy(t *testing.T) {
lib, err := dns.NewDefaultLookupLib()
assert.Nil(t, err)
//given
c := lb.NewGeneric(&lb.Config{
Dns: lib,
Strategy: first.FirstStrategy,
})
// when
srvName := "foo.service.fligl.io"
add1, err1 := c.Next(srvName)
add2, err2 := c.Next(srvName)
// then
assert.Nil(t, err1)
assert.Nil(t, err2)
assert.NotEqual(t, add1, add2)
}