@@ -5,48 +5,50 @@ import (
5
5
"testing"
6
6
"time"
7
7
8
+ "github.com/golang/mock/gomock"
8
9
"github.com/hashicorp/consul/api"
9
- "github.com/pkg/errors "
10
+ "github.com/mbobakov/grpc-consul-resolver/internal/mocks "
10
11
"github.com/stretchr/testify/require"
11
12
"google.golang.org/grpc/resolver"
12
13
)
13
14
14
15
func TestPopulateEndpoints (t * testing.T ) {
15
16
tests := []struct {
16
- name string
17
- input [][]string
18
- want []resolver.Address
17
+ name string
18
+ input [][]string
19
+ wantCalls [] []resolver.Address
19
20
}{
20
- {"one" , [][]string {{"127.0.0.1:50051" }}, []resolver.Address {{Addr : "127.0.0.1:50051" }}},
21
- {"sorted" ,
22
- [][]string {
23
- {"227.0.0.1:50051" , "127.0.0.1:50051" },
24
- },
25
- []resolver.Address {
26
- {Addr : "127.0.0.1:50051" },
27
- {Addr : "227.0.0.1:50051" },
21
+ {"one" ,
22
+ [][]string {{"127.0.0.1:50051" }},
23
+ [][]resolver.Address {
24
+ []resolver.Address {
25
+ {Addr : "127.0.0.1:50051" },
26
+ },
28
27
},
29
28
},
30
- {"multy " ,
29
+ {"sorted " ,
31
30
[][]string {
32
- {"127.0.0.1:50051" },
33
- {"127.0.0.1:50052" },
34
- {"127.0.0.1:50053" },
31
+ {"227.0.0.1:50051" , "127.0.0.1:50051" },
35
32
},
36
- []resolver.Address {
37
- {Addr : "127.0.0.1:50053" },
33
+ [][]resolver.Address {
34
+ []resolver.Address {
35
+ {Addr : "127.0.0.1:50051" },
36
+ {Addr : "227.0.0.1:50051" },
37
+ },
38
38
},
39
39
},
40
40
}
41
41
for _ , tt := range tests {
42
42
t .Run (tt .name , func (t * testing.T ) {
43
+ ctrl := gomock .NewController (t )
44
+ defer ctrl .Finish ()
43
45
var (
44
- got []resolver.Address
45
- in = make (chan []string , len (tt .input ))
46
+ in = make (chan []string , len (tt .input ))
46
47
)
47
- fcc := NewFakeClientConnDefaultFatal (t )
48
- fcc .NewAddressHook = func (cc []resolver.Address ) {
49
- got = cc
48
+
49
+ fcc := mocks .NewMockClientConn (ctrl )
50
+ for _ , aa := range tt .wantCalls {
51
+ fcc .EXPECT ().UpdateState (resolver.State {Addresses : aa }).Times (1 )
50
52
}
51
53
52
54
ctx , cancel := context .WithCancel (context .Background ())
@@ -56,8 +58,6 @@ func TestPopulateEndpoints(t *testing.T) {
56
58
in <- i
57
59
}
58
60
time .Sleep (time .Millisecond )
59
- require .True (t , fcc .NewAddressCalledN (len (tt .input )))
60
- require .Equal (t , tt .want , got )
61
61
})
62
62
}
63
63
}
@@ -66,21 +66,27 @@ func TestWatchConsulService(t *testing.T) {
66
66
tests := []struct {
67
67
name string
68
68
tgt target
69
- addr []string // port increased with 1 per invocation
70
- startPort int
71
- times uint64
72
- errorFromService bool
69
+ services []* api.ServiceEntry
70
+ errorFromService error
73
71
want []string
74
72
}{
75
- {"simple" , target {Service : "svc" , Wait : time .Second }, []string {"127.0.0.1" }, 100 , 3 , false , []string {"127.0.0.1:102" }},
76
- {"error" , target {}, []string {"127.0.0.1" }, 100 , 2 , true , nil },
77
- {"limit" , target {Limit : 1 }, []string {"127.0.0.1" , "127.0.0.2" , "127.0.0.3" }, 100 , 1 , false , []string {"127.0.0.1:100" }},
78
- {"limitOver" , target {Limit : 10 }, []string {"127.0.0.1" }, 100 , 1 , false , []string {"127.0.0.1:100" }},
73
+ {"simple" , target {Service : "svc" , Wait : time .Second },
74
+ []* api.ServiceEntry {
75
+ & api.ServiceEntry {
76
+ Service : & api.AgentService {Address : "127.0.0.1" , Port : 1024 },
77
+ },
78
+ },
79
+ nil ,
80
+ []string {"127.0.0.1:1024" },
81
+ },
82
+ // TODO: Add more tests-cases
79
83
}
80
84
for _ , tt := range tests {
81
85
t .Run (tt .name , func (t * testing.T ) {
82
86
ctx , cancel := context .WithCancel (context .Background ())
83
87
defer cancel ()
88
+ ctrl := gomock .NewController (t )
89
+ defer ctrl .Finish ()
84
90
85
91
var (
86
92
got []string
@@ -95,30 +101,38 @@ func TestWatchConsulService(t *testing.T) {
95
101
}
96
102
}
97
103
}()
98
- fconsul := NewFakeservicerDefaultFatal (t )
99
-
100
- fconsul .ServiceHook = func (s , tag string , hlz bool , q * api.QueryOptions ) ([]* api.ServiceEntry , * api.QueryMeta , error ) {
101
- require .Equal (t , tt .tgt .Service , s )
102
- require .Equal (t , tt .tgt .Tag , tag )
103
- require .Equal (t , tt .tgt .Healthy , hlz )
104
- require .Equal (t , tt .tgt .Wait , q .WaitTime )
105
- if q .WaitIndex >= tt .times {
106
- select {}
107
- }
108
- if tt .errorFromService {
109
- return nil , & api.QueryMeta {LastIndex : q .WaitIndex + 1 }, errors .New ("Error" )
110
- }
111
- var rr []* api.ServiceEntry
112
- for _ , a := range tt .addr {
113
- rr = append (rr , & api.ServiceEntry {Service : & api.AgentService {Address : a , Port : tt .startPort + int (q .WaitIndex )}})
114
- }
115
- return rr , & api.QueryMeta {LastIndex : q .WaitIndex + 1 }, nil
116
- }
104
+ fconsul := mocks .NewMockservicer (ctrl )
105
+ fconsul .EXPECT ().Service (tt .tgt .Service , tt .tgt .Tag , tt .tgt .Healthy , & api.QueryOptions {
106
+ WaitIndex : 0 ,
107
+ Near : tt .tgt .Near ,
108
+ WaitTime : tt .tgt .Wait ,
109
+ Datacenter : tt .tgt .Dc ,
110
+ AllowStale : tt .tgt .AllowStale ,
111
+ RequireConsistent : tt .tgt .RequireConsistent ,
112
+ }).
113
+ Times (1 ).
114
+ Return (tt .services , & api.QueryMeta {LastIndex : 1 }, tt .errorFromService )
115
+ fconsul .EXPECT ().Service (tt .tgt .Service , tt .tgt .Tag , tt .tgt .Healthy , & api.QueryOptions {
116
+ WaitIndex : 1 ,
117
+ Near : tt .tgt .Near ,
118
+ WaitTime : tt .tgt .Wait ,
119
+ Datacenter : tt .tgt .Dc ,
120
+ AllowStale : tt .tgt .AllowStale ,
121
+ RequireConsistent : tt .tgt .RequireConsistent ,
122
+ }).
123
+ Do (
124
+ func (svc string , tag string , h bool , opt * api.QueryOptions ) ([]* api.ServiceEntry , * api.QueryMeta , error ) {
125
+ if opt .WaitIndex > 0 {
126
+ select {}
127
+ }
128
+ return tt .services , & api.QueryMeta {LastIndex : 1 }, tt .errorFromService
129
+ },
130
+ ).Times (1 ).
131
+ Return (tt .services , & api.QueryMeta {LastIndex : 1 }, tt .errorFromService )
117
132
118
133
go watchConsulService (ctx , fconsul , tt .tgt , out )
119
- time .Sleep (15 * time .Millisecond )
134
+ time .Sleep (5 * time .Millisecond )
120
135
121
- require .True (t , fconsul .ServiceCalledN (int (tt .times )))
122
136
require .Equal (t , tt .want , got )
123
137
})
124
138
}
0 commit comments