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 9762655b05ad CAMEL-26973: camel-mongodb - a DST fake world for the 
change streams consumer (#30135)
9762655b05ad is described below

commit 9762655b05ad0af853c60bc526de61033bc0f4f8
Author: allthingssecurity <[email protected]>
AuthorDate: Tue Nov 17 20:38:20 2026 +0530

    CAMEL-26973: camel-mongodb - a DST fake world for the change streams 
consumer (#30135)
    
    Adds a camel-test-dst world for the camel-mongodb change streams consumer,
    in the dst package of its tests: MongoDbChangeStreamsDstTest (1000 seeds),
    MongoDbChangeStreamsWorld and FakeMongo. The real consumer runs against a
    fake MongoDB collection and its change stream, behind the MongoClient of
    the connectionBean option, written from the MongoDB manual and the Java
    driver: events in order with their resume tokens, resumeAfter and
    startAfter (resumeAfter of an invalidate token fails with code 260), a
    dropped collection (drop and invalidate events, then the change stream
    ends) made again by an insert, a server that cannot be reached for a while
    (a cursor cannot be opened, a getMore fails, and the driver resumes the
    change stream until server selection times out), and fullDocument. The
    resume token is stored in a durable changeStreamTokenRepository.
    
    The seed picks cursorRegenerationDelay, fullDocument, the server selection
    timeout and the fault rates, and injects collection drops, unreachable
    periods, failed getMores, failed and slow processing, and route restarts.
    
    Invariants: the route starts once the collection exists and the server
    can be reached; the consumer thread runs as long as the route is started; a
    resume token is stored only after its exchange completed; a stored token
    never passes an event that was not routed; once the faults cleared every
    event after the first change stream is routed; the consumer uses the
    client as documented.
    
    The consumer's stop waits for its thread, which the one stack of the
    harness cannot run, so a route restart interrupts the consumer thread at
    its next call of the change stream (as a forced shutdown does) and then
    stops the route. Not explored: the graceful stop of the consumer, tailable
    cursors, streamFilter and the ResumeStrategy. A cursor that the consumer
    thread opens again before any exchange completed starts at the current
    time; that case is looked at separately and counted with
    -Ddst.mongodb.strictFirstCursor=true.
    
    Self-check: the world fails on the code before CAMEL-25946 and before
    CAMEL-25679; 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]>
---
 .../apache/camel/catalog/components/mongodb.json   |   3 +
 .../org/apache/camel/catalog/docs/test-dst.adoc    |  19 +-
 components/camel-mongodb/pom.xml                   |  26 ++
 .../apache/camel/component/mongodb/mongodb.json    |   3 +
 .../camel/component/mongodb/dst/FakeMongo.java     | 489 +++++++++++++++++++++
 .../mongodb/dst/MongoDbChangeStreamsDstTest.java   |  36 ++
 .../mongodb/dst/MongoDbChangeStreamsWorld.java     | 471 ++++++++++++++++++++
 .../src/test/resources/log4j2.properties           |   9 +-
 .../camel-test-dst/src/main/docs/test-dst.adoc     |  19 +-
 9 files changed, 1070 insertions(+), 5 deletions(-)

diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/mongodb.json
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/mongodb.json
index b3d24ab7922e..d25475fb600d 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/mongodb.json
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/mongodb.json
@@ -123,5 +123,8 @@
     "tls": { "index": 56, "kind": "parameter", "displayName": "Tls", "group": 
"security", "label": "security", "required": false, "type": "boolean", 
"javaType": "boolean", "deprecated": false, "autowired": false, "secret": 
false, "defaultValue": false, "description": "Specifies that all communication 
with MongoDB instances should use TLS. Supersedes the ssl option. Default: 
false" },
     "tlsAllowInvalidHostnames": { "index": 57, "kind": "parameter", 
"displayName": "Tls Allow Invalid Hostnames", "group": "security", "label": 
"security", "required": false, "type": "boolean", "javaType": "boolean", 
"deprecated": false, "autowired": false, "secret": false, "security": 
"insecure:ssl", "defaultValue": false, "description": "Specifies that the 
driver should allow invalid hostnames in the certificate for TLS connections. 
Supersedes sslInvalidHostNameAllowed. Has the same ef [...]
     "username": { "index": 58, "kind": "parameter", "displayName": "Username", 
"group": "security", "label": "security", "required": false, "type": "string", 
"javaType": "java.lang.String", "deprecated": false, "autowired": false, 
"secret": true, "security": "secret", "description": "Username for mongodb 
connection" }
+  },
+  "dstWorlds": {
+    "MongoDbChangeStreamsWorld": { "javaType": 
"org.apache.camel.component.mongodb.dst.MongoDbChangeStreamsWorld", "fake": 
"FakeMongo: a MongoDB collection and its change stream (resumeAfter, 
startAfter, drop and invalidate events, the resume of the driver), behind the 
MongoClient of the connectionBean option", "covers": [ "the change streams 
consumer with a store of its resume token (changeStreamTokenRepository)", "a 
dropped collection, made again by an insert", "a server that cannot be [...]
   }
 }
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 a78c6d18b373..e3ee21b12f26 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
@@ -135,6 +135,16 @@ a|* explicit acknowledgement with onFailure RELEASE or 
REJECT
 * suspend and resume of the CamelContext
 |CAMEL-25872, CAMEL-25910
 
+|camel-mongodb
+|`MongoDbChangeStreamsWorld` (`MongoDbChangeStreamsDstTest`)
+|FakeMongo: a MongoDB collection and its change stream (resumeAfter, 
startAfter, drop and invalidate events, the resume of the driver), behind the 
MongoClient of the connectionBean option
+a|* the change streams consumer with a store of its resume token 
(changeStreamTokenRepository)
+* a dropped collection, made again by an insert
+* a server that cannot be reached for a while: the cursor cannot be opened, a 
getMore fails
+* route restarts (the consumer thread interrupted, then the route stopped and 
started), resuming after the stored token
+* the fullDocument option and cursorRegenerationDelay
+|CAMEL-25679, CAMEL-25946, CAMEL-26992
+
 |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
@@ -615,14 +625,15 @@ EH=components/camel-azure/camel-azure-eventhubs
 SLACK=components/camel-slack
 SHARE=components/camel-kafka-share
 PULSAR=components/camel-pulsar
+MONGODB=components/camel-mongodb
 MVN="./mvnw -B -Papache-snapshots test -Dsurefire.failIfNoSpecifiedTests=false 
-Dsurefire.rerunFailingTestsCount=0"
 
 # the harness self-tests
 $MVN -pl $DST -Dtest='Dst*Test'
 
 # the worlds, 1000 seeds each, then 5000 seeds without stopping at the first 
failing one
-$MVN -pl $DST,$SQS,$KINESIS,$EH,$SLACK,$SHARE,$PULSAR -Dtest='*DstTest'
-$MVN -pl $DST,$SQS,$KINESIS,$EH,$SLACK,$SHARE,$PULSAR -Dtest='*DstTest' 
-Ddst.seeds=5000 -Ddst.maxFailures=100000
+$MVN -pl $DST,$SQS,$KINESIS,$EH,$SLACK,$SHARE,$PULSAR,$MONGODB 
-Dtest='*DstTest'
+$MVN -pl $DST,$SQS,$KINESIS,$EH,$SLACK,$SHARE,$PULSAR,$MONGODB 
-Dtest='*DstTest' -Ddst.seeds=5000 -Ddst.maxFailures=100000
 
 # the self-check: revert the main code of a past fix locally (do not commit), 
run the world, restore
 git show d09593b4a584 -- $SQS/src/main | git apply -R          # CAMEL-25700
@@ -636,6 +647,10 @@ $MVN -pl $DST,$SQS -Dtest=SqsVisibilityDstTest -Ddst.seed=8
 diff /tmp/seed-8.first $SQS/target/dst/SqsVisibilityDstTest.*/seed-8.trace && 
echo identical
 ----
 
+For camel-mongodb (`MongoDbChangeStreamsDstTest`) the past fixes are the main 
code of 5f10c225652e (CAMEL-25946) and of
+3af2c90d63ad (CAMEL-25679), reverted by hand. Its world also found CAMEL-26992 
(fixed by 1eb59175e600): a cursor that
+the consumer opened again before any exchange completed started at the current 
time and skipped the events in between.
+
 The past fixes of the other worlds are 83aaa80754f9 (CAMEL-25683, Event Hubs), 
68329f0919bc (CAMEL-25684, Slack), the
 main code of b62e90ddb2d9 (CAMEL-25869, Pulsar), the main code of 20c94435789e 
(CAMEL-25872, Kafka share, whose world
 was also checked with planted faults), and the main code of 043ac9ec84ab 
(CAMEL-25569) and of 847ee9affb4c
diff --git a/components/camel-mongodb/pom.xml b/components/camel-mongodb/pom.xml
index a9c840fd12fa..5bf02f2c819e 100644
--- a/components/camel-mongodb/pom.xml
+++ b/components/camel-mongodb/pom.xml
@@ -34,6 +34,8 @@
     <properties>
         <!-- MongoDB 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>
@@ -113,5 +115,29 @@
             <artifactId>commons-lang3</artifactId>
             <scope>test</scope>
         </dependency>
+        <!-- deterministic simulation test of the change streams consumer (dst 
package) -->
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-test-dst</artifactId>
+            <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-mongodb/src/generated/resources/META-INF/org/apache/camel/component/mongodb/mongodb.json
 
b/components/camel-mongodb/src/generated/resources/META-INF/org/apache/camel/component/mongodb/mongodb.json
index b3d24ab7922e..d25475fb600d 100644
--- 
a/components/camel-mongodb/src/generated/resources/META-INF/org/apache/camel/component/mongodb/mongodb.json
+++ 
b/components/camel-mongodb/src/generated/resources/META-INF/org/apache/camel/component/mongodb/mongodb.json
@@ -123,5 +123,8 @@
     "tls": { "index": 56, "kind": "parameter", "displayName": "Tls", "group": 
"security", "label": "security", "required": false, "type": "boolean", 
"javaType": "boolean", "deprecated": false, "autowired": false, "secret": 
false, "defaultValue": false, "description": "Specifies that all communication 
with MongoDB instances should use TLS. Supersedes the ssl option. Default: 
false" },
     "tlsAllowInvalidHostnames": { "index": 57, "kind": "parameter", 
"displayName": "Tls Allow Invalid Hostnames", "group": "security", "label": 
"security", "required": false, "type": "boolean", "javaType": "boolean", 
"deprecated": false, "autowired": false, "secret": false, "security": 
"insecure:ssl", "defaultValue": false, "description": "Specifies that the 
driver should allow invalid hostnames in the certificate for TLS connections. 
Supersedes sslInvalidHostNameAllowed. Has the same ef [...]
     "username": { "index": 58, "kind": "parameter", "displayName": "Username", 
"group": "security", "label": "security", "required": false, "type": "string", 
"javaType": "java.lang.String", "deprecated": false, "autowired": false, 
"secret": true, "security": "secret", "description": "Username for mongodb 
connection" }
+  },
+  "dstWorlds": {
+    "MongoDbChangeStreamsWorld": { "javaType": 
"org.apache.camel.component.mongodb.dst.MongoDbChangeStreamsWorld", "fake": 
"FakeMongo: a MongoDB collection and its change stream (resumeAfter, 
startAfter, drop and invalidate events, the resume of the driver), behind the 
MongoClient of the connectionBean option", "covers": [ "the change streams 
consumer with a store of its resume token (changeStreamTokenRepository)", "a 
dropped collection, made again by an insert", "a server that cannot be [...]
   }
 }
diff --git 
a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/dst/FakeMongo.java
 
b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/dst/FakeMongo.java
new file mode 100644
index 000000000000..38f4885bfddd
--- /dev/null
+++ 
b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/dst/FakeMongo.java
@@ -0,0 +1,489 @@
+/*
+ * 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.mongodb.dst;
+
+import java.lang.reflect.Proxy;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.function.Consumer;
+
+import com.mongodb.MongoCommandException;
+import com.mongodb.MongoInterruptedException;
+import com.mongodb.MongoSocketReadException;
+import com.mongodb.MongoTimeoutException;
+import com.mongodb.ReadPreference;
+import com.mongodb.ServerAddress;
+import com.mongodb.client.ChangeStreamIterable;
+import com.mongodb.client.ListCollectionNamesIterable;
+import com.mongodb.client.MongoChangeStreamCursor;
+import com.mongodb.client.MongoClient;
+import com.mongodb.client.MongoCollection;
+import com.mongodb.client.MongoDatabase;
+import com.mongodb.client.model.changestream.ChangeStreamDocument;
+import com.mongodb.client.model.changestream.FullDocument;
+import com.mongodb.client.model.changestream.OperationType;
+import org.apache.camel.test.dst.Dst;
+import org.bson.BsonDocument;
+import org.bson.BsonInt32;
+import org.bson.BsonString;
+import org.bson.BsonTimestamp;
+import org.bson.Document;
+
+/**
+ * A fake MongoDB deployment (a replica set seen as one server) with one 
database, one collection and its change stream,
+ * behind the MongoClient of the connectionBean option. Written from the 
MongoDB manual (8.0) and the Java driver
+ * documentation (5.x); the rules it follows are quoted where they are 
implemented and marked:
+ * <ul>
+ * <li>[M] MongoDB manual, "Change Streams", "Resume a Change Stream", "Change 
Events" (drop, invalidate);</li>
+ * <li>[D] the Java driver: ChangeStreamIterable and MongoChangeStreamCursor 
Javadoc, and the resume of a change stream
+ * on a resumable error (ChangeStreamBatchCursor of driver 5.13, read with 
javap);</li>
+ * <li>[A] an assumption, where the documents leave the behaviour open; the 
seed picks between the allowed choices.</li>
+ * </ul>
+ * <p>
+ * Modelled: the events of the collection in order (insert, update, replace, 
delete) with their resume tokens; a change
+ * stream opened at the current time, after a resume token (resumeAfter) or 
after an invalidate event (startAfter); a
+ * dropped collection, whose open change streams get a drop and an invalidate 
event and are then closed, and which an
+ * insert makes again; the server out of reach for a while (a change stream 
cannot be opened, a getMore fails, and the
+ * driver resumes the change stream itself on that resumable error until 
server selection times out); the fullDocument
+ * option (the document of an update is only there with updateLookup).
+ * <p>
+ * Not modelled: the oplog window (a resume token that fell out of the oplog, 
ChangeStreamHistoryLost), sharded
+ * clusters, pipelines (streamFilter), pre-images, renames and dropDatabase 
(they end the change stream as a drop does),
+ * transactions, batching (one event per getMore), and the post batch resume 
token of an empty getMore (the driver then
+ * resumes from the last event it returned, which is the same position here, 
as events are never filtered).
+ * <p>
+ * Timing (the one-stack rule of the harness): a getMore waits up to its 
maxAwaitTime for an event and takes a round
+ * trip; writes of the other clients take effect at once and never block.
+ */
+final class FakeMongo {
+
+    static final String DATABASE = "dst";
+    static final String COLLECTION = "orders";
+    /** [D] MongoClientSettings: serverSelectionTimeout defaults to 30 
seconds. */
+    long serverSelectionTimeout = 30_000;
+
+    private final Dst dst;
+    final List<String> violations = new ArrayList<>();
+    boolean faults = true;
+    double getMoreFailRate;
+    double slowRate;
+
+    private final List<Event> oplog = new ArrayList<>();
+    private boolean collectionExists = true;
+    private long unreachableUntil;
+    private final MongoClient client;
+    private final List<FakeCursor> cursors = new ArrayList<>();
+    /** The consumer thread is interrupted at its next call of a cursor (see 
the world). */
+    boolean interruptRequested;
+    Consumer<String> onOpen = s -> {
+    };
+    Runnable onInterrupt = () -> {
+    };
+
+    /** An entry of the change stream of the collection. seq is 1-based (the 
index in the oplog + 1). */
+    record Event(long seq, OperationType type, String id) {
+        BsonDocument token() {
+            return new BsonDocument("_data", new 
BsonString(String.format("%016x", seq)));
+        }
+    }
+
+    FakeMongo(Dst dst) {
+        this.dst = dst;
+        this.client = proxy(MongoClient.class, (m, args) -> switch (m) {
+            case "getDatabase" -> database((String) args[0]);
+            case "getClusterDescription" -> null;
+            default -> notModelled("MongoClient." + m);
+        });
+    }
+
+    MongoClient client() {
+        return client;
+    }
+
+    static long seqOf(String tokenJson) {
+        return 
Long.parseLong(BsonDocument.parse(tokenJson).getString("_data").getValue(), 16);
+    }
+
+    // 
------------------------------------------------------------------------------------------------------------
+    // the other clients and the operator
+
+    /** Another client writes: [M] an insert makes the collection when it does 
not exist. */
+    Event write(OperationType type, String id) {
+        if (!collectionExists) {
+            if (type != OperationType.INSERT) {
+                return null;
+            }
+            collectionExists = true;
+            dst.trace("mongo: collection made again by an insert");
+        }
+        return append(type, id);
+    }
+
+    /**
+     * The collection is dropped: [M] "drop: Occurs when a collection is 
dropped"; "invalidate: ... occur after a drop
+     * ... closes the change stream cursor".
+     */
+    void drop() {
+        if (!collectionExists) {
+            return;
+        }
+        collectionExists = false;
+        append(OperationType.DROP, null);
+        append(OperationType.INVALIDATE, null);
+        dst.trace("mongo: collection dropped");
+    }
+
+    /** The server cannot be reached by the consumer for {@code millis}. */
+    void unreachable(long millis) {
+        unreachableUntil = Math.max(unreachableUntil, dst.now() + millis);
+        dst.trace("mongo: unreachable until t=" + unreachableUntil);
+    }
+
+    boolean collectionExists() {
+        return collectionExists;
+    }
+
+    boolean isReachable() {
+        return dst.now() >= unreachableUntil;
+    }
+
+    List<Event> oplog() {
+        return oplog;
+    }
+
+    private Event append(OperationType type, String id) {
+        Event e = new Event(oplog.size() + 1, type, id);
+        oplog.add(e);
+        return e;
+    }
+
+    // 
------------------------------------------------------------------------------------------------------------
+    // the client
+
+    private MongoDatabase database(String name) {
+        if (!DATABASE.equals(name)) {
+            violations.add("unexpected database " + name);
+        }
+        return proxy(MongoDatabase.class, (m, args) -> switch (m) {
+            case "getName" -> DATABASE;
+            case "getCollection" -> collection((String) args[0]);
+            case "listCollectionNames" -> collectionNames();
+            case "runCommand" -> runCommand(args);
+            default -> notModelled("MongoDatabase." + m);
+        });
+    }
+
+    /**
+     * [M] ping: the reply of a replica set carries operationTime, the 
timestamp of the newest oplog entry for read
+     * concern local (runCommand reference). The event of seq s has the 
operation time (1, s).
+     */
+    private BsonDocument runCommand(Object[] args) {
+        if (args.length != 3 || !(args[0] instanceof BsonDocument cmd) || 
!cmd.containsKey("ping")
+                || !ReadPreference.primary().equals(args[1]) || args[2] != 
BsonDocument.class) {
+            violations.add("not modelled: runCommand " + args[0]);
+            throw new UnsupportedOperationException("dst: not modelled: 
runCommand");
+        }
+        checkInterrupt();
+        selectServer("ping");
+        roundTrip("ping");
+        return new BsonDocument("ok", new 
BsonInt32(1)).append("operationTime", new BsonTimestamp(1, oplog.size()));
+    }
+
+    private ListCollectionNamesIterable collectionNames() {
+        selectServer("listCollectionNames");
+        roundTrip("listCollectionNames");
+        List<String> names = collectionExists ? List.of(COLLECTION) : 
List.of();
+        return proxy(ListCollectionNamesIterable.class, (m, args) -> switch 
(m) {
+            case "iterator" -> names.iterator();
+            case "spliterator" -> names.spliterator();
+            case "forEach" -> {
+                names.forEach((Consumer<? super String>) args[0]);
+                yield null;
+            }
+            default -> notModelled("ListCollectionNamesIterable." + m);
+        });
+    }
+
+    private MongoCollection<Document> collection(String name) {
+        if (!COLLECTION.equals(name)) {
+            violations.add("unexpected collection " + name);
+        }
+        return proxy(MongoCollection.class, (m, args) -> switch (m) {
+            case "watch" -> {
+                if (args != null && args.length > 0 && !(args[0] instanceof 
List)) {
+                    yield notModelled("watch(" + 
args[0].getClass().getSimpleName() + ")");
+                }
+                if (args != null && args.length > 0 && !((List<?>) 
args[0]).isEmpty()) {
+                    yield notModelled("watch with a pipeline");
+                }
+                yield new Watch().proxy();
+            }
+            case "getNamespace" -> null;
+            case "getReadPreference" -> ReadPreference.primary();
+            default -> notModelled("MongoCollection." + m);
+        });
+    }
+
+    /** The options of a change stream, up to the iterator that opens it. */
+    private final class Watch {
+        BsonDocument resumeAfter;
+        BsonDocument startAfter;
+        BsonTimestamp startAtOperationTime;
+        FullDocument fullDocument = FullDocument.DEFAULT;
+        ChangeStreamIterable<Document> self;
+
+        ChangeStreamIterable<Document> proxy() {
+            self = FakeMongo.this.proxy(ChangeStreamIterable.class, (m, args) 
-> switch (m) {
+                case "fullDocument" -> {
+                    fullDocument = (FullDocument) args[0];
+                    yield self;
+                }
+                case "resumeAfter" -> {
+                    resumeAfter = (BsonDocument) args[0];
+                    yield self;
+                }
+                case "startAfter" -> {
+                    startAfter = (BsonDocument) args[0];
+                    yield self;
+                }
+                case "startAtOperationTime" -> {
+                    startAtOperationTime = (BsonTimestamp) args[0];
+                    yield self;
+                }
+                case "batchSize", "maxAwaitTime" -> self;
+                case "iterator", "cursor" -> open(this);
+                default -> notModelled("ChangeStreamIterable." + m);
+            });
+            return self;
+        }
+    }
+
+    private void roundTrip(String what) {
+        if (faults && dst.chance(slowRate)) {
+            dst.block(dst.between(200, 3000), "slow " + what);
+        } else {
+            dst.block(dst.between(1, 20), what);
+        }
+    }
+
+    /** [D] server selection: waits for the server up to 
serverSelectionTimeout, then MongoTimeoutException. */
+    private void selectServer(String what) {
+        if (dst.now() < unreachableUntil) {
+            long wait = Math.min(unreachableUntil - dst.now(), 
serverSelectionTimeout);
+            dst.block(wait, "server selection for " + what);
+            if (dst.now() < unreachableUntil) {
+                throw new MongoTimeoutException(
+                        "dst: timed out after " + serverSelectionTimeout
+                                                + " ms while waiting for a 
server (" + what + ")");
+            }
+        }
+    }
+
+    /** [M] the aggregate that opens a change stream. */
+    private MongoChangeStreamCursor<ChangeStreamDocument<Document>> open(Watch 
w) {
+        checkInterrupt();
+        selectServer("open the change stream");
+        roundTrip("aggregate $changeStream");
+        int position;
+        if (w.startAfter != null || w.resumeAfter != null) {
+            BsonDocument token = w.startAfter != null ? w.startAfter : 
w.resumeAfter;
+            long seq;
+            try {
+                seq = Long.parseLong(token.getString("_data").getValue(), 16);
+            } catch (RuntimeException e) {
+                violations.add("a resume token that the server did not make: " 
+ token.toJson());
+                throw command(260, "InvalidResumeToken", "dst: the resume 
token cannot be decoded");
+            }
+            // [A] a token of an event of the collection before it was dropped 
and made again still resumes, as long as
+            // the oplog has it (the oplog window is not modelled): the change 
stream delivers the events after it,
+            // also the drop and the invalidate of the earlier collection, as 
a token is a position in the oplog
+            if (seq < 1 || seq > oplog.size()) {
+                violations.add("a resume token that the server did not make: " 
+ token.toJson());
+                throw command(260, "InvalidResumeToken", "dst: the resume 
token was not found");
+            }
+            if (w.startAfter == null && oplog.get((int) seq - 1).type() == 
OperationType.INVALIDATE) {
+                // [M] "You cannot use resumeAfter to resume a change stream 
after an invalidate event ... Starting in
+                // MongoDB 4.2, you can use startAfter to start a new change 
stream after an invalidate event."
+                throw command(260, "InvalidResumeToken",
+                        "Attempting to resume a change stream using 
'resumeAfter' is not allowed from an invalidate"
+                                                         + " notification.");
+            }
+            position = (int) seq;
+        } else if (w.startAtOperationTime != null) {
+            // [M] "The change stream will only provide changes that occurred 
at or after the specified timestamp" (the
+            // oplog window is not modelled)
+            long time = w.startAtOperationTime.getInc();
+            if (w.startAtOperationTime.getTime() != 1 || time < 1 || time > 
oplog.size() + 1) {
+                violations.add("an operation time that the server did not 
report: " + w.startAtOperationTime);
+            }
+            position = (int) Math.max(0, Math.min(oplog.size(), time - 1));
+        } else {
+            // [M] a change stream without a resume token starts at the 
current time
+            position = oplog.size();
+        }
+        FakeCursor c = new FakeCursor(position, w.fullDocument);
+        cursors.add(c);
+        dst.trace("mongo: change stream opened at " + position + (w.startAfter 
!= null ? " (startAfter)"
+                : w.resumeAfter != null ? " (resumeAfter)"
+                : w.startAtOperationTime != null ? " (startAtOperationTime)" : 
" (now)"));
+        String how = w.startAfter != null ? "startAfter" : w.resumeAfter != 
null ? "resumeAfter"
+                : w.startAtOperationTime != null ? "startAtOperationTime" : 
"now";
+        // the consumer thread runs on the executor named after the endpoint; 
the route start opens the first cursor
+        onOpen.accept(how + (dst.currentExecutor().startsWith("mongodb:") ? " 
(consumer thread)" : " (route start)"));
+        return c.proxy;
+    }
+
+    /**
+     * [D] a call of an interrupted thread throws MongoInterruptedException 
and keeps the interrupt flag, so the next
+     * call throws it too.
+     */
+    private void checkInterrupt() {
+        if (!dst.currentExecutor().startsWith("mongodb:")) {
+            // only the consumer thread (its executor is named after the 
endpoint) is interrupted
+            return;
+        }
+        if (interruptRequested) {
+            interruptRequested = false;
+            Thread.currentThread().interrupt();
+            dst.trace("mongo: the consumer thread is interrupted");
+            onInterrupt.run();
+        }
+        if (Thread.currentThread().isInterrupted()) {
+            throw new MongoInterruptedException("dst: interrupted", null);
+        }
+    }
+
+    private static MongoCommandException command(int code, String name, String 
message) {
+        BsonDocument response = new BsonDocument("ok", new 
BsonInt32(0)).append("code", new BsonInt32(code))
+                .append("codeName", new BsonString(name)).append("errmsg", new 
BsonString(message));
+        return new MongoCommandException(response, new ServerAddress("fake", 
27017));
+    }
+
+    /** The position of the consumer in the change stream that it opened most 
recently. */
+    int lastCursorStart() {
+        return cursors.isEmpty() ? -1 : cursors.get(cursors.size() - 1).start;
+    }
+
+    private final class FakeCursor {
+        final int start;
+        final FullDocument fullDocument;
+        final MongoChangeStreamCursor<ChangeStreamDocument<Document>> proxy;
+        int position;
+        boolean closed;
+        boolean invalidated;
+        BsonDocument cachedToken;
+
+        FakeCursor(int position, FullDocument fullDocument) {
+            this.start = position;
+            this.position = position;
+            this.fullDocument = fullDocument;
+            this.proxy = FakeMongo.this.proxy(MongoChangeStreamCursor.class, 
(m, args) -> switch (m) {
+                case "hasNext" -> hasNext();
+                case "next" -> next();
+                case "close" -> {
+                    closed = true;
+                    yield null;
+                }
+                case "getResumeToken" -> cachedToken;
+                default -> notModelled("MongoChangeStreamCursor." + m);
+            });
+        }
+
+        /** [D] hasNext blocks until the next change, and returns false once 
the server closed the cursor. */
+        private boolean hasNext() {
+            if (closed) {
+                violations.add("hasNext on a closed cursor");
+                throw new IllegalStateException("Cursor has been closed");
+            }
+            while (true) {
+                checkInterrupt();
+                if (invalidated) {
+                    // [M] the invalidate event closes the change stream 
cursor: no event after it on this cursor
+                    return false;
+                }
+                if (position < oplog.size()) {
+                    return true;
+                }
+                // a getMore: waits up to maxAwaitTime (1 s by default) for an 
event, then answers
+                dst.awaitUntil(() -> position < oplog.size() || 
interruptRequested, 1000, "getMore");
+                roundTrip("getMore");
+                if (dst.now() < unreachableUntil || faults && 
dst.chance(getMoreFailRate)) {
+                    // [D] a resumable error (isResumableError is true for 
network errors): the driver opens the change
+                    // stream again after its cached token 
(ChangeStreamBatchCursor.resumeableOperation). The resume
+                    // itself is outside that retry, so what the resume throws 
reaches the caller: a server selection
+                    // timeout, or a network error of the resume aggregate
+                    dst.trace("mongo: getMore fails, the driver resumes the 
change stream");
+                    selectServer("resume the change stream");
+                    roundTrip("aggregate $changeStream (resume)");
+                    if (faults && dst.chance(0.5)) {
+                        // [A] the resume aggregate fails with a network error 
too
+                        throw new MongoSocketReadException(
+                                "dst: the connection was reset", new 
ServerAddress("fake", 27017));
+                    }
+                }
+            }
+        }
+
+        private ChangeStreamDocument<Document> next() {
+            if (!hasNext()) {
+                throw new NoSuchElementException();
+            }
+            Event e = oplog.get(position++);
+            if (e.type() == OperationType.INVALIDATE) {
+                invalidated = true;
+            }
+            cachedToken = e.token();
+            BsonDocument key = e.id() != null ? new BsonDocument("_id", new 
BsonString(e.id())) : null;
+            Document full = null;
+            if (e.type() == OperationType.INSERT || e.type() == 
OperationType.REPLACE
+                    || e.type() == OperationType.UPDATE && fullDocument == 
FullDocument.UPDATE_LOOKUP) {
+                full = new Document("_id", e.id()).append("seq", e.seq());
+            }
+            BsonDocument ns = e.type() == OperationType.INVALIDATE
+                    ? null : new BsonDocument("db", new 
BsonString(DATABASE)).append("coll", new BsonString(COLLECTION));
+            // positional arguments of the MongoDB Java driver 5.14: 
operationType, resumeToken, ns, nsType, to,
+            // fullDocument, fullDocumentBeforeChange, documentKey, 
clusterTime, updateDescription, txnNumber, lsid,
+            // wallTime, splitEvent, extraElements
+            return new ChangeStreamDocument<>(
+                    e.type().getValue(), e.token(), ns, null, null, full, 
null, key, null, null, null, null, null, null,
+                    null);
+        }
+    }
+
+    // 
------------------------------------------------------------------------------------------------------------
+
+    private interface Handler {
+        Object invoke(String method, Object[] args) throws Throwable;
+    }
+
+    @SuppressWarnings("unchecked")
+    private <T> T proxy(Class<?> type, Handler handler) {
+        return (T) Proxy.newProxyInstance(FakeMongo.class.getClassLoader(), 
new Class<?>[] { type },
+                (p, method, args) -> switch (method.getName()) {
+                    case "toString" -> "FakeMongo." + type.getSimpleName();
+                    case "hashCode" -> System.identityHashCode(p);
+                    case "equals" -> p == args[0];
+                    default -> handler.invoke(method.getName(), args);
+                });
+    }
+
+    private Object notModelled(String what) {
+        violations.add("not modelled: " + what);
+        throw new UnsupportedOperationException("dst: not modelled: " + what);
+    }
+}
diff --git 
a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/dst/MongoDbChangeStreamsDstTest.java
 
b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/dst/MongoDbChangeStreamsDstTest.java
new file mode 100644
index 000000000000..01c168282a0c
--- /dev/null
+++ 
b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/dst/MongoDbChangeStreamsDstTest.java
@@ -0,0 +1,36 @@
+/*
+ * 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.mongodb.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-mongodb change streams consumer 
against a fake MongoDB, with dropped
+ * collections, a server that cannot be reached, failed getMores, failed and 
slow processing, and route restarts: a
+ * stored resume token never passes an event that was not routed, and once the 
faults cleared every event is routed. A
+ * failing seed is replayed with {@code -Ddst.seed=<seed>}; {@code 
-Ddst.seeds=5000} runs more seeds.
+ */
+@DstInstrument("org.apache.camel.component.mongodb")
+class MongoDbChangeStreamsDstTest {
+
+    @DstTest(seeds = 1000)
+    void everyEventIsRoutedAfterAStopOrADrop(Dst dst) throws Exception {
+        new MongoDbChangeStreamsWorld(dst).run();
+    }
+}
diff --git 
a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/dst/MongoDbChangeStreamsWorld.java
 
b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/dst/MongoDbChangeStreamsWorld.java
new file mode 100644
index 000000000000..f2bc43e7b0d4
--- /dev/null
+++ 
b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/dst/MongoDbChangeStreamsWorld.java
@@ -0,0 +1,471 @@
+/*
+ * 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.mongodb.dst;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.concurrent.TimeUnit;
+
+import com.mongodb.client.model.changestream.OperationType;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mongodb.MongoDbConstants;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.resume.Resumable;
+import org.apache.camel.spi.StateRepository;
+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.DstWorld;
+import org.apache.camel.test.dst.Invariants;
+
+/**
+ * The world around the camel-mongodb change streams consumer: a fake MongoDB 
with one collection ({@link FakeMongo})
+ * given with the connectionBean option, other clients that insert, update, 
replace and delete documents, a route whose
+ * processing is sometimes slow and sometimes fails, a durable store of the 
resume token (changeStreamTokenRepository),
+ * and an operator that drops the collection (other clients make it again with 
an insert), makes the server unreachable
+ * for a while, and stops and starts the route.
+ * <p>
+ * The seed picks cursorRegenerationDelay, the fullDocument option, how long 
the server is unreachable (shorter or
+ * longer than the server selection timeout), and the fault rates.
+ * <p>
+ * The consumer thread loops for ever and its stop waits for it, which the one 
stack of the harness cannot run: the stop
+ * would wait above the thread it waits for. A route stop is therefore made in 
two steps: the thread is interrupted at
+ * its next call of the change stream (as a forced shutdown of its executor 
does; the driver throws
+ * MongoInterruptedException and keeps the interrupt flag, and the thread 
ends), then the route is stopped. The phases
+ * of the run (start, run until the stop, stop) are in the test body, so the 
interrupt flag of the one thread of the
+ * harness is cleared as soon as the consumer thread ended: the condition of 
the test body runs right after the consumer
+ * task returned, as no other task of the world blocks. The consumer is never 
stopped in the middle of an exchange, and
+ * a stop does not close the cursor under a waiting getMore. The operator does 
not drop the collection or make the
+ * server unreachable while the route starts, so a start that fails is the 
consumer's ("the route starts once the
+ * collection exists and the server can be reached").
+ * <p>
+ * Properties: the consumer thread runs as long as the route is started; every 
exchange carries a resume token; a resume
+ * token is stored only after its exchange completed; a stored token never 
passes an event that was not routed; once the
+ * faults cleared, every event of the collection after the first change stream 
was routed, also after a drop of the
+ * collection, a restart of the route and a failed getMore or cursor open; the 
consumer uses the client as documented.
+ * Events may be routed more than once (a restart or a new cursor resumes 
after the stored token), and an event whose
+ * exchange failed is not routed again when a later one completed (the 
consumer stores the token of the later one).
+ * <p>
+ * A cursor that the consumer thread opens again before any exchange completed 
(the first change stream failed and the
+ * driver could not resume it) has no token to resume after: it starts at the 
operation time that a ping read just
+ * before the first change stream was opened (startAtOperationTime), so the 
events written since then are routed too
+ * (CAMEL-26992).
+ * <p>
+ * Not explored: the graceful stop of the consumer (keepRunning set to false 
and the cursor closed under a waiting
+ * getMore or an exchange in flight), which the one stack cannot run; tailable 
cursors, streamFilter, the ResumeStrategy
+ * of the consumer, a change stream without a token repository after a restart 
(it starts at the current time by
+ * design), and camel-mongodb-gridfs.
+ */
+@DstWorld(component = "mongodb",
+          fake = "FakeMongo: a MongoDB collection and its change stream 
(resumeAfter, startAfter, drop and invalidate"
+                 + " events, the resume of the driver), behind the MongoClient 
of the connectionBean option",
+          covers = {
+                  "the change streams consumer with a store of its resume 
token (changeStreamTokenRepository)",
+                  "a dropped collection, made again by an insert",
+                  "a server that cannot be reached for a while: the cursor 
cannot be opened, a getMore fails",
+                  "route restarts (the consumer thread interrupted, then the 
route stopped and started), resuming after the stored token",
+                  "the fullDocument option and cursorRegenerationDelay" },
+          faults = {
+                  "the collection is dropped", "the server cannot be reached", 
"a getMore fails",
+                  "the server answers slowly", "processing fails", "processing 
is slow" },
+          invariants = {
+                  "the consumer uses the client as documented",
+                  "the route starts once the collection exists and the server 
can be reached",
+                  "the consumer thread runs as long as the route is started",
+                  "a resume token is stored only after its exchange completed",
+                  "a stored resume token never passes an event that was not 
routed",
+                  "every exchange carries a resume token",
+                  "every event is routed once the faults cleared" },
+          issues = { "CAMEL-25679", "CAMEL-25946", "CAMEL-26992" })
+final class MongoDbChangeStreamsWorld {
+
+    static final String ROUTE = "changes";
+
+    private final Dst dst;
+    private final FakeMongo mongo;
+    private final long regenerationDelay;
+    private final boolean updateLookup;
+    private final double processFailRate;
+    private final List<String> violations = new ArrayList<>();
+    private final Set<Long> routed = new HashSet<>();
+    private final Set<Long> completed = new HashSet<>();
+    private final Set<Long> excused = new HashSet<>();
+    private final Map<String, String> tokens = new HashMap<>();
+    private final List<String> liveIds = new ArrayList<>();
+    private boolean faults = true;
+    private int writesLeft;
+    private int opsLeft;
+    private int nextId;
+    private boolean lifetime;
+    private boolean endRequested;
+    private long endRequestedAt;
+    private boolean finalRequested;
+    private boolean starting;
+    private boolean interrupted;
+    private String finalCheck;
+
+    MongoDbChangeStreamsWorld(Dst dst) {
+        this.dst = dst;
+        this.mongo = new FakeMongo(dst);
+        this.regenerationDelay = dst.pick(List.of(0L, 100L, 1000L, 5000L));
+        this.updateLookup = dst.chance(0.5);
+        this.processFailRate = dst.pick(List.of(0.0, 0.05, 0.2));
+        mongo.getMoreFailRate = dst.pick(List.of(0.0, 0.02, 0.1));
+        mongo.slowRate = dst.pick(List.of(0.0, 0.05));
+        mongo.serverSelectionTimeout = dst.pick(List.of(2000L, 30_000L));
+        mongo.onOpen = this::opened;
+        mongo.onInterrupt = () -> interrupted = true;
+        dst.trace("world: " + config());
+    }
+
+    private String config() {
+        return "cursorRegenerationDelay=" + regenerationDelay + " 
fullDocument=" + (updateLookup ? "updateLookup" : "default")
+               + " serverSelectionTimeout=" + mongo.serverSelectionTimeout + " 
getMoreFail=" + mongo.getMoreFailRate
+               + " slow=" + mongo.slowRate + " processFail=" + processFailRate;
+    }
+
+    void run() throws Exception {
+        DefaultCamelContext context = DstCamel.newContext(dst);
+        context.getRegistry().bind("mongo", mongo.client());
+        context.getRegistry().bind("tokens", new TokenStore());
+        String uri = "mongodb:mongo?database=" + FakeMongo.DATABASE + 
"&collection=" + FakeMongo.COLLECTION
+                     + 
"&consumerType=changeStreams&changeStreamTokenRepository=#tokens&cursorRegenerationDelay="
+                     + regenerationDelay + (updateLookup ? 
"&fullDocument=updateLookup" : "");
+        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 {
+            // some documents before the consumer starts: they are not in its 
first change stream
+            for (int i = 0; i < 3; i++) {
+                write();
+            }
+            planWrites();
+            planOperations(operator);
+            operator.scheduleWithFixedDelay(() -> finisher(operator), 1000, 
500, TimeUnit.MILLISECONDS);
+            while (true) {
+                startRoute(context);
+                lifetime = true;
+                dst.runUntil(this::consumerEnded, "the end of the consumer 
thread");
+                lifetime = false;
+                mongo.interruptRequested = false;
+                context.getRouteController().stopRoute(ROUTE);
+                dst.trace("world: route stopped");
+                if (finalRequested) {
+                    break;
+                }
+                endRequested = false;
+                // the route stays stopped for a while
+                dst.runFor(dst.between(100, 5000));
+            }
+            dst.check(
+                    Invariants.of("the route starts once the collection exists 
and the server can be reached",
+                            () -> violations.stream().noneMatch(v -> 
v.startsWith("start"))
+                                    ? null : violations + " (" + config() + 
")"),
+                    Invariants.of("the consumer thread runs as long as the 
route is started",
+                            () -> violations.stream().noneMatch(v -> 
v.startsWith("thread"))
+                                    ? null : violations + " (" + config() + 
")"),
+                    Invariants.of("the consumer uses the client as documented",
+                            () -> mongo.violations.isEmpty() ? null : 
mongo.violations + " (" + config() + ")"),
+                    Invariants.of("every exchange carries a resume token",
+                            () -> violations.stream().noneMatch(v -> 
v.startsWith("token"))
+                                    ? null : violations + " (" + config() + 
")"),
+                    Invariants.of("a resume token is stored only after its 
exchange completed",
+                            () -> violations.stream().noneMatch(v -> 
v.startsWith("stored the token of"))
+                                    ? null : violations + " (" + config() + 
")"),
+                    Invariants.of("a stored resume token never passes an event 
that was not routed",
+                            () -> violations.stream().noneMatch(v -> 
v.startsWith("stored a token past"))
+                                    ? null : violations + " (" + config() + 
")"),
+                    Invariants.of("every event is routed once the faults 
cleared",
+                            () -> finalCheck == null ? null : finalCheck + " 
(" + config() + ")"));
+        } finally {
+            dst.dropPending();
+            context.stop();
+        }
+    }
+
+    /**
+     * The condition of the phase of a consumer thread, checked by the loop of 
the test body between its tasks: the
+     * consumer thread ended after its interrupt (the interrupt flag of the 
thread of the harness is cleared here), or
+     * it ended earlier on its own and the stop was asked a minute ago.
+     */
+    private boolean consumerEnded() {
+        if (interrupted) {
+            interrupted = false;
+            Thread.interrupted();
+            return true;
+        }
+        if (endRequested && dst.now() - endRequestedAt > 60_000) {
+            // the consumer thread did not take the interrupt: it ended on its 
own before, while the route is started
+            violations.add("thread: the consumer thread ended while the route 
was started (seen at t=" + dst.now() + ")");
+            return true;
+        }
+        return false;
+    }
+
+    private void startRoute(DefaultCamelContext context) {
+        while (true) {
+            // the operator starts the route once the collection exists and 
the server can be reached: a route whose
+            // consumer cannot be made (the endpoint checks that the 
collection exists) is not started again here, as
+            // that is about the route controller, not this consumer
+            if (!mongo.collectionExists() || !mongo.isReachable()) {
+                if (!mongo.collectionExists() && writesLeft == 0) {
+                    write(OperationType.INSERT);
+                }
+                dst.runFor(1000);
+                continue;
+            }
+            starting = true;
+            try {
+                context.getRouteController().startRoute(ROUTE);
+                dst.trace("world: route started");
+                return;
+            } catch (Exception e) {
+                // the collection exists and the server can be reached, and 
nothing of the world changes while the route
+                // starts: a start that fails is the consumer's
+                violations.add("start: the route did not start at t=" + 
dst.now() + ": " + e.getMessage());
+                return;
+            } finally {
+                starting = false;
+            }
+        }
+    }
+
+    /**
+     * The first change stream of a route start without a stored token starts 
at the current time: the events before it
+     * are not expected.
+     */
+    private void opened(String how) {
+        // only the route start opens a change stream at the current time by 
design. A cursor that the consumer thread
+        // opens again before any exchange completed starts where the first 
one started (CAMEL-26992), so the events
+        // written in between are expected
+        if (completed.isEmpty() && "now (route start)".equals(how)) {
+            for (FakeMongo.Event e : mongo.oplog()) {
+                if (e.seq() <= mongo.lastCursorStart() && 
!routed.contains(e.seq())) {
+                    excused.add(e.seq());
+                }
+            }
+        }
+    }
+
+    private void process(Exchange exchange) {
+        Resumable offset = exchange.getMessage().getHeader(Exchange.OFFSET, 
Resumable.class);
+        if (offset == null) {
+            violations.add("token: an exchange without a resume token");
+            return;
+        }
+        long seq = FakeMongo.seqOf((String) offset.getLastOffset().getValue());
+        String type = 
exchange.getMessage().getHeader(MongoDbConstants.STREAM_OPERATION_TYPE, 
String.class);
+        routed.add(seq);
+        if (faults && dst.chance(0.05)) {
+            dst.block(dst.between(1000, 10_000), "slow processing of " + seq);
+        } else {
+            dst.block(dst.between(0, 100), "processing of " + seq);
+        }
+        if (faults && dst.chance(processFailRate)) {
+            throw Dst.injected(new IllegalStateException("dst: processing 
failed for event " + seq + " (" + type + ")"));
+        }
+        completed.add(seq);
+    }
+
+    /** A durable store of the resume token (a database table, say): it keeps 
its state when the consumer stops. */
+    private final class TokenStore implements StateRepository<String, String> {
+        @Override
+        public void setState(String key, String value) {
+            long seq = FakeMongo.seqOf(value);
+            if (!completed.contains(seq)) {
+                violations.add("stored the token of event " + seq + " whose 
exchange did not complete, at t=" + dst.now());
+            }
+            for (FakeMongo.Event e : mongo.oplog()) {
+                if (e.seq() < seq && isData(e) && !routed.contains(e.seq()) && 
!excused.contains(e.seq())) {
+                    violations.add("stored a token past event " + e.seq() + " 
(" + e.type().getValue()
+                                   + "), which was not routed, at t=" + 
dst.now() + " (token of event " + seq + ")");
+                    break;
+                }
+            }
+            tokens.put(key, value);
+        }
+
+        @Override
+        public String getState(String key) {
+            return tokens.get(key);
+        }
+
+        @Override
+        public void start() {
+            // a database table: nothing to start
+        }
+
+        @Override
+        public void stop() {
+            // keeps its state
+        }
+    }
+
+    private static boolean isData(FakeMongo.Event e) {
+        return e.type() == OperationType.INSERT || e.type() == 
OperationType.UPDATE
+                || e.type() == OperationType.REPLACE || e.type() == 
OperationType.DELETE;
+    }
+
+    // 
------------------------------------------------------------------------------------------------------------
+    // the load and the operator
+
+    private void write() {
+        if (liveIds.isEmpty() || dst.chance(0.4)) {
+            write(OperationType.INSERT);
+        } else {
+            write(dst.pick(List.of(OperationType.UPDATE, 
OperationType.REPLACE, OperationType.DELETE)));
+        }
+    }
+
+    private void write(OperationType type) {
+        if (!mongo.collectionExists()) {
+            liveIds.clear();
+            type = OperationType.INSERT;
+        }
+        String id;
+        if (type == OperationType.INSERT || liveIds.isEmpty()) {
+            type = OperationType.INSERT;
+            id = "d" + nextId++;
+            liveIds.add(id);
+        } else {
+            id = dst.pick(liveIds);
+            if (type == OperationType.DELETE) {
+                liveIds.remove(id);
+            }
+        }
+        mongo.write(type, id);
+    }
+
+    private void planWrites() {
+        DstExecutor writer = dst.executor("writer", 1);
+        long t = dst.between(0, 500);
+        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++) {
+                writesLeft++;
+                writer.schedule("write", t, () -> {
+                    write();
+                    writesLeft--;
+                });
+                t += dst.between(0, 300);
+            }
+            t += dst.between(0, 20_000);
+        }
+    }
+
+    private void planOperations(DstExecutor operator) {
+        int ops = (int) dst.between(0, 4);
+        long t = dst.between(500, 5000);
+        for (int i = 0; i < ops; i++) {
+            opsLeft++;
+            int kind = (int) dst.between(0, 3);
+            if (kind == 0) {
+                operator.schedule("drop collection", t, () -> 
notWhileStarting(operator, "drop collection", () -> {
+                    mongo.drop();
+                    opsLeft--;
+                }));
+            } else if (kind == 1) {
+                long length = dst.chance(0.5)
+                        ? dst.between(100, mongo.serverSelectionTimeout)
+                        : dst.between(mongo.serverSelectionTimeout, 3 * 
mongo.serverSelectionTimeout);
+                operator.schedule("server unreachable", t, () -> 
notWhileStarting(operator, "server unreachable", () -> {
+                    mongo.unreachable(length);
+                    opsLeft--;
+                }));
+            } else {
+                operator.schedule("stop route", t, () -> {
+                    requestEnd();
+                    opsLeft--;
+                });
+            }
+            t += dst.between(1000, 20_000);
+        }
+    }
+
+    /**
+     * Runs an operation of the operator once no route start is in progress: a 
drop or an outage that came in the middle
+     * of a start would make the start fail, and the restart of a route whose 
start failed is about the route
+     * controller, not this consumer.
+     */
+    private void notWhileStarting(DstExecutor operator, String what, Runnable 
operation) {
+        if (starting) {
+            operator.schedule(what + " (a route start is in progress)", 100,
+                    () -> notWhileStarting(operator, what, operation));
+            return;
+        }
+        operation.run();
+    }
+
+    /** Asks the consumer thread to end at its next call of the change stream 
(see the class Javadoc). */
+    private void requestEnd() {
+        if (!lifetime || endRequested) {
+            return;
+        }
+        endRequested = true;
+        endRequestedAt = dst.now();
+        mongo.interruptRequested = true;
+        dst.trace("world: stop the route (interrupt the consumer thread)");
+    }
+
+    private long drainDeadline = -1;
+
+    private void finisher(DstExecutor operator) {
+        if (finalRequested || writesLeft > 0 || opsLeft > 0) {
+            return;
+        }
+        if (drainDeadline < 0) {
+            faults = false;
+            mongo.faults = false;
+            dst.trace("world: faults cleared");
+            if (!mongo.collectionExists()) {
+                write(OperationType.INSERT);
+            }
+            drainDeadline = dst.now() + 10 * 60_000L;
+            return;
+        }
+        if (!lifetime || endRequested) {
+            return;
+        }
+        Set<Long> missing = new TreeSet<>();
+        for (FakeMongo.Event e : mongo.oplog()) {
+            if (isData(e) && !routed.contains(e.seq()) && 
!excused.contains(e.seq())) {
+                missing.add(e.seq());
+            }
+        }
+        if (!missing.isEmpty() && dst.now() < drainDeadline) {
+            return;
+        }
+        if (!missing.isEmpty()) {
+            finalCheck = "events never routed: " + missing + " of " + 
mongo.oplog().size() + " (collection exists="
+                         + mongo.collectionExists() + ", stored token " + 
tokens + ")";
+        }
+        finalRequested = true;
+        requestEnd();
+    }
+}
diff --git a/components/camel-mongodb/src/test/resources/log4j2.properties 
b/components/camel-mongodb/src/test/resources/log4j2.properties
index 06895488514d..9798988967b7 100644
--- a/components/camel-mongodb/src/test/resources/log4j2.properties
+++ b/components/camel-mongodb/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-mongodb-test.log
+appender.file.filePattern = target/camel-mongodb-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 a78c6d18b373..e3ee21b12f26 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
@@ -135,6 +135,16 @@ a|* explicit acknowledgement with onFailure RELEASE or 
REJECT
 * suspend and resume of the CamelContext
 |CAMEL-25872, CAMEL-25910
 
+|camel-mongodb
+|`MongoDbChangeStreamsWorld` (`MongoDbChangeStreamsDstTest`)
+|FakeMongo: a MongoDB collection and its change stream (resumeAfter, 
startAfter, drop and invalidate events, the resume of the driver), behind the 
MongoClient of the connectionBean option
+a|* the change streams consumer with a store of its resume token 
(changeStreamTokenRepository)
+* a dropped collection, made again by an insert
+* a server that cannot be reached for a while: the cursor cannot be opened, a 
getMore fails
+* route restarts (the consumer thread interrupted, then the route stopped and 
started), resuming after the stored token
+* the fullDocument option and cursorRegenerationDelay
+|CAMEL-25679, CAMEL-25946, CAMEL-26992
+
 |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
@@ -615,14 +625,15 @@ EH=components/camel-azure/camel-azure-eventhubs
 SLACK=components/camel-slack
 SHARE=components/camel-kafka-share
 PULSAR=components/camel-pulsar
+MONGODB=components/camel-mongodb
 MVN="./mvnw -B -Papache-snapshots test -Dsurefire.failIfNoSpecifiedTests=false 
-Dsurefire.rerunFailingTestsCount=0"
 
 # the harness self-tests
 $MVN -pl $DST -Dtest='Dst*Test'
 
 # the worlds, 1000 seeds each, then 5000 seeds without stopping at the first 
failing one
-$MVN -pl $DST,$SQS,$KINESIS,$EH,$SLACK,$SHARE,$PULSAR -Dtest='*DstTest'
-$MVN -pl $DST,$SQS,$KINESIS,$EH,$SLACK,$SHARE,$PULSAR -Dtest='*DstTest' 
-Ddst.seeds=5000 -Ddst.maxFailures=100000
+$MVN -pl $DST,$SQS,$KINESIS,$EH,$SLACK,$SHARE,$PULSAR,$MONGODB 
-Dtest='*DstTest'
+$MVN -pl $DST,$SQS,$KINESIS,$EH,$SLACK,$SHARE,$PULSAR,$MONGODB 
-Dtest='*DstTest' -Ddst.seeds=5000 -Ddst.maxFailures=100000
 
 # the self-check: revert the main code of a past fix locally (do not commit), 
run the world, restore
 git show d09593b4a584 -- $SQS/src/main | git apply -R          # CAMEL-25700
@@ -636,6 +647,10 @@ $MVN -pl $DST,$SQS -Dtest=SqsVisibilityDstTest -Ddst.seed=8
 diff /tmp/seed-8.first $SQS/target/dst/SqsVisibilityDstTest.*/seed-8.trace && 
echo identical
 ----
 
+For camel-mongodb (`MongoDbChangeStreamsDstTest`) the past fixes are the main 
code of 5f10c225652e (CAMEL-25946) and of
+3af2c90d63ad (CAMEL-25679), reverted by hand. Its world also found CAMEL-26992 
(fixed by 1eb59175e600): a cursor that
+the consumer opened again before any exchange completed started at the current 
time and skipped the events in between.
+
 The past fixes of the other worlds are 83aaa80754f9 (CAMEL-25683, Event Hubs), 
68329f0919bc (CAMEL-25684, Slack), the
 main code of b62e90ddb2d9 (CAMEL-25869, Pulsar), the main code of 20c94435789e 
(CAMEL-25872, Kafka share, whose world
 was also checked with planted faults), and the main code of 043ac9ec84ab 
(CAMEL-25569) and of 847ee9affb4c

Reply via email to