Skip to content

Commit

Permalink
options builder aroc (#835)
Browse files Browse the repository at this point in the history
  • Loading branch information
scottf authored Jan 25, 2023
1 parent 48ed141 commit cc37a43
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 69 deletions.
23 changes: 11 additions & 12 deletions src/main/java/io/nats/client/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -565,25 +565,24 @@ public Thread newThread(Runnable r) {
}
}

// ----------------------------------------------------------------------------------------------------
// BUILDER
// ----------------------------------------------------------------------------------------------------
/**
* Options are created using a Builder. The builder supports chaining and will
* create a default set of options if no methods are calls. The builder can also
* be created from a properties object using the property names defined with the
* prefix PROP_ in this class.
*
* <p>{@code new Options.Builder().build()}}.
*
* <p>A common usage for testing might be {@code new Options.Builder().server(myserverurl).noReconnect.build()}
*/

/**
* Creates a builder for the options.
* Creates a builder for the options in a fluent style
* @return the builder.
*/
public static Builder builder() {
return new Builder();
}

/**
* Options are created using a Builder. The builder supports chaining and will
* create a default set of options if no methods are calls. The builder can also
* be created from a properties object using the property names defined with the
* prefix PROP_ in this class.
* <p>A common usage for testing might be {@code new Options.Builder().server(myserverurl).noReconnect.build()}
*/
public static class Builder {

// ----------------------------------------------------------------------------------------------------
Expand Down
114 changes: 57 additions & 57 deletions src/test/java/io/nats/service/ServiceTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,10 @@ public void testServiceWorkflow() throws Exception {
StatsResponse sr = (StatsResponse)response;
assertEquals(exp.getStarted(), sr.getStarted());
for (int x = 0; x < 3; x++) {
EndpointResponse es = exp.getEndpointStats().get(x);
if (!es.getName().equals(ECHO_ENDPOINT_NAME)) {
EndpointResponse er = exp.getEndpointStats().get(x);
if (!er.getName().equals(ECHO_ENDPOINT_NAME)) {
// echo endpoint has data that will vary
assertEquals(es, sr.getEndpointStats().get(x));
assertEquals(er, sr.getEndpointStats().get(x));
}
}
};
Expand All @@ -270,19 +270,19 @@ public void testServiceWorkflow() throws Exception {
StatsResponse sr = service1.getStatsResponse();
assertTrue(zdt.isBefore(sr.getStarted()));
for (int x = 0; x < 3; x++) {
EndpointResponse es = sr.getEndpointStats().get(x);
assertEquals(0, es.getNumRequests());
assertEquals(0, es.getNumErrors());
assertEquals(0, es.getProcessingTime());
assertEquals(0, es.getAverageProcessingTime());
assertNull(es.getLastError());
if (es.getName().equals(ECHO_ENDPOINT_NAME)) {
assertNotNull(es.getData());
EndpointResponse er = sr.getEndpointStats().get(x);
assertEquals(0, er.getNumRequests());
assertEquals(0, er.getNumErrors());
assertEquals(0, er.getProcessingTime());
assertEquals(0, er.getAverageProcessingTime());
assertNull(er.getLastError());
if (er.getName().equals(ECHO_ENDPOINT_NAME)) {
assertNotNull(er.getData());
}
else {
assertNull(es.getData());
assertNull(er.getData());
}
assertTrue(zdt.isBefore(es.getStarted()));
assertTrue(zdt.isBefore(er.getStarted()));
}

// shutdown
Expand Down Expand Up @@ -615,10 +615,10 @@ public void testHandlerException() throws Exception {
assertEquals("java.lang.RuntimeException: handler-problem", m.getHeaders().getFirst(NATS_SERVICE_ERROR));
assertEquals("500", m.getHeaders().getFirst(NATS_SERVICE_ERROR_CODE));
StatsResponse sr = exService.getStatsResponse();
EndpointResponse es = sr.getEndpointStats().get(0);
assertEquals(1, es.getNumRequests());
assertEquals(1, es.getNumErrors());
assertEquals("java.lang.RuntimeException: handler-problem", es.getLastError());
EndpointResponse er = sr.getEndpointStats().get(0);
assertEquals(1, er.getNumRequests());
assertEquals(1, er.getNumErrors());
assertEquals("java.lang.RuntimeException: handler-problem", er.getLastError());
});
}

Expand Down Expand Up @@ -870,31 +870,31 @@ public void testEndpointResponseConstruction() {
.verify();
ZonedDateTime zdt = DateTimeUtils.gmtNow();

EndpointResponse es = new EndpointResponse("name", "subject", 0, 0, 0, null, null, zdt);
assertEquals("name", es.getName());
assertEquals("subject", es.getSubject());
assertNull(es.getSchema());
assertNull(es.getLastError());
assertNull(es.getData());
assertEquals(0, es.getNumRequests());
assertEquals(0, es.getNumErrors());
assertEquals(0, es.getProcessingTime());
assertEquals(0, es.getAverageProcessingTime());
assertEquals(zdt, es.getStarted());

es = new EndpointResponse("name", "subject", 2, 4, 10, "lastError", data, zdt);
assertEquals("name", es.getName());
assertEquals("subject", es.getSubject());
assertNull(es.getSchema());
assertEquals("lastError", es.getLastError());
assertEquals("\"data\"", es.getData().toString());
assertEquals(2, es.getNumRequests());
assertEquals(4, es.getNumErrors());
assertEquals(10, es.getProcessingTime());
assertEquals(5, es.getAverageProcessingTime());
assertEquals(zdt, es.getStarted());

String j = es.toJson();
EndpointResponse er = new EndpointResponse("name", "subject", 0, 0, 0, null, null, zdt);
assertEquals("name", er.getName());
assertEquals("subject", er.getSubject());
assertNull(er.getSchema());
assertNull(er.getLastError());
assertNull(er.getData());
assertEquals(0, er.getNumRequests());
assertEquals(0, er.getNumErrors());
assertEquals(0, er.getProcessingTime());
assertEquals(0, er.getAverageProcessingTime());
assertEquals(zdt, er.getStarted());

er = new EndpointResponse("name", "subject", 2, 4, 10, "lastError", data, zdt);
assertEquals("name", er.getName());
assertEquals("subject", er.getSubject());
assertNull(er.getSchema());
assertEquals("lastError", er.getLastError());
assertEquals("\"data\"", er.getData().toString());
assertEquals(2, er.getNumRequests());
assertEquals(4, er.getNumErrors());
assertEquals(10, er.getProcessingTime());
assertEquals(5, er.getAverageProcessingTime());
assertEquals(zdt, er.getStarted());

String j = er.toJson();
assertTrue(j.startsWith("{"));
assertTrue(j.contains("\"name\":\"name\""));
assertTrue(j.contains("\"subject\":\"subject\""));
Expand All @@ -905,22 +905,22 @@ public void testEndpointResponseConstruction() {
assertTrue(j.contains("\"num_errors\":4"));
assertTrue(j.contains("\"processing_time\":10"));
assertTrue(j.contains("\"average_processing_time\":5"));
assertEquals(toKey(EndpointResponse.class) + j, es.toString());
assertEquals(toKey(EndpointResponse.class) + j, er.toString());

Schema schema = new Schema("req", "res");
es = new EndpointResponse("name", "subject", schema);
assertEquals("name", es.getName());
assertEquals("subject", es.getSubject());
assertEquals(schema, es.getSchema());
assertNull(es.getLastError());
assertNull(es.getData());
assertEquals(0, es.getNumRequests());
assertEquals(0, es.getNumErrors());
assertEquals(0, es.getProcessingTime());
assertEquals(0, es.getAverageProcessingTime());
assertNull(es.getStarted());

j = es.toJson();
er = new EndpointResponse("name", "subject", schema);
assertEquals("name", er.getName());
assertEquals("subject", er.getSubject());
assertEquals(schema, er.getSchema());
assertNull(er.getLastError());
assertNull(er.getData());
assertEquals(0, er.getNumRequests());
assertEquals(0, er.getNumErrors());
assertEquals(0, er.getProcessingTime());
assertEquals(0, er.getAverageProcessingTime());
assertNull(er.getStarted());

j = er.toJson();
assertTrue(j.startsWith("{"));
assertTrue(j.contains("\"name\":\"name\""));
assertTrue(j.contains("\"subject\":\"subject\""));
Expand All @@ -931,7 +931,7 @@ public void testEndpointResponseConstruction() {
assertFalse(j.contains("\"num_errors\":"));
assertFalse(j.contains("\"processing_time\":"));
assertFalse(j.contains("\"average_processing_time\":"));
assertEquals(toKey(EndpointResponse.class) + j, es.toString());
assertEquals(toKey(EndpointResponse.class) + j, er.toString());
}

@Test
Expand Down

0 comments on commit cc37a43

Please sign in to comment.