Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Direct Message Subject Header May Contain Multiple Subjects #1016

Merged
merged 4 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/io/nats/client/api/MessageInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public MessageInfo(Message msg, String streamName, boolean direct) {

if (direct) {
this.headers = msg.getHeaders();
this.subject = headers.getFirst(NATS_SUBJECT);
this.subject = headers.getLast(NATS_SUBJECT);
this.data = msg.getData();
seq = Long.parseLong(headers.getFirst(NATS_SEQUENCE));
time = DateTimeUtils.parseDateTime(headers.getFirst(NATS_TIMESTAMP));
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/nats/client/impl/Headers.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,17 @@ public String getFirst(String key) {
return values == null ? null : values.get(0);
}

/**
* Returns the last value for the specific (case sensitive) key.
* Will be {@code null} if the key is not found.
*
* @return the last value for the case sensitive key.
*/
public String getLast(String key) {
List<String> values = valuesMap.get(key);
return values == null ? null : values.get(values.size() - 1);
}

/**
* Returns a {@link List} view of the values for the specific (case insensitive) key.
* Will be {@code null} if the key is not found.
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/io/nats/client/impl/HeadersTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -357,15 +357,19 @@ public void remove_collection_work() {
}

@Test
public void getFirsts() {
public void testGetFirstGetLast() {
Headers headers = new Headers();
assertNull(headers.getFirst(KEY1));
assertNull(headers.getLast(KEY1));
headers.add(KEY1, VAL1);
assertEquals(VAL1, headers.getFirst(KEY1));
assertEquals(VAL1, headers.getLast(KEY1));
headers.add(KEY1, VAL2);
assertEquals(VAL1, headers.getFirst(KEY1));
assertEquals(VAL2, headers.getLast(KEY1));
headers.put(KEY1, VAL3);
assertEquals(VAL3, headers.getFirst(KEY1));
assertEquals(VAL3, headers.getLast(KEY1));
}

private void remove(
Expand Down
93 changes: 69 additions & 24 deletions src/test/java/io/nats/client/impl/JetStreamManagementTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ public void testStreamCreateWithNoSubject() throws Exception {
JetStreamManagement jsm = nc.jetStreamManagement();

StreamConfiguration sc = StreamConfiguration.builder()
.name(STREAM)
.storageType(StorageType.Memory)
.build();
.name(STREAM)
.storageType(StorageType.Memory)
.build();

StreamInfo si = jsm.addStream(sc);
assertTrue(now <= si.getCreateTime().toEpochSecond());
Expand Down Expand Up @@ -272,8 +272,8 @@ public void testAddUpdateStreamInvalids() throws Exception {

// cannot change MaxConsumers
StreamConfiguration scMaxCon = getTestStreamConfigurationBuilder()
.maxConsumers(2)
.build();
.maxConsumers(2)
.build();
assertThrows(JetStreamApiException.class, () -> jsm.updateStream(scMaxCon));

StreamConfiguration scReten = getTestStreamConfigurationBuilder()
Expand Down Expand Up @@ -306,9 +306,9 @@ private static StreamConfiguration getTestStreamConfiguration() {

private static StreamConfiguration.Builder getTestStreamConfigurationBuilder() {
return StreamConfiguration.builder()
.name(STREAM)
.storageType(StorageType.Memory)
.subjects(subject(0), subject(1));
.name(STREAM)
.storageType(StorageType.Memory)
.subjects(subject(0), subject(1));
}

@Test
Expand Down Expand Up @@ -596,7 +596,7 @@ public void testDeleteStream() throws Exception {
runInJsServer(nc -> {
JetStreamManagement jsm = nc.jetStreamManagement();
JetStreamApiException jsapiEx =
assertThrows(JetStreamApiException.class, () -> jsm.deleteStream(STREAM));
assertThrows(JetStreamApiException.class, () -> jsm.deleteStream(STREAM));
assertEquals(10059, jsapiEx.getApiErrorCode());

createDefaultTestStream(jsm);
Expand Down Expand Up @@ -681,7 +681,7 @@ public void testAddDeleteConsumer() throws Exception {

final ConsumerConfiguration cc = ConsumerConfiguration.builder().build();
IllegalArgumentException iae =
assertThrows(IllegalArgumentException.class, () -> jsm.addOrUpdateConsumer(null, cc));
assertThrows(IllegalArgumentException.class, () -> jsm.addOrUpdateConsumer(null, cc));
assertTrue(iae.getMessage().contains("Stream cannot be null or empty"));
iae = assertThrows(IllegalArgumentException.class, () -> jsm.addOrUpdateConsumer(STREAM, null));
assertTrue(iae.getMessage().contains("Config cannot be null"));
Expand Down Expand Up @@ -826,12 +826,12 @@ private ConsumerConfiguration prepForUpdateTest(JetStreamManagement jsm) throws
catch (Exception e) { /* ignore */ }

ConsumerConfiguration cc = ConsumerConfiguration.builder()
.durable(durable(1))
.ackPolicy(AckPolicy.Explicit)
.deliverSubject(deliver(1))
.maxDeliver(3)
.filterSubject(SUBJECT_GT)
.build();
.durable(durable(1))
.ackPolicy(AckPolicy.Explicit)
.deliverSubject(deliver(1))
.maxDeliver(3)
.filterSubject(SUBJECT_GT)
.build();
assertValidAddOrUpdate(jsm, cc);
return cc;
}
Expand Down Expand Up @@ -966,9 +966,9 @@ private void addConsumers(JetStreamManagement jsm, String stream, int count, Str
for (int x = 1; x <= count; x++) {
String dur = durable(durableVary, x);
ConsumerConfiguration cc = ConsumerConfiguration.builder()
.durable(dur)
.filterSubject(filterSubject)
.build();
.durable(dur)
.filterSubject(filterSubject)
.build();
ConsumerInfo ci = jsm.addOrUpdateConsumer(stream, cc);
assertEquals(dur, ci.getName());
assertEquals(dur, ci.getConsumerConfiguration().getDurable());
Expand Down Expand Up @@ -1091,16 +1091,16 @@ public void testConsumerReplica() throws Exception {
createMemoryStream(jsm, STREAM, subject(0), subject(1));

final ConsumerConfiguration cc0 = ConsumerConfiguration.builder()
.durable(durable(0))
.build();
.durable(durable(0))
.build();
ConsumerInfo ci = jsm.addOrUpdateConsumer(STREAM, cc0);
// server returns 0 when value is not set
assertEquals(0, ci.getConsumerConfiguration().getNumReplicas());

final ConsumerConfiguration cc1 = ConsumerConfiguration.builder()
.durable(durable(0))
.numReplicas(1)
.build();
.durable(durable(0))
.numReplicas(1)
.build();
ci = jsm.addOrUpdateConsumer(STREAM, cc1);
assertEquals(1, ci.getConsumerConfiguration().getNumReplicas());
});
Expand Down Expand Up @@ -1240,4 +1240,49 @@ private void validateMessageGetRequest(
assertEquals(lastBySubject != null, mgr.isLastBySubject());
assertEquals(nextBySubject != null, mgr.isNextBySubject());
}

@Test
public void testDirectMessageRepublishedSubject() throws Exception {
jsServer.run(nc -> {
JetStreamManagement jsm = nc.jetStreamManagement();
String streamBucketName = "sb-" + variant(null);
String subject = subject();
String streamSubject = subject + ".>";
String publishSubject1 = subject + ".one";
String publishSubject2 = subject + ".two";
String publishSubject3 = subject + ".three";
String republishDest = "$KV." + streamBucketName + ".>";

StreamConfiguration sc = StreamConfiguration.builder()
.name(streamBucketName)
.storageType(StorageType.Memory)
.subjects(streamSubject)
.republish(Republish.builder().source(">").destination(republishDest).build())
.build();
jsm.addStream(sc);

KeyValueConfiguration kvc = KeyValueConfiguration.builder().name(streamBucketName).build();
nc.keyValueManagement().create(kvc);
KeyValue kv = nc.keyValue(streamBucketName);

nc.publish(publishSubject1, "uno".getBytes());
nc.jetStream().publish(publishSubject2, "dos".getBytes());
kv.put(publishSubject3, "tres");

KeyValueEntry kve1 = kv.get(publishSubject1);
assertEquals(streamBucketName, kve1.getBucket());
assertEquals(publishSubject1, kve1.getKey());
assertEquals("uno", kve1.getValueAsString());

KeyValueEntry kve2 = kv.get(publishSubject2);
assertEquals(streamBucketName, kve2.getBucket());
assertEquals(publishSubject2, kve2.getKey());
assertEquals("dos", kve2.getValueAsString());

KeyValueEntry kve3 = kv.get(publishSubject3);
assertEquals(streamBucketName, kve3.getBucket());
assertEquals(publishSubject3, kve3.getKey());
assertEquals("tres", kve3.getValueAsString());
});
}
}