Closed wittyameta closed 6 years ago
I understand your problem but it's the nature of asynchronous execution. To solve this, in my integration test, I have the following pattern:
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean success = new AtomicBoolean(false);
//When
manager
.dsl()
.update()
.fromBaseTable()
...
.withLwtResultListener(new LWTResultListener() {
@Override
public void onSuccess() {
success.getAndSet(true);
}
@Override
public void onError(LWTResult lwtResult) {
}
})
.withResultSetAsyncListener(rs -> {
latch.countDown();
return rs;
})
.executeAsync();
//Then
latch.await();
assertThat(success.get()).isTrue();
First I declare 2 variables:
AtomicXXXX
class) that is by nature thread-safe to update the result of the asynchronous operationWith withLwtResultListener()
I passed in an instance of LWTResultListener
to set the atomic boolean value to true
However, if you only do that, the main thread will call immediately assertThat(success.get()).isTrue()
before the LWTResultListener
has a chance to be called ...
Therefore I need to add an withResultSetAsyncListener()
to trigger the latch count down when the result set is available asynchronously.
In the main execution thread, I block on latch.await()
until the latch.countDown()
is called.
You can adapt this pattern to your use-case
Thanks!
I tried using a custom ResultListener, but it doesn't always work correctly.
From my caller class, I call
isApplied()
but it seems that theonSuccess
method is called asynchronously. The caller class doesn't see the updated value ofapplied
field