11/*
2- * Copyright 2014-2018 the original author or authors.
2+ * Copyright 2014-2019 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
@@ -62,10 +62,13 @@ public void setup() {
6262 }
6363
6464 @ Test
65- public void test_application_ready () {
65+ public void should_discover_instances_when_application_is_ready () {
6666 when (discovery .getServices ()).thenReturn (Collections .singletonList ("service" ));
67- when (discovery .getInstances ("service" )).thenReturn (
68- Collections .singletonList (new DefaultServiceInstance ("service" , "localhost" , 80 , false )));
67+ when (discovery .getInstances ("service" )).thenReturn (Collections .singletonList (new DefaultServiceInstance ("service" ,
68+ "localhost" ,
69+ 80 ,
70+ false
71+ )));
6972
7073 listener .onApplicationReady (null );
7174
@@ -76,10 +79,13 @@ public void test_application_ready() {
7679
7780
7881 @ Test
79- public void test_ignore () {
82+ public void should_not_register_instance_when_serviceId_is_ignored () {
8083 when (discovery .getServices ()).thenReturn (singletonList ("service" ));
81- when (discovery .getInstances ("service" )).thenReturn (
82- singletonList (new DefaultServiceInstance ("service" , "localhost" , 80 , false )));
84+ when (discovery .getInstances ("service" )).thenReturn (singletonList (new DefaultServiceInstance ("service" ,
85+ "localhost" ,
86+ 80 ,
87+ false
88+ )));
8389
8490 listener .setIgnoredServices (singleton ("service" ));
8591 listener .onInstanceRegistered (new InstanceRegisteredEvent <>(new Object (), null ));
@@ -88,10 +94,13 @@ public void test_ignore() {
8894 }
8995
9096 @ Test
91- public void test_matching () {
97+ public void should_register_instance_when_serviceId_is_not_ignored () {
9298 when (discovery .getServices ()).thenReturn (singletonList ("service" ));
93- when (discovery .getInstances ("service" )).thenReturn (
94- singletonList (new DefaultServiceInstance ("service" , "localhost" , 80 , false )));
99+ when (discovery .getInstances ("service" )).thenReturn (singletonList (new DefaultServiceInstance ("service" ,
100+ "localhost" ,
101+ 80 ,
102+ false
103+ )));
95104
96105 listener .setServices (singleton ("notService" ));
97106 listener .onInstanceRegistered (new InstanceRegisteredEvent <>(new Object (), null ));
@@ -100,10 +109,13 @@ public void test_matching() {
100109 }
101110
102111 @ Test
103- public void test_ignore_pattern () {
112+ public void should_not_register_instance_when_serviceId_matches_ignored_pattern () {
104113 when (discovery .getServices ()).thenReturn (asList ("service" , "rabbit-1" , "rabbit-2" ));
105- when (discovery .getInstances ("service" )).thenReturn (
106- singletonList (new DefaultServiceInstance ("service" , "localhost" , 80 , false )));
114+ when (discovery .getInstances ("service" )).thenReturn (singletonList (new DefaultServiceInstance ("service" ,
115+ "localhost" ,
116+ 80 ,
117+ false
118+ )));
107119
108120 listener .setIgnoredServices (singleton ("rabbit-*" ));
109121 listener .onInstanceRegistered (new InstanceRegisteredEvent <>(new Object (), null ));
@@ -114,10 +126,13 @@ public void test_ignore_pattern() {
114126 }
115127
116128 @ Test
117- public void test_matching_pattern () {
129+ public void should_register_instances_when_serviceId_matches_wanted_pattern () {
118130 when (discovery .getServices ()).thenReturn (asList ("service" , "rabbit-1" , "rabbit-2" ));
119- when (discovery .getInstances ("service" )).thenReturn (
120- singletonList (new DefaultServiceInstance ("service" , "localhost" , 80 , false )));
131+ when (discovery .getInstances ("service" )).thenReturn (singletonList (new DefaultServiceInstance ("service" ,
132+ "localhost" ,
133+ 80 ,
134+ false
135+ )));
121136
122137 listener .setServices (singleton ("ser*" ));
123138 listener .onInstanceRegistered (new InstanceRegisteredEvent <>(new Object (), null ));
@@ -128,12 +143,18 @@ public void test_matching_pattern() {
128143 }
129144
130145 @ Test
131- public void test_matching_and_ignore_pattern () {
146+ public void should_register_instances_when_serviceId_matches_wanted_pattern_and_igonred_pattern () {
132147 when (discovery .getServices ()).thenReturn (asList ("service-1" , "service" , "rabbit-1" , "rabbit-2" ));
133- when (discovery .getInstances ("service" )).thenReturn (
134- singletonList (new DefaultServiceInstance ("service" , "localhost" , 80 , false )));
135- when (discovery .getInstances ("service-1" )).thenReturn (
136- singletonList (new DefaultServiceInstance ("service-1" , "localhost" , 80 , false )));
148+ when (discovery .getInstances ("service" )).thenReturn (singletonList (new DefaultServiceInstance ("service" ,
149+ "localhost" ,
150+ 80 ,
151+ false
152+ )));
153+ when (discovery .getInstances ("service-1" )).thenReturn (singletonList (new DefaultServiceInstance ("service-1" ,
154+ "localhost" ,
155+ 80 ,
156+ false
157+ )));
137158
138159 listener .setServices (singleton ("ser*" ));
139160 listener .setIgnoredServices (singleton ("service-*" ));
@@ -145,10 +166,13 @@ public void test_matching_and_ignore_pattern() {
145166 }
146167
147168 @ Test
148- public void test_register_and_convert () {
169+ public void should_register_instance_when_new_service_instance_is_discovered () {
149170 when (discovery .getServices ()).thenReturn (singletonList ("service" ));
150- when (discovery .getInstances ("service" )).thenReturn (
151- singletonList (new DefaultServiceInstance ("service" , "localhost" , 80 , false )));
171+ when (discovery .getInstances ("service" )).thenReturn (singletonList (new DefaultServiceInstance ("service" ,
172+ "localhost" ,
173+ 80 ,
174+ false
175+ )));
152176
153177 listener .onInstanceRegistered (new InstanceRegisteredEvent <>(new Object (), null ));
154178
@@ -159,18 +183,19 @@ public void test_register_and_convert() {
159183 assertThat (registration .getServiceUrl ()).isEqualTo ("http://localhost:80/" );
160184 assertThat (registration .getName ()).isEqualTo ("service" );
161185 }).verifyComplete ();
162-
163-
164186 }
165187
166188 @ Test
167- public void single_discovery_for_same_heartbeat () {
189+ public void should_only_discover_new_instances_when_new_heartbeat_is_emitted () {
168190 Object heartbeat = new Object ();
169191 listener .onParentHeartbeat (new ParentHeartbeatEvent (new Object (), heartbeat ));
170192
171193 when (discovery .getServices ()).thenReturn (singletonList ("service" ));
172- when (discovery .getInstances ("service" )).thenReturn (
173- singletonList (new DefaultServiceInstance ("service" , "localhost" , 80 , false )));
194+ when (discovery .getInstances ("service" )).thenReturn (singletonList (new DefaultServiceInstance ("service" ,
195+ "localhost" ,
196+ 80 ,
197+ false
198+ )));
174199
175200 listener .onApplicationEvent (new HeartbeatEvent (new Object (), heartbeat ));
176201 StepVerifier .create (registry .getInstances ()).verifyComplete ();
@@ -182,14 +207,13 @@ public void single_discovery_for_same_heartbeat() {
182207 }
183208
184209 @ Test
185- public void deregister_removed_app () {
210+ public void should_remove_instances_when_they_are_no_longer_available_in_discovery () {
186211 StepVerifier .create (registry .register (Registration .create ("ignored" , "http://health" ).build ()))
187212 .consumeNextWith (id -> { })
188213 .verifyComplete ();
189- StepVerifier .create (
190- registry .register (Registration .create ("different-source" , "http://health2" ).source ("http-api" ).build ()))
191- .consumeNextWith (id -> { })
192- .verifyComplete ();
214+ StepVerifier .create (registry .register (Registration .create ("different-source" , "http://health2" )
215+ .source ("http-api" )
216+ .build ())).consumeNextWith (id -> { }).verifyComplete ();
193217 listener .setIgnoredServices (singleton ("ignored" ));
194218
195219 List <ServiceInstance > instances = new ArrayList <>();
@@ -235,4 +259,28 @@ public void deregister_removed_app() {
235259 listener .onApplicationEvent (new HeartbeatEvent (new Object (), new Object ()));
236260 verify (registry , times (1 )).deregister (any (InstanceId .class ));
237261 }
262+
263+
264+ @ Test
265+ public void should_not_throw_error_when_conversion_fails_and_proceed_with_next_instance () {
266+ when (discovery .getServices ()).thenReturn (singletonList ("service" ));
267+ when (discovery .getInstances ("service" )).thenReturn (asList (new DefaultServiceInstance ("service" ,
268+ "localhost" ,
269+ 80 ,
270+ false
271+ ), new DefaultServiceInstance ("error" , "localhost" , 80 , false )));
272+ listener .setConverter (instance -> {
273+ if (instance .getServiceId ().equals ("error" )) {
274+ throw new IllegalStateException ("Test-Error" );
275+ } else {
276+ return new DefaultServiceInstanceConverter ().convert (instance );
277+ }
278+ });
279+
280+ listener .onInstanceRegistered (new InstanceRegisteredEvent <>(new Object (), null ));
281+
282+ StepVerifier .create (registry .getInstances ())
283+ .assertNext (a -> assertThat (a .getRegistration ().getName ()).isEqualTo ("service" ))
284+ .verifyComplete ();
285+ }
238286}
0 commit comments