Skip to content

Commit

Permalink
Polish RESTEasy Reactive SSE tests
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Feb 16, 2021
1 parent 2c18492 commit 3690abf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-validator-deployment</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ public void testParser() {
testParser("data:DATA\nretry:ASD\n\n", "DATA", null, null, null, SseEvent.RECONNECT_NOT_SET);

// two events
testParser(Arrays.asList("data:foo\n\ndata:bar\n\n"),
testParser(Collections.singletonList("data:foo\n\ndata:bar\n\n"),
Arrays.asList(new InboundSseEventImpl(null, null)
.setData("foo"),
new InboundSseEventImpl(null, null)
.setData("bar")));
// two events with data
testParser(Arrays.asList("data:DATA\nid:ID\n:COMMENT\nretry:23\nevent:NAME\n\n"
testParser(Collections.singletonList("data:DATA\nid:ID\n:COMMENT\nretry:23\nevent:NAME\n\n"
+ "data:DATA2\nid:ID2\n:COMMENT2\nretry:232\nevent:NAME2\n\n"),
Arrays.asList(new InboundSseEventImpl(null, null)
.setData("DATA")
Expand All @@ -80,7 +80,7 @@ public void testParser() {
.setReconnectDelay(232)
.setName("NAME2")));
// two events with data, only ID is persistent
testParser(Arrays.asList("data:DATA\nid:ID\n:COMMENT\nretry:23\nevent:NAME\n\n"
testParser(Collections.singletonList("data:DATA\nid:ID\n:COMMENT\nretry:23\nevent:NAME\n\n"
+ "data:DATA2\n\n"),
Arrays.asList(new InboundSseEventImpl(null, null)
.setData("DATA")
Expand All @@ -106,19 +106,20 @@ public void testParser() {
.setData("bar")));
// one event in two buffers
testParser(Arrays.asList("data:f", "oo\n\n"),
Arrays.asList(new InboundSseEventImpl(null, null)
Collections.singletonList(new InboundSseEventImpl(null, null)
.setData("foo")));
// one event in two buffers within a utf-8 char
testParserWithBytes(
Arrays.asList(new byte[] { 'd', 'a', 't', 'a', ':', (byte) 0b11000010 },
new byte[] { (byte) 0b10100010, '\n', '\n' }),
Arrays.asList(new InboundSseEventImpl(null, null)
Collections.singletonList(new InboundSseEventImpl(null, null)
.setData("¢")));

// BOM
testParserWithBytes(
Arrays.asList(new byte[] { (byte) 0xFE, (byte) 0xFF, 'd', 'a', 't', 'a', ':', 'f', 'o', 'o', '\n', '\n' }),
Arrays.asList(new InboundSseEventImpl(null, null)
Collections.singletonList(
new byte[] { (byte) 0xFE, (byte) 0xFF, 'd', 'a', 't', 'a', ':', 'f', 'o', 'o', '\n', '\n' }),
Collections.singletonList(new InboundSseEventImpl(null, null)
.setData("foo")));

// invalid BOM location
Expand All @@ -128,25 +129,26 @@ public void testParser() {
Arrays.asList(new byte[] { 'd', 'a', 't', 'a', ':', 'f', 'o', 'o', '\n', '\n' },
new byte[] { (byte) 0xFE, (byte) 0xFF, 'd', 'a', 't', 'a', ':', 'f', 'o', 'o', '\n',
'\n' }),
Arrays.asList(new InboundSseEventImpl(null, null)
Collections.singletonList(new InboundSseEventImpl(null, null)
.setData("foo"))));
// invalid UTF-8
Assertions.assertThrows(IllegalStateException.class,
() -> testParserWithBytes(
Arrays.asList(new byte[] { 'd', 'a', 't', 'a', ':', (byte) 0b1111_1000, 'f', 'o', 'o', '\n', '\n' }),
Arrays.asList()));
Collections.singletonList(
new byte[] { 'd', 'a', 't', 'a', ':', (byte) 0b1111_1000, 'f', 'o', 'o', '\n', '\n' }),
Collections.emptyList()));
}

private void testParser(String event, String data, String comment, String lastId, String name, long reconnectDelay) {
if (data != null) {
testParser(Arrays.asList(event), Arrays.asList(new InboundSseEventImpl(null, null)
testParser(Collections.singletonList(event), Collections.singletonList(new InboundSseEventImpl(null, null)
.setData(data)
.setComment(comment)
.setId(lastId)
.setName(name)
.setReconnectDelay(reconnectDelay)));
} else {
testParser(Arrays.asList(event), Collections.emptyList());
testParser(Collections.singletonList(event), Collections.emptyList());
}
}

Expand All @@ -159,7 +161,7 @@ private void testParserWithBytes(List<byte[]> events, List<InboundSseEvent> expe
SseEventSourceImpl eventSource = new SseEventSourceImpl(null, 500, TimeUnit.MILLISECONDS);
SseParser parser = eventSource.getSseParser();
CountDownLatch latch = new CountDownLatch(expectedEvents.size());
List<InboundSseEvent> receivedEvents = new ArrayList<>(expectedEvents.size());
List<InboundSseEvent> receivedEvents = Collections.synchronizedList(new ArrayList<>(expectedEvents.size()));
eventSource.register(evt -> {
latch.countDown();
receivedEvents.add(evt);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.quarkus.resteasy.reactive.jsonb.deployment.test.sse;

import static org.assertj.core.api.Assertions.assertThat;

import java.net.URI;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
Expand All @@ -18,7 +20,6 @@
import org.jboss.resteasy.reactive.client.impl.MultiInvoker;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

Expand Down Expand Up @@ -53,7 +54,7 @@ private void testSse(String path) throws Exception {
try (SseEventSource eventSource = SseEventSource.target(target).reconnectingEvery(Integer.MAX_VALUE, TimeUnit.SECONDS)
.build()) {
CompletableFuture<List<String>> res = new CompletableFuture<>();
List<String> collect = new ArrayList<>();
List<String> collect = Collections.synchronizedList(new ArrayList<>());
eventSource.register(new Consumer<InboundSseEvent>() {
@Override
public void accept(InboundSseEvent inboundSseEvent) {
Expand All @@ -68,49 +69,45 @@ public void accept(Throwable throwable) {
res.complete(collect);
});
eventSource.open();
Assertions.assertEquals(Arrays.asList("hello", "stef"), res.get(5, TimeUnit.SECONDS));
assertThat(res.get(5, TimeUnit.SECONDS)).containsExactly("hello", "stef");
}
}

@Test
public void testMultiFromSse() throws Exception {
public void testMultiFromSse() {
testMulti("sse");
}

@Test
public void testMultiFromMulti() throws Exception {
public void testMultiFromMulti() {
testMulti("sse/multi");
}

private void testMulti(String path) {
Client client = ClientBuilder.newBuilder().build();
WebTarget target = client.target(uri.toString() + path);
Multi<String> multi = target.request().rx(MultiInvoker.class).get(String.class);
List<String> list = multi.collectItems().asList().await().atMost(Duration.ofSeconds(30));
Assertions.assertEquals(2, list.size());
Assertions.assertEquals("hello", list.get(0));
Assertions.assertEquals("stef", list.get(1));
List<String> list = multi.collect().asList().await().atMost(Duration.ofSeconds(30));
assertThat(list).containsExactly("hello", "stef");
}

@Test
public void testJsonMultiFromSse() throws Exception {
public void testJsonMultiFromSse() {
testJsonMulti("sse/json");
testJsonMulti("sse/json2");
testJsonMulti("sse/blocking/json");
}

@Test
public void testJsonMultiFromMulti() throws Exception {
public void testJsonMultiFromMulti() {
testJsonMulti("sse/json/multi");
}

private void testJsonMulti(String path) {
Client client = ClientBuilder.newBuilder().build();
WebTarget target = client.target(uri.toString() + path);
Multi<Message> multi = target.request().rx(MultiInvoker.class).get(Message.class);
List<Message> list = multi.collectItems().asList().await().atMost(Duration.ofSeconds(30));
Assertions.assertEquals(2, list.size());
Assertions.assertEquals("hello", list.get(0).name);
Assertions.assertEquals("stef", list.get(1).name);
List<Message> list = multi.collect().asList().await().atMost(Duration.ofSeconds(30));
assertThat(list).extracting("name").containsExactly("hello", "stef");
}
}

0 comments on commit 3690abf

Please sign in to comment.