Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,20 @@ public Subscription executeBatch(final List<BatchTask> tasks, final Observer<Bat
return new Subscription() {
@Override
public void close() {
if (internalObserver.doRelease()) {
if (internalObserver.doRelease() && subscription != null) {
subscription.close();
}
}

@Override
public boolean isUnsubscribed() {
return internalObserver.isUnsubscribed() && subscription.isUnsubscribed();
return internalObserver.isUnsubscribed()
&& (subscription == null || subscription.isUnsubscribed());
}

@Override
public Object getReturnValue() {
return subscription.getReturnValue();
return subscription == null ? null : subscription.getReturnValue();
}
};
} catch (Throwable t) {
Expand All @@ -195,14 +196,15 @@ public Subscription queryBatch(final BatchToken token, final Observer<BatchResul
return new Subscription() {
@Override
public void close() {
if (internalObserver.doRelease()) {
if (internalObserver.doRelease() && subscription != null) {
subscription.close();
}
}

@Override
public boolean isUnsubscribed() {
return internalObserver.isUnsubscribed() && subscription.isUnsubscribed();
return internalObserver.isUnsubscribed()
&& (subscription == null || subscription.isUnsubscribed());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
Expand Down Expand Up @@ -247,11 +248,16 @@ public void onNext(BatchResult batchResult) {
}
};

Subscription sub = facade.executeBatch(batch.build(), observer, options);
assertEquals(results.size(), batch.build().size());
assertTrue(isComplete.get());
assertFalse(hasError.get());
assertNull(sub.getReturnValue());
final Subscription sub = facade.executeBatch(batch.build(), observer, options);
try {
assertEquals(results.size(), batch.build().size());
assertTrue(isComplete.get());
assertFalse(hasError.get());
assertNull(sub.getReturnValue());
} finally {
// the subscription holds a pooled connector until it is closed
sub.close();
}
}

@Test
Expand Down Expand Up @@ -298,19 +304,27 @@ public void onNext(BatchResult batchResult) {
}
};

Subscription sub = facade.executeBatch(batch.build(), observer, options);
assertEquals(results.size(), 0);
assertFalse(isComplete.get());
assertFalse(hasError.get());
assertNotNull(sub.getReturnValue());

Thread.sleep(500);
sub = facade.queryBatch((BatchToken) sub.getReturnValue(), observer, options);

assertEquals(results.size(), batch.build().size());
assertTrue(isComplete.get());
assertFalse(hasError.get());
assertNull(sub.getReturnValue());
final Subscription sub = facade.executeBatch(batch.build(), observer, options);
try {
assertEquals(results.size(), 0);
assertFalse(isComplete.get());
assertFalse(hasError.get());
assertNotNull(sub.getReturnValue());

Thread.sleep(500);
final Subscription query = facade.queryBatch((BatchToken) sub.getReturnValue(), observer, options);
try {
assertEquals(results.size(), batch.build().size());
assertTrue(isComplete.get());
assertFalse(hasError.get());
assertNull(query.getReturnValue());
} finally {
query.close();
}
} finally {
// the subscription holds a pooled connector until it is closed
sub.close();
}
}

@Test
Expand Down Expand Up @@ -358,19 +372,27 @@ public void onNext(BatchResult batchResult) {
}
};

Subscription sub = facade.executeBatch(batch.build(), observer, options);
assertEquals(results.size(), 0);
assertFalse(isComplete.get());
assertFalse(hasError.get());
assertNotNull(sub.getReturnValue());

Thread.sleep(500);
sub = facade.queryBatch((BatchToken) sub.getReturnValue(), observer, options);

assertEquals(results.size(), 2);
assertFalse(isComplete.get());
assertTrue(hasError.get());
assertNull(sub.getReturnValue());
final Subscription sub = facade.executeBatch(batch.build(), observer, options);
try {
assertEquals(results.size(), 0);
assertFalse(isComplete.get());
assertFalse(hasError.get());
assertNotNull(sub.getReturnValue());

Thread.sleep(500);
final Subscription query = facade.queryBatch((BatchToken) sub.getReturnValue(), observer, options);
try {
assertEquals(results.size(), 2);
assertFalse(isComplete.get());
assertTrue(hasError.get());
assertNull(query.getReturnValue());
} finally {
query.close();
}
} finally {
// the subscription holds a pooled connector until it is closed
sub.close();
}
}

