|
31 | 31 | import io.weaviate.client6.v1.internal.orm.CollectionDescriptor; |
32 | 32 |
|
33 | 33 | public class BatchContextTest { |
34 | | - private static Thread TEST_THREAD = Thread.currentThread(); |
| 34 | + private static final Thread TEST_THREAD = Thread.currentThread(); |
35 | 35 |
|
36 | 36 | private static final CollectionDescriptor<Map<String, Object>> DESCRIPTOR = CollectionDescriptor |
37 | 37 | .ofMap("BatchContextTest"); |
@@ -119,12 +119,13 @@ public void test_sendOneBatch() throws Exception { |
119 | 119 | Assertions.assertThat(tasks) |
120 | 120 | .extracting(TaskHandle::isAcked).allMatch(CompletableFuture::isDone); |
121 | 121 |
|
122 | | - server.emitEvent(new Event.Results(received, Collections.emptyMap())); |
| 122 | + Future<?> results = server.emitEvent(new Event.Results(received, Collections.emptyMap())); |
123 | 123 |
|
124 | 124 | // Since MockServer runs in the same thread as this test, |
125 | 125 | // the context will be updated before the last emitEvent returns. |
126 | 126 | context.close(); |
127 | 127 |
|
| 128 | + results.get(); // Wait until the Results event has been processed. |
128 | 129 | Assertions.assertThat(tasks).extracting(TaskHandle::result) |
129 | 130 | .allMatch(CompletableFuture::isDone) |
130 | 131 | .extracting(CompletableFuture::get).extracting(TaskHandle.Result::error) |
@@ -152,9 +153,12 @@ public void test_drainOnClose() throws Exception { |
152 | 153 | Assertions.assertThat(tasks) |
153 | 154 | .extracting(TaskHandle::isAcked).allMatch(CompletableFuture::isDone); |
154 | 155 |
|
155 | | - server.emitEvent(new Event.Results(received, Collections.emptyMap())); |
156 | | - } catch (InterruptedException e) { |
157 | | - throw new RuntimeException("mock server interrupted", e); |
| 156 | + // Wait until the Results event's been processed to guarantee |
| 157 | + // that the tasks' futures are completed before asserting. |
| 158 | + Future<?> results = server.emitEvent(new Event.Results(received, Collections.emptyMap())); |
| 159 | + results.get(); |
| 160 | + } catch (Exception e) { |
| 161 | + throw new RuntimeException(e); |
158 | 162 | } |
159 | 163 | }); |
160 | 164 |
|
@@ -342,33 +346,28 @@ public void test_reconnect_DrainAfterStreamHangup() throws Exception { |
342 | 346 |
|
343 | 347 | // Ack the latest batch, but now Before sending back the results |
344 | 348 | // for either one, hang up the stream. |
| 349 | + // On hangup, client should re-populate the batch from the WIP buffer. |
| 350 | + // Add one more item to overflow, so that there are now 3 pending batches. |
345 | 351 | ack(); |
346 | 352 | server.hangup(); |
347 | | - |
348 | | - // On hangup, client should re-populate the batch from the WIP buffer. |
349 | | - // Add one more item to trigger the flush. |
350 | 353 | tasks.add(context.add(WeaviateObject.of())); |
351 | 354 |
|
352 | | - // The client should try to reconnect, because the context is still open. |
353 | | - // Once the server starts accepting connections again, the client will |
354 | | - // flush the 2 full batches and pause, waiting for the next event. |
| 355 | + // The client will try to reconnect, because the context is still open. |
| 356 | + // When the server starts accepting connections again, the client should |
| 357 | + // drain the remaining BATCH_SIZE+1 objects as we close the context. |
355 | 358 | server.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.START); |
356 | 359 | server.emitEvent(Event.STARTED); |
357 | | - |
358 | | - List<String> ids = ack(); |
359 | | - server.emitEvent(new Event.Results(ids, Collections.emptyMap())); |
360 | | - |
361 | | - ids = ack(); |
362 | | - server.emitEvent(new Event.Results(ids, Collections.emptyMap())); |
363 | | - |
364 | | - // There is now 1 item remaining in the batch. |
365 | | - // On context close, the client will drain it. |
366 | 360 | Future<?> backgroundAcks = EXEC.submit(() -> { |
367 | 361 | try { |
368 | | - List<String> id = ack(); |
369 | | - Assertions.assertThat(id).as("drained item").hasSize(1); |
370 | | - Future<?> applied = server.emitEvent(new Event.Results(id, Collections.emptyMap())); |
371 | | - // applied.get(); |
| 362 | + List<String> ids = ack(); |
| 363 | + server.emitEvent(new Event.Results(ids, Collections.emptyMap())); |
| 364 | + |
| 365 | + ids = ack(); |
| 366 | + server.emitEvent(new Event.Results(ids, Collections.emptyMap())); |
| 367 | + |
| 368 | + ids = ack(); |
| 369 | + Future<?> lastEvent = server.emitEvent(new Event.Results(ids, Collections.emptyMap())); |
| 370 | + lastEvent.get(); |
372 | 371 | } catch (Exception e) { |
373 | 372 | throw new RuntimeException(e); |
374 | 373 | } |
@@ -398,7 +397,10 @@ public void test_add_closed() throws Exception { |
398 | 397 | context.add(WeaviateObject.of(o -> o.properties(Map.of()))); |
399 | 398 | } |
400 | 399 |
|
401 | | - /** Read the next Data message from the stream and ACK it. */ |
| 400 | + /** |
| 401 | + * Read the next Data message from the stream and ACK it. |
| 402 | + * This method does not wait for the server to process the Acks. |
| 403 | + */ |
402 | 404 | private List<String> ack() throws InterruptedException { |
403 | 405 | WeaviateProtoBatch.BatchStreamRequest.Data data = server |
404 | 406 | .expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.DATA) |
|
0 commit comments