Skip to content

Commit e6b9fa3

Browse files
committed
Adding assertion to sse test
1 parent e112791 commit e6b9fa3

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

chapter-5/chapter-5-quarkus-kafka-streams/src/test/java/org/acme/rest/PriceResourceTest.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.acme.rest;
22

3+
import static org.assertj.core.api.Assertions.assertThat;
34
import static org.awaitility.Awaitility.await;
45

56
import java.net.URI;
@@ -31,10 +32,17 @@ void testPricesEventStream() {
3132

3233
List<Double> received = new CopyOnWriteArrayList<>();
3334

34-
SseEventSource source = SseEventSource.target(target).build();
35-
source.register(inboundSseEvent -> received.add(Double.valueOf(inboundSseEvent.readData())));
36-
source.open();
37-
await().atMost(Duration.ofSeconds(20)).until(() -> received.size() == 3);
38-
source.close();
35+
try (SseEventSource source = SseEventSource.target(target).build()) {
36+
source.register(inboundSseEvent -> received.add(Double.valueOf(inboundSseEvent.readData())));
37+
source.open();
38+
39+
await()
40+
.atMost(Duration.ofSeconds(20))
41+
.until(() -> received.size() == 3);
42+
}
43+
44+
assertThat(received)
45+
.hasSize(3)
46+
.allMatch(value -> (value >= 0) && (value < 100));
3947
}
4048
}

chapter-5/chapter-5-quarkus-kafka-streams/src/test/java/org/acme/service/PriceGeneratorTests.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import java.time.Duration;
66
import java.util.List;
7-
import java.util.function.Predicate;
87

98
import org.junit.jupiter.api.Test;
109

@@ -13,23 +12,20 @@
1312
class PriceGeneratorTests {
1413
PriceGenerator priceGenerator = new PriceGenerator();
1514

16-
private static final Predicate<Integer> VALUE_TEST =
17-
value -> (value >= 0) && (value < 100);
18-
1915
@Test
2016
public void generatesProperly() {
2117
List<Integer> prices = this.priceGenerator.generate()
2218
.select().first(2)
2319
.subscribe().withSubscriber(AssertSubscriber.create(2))
2420
.assertSubscribed()
25-
.awaitNextItem(Duration.ofSeconds(7)) // Needs to be a second or 2 more than the timing of actual events
26-
.awaitNextItem(Duration.ofSeconds(7))
21+
.awaitNextItem(Duration.ofSeconds(8)) // Needs to be a few seconds more than the timing of actual events
22+
.awaitNextItem(Duration.ofSeconds(8))
2723
.awaitCompletion(Duration.ofSeconds(15))
2824
.assertCompleted()
2925
.getItems();
3026

3127
assertThat(prices)
3228
.hasSize(2)
33-
.allMatch(VALUE_TEST);
29+
.allMatch(value -> (value >= 0) && (value < 100));
3430
}
3531
}

chapter-5/chapter-5-spring-kafka-streams/src/test/java/org/acme/Chapter5SpringKafkaStreamsApplicationTests.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,7 @@ void sseWorks() {
3636

3737
assertThat(emittedPrices)
3838
.isNotNull()
39-
.hasSize(3);
40-
41-
emittedPrices.forEach(price ->
42-
assertThat(price)
43-
.isNotNull()
44-
.isGreaterThan(0)
45-
);
39+
.hasSize(3)
40+
.allMatch(value -> (value >= 0) && (value < 100));
4641
}
4742
}

0 commit comments

Comments
 (0)