@Test
Expand All @@ -384,7 +406,7 @@ public void testBatchUseCase3() throws Exception {
api.setProducerBufferSize(0);

ConnectorFacadeFactory facf = ConnectorFacadeFactory.getInstance();
ConnectorFacade facade = facf.newInstance(api);
final ConnectorFacade facade = facf.newInstance(api);

OperationOptionsBuilder builder = new OperationOptionsBuilder();
builder.setOption(OperationOptions.OP_FAIL_ON_ERROR, true);
Expand All @@ -399,17 +421,45 @@ public void testBatchUseCase3() throws Exception {
final List<Object> results = new ArrayList<Object>();
final AtomicBoolean isComplete = new AtomicBoolean(false);
final AtomicBoolean hasError = new AtomicBoolean(false);
final AtomicReference<BatchToken> lastToken = new AtomicReference<BatchToken>();
final AtomicReference<Object> queryOnCompleted = new AtomicReference<Object>("not called");
final AtomicReference<Throwable> queryOnCompletedError = new AtomicReference<Throwable>();

Observer<BatchResult> observer = new Observer<BatchResult>() {
public void onCompleted() {
isComplete.set(true);
// runs on the processor thread inside its onCompleted() call: if the connector
// marked the token complete only after that call, queryBatch() would see it incomplete
try {
final Subscription query = facade.queryBatch(lastToken.get(), new Observer<BatchResult>() {
public void onCompleted() {
}

public void onError(Throwable e) {
}

public void onNext(BatchResult batchResult) {
}
}, options);
try {
queryOnCompleted.set(query.getReturnValue());
} finally {
query.close();
}
} catch (Throwable t) {
// an exception here would reach the processor's catch, whose onError is dropped
// once the observer is released, so record it for the test thread instead
queryOnCompletedError.set(t);
} finally {
isComplete.set(true);
}
}

public void onError(Throwable e) {
hasError.set(true);
}

public void onNext(BatchResult batchResult) {
lastToken.set(batchResult.getToken());
results.add(batchResult);
if (batchResult.getError()) {
hasError.set(true);
Expand All @@ -418,20 +468,35 @@ public void onNext(BatchResult batchResult) {
};

final List<BatchTask> batchTasks = batch.build();
Subscription sub = facade.executeBatch(batchTasks, observer, options);
assertNotNull(sub.getReturnValue());

final long timeout = System.currentTimeMillis() + 3000;
while (!isComplete.get() && System.currentTimeMillis() < timeout) {
Thread.sleep(100);
}
final Subscription sub = facade.executeBatch(batchTasks, observer, options);
try {
assertNotNull(sub.getReturnValue());

assertEquals(results.size(), batchTasks.size());
assertTrue(isComplete.get());
assertFalse(hasError.get());
final long timeout = System.currentTimeMillis() + 3000;
while (!isComplete.get() && System.currentTimeMillis() < timeout) {
Thread.sleep(100);
}

sub = facade.queryBatch((BatchToken) sub.getReturnValue(), observer, options);
assertNull(sub.getReturnValue());
assertEquals(results.size(), batchTasks.size());
assertTrue(isComplete.get());
assertFalse(hasError.get());
assertNull(queryOnCompletedError.get());
// a queryBatch() made from onCompleted() finds the batch complete
assertNull(queryOnCompleted.get());

final Subscription query = facade.queryBatch((BatchToken) sub.getReturnValue(), observer, options);
try {
assertNull(query.getReturnValue());
} finally {
query.close();
}
// the connector returned no subscription for the completed batch, and close()
// released the observer, so this checks the wrapper's null-subscription path
assertTrue(query.isUnsubscribed());
} finally {
// the subscription holds a pooled connector until it is closed
sub.close();
}
}

@Test
Expand Down Expand Up @@ -479,17 +544,22 @@ public void onNext(BatchResult batchResult) {
}
};

Subscription sub = facade.executeBatch(batch.build(), observer, options);
assertEquals(results.size(), 0);
assertFalse(isComplete.get());
assertFalse(hasError.get());
assertNotNull(sub.getReturnValue());
final Subscription sub = facade.executeBatch(batch.build(), observer, options);
try {
assertNotNull(sub.getReturnValue());

Thread.sleep(500);
final long timeout = System.currentTimeMillis() + 3000;
while (!hasError.get() && System.currentTimeMillis() < timeout) {
Thread.sleep(100);
}

assertEquals(results.size(), 2);
assertFalse(isComplete.get());
assertTrue(hasError.get());
assertEquals(results.size(), 2);
assertFalse(isComplete.get());
assertTrue(hasError.get());
} finally {
// the subscription holds a pooled connector until it is closed
sub.close();
}
}

@Test
Expand Down Expand Up @@ -539,11 +609,16 @@ public void onNext(BatchResult batchResult) {
}
};

Subscription sub = facade.executeBatch(batch.build(), observer, options);
assertEquals(results.size(), 2);
//Batch process is complete but handler failed to receive all
assertTrue(hasError.get() ^ isComplete.get());
assertNull(sub.getReturnValue());
assertTrue(sub.isUnsubscribed());
final Subscription sub = facade.executeBatch(batch.build(), observer, options);
try {
assertEquals(results.size(), 2);
//Batch process is complete but handler failed to receive all
assertTrue(hasError.get() ^ isComplete.get());
assertNull(sub.getReturnValue());
assertTrue(sub.isUnsubscribed());
} finally {
// the subscription holds a pooled connector until it is closed
sub.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,6 @@ public Schema schema() {
return builder.build();
}

BatchUseCase3Processor processorUseCase3 = null;

@Override
public Subscription executeBatch(final List<BatchTask> tasks, final Observer<BatchResult> observer,
final OperationOptions options) {
Expand Down Expand Up @@ -268,8 +266,7 @@ public Object getReturnValue() {
}
};
} else if (options.getOptions().containsKey("TEST_USECASE3")) {
processorUseCase3 = new BatchUseCase3Processor();
final BatchToken token = processorUseCase3.executeBatch(tasks, options, observer);
final BatchToken token = new BatchUseCase3Processor().executeBatch(tasks, options, observer);

return new Subscription() {
@Override
Expand Down Expand Up @@ -488,8 +485,10 @@ public void run() {
}
}
if (complete) {
observer.onCompleted();
// mark the token complete first: an observer that saw onCompleted() may
// call queryBatch() at once and must find the batch complete
BatchRemoteCache.setComplete(token);
observer.onCompleted();
}
} catch (Exception e) {
// interrupted
Expand All @@ -498,7 +497,6 @@ public void run() {
}
}
BatchRemoteCache.flushTasks(token);
processorUseCase3 = null;
}
}
}
Loading