|
17 | 17 |
|
18 | 18 |
|
19 | 19 | import static com.mongodb.kafka.connect.source.MongoSourceConfig.COLLECTION_CONFIG; |
| 20 | +import static com.mongodb.kafka.connect.source.MongoSourceConfig.COPY_EXISTING_QUEUE_SIZE_CONFIG; |
20 | 21 | import static com.mongodb.kafka.connect.source.SourceTestHelper.TEST_COLLECTION; |
21 | 22 | import static com.mongodb.kafka.connect.source.SourceTestHelper.TEST_DATABASE; |
22 | 23 | import static com.mongodb.kafka.connect.source.SourceTestHelper.createConfigMap; |
23 | 24 | import static com.mongodb.kafka.connect.source.SourceTestHelper.createSourceConfig; |
| 25 | +import static java.lang.String.format; |
24 | 26 | import static java.util.Arrays.asList; |
25 | 27 | import static junit.framework.TestCase.assertTrue; |
26 | 28 | import static org.junit.jupiter.api.Assertions.assertEquals; |
|
31 | 33 | import static org.mockito.Mockito.when; |
32 | 34 |
|
33 | 35 | import java.util.ArrayList; |
| 36 | +import java.util.Arrays; |
34 | 37 | import java.util.HashMap; |
35 | 38 | import java.util.List; |
36 | 39 | import java.util.Map; |
37 | 40 | import java.util.Optional; |
38 | 41 | import java.util.function.Consumer; |
| 42 | +import java.util.stream.Collectors; |
| 43 | +import java.util.stream.IntStream; |
39 | 44 |
|
40 | 45 | import org.junit.jupiter.api.DisplayName; |
41 | 46 | import org.junit.jupiter.api.Test; |
@@ -111,6 +116,42 @@ void testReturnsTheExpectedCollectionResults() { |
111 | 116 | assertEquals(expected, results); |
112 | 117 | } |
113 | 118 |
|
| 119 | + @Test |
| 120 | + @DisplayName("test blocks adding docs to the queue") |
| 121 | + void testBlocksAddingResultsToTheQueue() { |
| 122 | + List<BsonDocument> docs = IntStream.range(0, 10).mapToObj(i -> |
| 123 | + BsonDocument.parse(format("{'_id': {'_id': %s, 'copy': true}}", i)) |
| 124 | + ).collect(Collectors.toList()); |
| 125 | + |
| 126 | + when(mongoClient.getDatabase(TEST_DATABASE)).thenReturn(mongoDatabase); |
| 127 | + when(mongoDatabase.getCollection(TEST_COLLECTION, BsonDocument.class)).thenReturn(mongoCollection); |
| 128 | + when(mongoCollection.aggregate(anyList())).thenReturn(aggregateIterable); |
| 129 | + doCallRealMethod().when(aggregateIterable).forEach(any(Consumer.class)); |
| 130 | + when(aggregateIterable.iterator()).thenReturn(cursor); |
| 131 | + |
| 132 | + Boolean[] hasNextResponses = new Boolean[docs.size()]; |
| 133 | + Arrays.fill(hasNextResponses, true); |
| 134 | + hasNextResponses[hasNextResponses.length - 1] = false; |
| 135 | + |
| 136 | + when(cursor.hasNext()).thenReturn(true, hasNextResponses); |
| 137 | + when(cursor.next()).thenReturn(docs.get(0), docs.subList(1, docs.size()).toArray(new BsonDocument[docs.size() - 1])); |
| 138 | + |
| 139 | + List<Optional<BsonDocument>> results; |
| 140 | + try (MongoCopyDataManager copyExistingDataManager = |
| 141 | + new MongoCopyDataManager(createSourceConfig(COPY_EXISTING_QUEUE_SIZE_CONFIG, "1"), mongoClient)) { |
| 142 | + sleep(); |
| 143 | + results = IntStream.range(0, 11).mapToObj(i -> { |
| 144 | + sleep(200); |
| 145 | + return copyExistingDataManager.poll(); |
| 146 | + } |
| 147 | + ).collect(Collectors.toList()); |
| 148 | + } |
| 149 | + |
| 150 | + List<Optional<BsonDocument>> expected = docs.stream().map(Optional::of).collect(Collectors.toList()); |
| 151 | + expected.add(Optional.empty()); |
| 152 | + assertEquals(expected, results); |
| 153 | + } |
| 154 | + |
114 | 155 | @Test |
115 | 156 | @DisplayName("test returns the expected database results") |
116 | 157 | void testReturnsTheExpectedDatabaseResults() { |
@@ -231,11 +272,15 @@ void testReturnsTheExpectedClientResults() { |
231 | 272 | assertEquals(results.get(results.size() - 1), Optional.empty()); |
232 | 273 | } |
233 | 274 |
|
234 | | - private void sleep() { |
| 275 | + private void sleep(final int millis) { |
235 | 276 | try { |
236 | | - Thread.sleep(500); |
| 277 | + Thread.sleep(millis); |
237 | 278 | } catch (InterruptedException e) { |
238 | 279 | // ignore |
239 | 280 | } |
240 | 281 | } |
| 282 | + |
| 283 | + private void sleep() { |
| 284 | + sleep(500); |
| 285 | + } |
241 | 286 | } |
0 commit comments