@@ -56,6 +56,8 @@ public class ServiceTests extends JetStreamTestBase {
56
56
public static final String SORT_ENDPOINT_DESCENDING_NAME = "SortEndpointDescending" ;
57
57
public static final String SORT_ENDPOINT_ASCENDING_SUBJECT = "ascending" ;
58
58
public static final String SORT_ENDPOINT_DESCENDING_SUBJECT = "descending" ;
59
+ public static final String REVERSE_ENDPOINT_NAME = "ReverseEndpoint" ;
60
+ public static final String REVERSE_ENDPOINT_SUBJECT = "reverse" ;
59
61
public static final String CUSTOM_QGROUP = "customQ" ;
60
62
61
63
@ Test
@@ -196,7 +198,7 @@ public void testServiceWorkflow() throws Exception {
196
198
Discovery discovery = new Discovery (clientNc , 500 , 5 );
197
199
198
200
// ping discovery
199
- Verifier pingVerifier = (expected , response ) -> assertTrue ( response instanceof PingResponse );
201
+ Verifier pingVerifier = (expected , response ) -> assertInstanceOf ( PingResponse . class , response );
200
202
verifyDiscovery (discovery .ping (), pingVerifier , pingResponse1 , pingResponse2 );
201
203
verifyDiscovery (discovery .ping (SERVICE_NAME_1 ), pingVerifier , pingResponse1 );
202
204
verifyDiscovery (discovery .ping (SERVICE_NAME_2 ), pingVerifier , pingResponse2 );
@@ -206,7 +208,7 @@ public void testServiceWorkflow() throws Exception {
206
208
207
209
// info discovery
208
210
Verifier infoVerifier = (expected , response ) -> {
209
- assertTrue ( response instanceof InfoResponse );
211
+ assertInstanceOf ( InfoResponse . class , response );
210
212
InfoResponse exp = (InfoResponse ) expected ;
211
213
InfoResponse r = (InfoResponse ) response ;
212
214
assertEquals (exp .getDescription (), r .getDescription ());
@@ -221,7 +223,7 @@ public void testServiceWorkflow() throws Exception {
221
223
222
224
// stats discovery
223
225
Verifier statsVerifier = (expected , response ) -> {
224
- assertTrue ( response instanceof StatsResponse );
226
+ assertInstanceOf ( StatsResponse . class , response );
225
227
StatsResponse exp = (StatsResponse ) expected ;
226
228
StatsResponse sr = (StatsResponse ) response ;
227
229
assertEquals (exp .getStarted (), sr .getStarted ());
@@ -241,6 +243,44 @@ public void testServiceWorkflow() throws Exception {
241
243
assertNull (discovery .stats (SERVICE_NAME_1 , "badId" ));
242
244
assertNull (discovery .stats ("bad" , "badId" ));
243
245
246
+ // ---------------------------------------------------------------------------
247
+ // TEST ADDING AN ENDPOINT TO A RUNNING SERVICE
248
+ // ---------------------------------------------------------------------------
249
+ Endpoint endReverse = Endpoint .builder ()
250
+ .name (REVERSE_ENDPOINT_NAME )
251
+ .subject (REVERSE_ENDPOINT_SUBJECT )
252
+ .build ();
253
+
254
+ ServiceEndpoint seRev1 = ServiceEndpoint .builder ()
255
+ .endpoint (endReverse )
256
+ .handler (new ReverseHandler (serviceNc1 ))
257
+ .build ();
258
+
259
+ service1 .addServiceEndpoint (seRev1 );
260
+
261
+ for (int x = 0 ; x < requestCount ; x ++) {
262
+ verifyServiceExecution (clientNc , REVERSE_ENDPOINT_NAME , REVERSE_ENDPOINT_SUBJECT , null );
263
+ }
264
+ infoResponse1 = service1 .getInfoResponse ();
265
+ boolean found = false ;
266
+ for (Endpoint e : infoResponse1 .getEndpoints ()) {
267
+ if (e .getName ().equals (REVERSE_ENDPOINT_NAME )) {
268
+ found = true ;
269
+ break ;
270
+ }
271
+ }
272
+ assertTrue (found );
273
+
274
+ statsResponse1 = service1 .getStatsResponse ();
275
+ found = false ;
276
+ for (EndpointStats e : statsResponse1 .getEndpointStatsList ()) {
277
+ if (e .getName ().equals (REVERSE_ENDPOINT_NAME )) {
278
+ found = true ;
279
+ break ;
280
+ }
281
+ }
282
+ assertTrue (found );
283
+
244
284
// test reset
245
285
ZonedDateTime zdt = DateTimeUtils .gmtNow ();
246
286
sleep (1 );
@@ -318,6 +358,9 @@ private static void verifyServiceExecution(Connection nc, String endpointName, S
318
358
case SORT_ENDPOINT_DESCENDING_NAME :
319
359
assertEquals (sortD (request ), response );
320
360
break ;
361
+ case REVERSE_ENDPOINT_NAME :
362
+ assertEquals (reverse (request ), response );
363
+ break ;
321
364
}
322
365
} catch (Exception e ) {
323
366
throw new RuntimeException (e );
@@ -363,6 +406,19 @@ public void onMessage(ServiceMessage smsg) {
363
406
}
364
407
}
365
408
409
+ static class ReverseHandler implements ServiceMessageHandler {
410
+ Connection conn ;
411
+
412
+ public ReverseHandler (Connection conn ) {
413
+ this .conn = conn ;
414
+ }
415
+
416
+ @ Override
417
+ public void onMessage (ServiceMessage smsg ) {
418
+ smsg .respond (conn , reverse (smsg .getData ()));
419
+ }
420
+ }
421
+
366
422
private static String echo (String data ) {
367
423
return "Echo " + data ;
368
424
}
@@ -394,6 +450,14 @@ private static String sortD(String data) {
394
450
return sortD (data .getBytes (StandardCharsets .UTF_8 ));
395
451
}
396
452
453
+ private static String reverse (String data ) {
454
+ return new StringBuilder (data ).reverse ().toString ();
455
+ }
456
+
457
+ private static String reverse (byte [] data ) {
458
+ return reverse (new String (data ));
459
+ }
460
+
397
461
@ Test
398
462
public void testDispatchers () throws Exception {
399
463
try (NatsTestServer ts = new NatsTestServer ()) {
@@ -1107,7 +1171,7 @@ public void testServiceResponsesConstruction() {
1107
1171
endMeta .put ("foo" , "bar" );
1108
1172
Endpoint ep = new Endpoint ("endfoo" , endMeta );
1109
1173
ServiceEndpoint se = new ServiceEndpoint (ep , m -> {}, null );
1110
- InfoResponse ir1 = new InfoResponse ("id" , "name" , "0.0.0" , metadata , "desc" , Collections . singletonList (se ) );
1174
+ InfoResponse ir1 = new InfoResponse ("id" , "name" , "0.0.0" , metadata , "desc" ). addServiceEndpoint (se );
1111
1175
InfoResponse ir2 = new InfoResponse (ir1 .toJson ().getBytes ());
1112
1176
validateApiInOutInfoResponse (ir1 );
1113
1177
validateApiInOutInfoResponse (ir2 );
@@ -1129,10 +1193,11 @@ public void testServiceResponsesConstruction() {
1129
1193
validateApiInOutStatsResponse (stat1 , serviceStarted , endStarteds , data );
1130
1194
validateApiInOutStatsResponse (stat2 , serviceStarted , endStarteds , data );
1131
1195
1132
- EqualsVerifier .simple ().forClass (PingResponse .class ).verify ();
1133
- EqualsVerifier .simple ().forClass (InfoResponse .class ).verify ();
1196
+ EqualsVerifier .simple ().forClass (PingResponse .class ).withIgnoredFields ( "serialized" ). verify ();
1197
+ EqualsVerifier .simple ().forClass (InfoResponse .class ).withIgnoredFields ( "serialized" ). verify ();
1134
1198
EqualsVerifier .simple ().forClass (StatsResponse .class )
1135
1199
.withPrefabValues (EndpointStats .class , statsList .get (0 ), statsList .get (1 ))
1200
+ .withIgnoredFields ("serialized" )
1136
1201
.verify ();
1137
1202
}
1138
1203
0 commit comments