|
| 1 | +package com.marklogic.client.test.datamovement; |
| 2 | + |
| 3 | +import com.marklogic.client.datamovement.DataMovementManager; |
| 4 | +import com.marklogic.client.datamovement.WriteBatcher; |
| 5 | +import com.marklogic.client.io.DocumentMetadataHandle; |
| 6 | +import com.marklogic.client.io.StringHandle; |
| 7 | +import com.marklogic.client.test.Common; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +import java.util.concurrent.TimeUnit; |
| 11 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 14 | + |
| 15 | +public class AwaitCompletionTest { |
| 16 | + |
| 17 | + @Test |
| 18 | + void test() throws Exception { |
| 19 | + DataMovementManager dmm = Common.newClient().newDataMovementManager(); |
| 20 | + AtomicBoolean listenerCompleted = new AtomicBoolean(false); |
| 21 | + WriteBatcher writeBatcher = dmm.newWriteBatcher().withBatchSize(1).onBatchSuccess(batch -> { |
| 22 | + try { |
| 23 | + // Intended to last longer than the duration passed to writeBacher.awaitCompletion. |
| 24 | + Thread.sleep(10000); |
| 25 | + listenerCompleted.set(true); |
| 26 | + } catch (InterruptedException e) { |
| 27 | + listenerCompleted.set(false); |
| 28 | + } |
| 29 | + }); |
| 30 | + dmm.startJob(writeBatcher); |
| 31 | + |
| 32 | + writeBatcher.add("/0doesnt-matter.xml", new DocumentMetadataHandle() |
| 33 | + .withPermission("rest-reader", DocumentMetadataHandle.Capability.READ, DocumentMetadataHandle.Capability.UPDATE), |
| 34 | + new StringHandle("<test/>")); |
| 35 | + writeBatcher.flushAsync(); |
| 36 | + writeBatcher.awaitCompletion(2, TimeUnit.SECONDS); |
| 37 | + dmm.stopJob(writeBatcher); |
| 38 | + |
| 39 | + assertFalse(listenerCompleted.get(), "The batcher should have waited 2 seconds for the batch listener to " + |
| 40 | + "completed, which should not occur since the listener is sleeping for 10 seconds. This ensures that a bug " + |
| 41 | + "is fixed where the duration passed to 'awaitCompletion' was mishandled and always resulted in a " + |
| 42 | + "duration of 0 seconds, which means 'wait until all batches are completed'."); |
| 43 | + } |
| 44 | +} |
0 commit comments