sialcasa / mvvmFX

an Application Framework for implementing the MVVM Pattern with JavaFX
Apache License 2.0
495 stars 104 forks source link

runningProperty on DelegateCommand does not change #595

Open pbauerochse opened 5 years ago

pbauerochse commented 5 years ago

Hi there,

I have a non-background DelegateCommand and tried to attach a listener to the runningProperty but it does not get called, when i start the command via the execute() method. Is this intended behaviour?

I'm using MvvmFx Version 1.8.0. Here's a unit-test to verify the behaviour:

@Test
void test() {
    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");
}

Cheers

Patrick

manuel-mauky commented 5 years ago

Thanks for the bugreport and the test-case. This is a bug. In the past we were using DelegateCommand in background-mode most of the time and didn't use the runningProperty when in synchronous mode so we missed this. However, of cause this should work an I've fixed it with #596 (many thanks for the unit test. It made fixing it much faster).