Skip to content

Commit 5e5ff5c

Browse files
committed
Commandes: Unit test refactor: Replace Thread.sleep by a .block and a .zip to fix a number of event and do a deterministic test
1 parent f41a99d commit 5e5ff5c

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

commandes/src/test/java/fr/duforat/demos/commandes/handler/ReactiveWebSocketIntegrationTest.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.springframework.web.reactive.socket.client.WebSocketClient;
1313

1414
import fr.duforat.demos.commandes.CommandesApplication;
15+
import reactor.core.publisher.Flux;
1516

1617
@SpringBootTest(classes = CommandesApplication.class)
1718
public class ReactiveWebSocketIntegrationTest {
@@ -21,9 +22,7 @@ public class ReactiveWebSocketIntegrationTest {
2122
@Test
2223
public void testNotificationsOnUpdates() throws Exception {
2324

24-
long durationInterval = 800;
25-
int countEmitedEvents = 2;
26-
long margin = 900;
25+
int emitedEventsNumber = 2;
2726
AtomicLong counter = new AtomicLong();
2827
URI uri = URI.create("ws://localhost:7000/event-emitter");
2928

@@ -33,14 +32,13 @@ public void testNotificationsOnUpdates() throws Exception {
3332
.receive()
3433
.map(WebSocketMessage::getPayloadAsText)
3534
.doOnNext(str -> counter.incrementAndGet())
35+
.zipWith(Flux.range(1, emitedEventsNumber))
3636
.then()
3737
.log();
3838

39-
}).subscribe();
39+
}).block();
4040

41-
Thread.sleep(countEmitedEvents * durationInterval + margin);
42-
43-
Assertions.assertThat(counter.get()).isEqualTo(countEmitedEvents);
41+
Assertions.assertThat(counter.get()).isEqualTo(emitedEventsNumber);
4442
}
4543

4644
}

0 commit comments

Comments
 (0)