Skip to content

Commit 67cae1a

Browse files
committed
add failing test case for starting multiple ChangeStreams
1 parent 94570da commit 67cae1a

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

test-common/src/main/java/de/bwaldvogel/mongo/backend/AbstractOplogTest.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,23 @@
77
import static de.bwaldvogel.mongo.backend.TestUtils.json;
88
import static de.bwaldvogel.mongo.backend.TestUtils.toArray;
99
import static java.util.Collections.singletonList;
10+
import static org.assertj.core.groups.Tuple.tuple;
1011
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
1112

1213
import java.time.Duration;
1314
import java.time.Instant;
15+
import java.util.AbstractMap;
1416
import java.util.ArrayList;
1517
import java.util.Arrays;
1618
import java.util.Date;
1719
import java.util.List;
20+
import java.util.Map;
1821
import java.util.NoSuchElementException;
1922
import java.util.UUID;
2023
import java.util.concurrent.TimeUnit;
24+
import java.util.stream.IntStream;
2125

26+
import org.assertj.core.api.Assertions;
2227
import org.bson.BsonDocument;
2328
import org.bson.BsonInt32;
2429
import org.bson.BsonTimestamp;
@@ -40,6 +45,8 @@
4045
import com.mongodb.reactivestreams.client.Success;
4146

4247
import de.bwaldvogel.mongo.oplog.OperationType;
48+
import io.reactivex.Flowable;
49+
import io.reactivex.schedulers.Schedulers;
4350
import io.reactivex.subscribers.TestSubscriber;
4451

4552
public abstract class AbstractOplogTest extends AbstractTest {
@@ -456,4 +463,67 @@ private static <T> T getSingleValue(TestSubscriber<T> subscriber) {
456463
return subscriber.values().get(0);
457464
}
458465

466+
@Test
467+
@Disabled
468+
public void testMultipleChangeStreams() throws InterruptedException {
469+
Flowable.fromPublisher(asyncCollection.insertOne(json("_id: 1")))
470+
.test().awaitDone(5, TimeUnit.SECONDS).assertComplete();
471+
472+
final int changeStreamCount = 32;
473+
474+
List<Bson> pipeline = singletonList(match(Filters.eq("fullDocument.bu", "abc")));
475+
476+
final TestSubscriber<Map<Integer, List<ChangeStreamDocument<Document>>>> streamSubscriber
477+
= new TestSubscriber<>();
478+
479+
Flowable.range(1, changeStreamCount)
480+
.flatMapSingle(index -> {
481+
return Flowable.fromPublisher(asyncCollection.watch(pipeline))
482+
.take(2)
483+
.toList()
484+
.map(changeStreamDocuments -> {
485+
return new AbstractMap.SimpleEntry<>(index, changeStreamDocuments);
486+
})
487+
.subscribeOn(Schedulers.io()); // subscribe to change streams concurrently
488+
})
489+
.toMap(Map.Entry::getKey, Map.Entry::getValue)
490+
.toFlowable()
491+
.subscribe(streamSubscriber);
492+
493+
// give time for all ChangeStream Publishers to be subscribed to
494+
// todo: expose API to get cursors from Backend and wait until 'changeStreamCount' cursors
495+
TimeUnit.SECONDS.sleep(5);
496+
497+
Flowable.concat(
498+
Flowable.fromPublisher(asyncCollection.insertOne(json("_id: 2, bu: 'abc'"))),
499+
Flowable.fromPublisher(asyncCollection.insertOne(json("_id: 3, bu: 'xyz'"))),
500+
Flowable.fromPublisher(asyncCollection.insertOne(json("_id: 4, bu: 'abc'")))
501+
).test().awaitDone(15, TimeUnit.SECONDS).assertComplete();
502+
503+
final Map<Integer, List<ChangeStreamDocument<Document>>> results = streamSubscriber
504+
.awaitDone(30, TimeUnit.SECONDS)
505+
.assertComplete()
506+
.assertValueCount(1)
507+
.values().get(0);
508+
509+
Assertions.assertThat(IntStream.rangeClosed(1, changeStreamCount))
510+
.allSatisfy(index -> {
511+
Assertions.assertThat(results).containsKey(index);
512+
513+
final List<ChangeStreamDocument<Document>> emits = results.get(index);
514+
Assertions.assertThat(emits).isNotNull()
515+
.extracting(
516+
document -> {
517+
return document.getDocumentKey().getInt32("_id").getValue();
518+
},
519+
document -> {
520+
return document.getFullDocument() != null
521+
? document.getFullDocument().getString("bu")
522+
: null;
523+
}
524+
)
525+
.containsExactly(tuple(2, "abc"), tuple(4, "abc"));
526+
});
527+
}
528+
459529
}

0 commit comments

Comments
 (0)