@@ -68,26 +68,15 @@ func TestStart(t *testing.T) {
68
68
lock : sync.RWMutex {},
69
69
}
70
70
71
- config := & consul.Config {}
72
-
73
- patches := gomonkey .ApplyPrivateMethod (reflect .TypeOf (reg ), "fetchAllServices" , func (_ * Consul , client * Client ) (map [consulService ]bool , error ) {
74
- return map [consulService ]bool {
75
- {ServiceName : "service1" , Tag : "tag1" }: true ,
76
- {ServiceName : "service2" , Tag : "tag2" }: true ,
77
- }, nil
78
- })
79
- patches .ApplyPrivateMethod (reg , "subscribe" , func (tag , serviceName string ) error { return nil })
80
- patches .ApplyPrivateMethod (reg , "unsubscribe" , func (serviceName string ) error { return nil })
81
- patches .ApplyPrivateMethod (reg , "removeService" , func (key consulService ) {})
71
+ config := & consul.Config {
72
+ ServerUrl : "::::::::::::" ,
73
+ }
82
74
83
75
err := reg .Start (config )
84
- assert .Nil (t , err )
85
-
76
+ assert .Error (t , err )
86
77
err = reg .Stop ()
87
78
assert .Nil (t , err )
88
79
89
- patches .Reset ()
90
-
91
80
reg = & Consul {
92
81
logger : log .NewLogger (& log.RegistryLoggerOptions {
93
82
Name : "test" ,
@@ -96,15 +85,25 @@ func TestStart(t *testing.T) {
96
85
subscriptions : make (map [string ]* watch.Plan ),
97
86
done : make (chan struct {}),
98
87
lock : sync.RWMutex {},
88
+ store : registry .FakeServiceEntryStore (),
99
89
}
100
90
101
- config = & consul.Config {
102
- ServerUrl : "::::::::::::" ,
103
- }
91
+ config = & consul.Config {}
92
+
93
+ patches := gomonkey .ApplyPrivateMethod (reg , "fetchAllServices" , func (_ * Consul , client * Client ) (map [consulService ]bool , error ) {
94
+ return map [consulService ]bool {
95
+ {ServiceName : "service1" , Tag : "tag1" }: true ,
96
+ {ServiceName : "service2" , Tag : "tag2" }: true ,
97
+ }, nil
98
+ })
99
+ patches .ApplyPrivateMethod (reg , "subscribe" , func (serviceName string ) error { return nil })
100
+ defer patches .Reset ()
104
101
105
102
err = reg .Start (config )
106
- assert .Error (t , err )
107
- close (reg .done )
103
+ assert .Nil (t , err )
104
+
105
+ err = reg .Stop ()
106
+ assert .Nil (t , err )
108
107
}
109
108
110
109
func TestRefresh (t * testing.T ) {
@@ -129,7 +128,7 @@ func TestRefresh(t *testing.T) {
129
128
"service2" : {"tag1" },
130
129
}
131
130
132
- patches := gomonkey .ApplyPrivateMethod (reflect . TypeOf ( reg ) , "fetchAllServices" , func (_ * Consul , client * Client ) (map [consulService ]bool , error ) {
131
+ patches := gomonkey .ApplyPrivateMethod (reg , "fetchAllServices" , func (_ * Consul , client * Client ) (map [consulService ]bool , error ) {
133
132
return map [consulService ]bool {
134
133
{ServiceName : "service1" , Tag : "tag1" }: true ,
135
134
{ServiceName : "service2" , Tag : "tag2" }: true ,
@@ -180,7 +179,7 @@ func TestFetchAllServices(t *testing.T) {
180
179
Token : "token" ,
181
180
}
182
181
183
- patches := gomonkey .ApplyMethod (reflect . TypeOf ( client .consulCatalog ) , "Services" , func (_ * api.Catalog , q * api.QueryOptions ) (map [string ][]string , * api.QueryMeta , error ) {
182
+ patches := gomonkey .ApplyMethod (client .consulCatalog , "Services" , func (_ * api.Catalog , q * api.QueryOptions ) (map [string ][]string , * api.QueryMeta , error ) {
184
183
return map [string ][]string {
185
184
"service1" : {"tag1" , "tag2" },
186
185
"service2" : {"tag3" },
@@ -209,7 +208,7 @@ func TestFetchAllServices(t *testing.T) {
209
208
Token : "token" ,
210
209
}
211
210
212
- patches := gomonkey .ApplyMethod (reflect . TypeOf ( client .consulCatalog ) , "Services" , func (_ * api.Catalog , q * api.QueryOptions ) (map [string ][]string , * api.QueryMeta , error ) {
211
+ patches := gomonkey .ApplyMethod (client .consulCatalog , "Services" , func (_ * api.Catalog , q * api.QueryOptions ) (map [string ][]string , * api.QueryMeta , error ) {
213
212
return nil , nil , errors .New ("mock error" )
214
213
})
215
214
defer patches .Reset ()
@@ -308,30 +307,16 @@ func TestGetServiceEntryKey(t *testing.T) {
308
307
}
309
308
}
310
309
311
- type fakeServiceEntryStore struct {
312
- }
313
-
314
- func (f * fakeServiceEntryStore ) Delete (service string ) {
315
- }
316
-
317
- func (f * fakeServiceEntryStore ) Update (service string , se * registry.ServiceEntryWrapper ) {
318
- }
319
-
320
310
func TestGetSubscribeCallback (t * testing.T ) {
321
311
reg := & Consul {
322
- store : & fakeServiceEntryStore {} ,
312
+ store : registry . FakeServiceEntryStore () ,
323
313
stopped : atomic.Bool {},
314
+ client : & Client {
315
+ NameSpace : "default" ,
316
+ DataCenter : "default-dc" ,
317
+ },
324
318
}
325
319
326
- patch := gomonkey .ApplyPrivateMethod (reflect .TypeOf (reg ), "getServiceEntryKey" , func (_ * Consul , serviceName string ) string {
327
- return "test.default.default-dc.earth.consul"
328
- })
329
- defer patch .Reset ()
330
-
331
- patch .ApplyPrivateMethod (reflect .TypeOf (reg ), "generateServiceEntry" , func (_ * Consul , host string , services []* api.ServiceEntry ) * registry.ServiceEntryWrapper {
332
- return & registry.ServiceEntryWrapper {}
333
- })
334
-
335
320
callback := reg .getSubscribeCallback ("" , "test-service" )
336
321
337
322
var services []* api.ServiceEntry
@@ -347,7 +332,7 @@ func TestReload(t *testing.T) {
347
332
logger : log .NewLogger (& log.RegistryLoggerOptions {
348
333
Name : "test" ,
349
334
}),
350
- store : & fakeServiceEntryStore {} ,
335
+ store : registry . FakeServiceEntryStore () ,
351
336
lock : sync.RWMutex {},
352
337
}
353
338
@@ -360,17 +345,17 @@ func TestReload(t *testing.T) {
360
345
})
361
346
362
347
service := consulService {"test-service" , "new-datacenter" }
363
- patches .ApplyPrivateMethod (reflect . TypeOf ( reg ) , "fetchAllServices" , func (client * Client ) (map [consulService ]bool , error ) {
348
+ patches .ApplyPrivateMethod (reg , "fetchAllServices" , func (client * Client ) (map [consulService ]bool , error ) {
364
349
return map [consulService ]bool {
365
350
service : true ,
366
351
}, nil
367
352
})
368
353
369
- patches .ApplyPrivateMethod (reflect . TypeOf ( reg ) , "subscribe" , func (_ * Consul , serviceName string ) error {
354
+ patches .ApplyPrivateMethod (reg , "subscribe" , func (_ * Consul , serviceName string ) error {
370
355
return nil
371
356
})
372
357
373
- patches .ApplyPrivateMethod (reflect . TypeOf ( reg ) , "unsubscribe" , func (_ * Consul , serviceName string ) error {
358
+ patches .ApplyPrivateMethod (reg , "unsubscribe" , func (_ * Consul , serviceName string ) error {
374
359
return nil
375
360
})
376
361
defer patches .Reset ()
@@ -395,7 +380,7 @@ func TestSubscribe(t *testing.T) {
395
380
logger : log .NewLogger (& log.RegistryLoggerOptions {
396
381
Name : "test" ,
397
382
}),
398
- store : & fakeServiceEntryStore {} ,
383
+ store : registry . FakeServiceEntryStore () ,
399
384
lock : sync.RWMutex {},
400
385
}
401
386
0 commit comments