Skip to content

Commit 1303422

Browse files
committed
test(batch): alias long variables
1 parent d743089 commit 1303422

2 files changed

Lines changed: 47 additions & 35 deletions

File tree

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public final class BatchContext<PropertiesT> implements Closeable {
222222
/** Executor for performing graceful shutdown sequence. */
223223
private final ExecutorService shutdownService = Executors.newSingleThreadExecutor();
224224

225-
/** Lightway check to ensure users cannot send on a closed context. */
225+
/** Lightweight check to ensure users cannot send on a closed context. */
226226
private volatile boolean closed;
227227

228228
BatchContext(
@@ -494,11 +494,14 @@ private final class Send implements Runnable {
494494

495495
@Override
496496
public void run() {
497+
String threadName = Thread.currentThread().getName();
498+
Thread.currentThread().setName("sender");
497499
try {
498500
trySend();
499501
} finally {
500502
System.out.println("sender countDown");
501503
workers.countDown();
504+
Thread.currentThread().setName(threadName);
502505
}
503506
}
504507

@@ -522,6 +525,10 @@ private void trySend() {
522525
System.out.println("took POISON");
523526
drain();
524527

528+
// FIXME(dyma): we should wait until the WIP is empty
529+
// and only then exit, in case the server restarts
530+
// after ack'ing the last batch.
531+
525532
messages.onNext(Message.stop());
526533
messages.onCompleted();
527534
return;
@@ -970,10 +977,10 @@ private void reconnectAfter(long delaySeconds) {
970977

971978
void scheduleReconnect(int reconnectIntervalSeconds) {
972979
reconnectExec.scheduleWithFixedDelay(() -> {
973-
if (Thread.currentThread().isInterrupted()) {
980+
if (!Thread.currentThread().isInterrupted()) {
974981
onEvent(Event.SHUTTING_DOWN);
975982
}
976-
if (Thread.currentThread().isInterrupted()) {
983+
if (!Thread.currentThread().isInterrupted()) {
977984
onEvent(Event.EOF);
978985
}
979986

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

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,13 @@ public static void closeExecutor() throws Exception {
128128
assert terminated : "EXEC did not terminate after 5s";
129129
}
130130

131+
private static final WeaviateProtoBatch.BatchStreamRequest.MessageCase START = WeaviateProtoBatch.BatchStreamRequest.MessageCase.START;
132+
private static final WeaviateProtoBatch.BatchStreamRequest.MessageCase STOP = WeaviateProtoBatch.BatchStreamRequest.MessageCase.STOP;
133+
private static final WeaviateProtoBatch.BatchStreamRequest.MessageCase DATA = WeaviateProtoBatch.BatchStreamRequest.MessageCase.DATA;
134+
131135
@Test
132136
public void test_sendOneBatch() throws Exception {
133-
in.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.START);
137+
in.expectMessage(START);
134138
out.emitEvent(Event.STARTED);
135139

136140
List<TaskHandle> tasks = new ArrayList<>();
@@ -161,7 +165,7 @@ public void test_sendOneBatch() throws Exception {
161165

162166
@Test
163167
public void test_drainOnClose() throws Exception {
164-
in.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.START);
168+
in.expectMessage(START);
165169
out.emitEvent(Event.STARTED);
166170

167171
List<TaskHandle> tasks = new ArrayList<>();
@@ -196,7 +200,7 @@ public void test_drainOnClose() throws Exception {
196200

197201
@Test
198202
public void test_backoff() throws Exception {
199-
in.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.START);
203+
in.expectMessage(START);
200204
out.emitEvent(Event.STARTED);
201205

202206
out.emitEvent(new Event.Backoff(BATCH_SIZE / 2));
@@ -236,7 +240,7 @@ public void test_backoff() throws Exception {
236240

237241
@Test
238242
public void test_backoffBacklog() throws Exception {
239-
in.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.START);
243+
in.expectMessage(START);
240244
out.emitEvent(Event.STARTED);
241245

242246
// Pre-fill the batch without triggering a flush (n-1).
@@ -280,14 +284,12 @@ public void test_backoffBacklog() throws Exception {
280284

281285
@Test
282286
public void test_reconnect_onShutdown() throws Exception {
283-
in.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.START);
287+
in.expectMessage(START);
284288
out.emitEvent(Event.STARTED);
285289

286290
out.emitEvent(Event.SHUTTING_DOWN);
287-
in.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.STOP);
288-
289-
// stream.eof();
290-
in.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.START);
291+
in.expectMessage(STOP);
292+
in.expectMessage(START);
291293

292294
// Not strictly necessary -- we can close the context
293295
// before a new connection is established.
@@ -296,7 +298,7 @@ public void test_reconnect_onShutdown() throws Exception {
296298

297299
@Test
298300
public void test_reconnect_onOom() throws Exception {
299-
in.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.START);
301+
in.expectMessage(START);
300302
out.emitEvent(Event.STARTED);
301303

302304
// OOM is the opposite of Ack -- trigger a flush first.
@@ -305,21 +307,21 @@ public void test_reconnect_onOom() throws Exception {
305307
}
306308

307309
// Respond with OOM and wait for the client to close its end of the stream.
308-
in.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.DATA);
310+
in.expectMessage(DATA);
309311
out.emitEvent(new Event.Oom(0));
310312

311313
// Close the server's end of the stream.
312-
in.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.STOP);
314+
in.expectMessage(STOP);
313315

314316
// Allow the client to reconnect to another "instance" and Ack the batch.
315-
in.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.START);
317+
in.expectMessage(START);
316318
out.emitEvent(Event.STARTED);
317319
recvDataAndAck();
318320
}
319321

320322
@Test
321323
public void test_reconnect_onStreamHangup() throws Exception {
322-
in.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.START);
324+
in.expectMessage(START);
323325
out.emitEvent(Event.STARTED);
324326

325327
// Trigger a flush.
@@ -328,12 +330,12 @@ public void test_reconnect_onStreamHangup() throws Exception {
328330
}
329331

330332
// Expect a new batch to arrive. Hangup the stream before sending the Acks.
331-
in.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.DATA);
333+
in.expectMessage(DATA);
332334
System.out.println("hangup the first time");
333335
out.hangup();
334336

335337
// The client should try to reconnect, because the context is still open.
336-
in.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.START);
338+
in.expectMessage(START);
337339
out.emitEvent(Event.STARTED);
338340

339341
// The previous batch hasn't been acked, so we should expect to receive it
@@ -344,7 +346,7 @@ public void test_reconnect_onStreamHangup() throws Exception {
344346
// item from the queue. Hangup the stream again, and add put another object
345347
// in the queue to wake the sender up.
346348
out.hangup();
347-
in.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.START);
349+
in.expectMessage(START);
348350
out.emitEvent(Event.STARTED);
349351
context.add(WeaviateObject.of());
350352
recvDataAndAck();
@@ -358,7 +360,7 @@ public void test_reconnect_onStreamHangup() throws Exception {
358360

359361
@Test
360362
public void test_reconnect_DrainAfterStreamHangup() throws Exception {
361-
in.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.START);
363+
in.expectMessage(START);
362364
out.emitEvent(Event.STARTED);
363365

364366
List<TaskHandle> tasks = new ArrayList<>();
@@ -385,7 +387,7 @@ public void test_reconnect_DrainAfterStreamHangup() throws Exception {
385387
// The client will try to reconnect, because the context is still open.
386388
// When the server starts accepting connections again, the client should
387389
// drain the remaining BATCH_SIZE+1 objects as we close the context.
388-
in.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.START);
390+
in.expectMessage(START);
389391
out.emitEvent(Event.STARTED);
390392
Future<?> backgroundAcks = BACKGROUND.submit(() -> {
391393
try {
@@ -414,22 +416,22 @@ public void test_reconnect_DrainAfterStreamHangup() throws Exception {
414416

415417
@Test
416418
public void test_closeAfterStreamHangup() throws Exception {
417-
in.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.START);
419+
in.expectMessage(START);
418420
out.emitEvent(Event.STARTED);
419421

420422
out.hangup();
421423
}
422424

423425
@Test
424426
public void test_maxReconnectRetries() throws Exception {
425-
in.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.START);
427+
in.expectMessage(START);
426428

427429
// Drop the connection several times until the client
428430
// exhausts its reconnect attempts.
429431
int retries = 0;
430432
while (retries < MAX_RECONNECT_RETRIES) {
431433
out.hangup();
432-
in.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.START);
434+
in.expectMessage(START);
433435
retries++;
434436
}
435437

@@ -447,14 +449,14 @@ public void test_maxReconnectRetries() throws Exception {
447449

448450
@Test(expected = IllegalStateException.class)
449451
public void test_add_closed() throws Exception {
450-
in.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.START);
452+
in.expectMessage(START);
451453
context.close();
452454
context.add(WeaviateObject.of(o -> o.properties(Map.of())));
453455
}
454456

455457
@Test(expected = IllegalStateException.class)
456458
public void test_startAfterClose() throws Exception {
457-
in.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.START);
459+
in.expectMessage(START);
458460
context.close();
459461
context.start();
460462
}
@@ -464,15 +466,18 @@ public void test_startAfterClose() throws Exception {
464466
* This method does not wait for the server to process the Acks.
465467
*/
466468
private List<String> recvDataAndAck() throws InterruptedException {
467-
WeaviateProtoBatch.BatchStreamRequest.Data data = in
468-
.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.DATA)
469-
.getData();
470-
List<String> ids = Stream.concat(
469+
List<String> received = recvData();
470+
out.emitEvent(new Event.Acks(received));
471+
return received;
472+
}
473+
474+
/** Read the next Data message from the stream. */
475+
private List<String> recvData() throws InterruptedException {
476+
WeaviateProtoBatch.BatchStreamRequest.Data data = in.expectMessage(DATA).getData();
477+
return Stream.concat(
471478
data.getObjects().getValuesList().stream().map(WeaviateProtoBatch.BatchObject::getUuid),
472479
data.getReferences().getValuesList().stream().map(BatchContextTest::getBeacon))
473480
.toList();
474-
out.emitEvent(new Event.Acks(ids));
475-
return ids;
476481
}
477482

478483
private void awaitResults(Collection<TaskHandle> tasks) throws Exception {
@@ -489,7 +494,7 @@ static String getBeacon(WeaviateProtoBatch.BatchReference reference) {
489494
}
490495

491496
/** OutboundStream is a mock which dispatches server-side events. */
492-
private final class OutboundStream {
497+
private static final class OutboundStream {
493498
private final StreamObserver<Event> stream;
494499

495500
OutboundStream(StreamObserver<Event> stream) {
@@ -515,7 +520,7 @@ void eof() {
515520
}
516521

517522
/** InboundStream is a spy which collects incoming messages in a queue. */
518-
private final class InboundStream implements StreamObserver<Message> {
523+
private static final class InboundStream implements StreamObserver<Message> {
519524
/**
520525
* Block until the next message arrives on the stream.
521526
* When it does, assert it's of the expected type.

0 commit comments

Comments
 (0)