Skip to content

Commit

Permalink
Better testing checks against server versions (#932)
Browse files Browse the repository at this point in the history
  • Loading branch information
scottf authored Jun 22, 2023
1 parent 884893b commit 51fc9d6
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 65 deletions.
8 changes: 4 additions & 4 deletions src/main/java/io/nats/client/support/ServerVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public ServerVersion(String v) {
}
mjr = Integer.parseInt(split[0]);
mnr = Integer.parseInt(split[1]);
ptch = Integer.parseInt(split[2]);
ptch = split.length < 3 ? -1 : Integer.parseInt(split[2]);

for (int i = 3; i < split.length; i++) {
if (i == 3) {
Expand All @@ -50,9 +50,9 @@ public ServerVersion(String v) {
mjr = -1;
}
if (mjr == -1) {
major = 0;
minor = 0;
patch = 0;
major = -1;
minor = -1;
patch = -1;
extra = null;
}
else {
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/io/nats/client/api/ServerInfoTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ public void testServerVersionComparisonsWork() {
assertTrue(si234.isNewerVersionThan("not-a-number.2.3"));
assertTrue(si234.isNewerVersionThan("1.not-a-number.3"));
assertTrue(si234.isNewerVersionThan("1.2.not-a-number"));
assertTrue(si234.isNewerVersionThan("2.3"));
assertFalse(si234.isOlderThanVersion("2.3"));
assertTrue(si235A1.isNewerVersionThan("2.3"));
assertFalse(si235A1.isOlderThanVersion("2.3"));

ServerInfo siPadded1 = new ServerInfo(json.replace("1.2.3", "1.20.30"));
ServerInfo siPadded2 = new ServerInfo(json.replace("1.2.3", "40.500.6000"));
Expand Down
28 changes: 13 additions & 15 deletions src/test/java/io/nats/client/api/StreamConfigurationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,19 @@ private StreamConfiguration getTestConfiguration() {

@Test
public void testRoundTrip() throws Exception {
runInJsServer(nc -> {
if (nc.getServerInfo().isNewerVersionThan("2.8.4")) {
StreamConfiguration sc = StreamConfiguration.builder(getTestConfiguration())
.mirror(null)
.sources()
.replicas(1)
.templateOwner(null)
.allowRollup(false)
.allowDirect(false)
.mirrorDirect(false)
.sealed(false)
.build();
JetStreamManagement jsm = nc.jetStreamManagement();
validate(jsm.addStream(sc).getConfiguration(), true);
}
runInJsServer(si -> si.isNewerVersionThan("2.8.4"), nc -> {
StreamConfiguration sc = StreamConfiguration.builder(getTestConfiguration())
.mirror(null)
.sources()
.replicas(1)
.templateOwner(null)
.allowRollup(false)
.allowDirect(false)
.mirrorDirect(false)
.sealed(false)
.build();
JetStreamManagement jsm = nc.jetStreamManagement();
validate(jsm.addStream(sc).getConfiguration(), true);
});
}

Expand Down
11 changes: 5 additions & 6 deletions src/test/java/io/nats/client/impl/JetStreamGeneralTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void testMiscMetaDataCoverage() {
@Test
public void testJetStreamSubscribe() throws Exception {
runInJsServer(nc -> {
boolean atLeast290 = ((NatsConnection)nc).getInfo().isSameOrNewerThanVersion("2.9.0");
boolean atLeast2dot9 = ((NatsConnection)nc).getInfo().isSameOrNewerThanVersion("2.9");

JetStream js = nc.jetStream();
JetStreamManagement jsm = nc.jetStreamManagement();
Expand Down Expand Up @@ -177,8 +177,7 @@ public void testJetStreamSubscribe() throws Exception {
unsubscribeEnsureNotBound(dispatcher, sub);
js.subscribe("", queue(102), dispatcher, mh -> {}, false, psoBind);

// test 2.9.0
if (atLeast290) {
if (atLeast2dot9) {
ConsumerConfiguration cc = builder().name(name(1)).build();
pso = PushSubscribeOptions.builder().configuration(cc).build();
sub = js.subscribe(SUBJECT, pso);
Expand Down Expand Up @@ -819,16 +818,16 @@ public void testSubscribeDurableConsumerMustMatch() throws Exception {

// metadata
Map<String, String> metadataA = new HashMap<>(); metadataA.put("a", "A");
Map<String, String> metadataB = new HashMap<>(); metadataA.put("b", "B");
Map<String, String> metadataB = new HashMap<>(); metadataB.put("b", "B");

if (nc.getServerInfo().isNewerVersionThan("2.9.99")) {
if (nc.getServerInfo().isSameOrNewerThanVersion("2.10")) {
// metadata server null versus new not null
nc.jetStreamManagement().addOrUpdateConsumer(STREAM, pushDurableBuilder().build());
changeExPush(js, pushDurableBuilder().metadata(metadataA), "metadata");

// metadata server not null versus new null
nc.jetStreamManagement().addOrUpdateConsumer(STREAM, pushDurableBuilder().metadata(metadataA).build());
changeExPush(js, pushDurableBuilder(), "metadata");
changeOkPush(js, pushDurableBuilder());

// metadata server not null versus new not null but different
changeExPush(js, pushDurableBuilder().metadata(metadataB), "metadata");
Expand Down
18 changes: 9 additions & 9 deletions src/test/java/io/nats/client/impl/JetStreamManagementTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void testStreamMetadata() throws Exception {
StreamInfo si = jsm.addStream(sc);
assertNotNull(si.getConfiguration());
sc = si.getConfiguration();
if (nc.getServerInfo().isNewerVersionThan("2.9.99")) {
if (nc.getServerInfo().isSameOrNewerThanVersion("2.10")) {
assertEquals(1, sc.getMetadata().size());
assertEquals("meta-bar", sc.getMetadata().get("meta-foo"));
}
Expand Down Expand Up @@ -639,7 +639,7 @@ public void testPurgeStreamAndOptions() throws Exception {
@Test
public void testAddDeleteConsumer() throws Exception {
runInJsServer(nc -> {
boolean atLeast290 = ((NatsConnection)nc).getInfo().isSameOrNewerThanVersion("2.9.0");
boolean atLeast2dot9 = ((NatsConnection)nc).getInfo().isSameOrNewerThanVersion("2.9");

JetStreamManagement jsm = nc.jetStreamManagement();

Expand Down Expand Up @@ -669,11 +669,11 @@ public void testAddDeleteConsumer() throws Exception {
createMemoryStream(jsm, STREAM, subjectDot(">"));

// with and w/o deliver subject for push/pull
addConsumer(jsm, atLeast290, 1, false, null, ConsumerConfiguration.builder()
addConsumer(jsm, atLeast2dot9, 1, false, null, ConsumerConfiguration.builder()
.durable(durable(1))
.build());

addConsumer(jsm, atLeast290, 2, true, null, ConsumerConfiguration.builder()
addConsumer(jsm, atLeast2dot9, 2, true, null, ConsumerConfiguration.builder()
.durable(durable(2))
.deliverSubject(deliver(2))
.build());
Expand All @@ -687,7 +687,7 @@ public void testAddDeleteConsumer() throws Exception {
assertThrows(JetStreamApiException.class, () -> jsm.deleteConsumer(STREAM, durable(1)));

// some testing of new name
if (atLeast290) {
if (atLeast2dot9) {
addConsumer(jsm, true, 3, false, null, ConsumerConfiguration.builder()
.durable(durable(3))
.name(durable(3))
Expand Down Expand Up @@ -717,10 +717,10 @@ public void testAddDeleteConsumer() throws Exception {
});
}

private static void addConsumer(JetStreamManagement jsm, boolean atLeast290, int id, boolean deliver, String fs, ConsumerConfiguration cc) throws IOException, JetStreamApiException {
private static void addConsumer(JetStreamManagement jsm, boolean atLeast2dot9, int id, boolean deliver, String fs, ConsumerConfiguration cc) throws IOException, JetStreamApiException {
ConsumerInfo ci = jsm.addOrUpdateConsumer(STREAM, cc);
assertEquals(durable(id), ci.getName());
if (atLeast290) {
if (atLeast2dot9) {
assertEquals(durable(id), ci.getConsumerConfiguration().getName());
}
assertEquals(durable(id), ci.getConsumerConfiguration().getDurable());
Expand Down Expand Up @@ -838,7 +838,7 @@ public void testConsumerMetadata() throws Exception {
.build();

ConsumerInfo ci = jsm.addOrUpdateConsumer(STREAM, cc);
if (nc.getServerInfo().isNewerVersionThan("2.9.99")) {
if (nc.getServerInfo().isSameOrNewerThanVersion("2.10")) {
assertEquals(1, ci.getConsumerConfiguration().getMetadata().size());
assertEquals("meta-bar", ci.getConsumerConfiguration().getMetadata().get("meta-foo"));
}
Expand All @@ -862,7 +862,7 @@ public void testCreateConsumersWithFilters() throws Exception {
List<ConsumerInfo> cis = jsm.getConsumers(STREAM);
assertEquals(SUBJECT, cis.get(0).getConsumerConfiguration().getFilterSubject());

if (nc.getServerInfo().isNewerVersionThan("2.9.99")) {
if (nc.getServerInfo().isSameOrNewerThanVersion("2.10")) {
// 2.10 and later you can set the filter to something that does not match
jsm.addOrUpdateConsumer(STREAM, builder.filterSubject(subjectDot("two-ten-allows-not-matching")).build());
cis = jsm.getConsumers(STREAM);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/io/nats/client/impl/KeyValueTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1351,7 +1351,7 @@ public void testKeyValueMirrorCrossDomains() throws Exception {

sleep(200); // make sure things get a chance to propagate
StreamInfo si = leaf.jetStreamManagement().getStreamInfo("KV_MIRROR");
if (hub.getServerInfo().isSameOrNewerThanVersion("2.9.0")) {
if (hub.getServerInfo().isSameOrNewerThanVersion("2.9")) {
assertTrue(si.getConfiguration().getMirrorDirect());
}
assertEquals(3, si.getStreamState().getMsgCount());
Expand Down
21 changes: 11 additions & 10 deletions src/test/java/io/nats/client/impl/SimplificationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
package io.nats.client.impl;

import io.nats.client.*;
import io.nats.client.api.ConsumerConfiguration;
import io.nats.client.api.ConsumerInfo;
import io.nats.client.api.MessageInfo;
import io.nats.client.api.StreamInfoOptions;
import io.nats.client.api.*;
import org.junit.jupiter.api.Test;

import java.io.IOException;
Expand All @@ -30,9 +27,13 @@

public class SimplificationTests extends JetStreamTestBase {

private boolean runTest(ServerInfo si) {
return si.isSameOrNewerThanVersion("2.9.1");
}

@Test
public void testStreamContext() throws Exception {
runInJsServer(nc -> {
runInJsServer(this::runTest, nc -> {
JetStreamManagement jsm = nc.jetStreamManagement();
JetStream js = nc.jetStream();

Expand Down Expand Up @@ -116,7 +117,7 @@ private static void _testStreamContext(JetStream js, StreamContext streamContext

@Test
public void testFetch() throws Exception {
runInJsServer(nc -> {
runInJsServer(this::runTest, nc -> {
createDefaultTestStream(nc);
JetStream js = nc.jetStream();
for (int x = 1; x <= 20; x++) {
Expand Down Expand Up @@ -225,7 +226,7 @@ private static String generateConsumerName(int maxMessages, int maxBytes) {

@Test
public void testIterableConsumer() throws Exception {
runInJsServer(nc -> {
runInJsServer(this::runTest, nc -> {
JetStreamManagement jsm = nc.jetStreamManagement();

createDefaultTestStream(jsm);
Expand Down Expand Up @@ -288,7 +289,7 @@ public void testIterableConsumer() throws Exception {

@Test
public void testConsumeWithHandler() throws Exception {
runInJsServer(nc -> {
runInJsServer(this::runTest, nc -> {
JetStreamManagement jsm = nc.jetStreamManagement();

createDefaultTestStream(jsm);
Expand Down Expand Up @@ -323,7 +324,7 @@ public void testConsumeWithHandler() throws Exception {

@Test
public void testNext() throws Exception {
runInJsServer(nc -> {
runInJsServer(this::runTest, nc -> {
JetStreamManagement jsm = nc.jetStreamManagement();

createDefaultTestStream(jsm);
Expand All @@ -348,7 +349,7 @@ public void testNext() throws Exception {

@Test
public void testCoverage() throws Exception {
runInJsServer(nc -> {
runInJsServer(this::runTest, nc -> {
JetStreamManagement jsm = nc.jetStreamManagement();

createDefaultTestStream(jsm);
Expand Down
61 changes: 41 additions & 20 deletions src/test/java/io/nats/client/utils/TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package io.nats.client.utils;

import io.nats.client.*;
import io.nats.client.api.ServerInfo;
import io.nats.client.impl.NatsMessage;
import io.nats.client.impl.TestHandler;
import io.nats.client.support.NatsJetStreamClientError;
Expand Down Expand Up @@ -75,54 +76,74 @@ public interface TwoServerTest {
void test(Connection nc1, Connection nc2) throws Exception;
}

public interface VersionCheck {
boolean runTest(ServerInfo si);
}

public static void runInServer(InServerTest inServerTest) throws Exception {
runInServer(false, false, inServerTest);
runInServer(false, false, null, null, inServerTest);
}

public static void runInServer(Options.Builder builder, InServerTest inServerTest) throws Exception {
runInServer(false, false, builder, inServerTest);
runInServer(false, false, builder, null, inServerTest);
}

public static void runInServer(boolean debug, InServerTest inServerTest) throws Exception {
runInServer(debug, false, inServerTest);
runInServer(debug, false, null, null, inServerTest);
}

public static void runInJsServer(InServerTest inServerTest) throws Exception {
runInServer(false, true, inServerTest);
runInServer(false, true, null, null, inServerTest);
}

public static void runInJsServer(ErrorListener el, InServerTest inServerTest) throws Exception {
Options.Builder builder = new Options.Builder().errorListener(el);
runInServer(false, true, builder, inServerTest);
runInServer(false, true, new Options.Builder().errorListener(el), null, inServerTest);
}

public static void runInJsServer(Options.Builder builder, InServerTest inServerTest) throws Exception {
runInServer(false, true, builder, inServerTest);
runInServer(false, true, builder, null, inServerTest);
}

public static void runInJsServer(VersionCheck vc, InServerTest inServerTest) throws Exception {
runInServer(false, true, null, vc, inServerTest);
}

public static void runInJsServer(boolean debug, InServerTest inServerTest) throws Exception {
runInServer(debug, true, inServerTest);
runInServer(debug, true, null, null, inServerTest);
}

public static void runInServer(boolean debug, boolean jetstream, InServerTest inServerTest) throws Exception {
try (NatsTestServer ts = new NatsTestServer(debug, jetstream);
Connection nc = standardConnection(ts.getURI()))
{
try {
inServerTest.test(nc);
}
finally {
if (jetstream) {
cleanupJs(nc);
}
}
}
runInServer(debug, jetstream, null, null, inServerTest);
}

public static void runInServer(boolean debug, boolean jetstream, Options.Builder builder, InServerTest inServerTest) throws Exception {
runInServer(debug, jetstream, builder, null, inServerTest);
}

private static ServerInfo runServerInfo;

public static void runInServer(boolean debug, boolean jetstream, Options.Builder builder, VersionCheck vc, InServerTest inServerTest) throws Exception {
if (vc != null && runServerInfo != null) {
if (!vc.runTest(runServerInfo)) {
return;
}
vc = null; // since we've already determined it should run, null this out so we don't check below
}

if (builder == null) {
builder = new Options.Builder();
}

try (NatsTestServer ts = new NatsTestServer(debug, jetstream);
Connection nc = standardConnection(builder.server(ts.getURI()).build()))
{
if (vc != null) {
runServerInfo = nc.getServerInfo();
if (!vc.runTest(runServerInfo)) {
return;
}
}

try {
inServerTest.test(nc);
}
Expand Down

0 comments on commit 51fc9d6

Please sign in to comment.