@@ -135,11 +135,16 @@ public void startContext() throws InterruptedException {
135135 context .start ();
136136
137137 in .expectMessage (START );
138- out .emitEvent (Event .STARTED );
138+ out .emitEventAsync (Event .STARTED );
139139 }
140140
141141 @ After
142142 public void reset () throws Exception {
143+ log .atDebug ()
144+ .addKeyValue ("contextClosed" , contextClosed )
145+ .addKeyValue ("testFailed" , testFailed )
146+ .log ("Begin test cleanup" );
147+
143148 // Do not attempt to close the context if it has been previously closed
144149 // by the test or the test has failed. In the latter case closing the
145150 // context may lead to a deadlock if the case hasn't scheduled Results
@@ -302,6 +307,7 @@ public void test_backoffBacklog() throws Exception {
302307 int batchSizeNew = BATCH_SIZE / 2 ;
303308
304309 // Force the last BATCH_SIZE / 2 - 1 items to be transferred to the backlog.
310+ // Await for this event to be processed before moving forward.
305311 out .emitEvent (new Event .Backoff (batchSizeNew ));
306312
307313 // The next item will go on the backlog and the trigger a flush,
@@ -327,14 +333,14 @@ public void test_backoffBacklog() throws Exception {
327333
328334 @ Test
329335 public void test_reconnect_onShutdown () throws Exception {
330- out .emitEvent (Event .SHUTTING_DOWN );
336+ out .emitEventAsync (Event .SHUTTING_DOWN );
331337 in .expectMessage (STOP );
332338 out .eof (true );
333339 in .expectMessage (START );
334340
335341 // Not strictly necessary -- we can close the context
336342 // before a new connection is established.
337- out .emitEvent (Event .STARTED );
343+ out .emitEventAsync (Event .STARTED );
338344 }
339345
340346 @ Test
@@ -348,18 +354,18 @@ public void test_reconnect_onOom() throws Exception {
348354
349355 // Respond with OOM and wait for the client to close its end of the stream.
350356 in .expectMessage (DATA );
351- out .emitEvent (new Event .Oom (0 ));
357+ out .emitEventAsync (new Event .Oom (0 ));
352358
353359 // Close the server's end of the stream.
354360 in .expectMessage (STOP );
355361
356362 // Allow the client to reconnect to another "instance" and Ack the batch.
357363 in .expectMessage (START );
358- out .emitEvent (Event .STARTED );
364+ out .emitEventAsync (Event .STARTED );
359365 recvDataAndAck ();
360366
361367 List <String > submitted = tasks .stream ().map (TaskHandle ::id ).toList ();
362- out .emitEvent (new Event .Results (submitted , Collections .emptyMap ()));
368+ out .emitEventAsync (new Event .Results (submitted , Collections .emptyMap ()));
363369 }
364370
365371 @ Test
@@ -376,7 +382,7 @@ public void test_reconnect_onStreamHangup() throws Exception {
376382
377383 // The client should try to reconnect, because the context is still open.
378384 in .expectMessage (START );
379- out .emitEvent (Event .STARTED );
385+ out .emitEventAsync (Event .STARTED );
380386
381387 // The previous batch hasn't been acked, so we should expect to receive it
382388 // again.
@@ -387,7 +393,7 @@ public void test_reconnect_onStreamHangup() throws Exception {
387393 // in the queue to wake the sender up.
388394 out .hangup ();
389395 in .expectMessage (START );
390- out .emitEvent (Event .STARTED );
396+ out .emitEventAsync (Event .STARTED );
391397 tasks .add (context .add (WeaviateObject .of ()));
392398 recvDataAndAck ();
393399
@@ -428,7 +434,7 @@ public void test_reconnect_DrainAfterStreamHangup() throws Exception {
428434 // When the server starts accepting connections again, the client should
429435 // drain the remaining BATCH_SIZE+1 objects as we close the context.
430436 in .expectMessage (START );
431- out .emitEvent (Event .STARTED );
437+ out .emitEventAsync (Event .STARTED );
432438 Future <?> backgroundAcks = backgroundThread .submit (() -> {
433439 try {
434440 recvDataAndAck ();
@@ -455,7 +461,7 @@ public void test_reconnect_DrainAfterStreamHangup() throws Exception {
455461 public void test_closeAfterStreamHangup () throws Exception {
456462 out .hangup ();
457463 in .expectMessage (START );
458- out .emitEvent (Event .STARTED );
464+ out .emitEventAsync (Event .STARTED );
459465 }
460466
461467 @ Test
@@ -493,7 +499,7 @@ public void test_startAfterClose() throws Exception {
493499 */
494500 private List <String > recvDataAndAck () throws InterruptedException {
495501 List <String > received = recvData ();
496- out .emitEvent (new Event .Acks (received ));
502+ out .emitEventAsync (new Event .Acks (received ));
497503 return received ;
498504 }
499505
@@ -521,7 +527,16 @@ private static final class OutboundStream {
521527 this .eventThread = eventThread ;
522528 }
523529
524- CompletableFuture <Void > emitEvent (Event event ) {
530+ /** Emit event on the current thread. */
531+ void emitEvent (Event event ) {
532+ assert event != Event .EOF : "must not use synthetic EOF event" ;
533+ assert !(event instanceof Event .StreamHangup ) : "must not use synthetic StreamHangup event" ;
534+
535+ stream .onNext (event );
536+ }
537+
538+ /** Emit event on the {@link #eventThread}. */
539+ CompletableFuture <Void > emitEventAsync (Event event ) {
525540 assert event != Event .EOF : "must not use synthetic EOF event" ;
526541 assert !(event instanceof Event .StreamHangup ) : "must not use synthetic StreamHangup event" ;
527542
@@ -551,7 +566,7 @@ CompletableFuture<Void> eof(boolean ok) {
551566 if (ok ) {
552567 // These are guaranteed to finish before onCompleted,
553568 // as eventThread is just 1 thread.
554- pendingEvents .forEach (this ::emitEvent );
569+ pendingEvents .forEach (this ::emitEventAsync );
555570 }
556571 return CompletableFuture .runAsync (stream ::onCompleted , eventThread );
557572 }
0 commit comments