@@ -56,6 +56,8 @@ public class ServiceTests extends JetStreamTestBase {
5656 public static final String SORT_ENDPOINT_DESCENDING_NAME = "SortEndpointDescending" ;
5757 public static final String SORT_ENDPOINT_ASCENDING_SUBJECT = "ascending" ;
5858 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" ;
5961 public static final String CUSTOM_QGROUP = "customQ" ;
6062
6163 @ Test
@@ -196,7 +198,7 @@ public void testServiceWorkflow() throws Exception {
196198 Discovery discovery = new Discovery (clientNc , 500 , 5 );
197199
198200 // ping discovery
199- Verifier pingVerifier = (expected , response ) -> assertTrue ( response instanceof PingResponse );
201+ Verifier pingVerifier = (expected , response ) -> assertInstanceOf ( PingResponse . class , response );
200202 verifyDiscovery (discovery .ping (), pingVerifier , pingResponse1 , pingResponse2 );
201203 verifyDiscovery (discovery .ping (SERVICE_NAME_1 ), pingVerifier , pingResponse1 );
202204 verifyDiscovery (discovery .ping (SERVICE_NAME_2 ), pingVerifier , pingResponse2 );
@@ -206,7 +208,7 @@ public void testServiceWorkflow() throws Exception {
206208
207209 // info discovery
208210 Verifier infoVerifier = (expected , response ) -> {
209- assertTrue ( response instanceof InfoResponse );
211+ assertInstanceOf ( InfoResponse . class , response );
210212 InfoResponse exp = (InfoResponse ) expected ;
211213 InfoResponse r = (InfoResponse ) response ;
212214 assertEquals (exp .getDescription (), r .getDescription ());
@@ -221,7 +223,7 @@ public void testServiceWorkflow() throws Exception {
221223
222224 // stats discovery
223225 Verifier statsVerifier = (expected , response ) -> {
224- assertTrue ( response instanceof StatsResponse );
226+ assertInstanceOf ( StatsResponse . class , response );
225227 StatsResponse exp = (StatsResponse ) expected ;
226228 StatsResponse sr = (StatsResponse ) response ;
227229 assertEquals (exp .getStarted (), sr .getStarted ());
@@ -241,6 +243,44 @@ public void testServiceWorkflow() throws Exception {
241243 assertNull (discovery .stats (SERVICE_NAME_1 , "badId" ));
242244 assertNull (discovery .stats ("bad" , "badId" ));
243245
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+
244284 // test reset
245285 ZonedDateTime zdt = DateTimeUtils .gmtNow ();
246286 sleep (1 );
@@ -318,6 +358,9 @@ private static void verifyServiceExecution(Connection nc, String endpointName, S
318358 case SORT_ENDPOINT_DESCENDING_NAME :
319359 assertEquals (sortD (request ), response );
320360 break ;
361+ case REVERSE_ENDPOINT_NAME :
362+ assertEquals (reverse (request ), response );
363+ break ;
321364 }
322365 } catch (Exception e ) {
323366 throw new RuntimeException (e );
@@ -363,6 +406,19 @@ public void onMessage(ServiceMessage smsg) {
363406 }
364407 }
365408
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+
366422 private static String echo (String data ) {
367423 return "Echo " + data ;
368424 }
@@ -394,6 +450,14 @@ private static String sortD(String data) {
394450 return sortD (data .getBytes (StandardCharsets .UTF_8 ));
395451 }
396452
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+
397461 @ Test
398462 public void testDispatchers () throws Exception {
399463 try (NatsTestServer ts = new NatsTestServer ()) {
@@ -1107,7 +1171,7 @@ public void testServiceResponsesConstruction() {
11071171 endMeta .put ("foo" , "bar" );
11081172 Endpoint ep = new Endpoint ("endfoo" , endMeta );
11091173 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 );
11111175 InfoResponse ir2 = new InfoResponse (ir1 .toJson ().getBytes ());
11121176 validateApiInOutInfoResponse (ir1 );
11131177 validateApiInOutInfoResponse (ir2 );
@@ -1129,10 +1193,11 @@ public void testServiceResponsesConstruction() {
11291193 validateApiInOutStatsResponse (stat1 , serviceStarted , endStarteds , data );
11301194 validateApiInOutStatsResponse (stat2 , serviceStarted , endStarteds , data );
11311195
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 ();
11341198 EqualsVerifier .simple ().forClass (StatsResponse .class )
11351199 .withPrefabValues (EndpointStats .class , statsList .get (0 ), statsList .get (1 ))
1200+ .withIgnoredFields ("serialized" )
11361201 .verify ();
11371202 }
11381203
0 commit comments