Skip to content

Commit

Permalink
Merge pull request #596 from sialcasa/595_fix_running_property_in_syn…
Browse files Browse the repository at this point in the history
…c_mode

#595 Fix runningProperty for DelegateCommand in sync mode
  • Loading branch information
manuel-mauky authored Aug 2, 2019
2 parents 4caff0a + 47503bb commit b5a8e6d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ public void execute() {
private ObjectProperty<State> getStateObjectPropertyReadable() {
return (ObjectProperty<State>) stateProperty();
}

/**
* In synchronous mode we need to simulate the lifecycle of the service and therefore manipulate the
* {@link Service#runningProperty()}.
*/
private BooleanProperty getRunningPropertyReadable() {
return (BooleanProperty) runningProperty();
}

protected void callActionAndSynthesizeServiceRun() {
try {
Expand All @@ -153,8 +161,11 @@ protected void callActionAndSynthesizeServiceRun() {
Action action = actionSupplier.get();
setWritableExceptionProperty(action);
bindServiceExceptionToTaskException();


getRunningPropertyReadable().set(true);
action.action();
getRunningPropertyReadable().set(false);

getStateObjectPropertyReadable().setValue(State.SUCCEEDED);
} catch (Exception e) {
setException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
Expand Down Expand Up @@ -415,4 +416,34 @@ private void sleep(long millis) {
e.printStackTrace();
}
}


@Test
void testRunningPropertyForSynchronousMode() {
CountDownLatch commandExecutedLatch = new CountDownLatch(1);
CountDownLatch listenerCalledLatch = new CountDownLatch(1);

Command command = new DelegateCommand(() -> new Action() {
@Override
protected void action() throws Exception {
commandExecutedLatch.countDown();
}
});

command.runningProperty().addListener((observable, oldValue, newValue) -> {
listenerCalledLatch.countDown();
});

// when
command.execute();

// then
Assertions.assertDoesNotThrow(() -> {
commandExecutedLatch.await(2, TimeUnit.SECONDS);
listenerCalledLatch.await(2, TimeUnit.SECONDS);
});

Assertions.assertEquals(0, commandExecutedLatch.getCount(), "Action was not executed");
Assertions.assertEquals(0, listenerCalledLatch.getCount(), "Listener was not invoked");
}
}

0 comments on commit b5a8e6d

Please sign in to comment.