Skip to content

Commit 4ede901

Browse files
committed
test(batch): test ClientError flow triggered by exhausted reconect retries
1 parent b75d6a6 commit 4ede901

2 files changed

Lines changed: 49 additions & 23 deletions

File tree

src/main/java/io/weaviate/client6/v1/api/collections/batch/BatchContext.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ public TaskHandle add(BatchReference reference) throws InterruptedException {
195195
}
196196

197197
void start() {
198-
System.out.println("RESET COUNTDOWN LATCH to [2]");
199198
workers = new CountDownLatch(2);
200199

201200
messages = streamFactory.createStream(new Recv());
@@ -216,7 +215,6 @@ void start() {
216215
* is reached.
217216
*/
218217
void reconnect() throws InterruptedException, ExecutionException {
219-
System.out.println("RESET COUNTDOWN LATCH to [1]");
220218
workers = new CountDownLatch(2);
221219

222220
messages = streamFactory.createStream(new Recv());
@@ -296,6 +294,10 @@ private void shutdown() {
296294
}
297295

298296
private void shutdownNow(Exception ex) {
297+
// Now report this error to the server and close the stream.
298+
closing.completeExceptionally(ex);
299+
messages.onError(Status.INTERNAL.withCause(ex).asRuntimeException());
300+
299301
// Terminate the "send" routine and wait for it to exit.
300302
// Since we're already in the error state we do not care
301303
// much if it throws or not.
@@ -305,11 +307,7 @@ private void shutdownNow(Exception ex) {
305307
} catch (Exception e) {
306308
}
307309

308-
// Now report this error to the server and close the stream.
309-
closing.completeExceptionally(ex);
310-
messages.onError(Status.INTERNAL.withCause(ex).asRuntimeException());
311-
312-
// Since shutdownNow is never triggerred by the "main" thread,
310+
// Since shutdownNow is never triggered by the "main" thread,
313311
// it may be blocked on trying to add to the queue. While batch
314312
// context is active, we own this thread and may interrupt it.
315313
parent.interrupt();
@@ -434,7 +432,10 @@ private void trySend() {
434432
if (task == TaskHandle.POISON) {
435433
System.out.println("took POISON");
436434
drain();
437-
break;
435+
436+
messages.onNext(Message.stop());
437+
messages.onCompleted();
438+
return;
438439
}
439440

440441
Data data = task.data();
@@ -447,11 +448,7 @@ private void trySend() {
447448
Thread.currentThread().interrupt();
448449
} catch (Exception e) {
449450
onEvent(new Event.ClientError(e));
450-
return;
451451
}
452-
453-
messages.onNext(Message.stop());
454-
messages.onCompleted();
455452
}
456453

457454
/**
@@ -817,8 +814,6 @@ public void onEnter(State prev) {
817814
return;
818815
}
819816

820-
// send.cancel(true);
821-
822817
if (!ServerShuttingDown.class.isAssignableFrom(prev.getClass())) {
823818
// This is NOT an orderly shutdown, we're reconnecting after a stream hangup.
824819
// Assume all WIP items have been lost and re-submit everything.

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

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static java.util.Objects.requireNonNull;
44

5+
import java.io.IOException;
56
import java.util.ArrayList;
67
import java.util.Collections;
78
import java.util.List;
@@ -57,7 +58,7 @@ public class BatchContextTest {
5758
private static final int MAX_SIZE_BYTES = 2 * 1024;
5859
private static final int BATCH_SIZE = 10;
5960
private static final int QUEUE_SIZE = 1;
60-
private static final int MAX_RECONNECT_RETRIES = 2;
61+
private static final int MAX_RECONNECT_RETRIES = 1;
6162

6263
private CompletableStreamFactory factory;
6364
private MockServer server;
@@ -390,6 +391,41 @@ public void test_closeAfterStreamHangup() throws Exception {
390391
server.hangup();
391392
}
392393

394+
@Test
395+
public void test_maxReconnectRetries() throws Exception {
396+
server.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.START);
397+
398+
// Drop the connection several times until the client exhausts its reconnect attempts.
399+
int retries = 0;
400+
while (retries < MAX_RECONNECT_RETRIES) {
401+
server.hangup();
402+
server.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.START);
403+
retries++;
404+
}
405+
406+
Future<?> fatalHangup = server.hangup();
407+
try {
408+
fatalHangup.get();
409+
} catch (InterruptedException ignored) {
410+
// BatchContext#shutdownNow might interrupt the parent thread.
411+
}
412+
413+
Assertions.assertThatThrownBy(() -> context.close())
414+
.isInstanceOf(IOException.class)
415+
.hasMessageContaining("Server unavailable");
416+
417+
// Cleanup: unset the context and factory to prevent test teardown code
418+
// from tripping on it while trying to close the context.
419+
context = null;
420+
421+
try {
422+
factory.close();
423+
} catch (InterruptedException ignored) {
424+
} finally {
425+
factory = null;
426+
}
427+
}
428+
393429
@Test(expected = IllegalStateException.class)
394430
public void test_add_closed() throws Exception {
395431
server.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.START);
@@ -465,12 +501,7 @@ public MockServer(
465501
BlockingQueue<WeaviateProtoBatch.BatchStreamRequest> requestQueue) {
466502
this.eventStream = requireNonNull(eventStream, "eventStream is null");
467503
this.requestQueue = requireNonNull(requestQueue, "requestQueue is null");
468-
this.eventExecutor = eventExecutor;
469-
}
470-
471-
/** Wait until the next request arrives. */
472-
WeaviateProtoBatch.BatchStreamRequest recv() throws InterruptedException {
473-
return requestQueue.take();
504+
this.eventExecutor = requireNonNull(eventExecutor, "eventExecutor is null");
474505
}
475506

476507
Future<?> emitEvent(Event event) {
@@ -492,8 +523,8 @@ Future<?> emitEvent(Event event) {
492523
}
493524

494525
/** Terminate the server-side of the stream abruptly. */
495-
void hangup() {
496-
emitEvent(new Event.StreamHangup(new RuntimeException("whaam!")));
526+
Future<?> hangup() {
527+
return emitEvent(new Event.StreamHangup(new RuntimeException("whaam!")));
497528
}
498529

499530
/**

0 commit comments

Comments
 (0)