forked from mbobakov/grpc-consul-resolver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver_test.go
125 lines (118 loc) · 2.04 KB
/
resolver_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package consul_test
import (
"testing"
"github.com/golang/mock/gomock"
"github.com/hashicorp/consul/api"
"google.golang.org/grpc/resolver"
consul "github.com/mbobakov/grpc-consul-resolver"
"github.com/mbobakov/grpc-consul-resolver/internal/mocks"
)
//go:generate mockgen -package mocks -destination internal/mocks/resolverClientConn.go google.golang.org/grpc/resolver ClientConn
func TestResolver_ChangesHandler(t *testing.T) {
tests := []struct {
name string
changes interface{}
updates int
addresses []resolver.Address
}{
{
"empty changes",
nil,
0,
nil,
},
{
"unexpected type",
[]string{"a", "b", "c"},
0,
nil,
},
{
"empty data",
[]*api.ServiceEntry{},
1,
nil,
},
{
"service addresses",
[]*api.ServiceEntry{
{
Service: &api.AgentService{
Address: "A",
},
},
{
Service: &api.AgentService{
Address: "B",
Port: 123,
},
},
},
1,
[]resolver.Address{
{Addr: "A"},
{Addr: "B:123"},
},
},
{
"node addresses",
[]*api.ServiceEntry{
{
Node: &api.Node{
Address: "A",
},
},
{
Node: &api.Node{
Address: "B",
},
},
},
1,
[]resolver.Address{
{Addr: "A"},
{Addr: "B"},
},
},
{
"mix",
[]*api.ServiceEntry{
{
Service: &api.AgentService{
Address: "A",
Port: 123,
},
Node: &api.Node{
Address: "B",
},
},
{
Service: &api.AgentService{
Port: 123,
},
Node: &api.Node{
Address: "D",
},
},
},
1,
[]resolver.Address{
{Addr: "A:123"},
{Addr: "D:123"},
},
},
}
ctrl := gomock.NewController(t)
defer ctrl.Finish()
t.Parallel()
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
mock := mocks.NewMockClientConn(ctrl)
mock.EXPECT().UpdateState(resolver.State{Addresses: test.addresses}).Times(test.updates)
r := consul.NewResolver(mock, nil)
r.ChangesHandler(1, test.changes)
})
}
}