This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new bb38302418ed CAMEL-26957: camel-nats - a DST fake world for the
JetStream pull consumer (#29995)
bb38302418ed is described below
commit bb38302418edee1e2bc789cc4eed8d98ac4a2dfa
Author: allthingssecurity <[email protected]>
AuthorDate: Sun Nov 15 20:35:00 2026 +0530
CAMEL-26957: camel-nats - a DST fake world for the JetStream pull consumer
(#29995)
…sumer
Adds a camel-test-dst world for the camel-nats JetStream pull consumer, in
the dst package of its tests: NatsConsumerDstTest (1000 seeds), NatsWorld
and FakeJetStream. The real consumer runs against a fake NATS server with
one JetStream stream, given as a shared jnats Connection with the connection
option, written from the NATS documentation and the jnats client: pull
fetches that wait up to their timeout for up to a batch, explicit ack, nak
and nak with a delay, AckWait redelivery, MaxDeliver, ephemeral and durable
consumers, a server restart that deletes the ephemeral consumers (409
Consumer Deleted for a waiting pull, an empty fetch and no consumer info
afterwards), a flush that times out, and acks that are lost.
The seed picks pullBatchSize, pullFetchTimeout, ackWait, maxDeliver,
nackWait, a durable or an ephemeral consumer and the fault rates, and
injects failed fetches, lost acks, flushes that time out, failed and slow
processing (now and then longer than AckWait), route restarts (also while
the consumer waits in a fetch) and server restarts.
Invariants: the consumer passes its ack policy, AckWait and MaxDeliver to
the server and uses the connection as documented; a message is acked only
after its exchange completed and nak'd only after it failed; a stopped
consumer leaves no subscription active; once the faults cleared every
message is acked, or given up after MaxDeliver (or skipped by a consumer
made again after its ack was lost).
Not explored: push subscriptions and core NATS subjects, a stop between two
messages of a fetched batch, the interrupt of the consumer thread by the
stop, and a start that comes while the stopped consumer task still
subscribes.
Self-check: on the code before CAMEL-26884 (6bc46f9cffc1 reverted) 346 of
2000 seeds fail; main passes 5000 of 5000 seeds.
The module's pom loads the byte code agent of camel-test-dst, the test log
is capped, and the catalog lists the world.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../org/apache/camel/catalog/components/nats.json | 3 +
.../org/apache/camel/catalog/docs/test-dst.adoc | 9 +
components/camel-nats/pom.xml | 32 ++
.../org/apache/camel/component/nats/nats.json | 3 +
.../camel/component/nats/dst/FakeJetStream.java | 532 +++++++++++++++++++++
.../component/nats/dst/NatsConsumerDstTest.java | 37 ++
.../apache/camel/component/nats/dst/NatsWorld.java | 331 +++++++++++++
.../src/test/resources/log4j2.properties | 9 +-
.../camel-test-dst/src/main/docs/test-dst.adoc | 9 +
9 files changed, 964 insertions(+), 1 deletion(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/nats.json
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/nats.json
index 38a5f000eb5a..226c8e3a9081 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/nats.json
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/nats.json
@@ -131,5 +131,8 @@
"credentialsFilePath": { "index": 41, "kind": "parameter", "displayName":
"Credentials File Path", "group": "security", "label": "security", "required":
false, "type": "string", "javaType": "java.lang.String", "deprecated": false,
"autowired": false, "secret": false, "configurationClass":
"org.apache.camel.component.nats.NatsConfiguration", "configurationField":
"configuration", "description": "If we use useCredentialsFile to true we'll
need to set the credentialsFilePath option. It [...]
"secure": { "index": 42, "kind": "parameter", "displayName": "Secure",
"group": "security", "label": "security", "required": false, "type": "boolean",
"javaType": "boolean", "deprecated": false, "autowired": false, "secret":
false, "defaultValue": false, "configurationClass":
"org.apache.camel.component.nats.NatsConfiguration", "configurationField":
"configuration", "description": "Set secure option indicating TLS is required"
},
"sslContextParameters": { "index": 43, "kind": "parameter", "displayName":
"Ssl Context Parameters", "group": "security", "label": "security", "required":
false, "type": "object", "javaType":
"org.apache.camel.support.jsse.SSLContextParameters", "deprecated": false,
"autowired": false, "secret": false, "configurationClass":
"org.apache.camel.component.nats.NatsConfiguration", "configurationField":
"configuration", "description": "To configure security using
SSLContextParameters" }
+ },
+ "dstWorlds": {
+ "NatsWorld": { "javaType":
"org.apache.camel.component.nats.dst.NatsWorld", "fake": "FakeJetStream: a
server with one JetStream stream and its pull consumers (AckWait, MaxDeliver,
Nak), behind a jnats Connection given with the connection option", "covers": [
"the JetStream pull consumer with explicit acks and naks, ephemeral or
durable", "AckWait and MaxDeliver", "route stop and start, also while the
consumer waits in a fetch", "a server restart that deletes the ephemeral
consumer, a [...]
}
}
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/test-dst.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/test-dst.adoc
index ffdc45e6d8a6..cd063a0bd418 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/test-dst.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/test-dst.adoc
@@ -103,6 +103,15 @@ a|* explicit acknowledgement with onFailure RELEASE or
REJECT
* suspend and resume of the CamelContext
|CAMEL-25872, CAMEL-25910
+|camel-nats
+|`NatsWorld` (`NatsConsumerDstTest`)
+|FakeJetStream: a server with one JetStream stream and its pull consumers
(AckWait, MaxDeliver, Nak), behind a jnats Connection given with the connection
option
+a|* the JetStream pull consumer with explicit acks and naks, ephemeral or
durable
+* AckWait and MaxDeliver
+* route stop and start, also while the consumer waits in a fetch
+* a server restart that deletes the ephemeral consumer, and the subscription
made again
+|CAMEL-26884
+
|camel-pulsar
|`PulsarWorld` (`PulsarConsumerDstTest`)
|FakePulsar: a broker and client for one topic and one durable subscription
diff --git a/components/camel-nats/pom.xml b/components/camel-nats/pom.xml
index 5d001e69ead9..d29004cd466e 100644
--- a/components/camel-nats/pom.xml
+++ b/components/camel-nats/pom.xml
@@ -34,6 +34,8 @@
<properties>
<!-- Nats container is not available on these platforms -->
<skipITs.s390x>true</skipITs.s390x>
+ <!-- the DST tests load the byte code agent of camel-test-dst when the
JVM starts (see test-dst.adoc) -->
+
<camel.surefire.fork.additional-vmargs>"-javaagent:${net.bytebuddy:byte-buddy-agent:jar}"</camel.surefire.fork.additional-vmargs>
</properties>
<dependencies>
@@ -67,5 +69,35 @@
<version>${slf4j-version}</version>
<scope>test</scope>
</dependency>
+ <!-- deterministic simulation test of the JetStream pull consumer (dst
package) -->
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-test-dst</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <version>${mockito-version}</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <!-- sets the property net.bytebuddy:byte-buddy-agent:jar to
the path of the agent of the DST tests -->
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-dependency-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>dst-agent-path</id>
+ <goals>
+ <goal>properties</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
</project>
diff --git
a/components/camel-nats/src/generated/resources/META-INF/org/apache/camel/component/nats/nats.json
b/components/camel-nats/src/generated/resources/META-INF/org/apache/camel/component/nats/nats.json
index 38a5f000eb5a..226c8e3a9081 100644
---
a/components/camel-nats/src/generated/resources/META-INF/org/apache/camel/component/nats/nats.json
+++
b/components/camel-nats/src/generated/resources/META-INF/org/apache/camel/component/nats/nats.json
@@ -131,5 +131,8 @@
"credentialsFilePath": { "index": 41, "kind": "parameter", "displayName":
"Credentials File Path", "group": "security", "label": "security", "required":
false, "type": "string", "javaType": "java.lang.String", "deprecated": false,
"autowired": false, "secret": false, "configurationClass":
"org.apache.camel.component.nats.NatsConfiguration", "configurationField":
"configuration", "description": "If we use useCredentialsFile to true we'll
need to set the credentialsFilePath option. It [...]
"secure": { "index": 42, "kind": "parameter", "displayName": "Secure",
"group": "security", "label": "security", "required": false, "type": "boolean",
"javaType": "boolean", "deprecated": false, "autowired": false, "secret":
false, "defaultValue": false, "configurationClass":
"org.apache.camel.component.nats.NatsConfiguration", "configurationField":
"configuration", "description": "Set secure option indicating TLS is required"
},
"sslContextParameters": { "index": 43, "kind": "parameter", "displayName":
"Ssl Context Parameters", "group": "security", "label": "security", "required":
false, "type": "object", "javaType":
"org.apache.camel.support.jsse.SSLContextParameters", "deprecated": false,
"autowired": false, "secret": false, "configurationClass":
"org.apache.camel.component.nats.NatsConfiguration", "configurationField":
"configuration", "description": "To configure security using
SSLContextParameters" }
+ },
+ "dstWorlds": {
+ "NatsWorld": { "javaType":
"org.apache.camel.component.nats.dst.NatsWorld", "fake": "FakeJetStream: a
server with one JetStream stream and its pull consumers (AckWait, MaxDeliver,
Nak), behind a jnats Connection given with the connection option", "covers": [
"the JetStream pull consumer with explicit acks and naks, ephemeral or
durable", "AckWait and MaxDeliver", "route stop and start, also while the
consumer waits in a fetch", "a server restart that deletes the ephemeral
consumer, a [...]
}
}
diff --git
a/components/camel-nats/src/test/java/org/apache/camel/component/nats/dst/FakeJetStream.java
b/components/camel-nats/src/test/java/org/apache/camel/component/nats/dst/FakeJetStream.java
new file mode 100644
index 000000000000..f978e57acb15
--- /dev/null
+++
b/components/camel-nats/src/test/java/org/apache/camel/component/nats/dst/FakeJetStream.java
@@ -0,0 +1,532 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.nats.dst;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.ZoneOffset;
+import java.time.ZonedDateTime;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeoutException;
+import java.util.function.Consumer;
+
+import io.nats.client.Connection;
+import io.nats.client.JetStream;
+import io.nats.client.JetStreamManagement;
+import io.nats.client.JetStreamStatusException;
+import io.nats.client.JetStreamSubscription;
+import io.nats.client.Message;
+import io.nats.client.PullSubscribeOptions;
+import io.nats.client.api.AckPolicy;
+import io.nats.client.api.ConsumerConfiguration;
+import io.nats.client.api.ConsumerInfo;
+import io.nats.client.api.DeliverPolicy;
+import io.nats.client.api.ServerInfo;
+import io.nats.client.api.StreamInfo;
+import io.nats.client.impl.NatsJetStreamMetaData;
+import io.nats.client.support.Status;
+import org.apache.camel.test.dst.Dst;
+import org.apache.camel.test.dst.DstLedger;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * A fake NATS server with one JetStream stream and its pull consumers, and
the connection camel-nats gets through its
+ * {@code connection} option (dynamic proxies of the jnats interfaces; Mockito
mocks of the jnats classes ServerInfo,
+ * StreamInfo, ConsumerInfo and NatsJetStreamMetaData). Written from the NATS
documentation, not from the Camel
+ * consumer:
+ * <ul>
+ * <li>[C] Consumers (https://docs.nats.io/nats-concepts/jetstream/consumers):
with AckExplicit a delivered message that
+ * is not acknowledged within AckWait is delivered again; "MaxDeliver: the
maximum number of times a specific message
+ * delivery will be attempted", after which it is not delivered again; an
ephemeral consumer is removed by the server
+ * after its inactive threshold, for example while the client is disconnected
or after a server restart; a new ephemeral
+ * consumer starts with no delivery state, so a message delivered before to
another consumer and not acknowledged (also
+ * one nak'd there with a delay) is delivered to it at once (simplified: the
fake keeps one state and one delivery count
+ * per message for all consumers, see subscribe).</li>
+ * <li>[A] Acknowledgements
+ *
(https://docs.nats.io/using-nats/developer/develop_jetstream/consumers#acknowledging-messages):
Ack, Nak (redeliver
+ * now) and Nak with a delay; an ack is a fire-and-forget publish, so it can
be lost (assumed here: the message is then
+ * delivered again after AckWait).</li>
+ * <li>[P] Pull (jnats 2.26.4 JetStreamSubscription.fetch,
NatsJetStreamPullSubscription._fetch and
+ * PullMessageManager.manageStatus): one pull request that expires with the
timeout, and a partial batch is held until
+ * the pull expires, the batch is full or a status ends the fetch, so it
returns what it got then; the server sends each
+ * message as soon as it is available, so its AckWait runs from then, also
while the fetch holds it; after the
+ * subscription was unsubscribed it throws IllegalStateException; while the
connection is down the pull request is
+ * buffered and the fetch ends empty at its timeout; when the server deletes
the consumer while the pull waits and the
+ * client is connected, the fetch throws JetStreamStatusException with status
409 "Consumer Deleted"; a 409 "Server
+ * Shutdown" (a server restart) is a warning, and the fetch keeps waiting
until its timeout and returns the messages it
+ * got before; a later pull to the missing consumer gets status 503, which
ends the fetch at once with no message;
+ * getConsumerInfo answers null for a consumer the server does not have.</li>
+ * <li>[F] flush (jnats Connection.flush): throws TimeoutException when the
server does not answer within the
+ * timeout.</li>
+ * </ul>
+ * Not modelled: push consumers and dispatchers, core NATS subjects, AckAll
and AckNone, MaxAckPending, the stream
+ * limits and retention, the reconnect of the connection itself (the
connection stays CONNECTED; a server restart is
+ * modelled as the deletion of the ephemeral consumer).
+ * <p>
+ * Timing (one-stack rule): a fetch waits first and takes its messages at the
end of its wait, and their AckWait runs
+ * from when each became available (published, released after AckWait or a
nak, or the end of a nak delay) or from the
+ * start of the fetch if later, as if the server had sent it then; acks and
naks take effect at once; flush blocks for
+ * its round trip.
+ */
+final class FakeJetStream {
+
+ static final String STREAM = "dst";
+ static final String TOPIC = "dst.orders";
+
+ enum State {
+ AVAILABLE,
+ PENDING,
+ ACKED,
+ GIVEN_UP
+ }
+
+ static final class Msg {
+ final long seq;
+ final String id;
+ State state = State.AVAILABLE;
+ int deliveries;
+ long redeliverAt;
+ /** When it became available last: published, released after AckWait
or a nak, or the end of a nak delay. */
+ long availableSince;
+ boolean ackLost;
+
+ Msg(long seq, String id) {
+ this.seq = seq;
+ this.id = id;
+ }
+ }
+
+ /** A pull consumer of the stream (ephemeral unless durable), and the
subscription of the client on it. */
+ final class Sub implements InvocationHandler {
+ final String name;
+ final long startSeq;
+ boolean active = true;
+ boolean deleted;
+ /**
+ * Deleted by a server restart: a waiting fetch gets 409 Server
Shutdown (a warning) instead of Consumer
+ * Deleted.
+ */
+ boolean serverShutdown;
+ boolean durable;
+ private Object proxySelf;
+
+ Sub(String name, long startSeq) {
+ this.name = name;
+ this.startSeq = startSeq;
+ }
+
+ @Override
+ public Object invoke(Object proxy, Method method, Object[] args)
throws Exception {
+ switch (method.getName()) {
+ case "toString":
+ return "FakeSubscription[" + name + "]";
+ case "hashCode":
+ return System.identityHashCode(this);
+ case "equals":
+ return proxy == args[0];
+ case "isActive":
+ return active;
+ case "getSubject":
+ return TOPIC;
+ case "getQueueName":
+ return null;
+ case "getConsumerName":
+ return name;
+ case "unsubscribe":
+ if (!active) {
+ throw new IllegalStateException("Subscription is
inactive");
+ }
+ active = false;
+ dst.trace("fake: " + name + " unsubscribe");
+ return null;
+ case "getConsumerInfo":
+ dst.block(dst.between(1, 30), name + " consumer info");
+ return deleted ? null : mock(ConsumerInfo.class);
+ case "fetch":
+ return fetch((Integer) args[0],
+ args[1] instanceof Duration d ? d.toMillis() :
(Long) args[1]);
+ default:
+ violations.add(name + " called " + method.getName() + "
(not modelled)");
+ throw new UnsupportedOperationException(method.getName());
+ }
+ }
+
+ /**
+ * [P] [C] holds a partial batch until the pull expires (the timeout),
the batch is full or a status ends it,
+ * then takes the messages available at the end of its wait.
+ */
+ private List<Message> fetch(int batch, long timeout) throws
InterruptedException {
+ if (!active) {
+ throw new IllegalStateException("Subscription is inactive");
+ }
+ long wait = Math.max(1, timeout);
+ if (deleted) {
+ // [P] a pull to a consumer the server does not have is
answered 503 (no responders), which jnats takes
+ // as the end of the fetch: it returns what it has, nothing
+ dst.block(dst.between(1, 20), name + " fetch");
+ return new ArrayList<>();
+ }
+ if (faults && dst.chance(fetchFailRate)) {
+ // [P] the connection is down: the pull request is buffered,
no message comes, and the fetch ends empty at
+ // its timeout
+ ledger.record("fetchFault", "s", name);
+ dst.trace("fake: " + name + " fetch while the connection is
down");
+ dst.awaitUntil(() -> !active, wait, name + " fetch while the
connection is down");
+ if (!active) {
+ throw new IllegalStateException("Subscription is
inactive");
+ }
+ return new ArrayList<>();
+ }
+ long fetchStart = dst.now();
+ dst.awaitUntil(() -> !active || deleted ||
availableCount(startSeq) >= batch, wait, name + " fetch");
+ if (!active) {
+ throw new IllegalStateException("Subscription is inactive");
+ }
+ dst.block(dst.between(1, 20), name + " fetch answer");
+ if (deleted && serverShutdown) {
+ // [P] 409 Server Shutdown is a warning: jnats keeps waiting
until the timeout and returns the messages the
+ // server sent before it shut down
+ List<Message> answer = take(batch, fetchStart);
+ long left = fetchStart + wait - dst.now();
+ if (left > 0) {
+ dst.awaitUntil(() -> !active, left, name + " fetch after
the server shut down");
+ }
+ if (!active) {
+ throw new IllegalStateException("Subscription is
inactive");
+ }
+ return answer;
+ }
+ if (deleted) {
+ throw new JetStreamStatusException(new Status(409,
Status.CONSUMER_DELETED));
+ }
+ return take(batch, fetchStart);
+ }
+
+ /** [C] takes up to batch available messages, with their AckWait from
when the server sent them. */
+ private List<Message> take(int batch, long fetchStart) {
+ redeliver();
+ List<Message> answer = new ArrayList<>();
+ for (Msg m : messages.values()) {
+ if (answer.size() >= batch) {
+ break;
+ }
+ // an AVAILABLE message with a later redeliverAt was nak'd
with a delay
+ if (m.seq < startSeq || m.state != State.AVAILABLE ||
m.redeliverAt > dst.now()) {
+ continue;
+ }
+ m.state = State.PENDING;
+ m.deliveries++;
+ // the server sent it when it became available, or when the
pull arrived: its AckWait runs from then
+ m.redeliverAt = Math.max(fetchStart, m.availableSince) +
ackWaitMillis;
+ String delivery = m.id + "#" + m.deliveries;
+ ledger.record("delivered", "s", delivery);
+ onDelivery.accept(delivery);
+ answer.add(message(m, delivery, (JetStreamSubscription)
proxySelf));
+ }
+ if (!answer.isEmpty()) {
+ dst.trace("fake: " + name + " fetch returns " + answer.size()
+ " messages");
+ }
+ return answer;
+ }
+ }
+
+ private final Dst dst;
+ private final DstLedger ledger;
+ final Map<Long, Msg> messages = new LinkedHashMap<>();
+ final List<Sub> subscriptions = new ArrayList<>();
+ final List<String> violations = new ArrayList<>();
+ long ackWaitMillis = 30_000;
+ int maxDeliver = -1;
+ double fetchFailRate;
+ double ackLossRate;
+ double flushFailRate;
+ boolean faults = true;
+ Consumer<String> onDelivery = d -> {
+ };
+ Consumer<String> onAck = d -> {
+ };
+ Consumer<String> onNak = d -> {
+ };
+ private final ZonedDateTime streamCreated;
+ private long nextSeq = 1;
+ private int subs;
+
+ FakeJetStream(Dst dst, DstLedger ledger) {
+ this.dst = dst;
+ this.ledger = ledger;
+ this.streamCreated =
ZonedDateTime.ofInstant(Instant.ofEpochMilli(Dst.EPOCH_MILLIS), ZoneOffset.UTC);
+ }
+
+ Msg publish(String id) {
+ Msg m = new Msg(nextSeq++, id);
+ m.availableSince = dst.now();
+ messages.put(m.seq, m);
+ ledger.record("published", "s", id);
+ return m;
+ }
+
+ /**
+ * [C] [P] the ephemeral consumers are gone: after a server restart (a
waiting fetch got 409 Server Shutdown), or
+ * deleted while the client is connected (a waiting fetch gets 409
Consumer Deleted); the seed picks which.
+ */
+ void restartServer() {
+ boolean shutdown = dst.chance(0.5);
+ dst.trace("fake: " + (shutdown ? "server restart" : "consumer deletion
while connected")
+ + ", the ephemeral consumers are deleted");
+ for (Sub s : subscriptions) {
+ if (!s.durable) {
+ s.deleted = true;
+ s.serverShutdown = shutdown;
+ }
+ }
+ }
+
+ /** [C] AckWait expired: delivered again, unless MaxDeliver was reached. */
+ void redeliver() {
+ for (Msg m : messages.values()) {
+ if (m.state == State.PENDING && dst.now() >= m.redeliverAt) {
+ giveUpOrRelease(m, "ack wait expired");
+ }
+ }
+ }
+
+ private void giveUpOrRelease(Msg m, String why) {
+ if (maxDeliver > 0 && m.deliveries >= maxDeliver) {
+ m.state = State.GIVEN_UP;
+ ledger.record("maxDeliver", "s", m.id);
+ } else {
+ m.state = State.AVAILABLE;
+ // the end of AckWait, or of the delay of a nak
+ m.availableSince = m.redeliverAt;
+ }
+ dst.trace("fake: " + m.id + " " + why + " -> " + m.state);
+ }
+
+ int availableCount(long from) {
+ redeliver();
+ int count = 0;
+ for (Msg m : messages.values()) {
+ if (m.seq >= from && m.state == State.AVAILABLE && m.redeliverAt
<= dst.now()) {
+ count++;
+ }
+ }
+ return count;
+ }
+
+ boolean settled() {
+ redeliver();
+ return messages.values().stream().allMatch(this::settled);
+ }
+
+ /**
+ * Acknowledged, or given up after MaxDeliver; or skipped by a consumer
created again after a server restart, when
+ * its ack was lost: the client counts it as acknowledged and the new
consumer starts after it (two faults
+ * together).
+ */
+ private boolean settled(Msg m) {
+ if (m.state == State.ACKED || m.state == State.GIVEN_UP) {
+ return true;
+ }
+ return m.ackLost && subscriptions.stream().filter(s -> s.active &&
!s.deleted).allMatch(s -> s.startSeq > m.seq)
+ && subscriptions.stream().anyMatch(s -> s.active &&
!s.deleted);
+ }
+
+ String unsettled() {
+ return messages.values().stream().filter(m -> !settled(m))
+ .map(m -> m.id + "=" + m.state + "/d" +
m.deliveries).toList().toString();
+ }
+
+ long activeSubscriptions() {
+ return subscriptions.stream().filter(s -> s.active).count();
+ }
+
+ private Message message(Msg m, String delivery, JetStreamSubscription
subscription) {
+ NatsJetStreamMetaData meta = mock(NatsJetStreamMetaData.class);
+ when(meta.streamSequence()).thenReturn(m.seq);
+ when(meta.deliveredCount()).thenReturn((long) m.deliveries);
+ int number = m.deliveries;
+ return (Message)
Proxy.newProxyInstance(Message.class.getClassLoader(), new Class<?>[] {
Message.class },
+ (proxy, method, args) -> switch (method.getName()) {
+ case "toString" -> "FakeMessage[" + delivery + "]";
+ case "hashCode" -> System.identityHashCode(proxy);
+ case "equals" -> proxy == args[0];
+ case "getData" -> m.id.getBytes();
+ case "getSubject" -> TOPIC;
+ case "getReplyTo" -> "$JS.ACK." + STREAM + "." + delivery;
+ case "getSID" -> "1";
+ case "getSubscription" -> subscription;
+ case "getConnection" -> null;
+ case "isStatusMessage" -> false;
+ case "getStatus" -> null;
+ case "hasHeaders" -> false;
+ case "getHeaders" -> null;
+ case "isJetStream" -> true;
+ case "metaData" -> meta;
+ case "ack" -> {
+ ack(m, number, delivery);
+ yield null;
+ }
+ case "nak" -> {
+ nak(m, number, delivery, 0);
+ yield null;
+ }
+ case "nakWithDelay" -> {
+ nak(m, number, delivery, args[0] instanceof Duration d
? d.toMillis() : (Long) args[0]);
+ yield null;
+ }
+ default -> {
+ violations.add("message " + delivery + ": " +
method.getName() + " (not modelled)");
+ throw new
UnsupportedOperationException(method.getName());
+ }
+ });
+ }
+
+ /** [A] an ack of a delivery: fire-and-forget, so it may be lost; an ack
of an older delivery is accepted. */
+ private void ack(Msg m, int number, String delivery) {
+ onAck.accept(delivery);
+ if (faults && dst.chance(ackLossRate)) {
+ ledger.record("ackLost", "s", delivery);
+ m.ackLost = true;
+ return;
+ }
+ if (m.state == State.PENDING || m.state == State.AVAILABLE) {
+ m.state = State.ACKED;
+ ledger.record("acked", "s", delivery);
+ }
+ }
+
+ private void nak(Msg m, int number, String delivery, long delay) {
+ onNak.accept(delivery);
+ if (m.state == State.PENDING && m.deliveries == number) {
+ m.redeliverAt = dst.now() + delay;
+ giveUpOrRelease(m, "nak" + (delay > 0 ? " with a delay of " +
delay + " ms" : ""));
+ }
+ }
+
+ /** The connection, given to the consumer with the connection option
(shared: the consumer must not close it). */
+ Connection connection() {
+ ServerInfo serverInfo = mock(ServerInfo.class);
+ when(serverInfo.isJetStreamAvailable()).thenReturn(true);
+ StreamInfo streamInfo = mock(StreamInfo.class);
+ when(streamInfo.getCreateTime()).thenReturn(streamCreated);
+ JetStreamManagement jsm = (JetStreamManagement) Proxy.newProxyInstance(
+ JetStreamManagement.class.getClassLoader(), new Class<?>[] {
JetStreamManagement.class },
+ (proxy, method, args) -> switch (method.getName()) {
+ case "getStreamInfo" -> {
+ dst.block(dst.between(1, 30), "stream info");
+ yield streamInfo;
+ }
+ case "toString" -> "FakeJetStreamManagement";
+ default -> {
+ violations.add("JetStreamManagement." +
method.getName() + " (not modelled)");
+ throw new
UnsupportedOperationException(method.getName());
+ }
+ });
+ JetStream js = (JetStream)
Proxy.newProxyInstance(JetStream.class.getClassLoader(),
+ new Class<?>[] { JetStream.class },
+ (proxy, method, args) -> {
+ if ("subscribe".equals(method.getName()) && args.length ==
2
+ && args[1] instanceof PullSubscribeOptions
options) {
+ return subscribe((String) args[0], options);
+ }
+ if ("toString".equals(method.getName())) {
+ return "FakeJetStream";
+ }
+ violations.add("JetStream." + method.getName() + " (not
modelled)");
+ throw new UnsupportedOperationException(method.getName());
+ });
+ return (Connection)
Proxy.newProxyInstance(Connection.class.getClassLoader(), new Class<?>[] {
Connection.class },
+ (proxy, method, args) -> switch (method.getName()) {
+ case "toString" -> "FakeConnection";
+ case "hashCode" -> System.identityHashCode(proxy);
+ case "equals" -> proxy == args[0];
+ case "getServerInfo" -> serverInfo;
+ case "jetStreamManagement" -> jsm;
+ case "jetStream" -> js;
+ case "getStatus" -> Connection.Status.CONNECTED;
+ case "flush" -> {
+ flush((Duration) args[0]);
+ yield null;
+ }
+ case "close" -> {
+ violations.add("the consumer closed the shared
connection");
+ yield null;
+ }
+ default -> {
+ violations.add("Connection." + method.getName() + "
(not modelled)");
+ throw new
UnsupportedOperationException(method.getName());
+ }
+ });
+ }
+
+ /** [F] */
+ private void flush(Duration timeout) throws TimeoutException {
+ if (faults && dst.chance(flushFailRate)) {
+ ledger.record("flushFault", "c", "");
+ dst.block(timeout.toMillis(), "flush times out");
+ throw Dst.injected(new TimeoutException("dst: Timeout or no
response waiting for NATS server"));
+ }
+ dst.block(dst.between(1, 30), "flush");
+ }
+
+ private JetStreamSubscription subscribe(String topic, PullSubscribeOptions
options) {
+ if (!TOPIC.equals(topic)) {
+ violations.add("subscribe to " + topic);
+ }
+ dst.block(dst.between(1, 50), "subscribe");
+ ConsumerConfiguration cc = options.getConsumerConfiguration();
+ long start = cc != null && cc.getDeliverPolicy() ==
DeliverPolicy.ByStartSequence ? cc.getStartSequence() : 1;
+ Sub sub = new Sub("sub-" + subs++, start);
+ JetStreamSubscription proxy = (JetStreamSubscription)
Proxy.newProxyInstance(
+ JetStreamSubscription.class.getClassLoader(), new Class<?>[] {
JetStreamSubscription.class }, sub);
+ sub.proxySelf = proxy;
+ sub.durable = cc != null && cc.getDurable() != null;
+ // the consumer must pass the configured ack policy, AckWait and
MaxDeliver to the server
+ if (cc == null || cc.getAckPolicy() != AckPolicy.Explicit ||
cc.getAckWait() == null
+ || cc.getAckWait().toMillis() != ackWaitMillis
+ || (maxDeliver > 0 ? cc.getMaxDeliver() != maxDeliver :
cc.getMaxDeliver() > 0)) {
+ violations.add(sub.name + " subscribed with " + (cc == null
+ ? "no consumer configuration"
+ : "ackPolicy=" + cc.getAckPolicy() + " ackWait=" +
cc.getAckWait() + " maxDeliver=" + cc.getMaxDeliver()));
+ }
+ if (!sub.durable) {
+ // [C] a new ephemeral consumer has no delivery state of its own:
a message delivered to an earlier
+ // consumer and not acknowledged, or nak'd there with a delay, is
new to it. Simplified: the fake keeps one
+ // state per message for all consumers, so an acknowledged
message, or one given up after MaxDeliver, is
+ // not delivered again (the real server would deliver it again),
and the delivery count goes on (it names
+ // the deliveries, which must stay unique)
+ for (Msg m : messages.values()) {
+ if (m.seq >= start && (m.state == State.PENDING || m.state ==
State.AVAILABLE)) {
+ m.state = State.AVAILABLE;
+ m.redeliverAt = dst.now();
+ m.availableSince = dst.now();
+ }
+ }
+ }
+ subscriptions.add(sub);
+ dst.trace("fake: " + sub.name + " subscribed from stream sequence " +
start);
+ return proxy;
+ }
+}
diff --git
a/components/camel-nats/src/test/java/org/apache/camel/component/nats/dst/NatsConsumerDstTest.java
b/components/camel-nats/src/test/java/org/apache/camel/component/nats/dst/NatsConsumerDstTest.java
new file mode 100644
index 000000000000..a13c45da2055
--- /dev/null
+++
b/components/camel-nats/src/test/java/org/apache/camel/component/nats/dst/NatsConsumerDstTest.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.nats.dst;
+
+import org.apache.camel.test.dst.Dst;
+import org.apache.camel.test.dst.DstInstrument;
+import org.apache.camel.test.dst.DstTest;
+
+/**
+ * Deterministic simulation test of the camel-nats JetStream pull consumer
against a fake server, with failed fetches,
+ * flushes that time out when the route stops, lost acks, server restarts and
route stops while the consumer fetches: a
+ * message is acknowledged only after its exchange completed, a stopped
consumer leaves no subscription active, and once
+ * the faults cleared every message is acknowledged. A failing seed is
replayed with {@code -Ddst.seed=<seed>};
+ * {@code -Ddst.seeds=5000} runs more seeds.
+ */
+@DstInstrument("org.apache.camel.component.nats")
+class NatsConsumerDstTest {
+
+ @DstTest(seeds = 1000)
+ void aStoppedConsumerLeavesNoSubscriptionActive(Dst dst) throws Exception {
+ new NatsWorld(dst).run();
+ }
+}
diff --git
a/components/camel-nats/src/test/java/org/apache/camel/component/nats/dst/NatsWorld.java
b/components/camel-nats/src/test/java/org/apache/camel/component/nats/dst/NatsWorld.java
new file mode 100644
index 000000000000..9495ee6f75af
--- /dev/null
+++
b/components/camel-nats/src/test/java/org/apache/camel/component/nats/dst/NatsWorld.java
@@ -0,0 +1,331 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.nats.dst;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.ServiceStatus;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.nats.NatsConstants;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.test.dst.Dst;
+import org.apache.camel.test.dst.DstCamel;
+import org.apache.camel.test.dst.DstExecutor;
+import org.apache.camel.test.dst.DstLedger;
+import org.apache.camel.test.dst.DstWorld;
+import org.apache.camel.test.dst.Invariants;
+
+/**
+ * The world around a camel-nats JetStream pull consumer: a fake NATS server
with one stream ({@link FakeJetStream}) on
+ * a connection shared through the connection option, a publisher, a route
whose processing is sometimes slower than
+ * AckWait and sometimes fails, and an operator that stops and starts the
route (also while the consumer waits in a
+ * fetch) and restarts the server or deletes the ephemeral consumers while
connected (a waiting fetch gets 409 Server
+ * Shutdown or 409 Consumer Deleted). Faults: a fetch ends empty at its
timeout while the connection is down, the flush
+ * of the stop times out, acks are lost.
+ * <p>
+ * The seed picks pullBatchSize, pullFetchTimeout, ackWait, maxDeliver,
nackWait, a durable or an ephemeral consumer,
+ * and the fault rates.
+ * <p>
+ * The consumer's fetch loop runs for ever, so it is the one endless task of
the run. The operator stops the route only
+ * when no exchange is in the route: a stop nested above an exchange that
blocks would wait for it for ever on the one
+ * stack. The stop of a consumer that waits in a fetch ends that fetch (its
subscription is unsubscribed), and the loop
+ * ends as soon as the stop returned.
+ * <p>
+ * Properties: a message is acknowledged only after its exchange completed,
and nak'd only after it failed; a stopped
+ * consumer leaves no subscription active on the shared connection, and does
not close it; once the faults cleared every
+ * message is acknowledged, or given up by the server after MaxDeliver (or
skipped by a consumer created again after a
+ * server restart, when the ack of the message was lost), and a message
published then is acknowledged (the fetch loop
+ * still runs); the consumer uses the connection as documented. Messages may
be delivered more than once (AckWait, lost
+ * acks, a consumer created again after a server restart).
+ * <p>
+ * Not explored: push subscriptions and core NATS subjects (their dispatchers
deliver on threads of jnats), manualAck,
+ * AckAll and AckNone, a connection created by the consumer itself; a stop
between two messages of a fetched batch (the
+ * operator stops the route only while no exchange is in it); the interrupt of
the consumer thread by the stop (the
+ * harness interrupts nothing), so only the flush and subscribe parts of
CAMEL-26884 are checked; a start that comes
+ * right after a stop, while the stopped consumer task still subscribes (the
operator starts the route again after 100
+ * ms or more).
+ */
+@DstWorld(component = "nats",
+ fake = "FakeJetStream: a server with one JetStream stream and its
pull consumers (AckWait, MaxDeliver, Nak),"
+ + " behind a jnats Connection given with the connection
option",
+ covers = {
+ "the JetStream pull consumer with explicit acks and naks,
ephemeral or durable",
+ "AckWait and MaxDeliver",
+ "route stop and start, also while the consumer waits in a
fetch",
+ "a server restart that deletes the ephemeral consumer, and
the subscription made again" },
+ faults = {
+ "a fetch ends empty while the connection is down", "the
flush of the stop times out", "an ack is lost",
+ "the server restarts (409 Server Shutdown) or the consumer
is deleted (409 Consumer Deleted)",
+ "processing fails", "processing is slower than AckWait" },
+ invariants = {
+ "the consumer uses the connection as documented",
+ "a message is acknowledged only after its exchange
completed",
+ "a message is nak'd only after its exchange failed",
+ "a stopped consumer leaves no subscription active",
+ "every message is acknowledged once the faults cleared, or
given up after MaxDeliver (or skipped by a"
+ + "
consumer made again after its ack was lost)" },
+ issues = { "CAMEL-26884" })
+final class NatsWorld {
+
+ static final String ROUTE = "nats";
+
+ private final Dst dst;
+ private final DstLedger ledger;
+ private final FakeJetStream nats;
+ private final int batch;
+ private final long fetchTimeout;
+ private final long nackWait;
+ private final boolean durable;
+ private final double processFailRate;
+ private final List<String> violations = new ArrayList<>();
+ private final List<String> wrongAcks = new ArrayList<>();
+ private final List<String> leftActive = new ArrayList<>();
+ private final Set<String> completed = new HashSet<>();
+ private final Set<String> failed = new HashSet<>();
+ private boolean faults = true;
+ private int sendsLeft;
+ private int opsLeft;
+ private int inRoute;
+ private boolean finished;
+ private String finalStatus;
+ private boolean finalSettled;
+ /** Published once the faults cleared: it must end acknowledged, so the
fetch loop still runs. */
+ private FakeJetStream.Msg end;
+
+ NatsWorld(Dst dst) {
+ this.dst = dst;
+ this.ledger = new DstLedger(dst);
+ this.nats = new FakeJetStream(dst, ledger);
+ this.batch = dst.pick(List.of(1, 10));
+ this.fetchTimeout = dst.pick(List.of(100L, 1000L, 5000L));
+ this.nackWait = dst.pick(List.of(0L, 1000L));
+ this.durable = dst.chance(0.3);
+ nats.ackWaitMillis = dst.pick(List.of(5000L, 30_000L));
+ nats.maxDeliver = dst.pick(List.of(-1, 3));
+ nats.fetchFailRate = dst.pick(List.of(0.0, 0.05));
+ nats.ackLossRate = dst.pick(List.of(0.0, 0.05));
+ nats.flushFailRate = dst.pick(List.of(0.0, 0.5));
+ this.processFailRate = dst.pick(List.of(0.0, 0.05, 0.2));
+ nats.onAck = d -> check(d, completed, "acknowledged");
+ nats.onNak = d -> check(d, failed, "nak'd");
+ dst.trace("world: " + config());
+ }
+
+ private String config() {
+ return "pullBatchSize=" + batch + " pullFetchTimeout=" + fetchTimeout
+ " ackWait=" + nats.ackWaitMillis
+ + " maxDeliver=" + nats.maxDeliver + " nackWait=" + nackWait +
" durable=" + durable + " fetchFail="
+ + nats.fetchFailRate + " ackLoss=" + nats.ackLossRate + "
flushFail=" + nats.flushFailRate
+ + " processFail=" + processFailRate;
+ }
+
+ void run() throws Exception {
+ DefaultCamelContext context = DstCamel.newContext(dst);
+ context.getRegistry().bind("nats", nats.connection());
+ String uri = "nats:" + FakeJetStream.TOPIC +
"?connection=#nats&jetstreamEnabled=true&jetstreamName="
+ + FakeJetStream.STREAM +
"&pullSubscription=true&ackPolicy=Explicit&ackWait=" + nats.ackWaitMillis
+ + (nats.maxDeliver > 0 ? "&maxDeliver=" + nats.maxDeliver
: "") + "&nackWait=" + nackWait
+ + "&pullBatchSize=" + batch + "&pullFetchTimeout=" +
fetchTimeout + "&flushConnection=true"
+ + "&flushTimeout=1000" + (durable ? "&durableName=dst" :
"");
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+ from(uri).routeId(ROUTE).autoStartup(false)
+ .process(exchange -> process(exchange));
+ }
+ });
+ context.start();
+ DstExecutor operator = dst.executor("operator", 1);
+ try {
+ operator.schedule("start route", 0, () -> startRoute(context));
+ planSends();
+ planOperations(context, operator);
+ operator.schedule("finisher", 1000, () -> finisher(context,
operator, -1));
+ dst.runUntil(() -> finished, "the finisher");
+ // the fetch loop ends after the stop returned
+ dst.runFor(10_000);
+ if (nats.activeSubscriptions() > 0) {
+ leftActive.add(nats.activeSubscriptions() + " active after the
final stop");
+ }
+ dst.check(
+ Invariants.of("the consumer uses the connection as
documented",
+ () -> nats.violations.isEmpty() &&
violations.isEmpty()
+ ? null : nats.violations + " " +
violations + " (" + config() + ")"),
+ Invariants.of("a message is acknowledged only after its
exchange completed",
+ () -> wrongAcks.stream().noneMatch(s ->
s.contains("acknowledged"))
+ ? null : wrongAcks + " (" + config() +
")"),
+ Invariants.of("a message is nak'd only after its exchange
failed",
+ () -> wrongAcks.stream().noneMatch(s ->
s.contains("nak'd"))
+ ? null : wrongAcks + " (" + config() +
")"),
+ Invariants.of("a stopped consumer leaves no subscription
active",
+ () -> leftActive.isEmpty() ? null : leftActive + "
(" + config() + ")"),
+ Invariants.of("every message is acknowledged once the
faults cleared, or given up after MaxDeliver (or"
+ + " skipped by a consumer made again after
its ack was lost)",
+ () -> finalSettled && "Started".equals(finalStatus)
+ ? null
+ : "route " + finalStatus + ", unsettled "
+ nats.unsettled() + ", end message "
+ + (end == null ? "not published" :
end.state) + " (" + config() + ")"));
+ } finally {
+ dst.dropPending();
+ context.stop();
+ }
+ }
+
+ private void check(String delivery, Set<String> allowed, String what) {
+ if (!allowed.contains(delivery)) {
+ wrongAcks.add(delivery + " " + what + " at t=" + dst.now());
+ }
+ }
+
+ private void process(Exchange exchange) {
+ String id = exchange.getMessage().getBody(String.class);
+ Long counter =
exchange.getMessage().getHeader(NatsConstants.NATS_DELIVERY_COUNTER,
Long.class);
+ String delivery = id + "#" + counter;
+ inRoute++;
+ try {
+ ledger.record("routed", "s", delivery);
+ if (faults && dst.chance(0.1)) {
+ dst.block(dst.between(nats.ackWaitMillis / 2, 2 *
nats.ackWaitMillis), "slow processing of " + delivery);
+ } else {
+ dst.block(dst.between(0, 200), "processing of " + delivery);
+ }
+ if (faults && dst.chance(processFailRate)) {
+ failed.add(delivery);
+ ledger.record("failed", "s", delivery);
+ throw Dst.injected(new IllegalStateException("dst: processing
failed for " + delivery));
+ }
+ completed.add(delivery);
+ ledger.record("completed", "s", delivery);
+ } finally {
+ inRoute--;
+ }
+ }
+
+ private void finisher(DefaultCamelContext context, DstExecutor operator,
long deadline) {
+ if (deadline == -1 && (sendsLeft > 0 || opsLeft > 0)) {
+ operator.schedule("finisher", 500, () -> finisher(context,
operator, -1));
+ return;
+ }
+ if (deadline == -1) {
+ long slow = dst.between(0, 10_000);
+ dst.trace("world: operations done, faults for another " + slow + "
ms");
+ operator.schedule("finisher", slow, () -> finisher(context,
operator, -2));
+ return;
+ }
+ if (deadline == -2) {
+ faults = false;
+ nats.faults = false;
+ end = nats.publish("end");
+ dst.trace("world: faults cleared, end message published");
+ long d = dst.now() + 30 * 60_000L;
+ operator.schedule("finisher", 500, () -> finisher(context,
operator, d));
+ return;
+ }
+ ServiceStatus status =
context.getRouteController().getRouteStatus(ROUTE);
+ boolean settled = nats.settled() && end.state ==
FakeJetStream.State.ACKED;
+ if ((inRoute > 0 || !settled || status != ServiceStatus.Started) &&
dst.now() < deadline) {
+ operator.schedule("finisher", 500, () -> finisher(context,
operator, deadline));
+ return;
+ }
+ finalStatus = String.valueOf(status);
+ finalSettled = settled;
+ if (inRoute > 0) {
+ violations.add("an exchange is still in the route at the
deadline");
+ } else {
+ try {
+ context.getRouteController().stopRoute(ROUTE);
+ } catch (Exception e) {
+ violations.add("final stop failed: " + e);
+ }
+ }
+ finished = true;
+ }
+
+ private void startRoute(DefaultCamelContext context) {
+ if (nats.activeSubscriptions() > 0) {
+ leftActive.add(nats.activeSubscriptions() + " active when the
route starts again at t=" + dst.now());
+ }
+ try {
+ context.getRouteController().startRoute(ROUTE);
+ ledger.record("routeStarted", "r", ROUTE);
+ } catch (Exception e) {
+ violations.add("start failed: " + e);
+ }
+ }
+
+ private void planSends() {
+ DstExecutor publisher = dst.executor("publisher", 1);
+ long t = dst.between(0, 500);
+ int n = 0;
+ int bursts = (int) dst.between(1, 4);
+ for (int b = 0; b < bursts; b++) {
+ int size = (int) dst.between(1, 15);
+ for (int i = 0; i < size; i++) {
+ String id = "m" + n++;
+ sendsLeft++;
+ publisher.schedule("publish " + id, t, () -> {
+ nats.publish(id);
+ sendsLeft--;
+ });
+ t += dst.between(0, 100);
+ }
+ t += dst.between(0, 20_000);
+ }
+ }
+
+ private void planOperations(DefaultCamelContext context, DstExecutor
operator) {
+ int ops = (int) dst.between(0, 3);
+ long t = dst.between(500, 3000);
+ for (int i = 0; i < ops; i++) {
+ opsLeft++;
+ if (dst.chance(0.7)) {
+ operator.schedule("stop route", t, () -> stopWhenIdle(context,
operator));
+ } else {
+ operator.schedule("server restart", t, () -> {
+ nats.restartServer();
+ opsLeft--;
+ });
+ }
+ t += dst.between(1000, 20_000);
+ }
+ }
+
+ /** Stops the route once no exchange is in it, then starts it again after
a while. */
+ private void stopWhenIdle(DefaultCamelContext context, DstExecutor
operator) {
+ if (context.getRouteController().getRouteStatus(ROUTE) !=
ServiceStatus.Started) {
+ opsLeft--;
+ return;
+ }
+ if (inRoute > 0) {
+ operator.schedule("stop route (an exchange is in the route)", 50,
() -> stopWhenIdle(context, operator));
+ return;
+ }
+ try {
+ context.getRouteController().stopRoute(ROUTE);
+ ledger.record("routeStopped", "r", ROUTE);
+ } catch (Exception e) {
+ violations.add("stop failed: " + e);
+ }
+ operator.schedule("start route", dst.between(100, 5000), () -> {
+ startRoute(context);
+ opsLeft--;
+ });
+ }
+}
diff --git a/components/camel-nats/src/test/resources/log4j2.properties
b/components/camel-nats/src/test/resources/log4j2.properties
index 87a2a3895027..4a59d9f2cc3d 100644
--- a/components/camel-nats/src/test/resources/log4j2.properties
+++ b/components/camel-nats/src/test/resources/log4j2.properties
@@ -15,9 +15,16 @@
## limitations under the License.
## ---------------------------------------------------------------------------
-appender.file.type = File
+# capped: the DST test runs the consumer for a thousand seeds
+appender.file.type = RollingFile
appender.file.name = file
appender.file.fileName = target/camel-nats-test.log
+appender.file.filePattern = target/camel-nats-test.%i.log
+appender.file.policies.type = Policies
+appender.file.policies.size.type = SizeBasedTriggeringPolicy
+appender.file.policies.size.size = 50MB
+appender.file.strategy.type = DefaultRolloverStrategy
+appender.file.strategy.max = 1
appender.file.layout.type = PatternLayout
appender.file.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n
appender.out.type = Console
diff --git a/components/camel-test/camel-test-dst/src/main/docs/test-dst.adoc
b/components/camel-test/camel-test-dst/src/main/docs/test-dst.adoc
index ffdc45e6d8a6..cd063a0bd418 100644
--- a/components/camel-test/camel-test-dst/src/main/docs/test-dst.adoc
+++ b/components/camel-test/camel-test-dst/src/main/docs/test-dst.adoc
@@ -103,6 +103,15 @@ a|* explicit acknowledgement with onFailure RELEASE or
REJECT
* suspend and resume of the CamelContext
|CAMEL-25872, CAMEL-25910
+|camel-nats
+|`NatsWorld` (`NatsConsumerDstTest`)
+|FakeJetStream: a server with one JetStream stream and its pull consumers
(AckWait, MaxDeliver, Nak), behind a jnats Connection given with the connection
option
+a|* the JetStream pull consumer with explicit acks and naks, ephemeral or
durable
+* AckWait and MaxDeliver
+* route stop and start, also while the consumer waits in a fetch
+* a server restart that deletes the ephemeral consumer, and the subscription
made again
+|CAMEL-26884
+
|camel-pulsar
|`PulsarWorld` (`PulsarConsumerDstTest`)
|FakePulsar: a broker and client for one topic and one durable subscription