Skip to content

Commit b75d6a6

Browse files
committed
test(batch): await Results event to avoid flaky tests
1 parent 40b0ade commit b75d6a6

1 file changed

Lines changed: 27 additions & 25 deletions

File tree

src/test/java/io/weaviate/client6/v1/api/collections/batch/BatchContextTest.java

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import io.weaviate.client6.v1.internal.orm.CollectionDescriptor;
3232

3333
public class BatchContextTest {
34-
private static Thread TEST_THREAD = Thread.currentThread();
34+
private static final Thread TEST_THREAD = Thread.currentThread();
3535

3636
private static final CollectionDescriptor<Map<String, Object>> DESCRIPTOR = CollectionDescriptor
3737
.ofMap("BatchContextTest");
@@ -119,12 +119,13 @@ public void test_sendOneBatch() throws Exception {
119119
Assertions.assertThat(tasks)
120120
.extracting(TaskHandle::isAcked).allMatch(CompletableFuture::isDone);
121121

122-
server.emitEvent(new Event.Results(received, Collections.emptyMap()));
122+
Future<?> results = server.emitEvent(new Event.Results(received, Collections.emptyMap()));
123123

124124
// Since MockServer runs in the same thread as this test,
125125
// the context will be updated before the last emitEvent returns.
126126
context.close();
127127

128+
results.get(); // Wait until the Results event has been processed.
128129
Assertions.assertThat(tasks).extracting(TaskHandle::result)
129130
.allMatch(CompletableFuture::isDone)
130131
.extracting(CompletableFuture::get).extracting(TaskHandle.Result::error)
@@ -152,9 +153,12 @@ public void test_drainOnClose() throws Exception {
152153
Assertions.assertThat(tasks)
153154
.extracting(TaskHandle::isAcked).allMatch(CompletableFuture::isDone);
154155

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);
158162
}
159163
});
160164

@@ -342,33 +346,28 @@ public void test_reconnect_DrainAfterStreamHangup() throws Exception {
342346

343347
// Ack the latest batch, but now Before sending back the results
344348
// 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.
345351
ack();
346352
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.
350353
tasks.add(context.add(WeaviateObject.of()));
351354

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.
355358
server.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.START);
356359
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.
366360
Future<?> backgroundAcks = EXEC.submit(() -> {
367361
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();
372371
} catch (Exception e) {
373372
throw new RuntimeException(e);
374373
}
@@ -398,7 +397,10 @@ public void test_add_closed() throws Exception {
398397
context.add(WeaviateObject.of(o -> o.properties(Map.of())));
399398
}
400399

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+
*/
402404
private List<String> ack() throws InterruptedException {
403405
WeaviateProtoBatch.BatchStreamRequest.Data data = server
404406
.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.DATA)

0 commit comments

Comments
 (0)