Skip to content

Commit

Permalink
Merge branch 'refs/heads/main' into main-2-11
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/io/nats/client/api/MessageBatchGetRequest.java
  • Loading branch information
scottf committed Nov 14, 2024
2 parents e242724 + 73611bc commit 3bfa65c
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 14 deletions.
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@ jacocoTestReport {
}
afterEvaluate { // only report on main library not examples
classDirectories.setFrom(files(classDirectories.files.collect {
fileTree(dir: it,
exclude: ['**/examples**'])
fileTree(dir: it, exclude: [
'**/examples/**', 'io/nats/client/support/Debug*'
])
}))
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/nats/client/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -1685,7 +1685,7 @@ public Builder serverPool(ServerPool serverPool) {
}

/**
* Set the DispatcherFactory implementation for connections to use instead of the default implementation
* Set the DispatcherFactory implementation for connections to use instead of the default implementation
* @param dispatcherFactory the implementation
* @return the Builder for chaining
*/
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/nats/client/support/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,9 @@ public static String required(String s, String label) {
return s;
}

@Deprecated
public static String required(String s1, String s2, String label) {
if (emptyAsNull(s1) == null && emptyAsNull(s2) == null) {
if (emptyAsNull(s1) == null || emptyAsNull(s2) == null) {
throw new IllegalArgumentException(label + " cannot be null or empty.");
}
return s1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@ public void testSourceBase() {
assertEquals(m1, mb.build());
assertEquals(s1.hashCode(), sb.build().hashCode());
assertEquals(m1.hashCode(), mb.build().hashCode());
assertEquals(sb.subjectTransforms, m1.getSubjectTransforms());

// coverage
m.getSubjectTransforms().get(0).toString();
}

private void assertNotEqualsEqualsHashcode(Source s, Mirror m, Source.Builder sb, Mirror.Builder mb) {
Expand Down
22 changes: 17 additions & 5 deletions src/test/java/io/nats/client/impl/DispatcherTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@

import java.io.IOException;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

Expand Down Expand Up @@ -916,12 +913,27 @@ public void testThrowOnUnsubWhenClosed() {
public void testThrowOnWrongSubscription() {
assertThrows(IllegalStateException.class, () -> {
try (NatsTestServer ts = new NatsTestServer(false);
Connection nc = Nats.connect(ts.getURI())) {
Connection nc = Nats.connect(ts.getURI())) {
Dispatcher d = nc.createDispatcher((msg) -> {});
Subscription sub2 = nc.subscribe("test");
d.unsubscribe(sub2);
assertFalse(true);
}
});
}

@Test
public void testDispatcherFactoryCoverage() throws Exception {
try (NatsTestServer ts = new NatsTestServer(false);
Connection nc = Nats.connect(Options.builder().server(ts.getURI()).useDispatcherWithExecutor().build()))
{
CountDownLatch latch = new CountDownLatch(1);
Dispatcher d = nc.createDispatcher((msg) -> latch.countDown());
assertInstanceOf(NatsDispatcherWithExecutor.class, d);
String subject = NUID.nextGlobalSequence();
d.subscribe(subject);
nc.publish(subject, null);
assertTrue(latch.await(1, TimeUnit.SECONDS));
}
}
}
3 changes: 3 additions & 0 deletions src/test/java/io/nats/client/impl/ErrorListenerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ public void testCoverage() {
// exercises the default implementation
_cover(new ErrorListenerLoggerImpl());

// exercises the console implementation
_cover(new ErrorListenerConsoleImpl());

// exercises a little more than the defaults
AtomicBoolean errorOccurredFlag = new AtomicBoolean();
AtomicBoolean exceptionOccurredFlag = new AtomicBoolean();
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/io/nats/client/impl/ReconnectTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,46 @@ private static Thread getReconnectOnConnectTestThread(AtomicReference<Connection
return t;
}

@Test
public void testForceReconnectOptionsBuilder() throws Exception {
ForceReconnectOptions fro = ForceReconnectOptions.builder().build();
assertFalse(fro.isForceClose());
assertFalse(fro.isFlush());
assertNull(fro.getFlushWait());

fro = ForceReconnectOptions.builder().forceClose().build();
assertTrue(fro.isForceClose());
assertFalse(fro.isFlush());
assertNull(fro.getFlushWait());

fro = ForceReconnectOptions.builder().flush(42).build();
assertFalse(fro.isForceClose());
assertTrue(fro.isFlush());
assertNotNull(fro.getFlushWait());
assertEquals(42, fro.getFlushWait().toMillis());

fro = ForceReconnectOptions.builder().flush(Duration.ofMillis(42)).build();
assertFalse(fro.isForceClose());
assertTrue(fro.isFlush());
assertNotNull(fro.getFlushWait());
assertEquals(42, fro.getFlushWait().toMillis());

fro = ForceReconnectOptions.builder().flush(null).build();
assertFalse(fro.isForceClose());
assertFalse(fro.isFlush());
assertNull(fro.getFlushWait());

fro = ForceReconnectOptions.builder().flush(-1).build();
assertFalse(fro.isForceClose());
assertFalse(fro.isFlush());
assertNull(fro.getFlushWait());

fro = ForceReconnectOptions.builder().flush(Duration.ofNanos(1)).build();
assertFalse(fro.isForceClose());
assertFalse(fro.isFlush());
assertNull(fro.getFlushWait());
}

@Test
public void testForceReconnect() throws Exception {
ListenerForTesting listener = new ListenerForTesting();
Expand Down
23 changes: 18 additions & 5 deletions src/test/java/io/nats/client/impl/SimplificationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@

import io.nats.client.*;
import io.nats.client.api.*;
import io.nats.client.support.DateTimeUtils;
import io.nats.client.support.SerializableConsumeOptions;
import io.nats.client.support.SerializableFetchConsumeOptions;
import io.nats.client.support.SerializableOrderedConsumerConfiguration;
import io.nats.client.support.*;
import io.nats.client.utils.TestBase;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -984,16 +981,32 @@ public void testOrderedConsumerBuilder() throws IOException, ClassNotFoundExcept
check_values(occ, zdt);
check_values(roundTripSerialize(occ), zdt);

// COVERAGE
// COVERAGE - depends on state from last occ
socc = new SerializableOrderedConsumerConfiguration();
socc.setOrderedConsumerConfiguration(occ);
occ = socc.getOrderedConsumerConfiguration();
check_values(occ, zdt);
check_values(roundTripSerialize(occ), zdt);

// Multiple and COVERAGE
occ = new OrderedConsumerConfiguration().filterSubjects("fs0", "fs1");
assertNull(occ.getFilterSubject());
assertTrue(occ.hasMultipleFilterSubjects());
assertNotNull(occ.getFilterSubjects());
assertEquals("fs0", occ.getFilterSubjects().get(0));
assertEquals("fs1", occ.getFilterSubjects().get(1));
occ = new OrderedConsumerConfiguration(JsonParser.parse(occ.toJson()));
assertNull(occ.getFilterSubject());
assertTrue(occ.hasMultipleFilterSubjects());
assertNotNull(occ.getFilterSubjects());
assertEquals("fs0", occ.getFilterSubjects().get(0));
assertEquals("fs1", occ.getFilterSubjects().get(1));
}

private static void check_default_values(OrderedConsumerConfiguration occ) {
assertEquals(">", occ.getFilterSubject());
assertNotNull(occ.getFilterSubject());
assertFalse(occ.hasMultipleFilterSubjects());
assertNull(occ.getDeliverPolicy());
assertEquals(ConsumerConfiguration.LONG_UNSET, occ.getStartSequence());
assertNull(occ.getStartTime());
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/io/nats/client/support/JsonUtilsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import static io.nats.client.support.ApiConstants.DESCRIPTION;
import static io.nats.client.support.DateTimeUtils.DEFAULT_TIME;
import static io.nats.client.support.DateTimeUtils.ZONE_ID_GMT;
import static io.nats.client.support.JsonUtils.*;
import static io.nats.client.support.JsonValueUtils.mapBuilder;
import static io.nats.client.utils.ResourceUtils.dataAsString;
Expand Down Expand Up @@ -440,6 +441,22 @@ public void testMiscCoverage() {
assertEquals("Foobar", normalize("fooBar"));
assertEquals("deprecated", objectString("name", "deprecated"));
assertEquals("name=null", objectString("name", null));

// coverage of deprecated
byte[] byte64 = json.getBytes();
String base64 = Base64.getEncoder().encodeToString(byte64);
ZonedDateTime zdt = ZonedDateTime.now().withZoneSameInstant(ZONE_ID_GMT);
StringBuilder sb = JsonUtils.beginJson();
addField(sb, "foo64", base64);
addField(sb, "zdt", zdt);
endJson(sb);
System.out.println(sb);

bytes = readBase64(sb.toString(), string_pattern("foo64"));
assertArrayEquals(byte64, bytes);

ZonedDateTime zdtRead = JsonUtils.readDate(sb.toString(), string_pattern("zdt"));
assertEquals(zdt, zdtRead);
}

@Test
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/io/nats/client/support/ValidatorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,40 @@ public void testValidatePrintable() {
assertThrows(IllegalArgumentException.class, () -> validatePrintable(HAS_SPACE, "label", true));
assertThrows(IllegalArgumentException.class, () -> validatePrintable(HAS_127, "label", true));
assertThrows(IllegalArgumentException.class, () -> validatePrintable(HAS_LOW, "label", true));

validatePrintableExceptWildDotGt(PLAIN, "label", true);
validatePrintableExceptWildDotGt(HAS_PRINTABLE, "label", true);
validatePrintableExceptWildDotGt(HAS_DASH, "label", true);
validatePrintableExceptWildDotGt(HAS_UNDER, "label", true);
validatePrintableExceptWildDotGt(HAS_DOLLAR, "label", true);
validatePrintableExceptWildDotGt(HAS_FWD_SLASH, "label", true);
validatePrintableExceptWildDotGt(HAS_BACK_SLASH, "label", true);
validatePrintableExceptWildDotGt(HAS_EQUALS, "label", true);
validatePrintableExceptWildDotGt(HAS_TIC, "label", true);

assertThrows(IllegalArgumentException.class, () -> validatePrintableExceptWildDotGt(HAS_DOT, "label", true));
assertThrows(IllegalArgumentException.class, () -> validatePrintableExceptWildDotGt(STAR_NOT_SEGMENT, "label", true));
assertThrows(IllegalArgumentException.class, () -> validatePrintableExceptWildDotGt(GT_NOT_SEGMENT, "label", true));
assertThrows(IllegalArgumentException.class, () -> validatePrintableExceptWildDotGt(HAS_SPACE, "label", true));
assertThrows(IllegalArgumentException.class, () -> validatePrintableExceptWildDotGt(HAS_127, "label", true));
assertThrows(IllegalArgumentException.class, () -> validatePrintableExceptWildDotGt(HAS_LOW, "label", true));

validatePrintableExceptWildDotGtSlashes(PLAIN, "label", true);
validatePrintableExceptWildDotGtSlashes(HAS_PRINTABLE, "label", true);
validatePrintableExceptWildDotGtSlashes(HAS_DASH, "label", true);
validatePrintableExceptWildDotGtSlashes(HAS_UNDER, "label", true);
validatePrintableExceptWildDotGtSlashes(HAS_DOLLAR, "label", true);
validatePrintableExceptWildDotGtSlashes(HAS_EQUALS, "label", true);
validatePrintableExceptWildDotGtSlashes(HAS_TIC, "label", true);

assertThrows(IllegalArgumentException.class, () -> validatePrintableExceptWildDotGtSlashes(HAS_FWD_SLASH, "label", true));
assertThrows(IllegalArgumentException.class, () -> validatePrintableExceptWildDotGtSlashes(HAS_BACK_SLASH, "label", true));
assertThrows(IllegalArgumentException.class, () -> validatePrintableExceptWildDotGtSlashes(HAS_DOT, "label", true));
assertThrows(IllegalArgumentException.class, () -> validatePrintableExceptWildDotGtSlashes(STAR_NOT_SEGMENT, "label", true));
assertThrows(IllegalArgumentException.class, () -> validatePrintableExceptWildDotGtSlashes(GT_NOT_SEGMENT, "label", true));
assertThrows(IllegalArgumentException.class, () -> validatePrintableExceptWildDotGtSlashes(HAS_SPACE, "label", true));
assertThrows(IllegalArgumentException.class, () -> validatePrintableExceptWildDotGtSlashes(HAS_127, "label", true));
assertThrows(IllegalArgumentException.class, () -> validatePrintableExceptWildDotGtSlashes(HAS_LOW, "label", true));
}

@Test
Expand Down Expand Up @@ -332,11 +366,14 @@ public void testValidateMustMatchIfBothSupplied() {
@Test
public void testValidateRequired() {
required("required", "label");
required("required1", "required2", "label");
required(new Object(), "label");
required(Collections.singletonList("list"), "label");
required(Collections.singletonMap("key", "value"), "label");

assertThrows(IllegalArgumentException.class, () -> required((String)null, "label"));
assertThrows(IllegalArgumentException.class, () -> required("no-second", null, "label"));
assertThrows(IllegalArgumentException.class, () -> required(null, "no-first", "label"));
assertThrows(IllegalArgumentException.class, () -> required(EMPTY, "label"));
assertThrows(IllegalArgumentException.class, () -> required((Object)null, "label"));
assertThrows(IllegalArgumentException.class, () -> required((List)null, "label"));
Expand Down Expand Up @@ -373,6 +410,9 @@ public void testValidateGtZero() {
assertEquals(1, validateGtZero(1, "test"));
assertThrows(IllegalArgumentException.class, () -> validateGtZero(0, "test"));
assertThrows(IllegalArgumentException.class, () -> validateGtZero(-1, "test"));
assertEquals(1, validateGtZero(1L, "test"));
assertThrows(IllegalArgumentException.class, () -> validateGtZero(0L, "test"));
assertThrows(IllegalArgumentException.class, () -> validateGtZero(-1L, "test"));
}

@Test
Expand Down

0 comments on commit 3bfa65c

Please sign in to comment.