Skip to content

c4_LifecycleHooks - one_to_catch_them_all solution is wrong #38

@xaviibaez

Description

@xaviibaez

In the solution:

@Test
public void one_to_catch_them_all() {
    AtomicInteger hooksTriggeredCounter = new AtomicInteger(0);

    Flux<Integer> temperatureFlux = room_temperature_service()
            .doFinally(signalType -> hooksTriggeredCounter.incrementAndGet());


    StepVerifier.create(temperatureFlux.take(0))
                .expectNextCount(0)
                .verifyComplete();

    StepVerifier.create(temperatureFlux.skip(20))
                .expectNextCount(0)
                .verifyComplete();

    StepVerifier.create(temperatureFlux.skip(20)
                                       .concatWith(Flux.error(new RuntimeException("oops"))))
                .expectError()
                .verify();

    Assertions.assertEquals(hooksTriggeredCounter.get(), 3);
}

Last assert is failing because hooksTriggeredCounter.get() is equal to 2. This is happening because take(0) on first StepVerifier not working.

On Flux.take documentation says:
Take only the first N values from this Flux, if available. If n is zero, the source isn't even subscribed to and the operator completes immediately upon subscription.

As documentations says, on taking 0 the subscriber dosen't trigger.

To solve this we can:

StepVerifier.create(temperatureFlux.take(1))
                .expectNextCount(1)
                .verifyComplete();

or

Assertions.assertEquals(hooksTriggeredCounter.get(), 2);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions