Skip to content

Commit

Permalink
[RESTEASY-2689]:Add a test case
Browse files Browse the repository at this point in the history
  • Loading branch information
jimma authored and asoldano committed Sep 18, 2020
1 parent a90b583 commit beebf5d
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.jboss.resteasy.test.providers.sse;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -341,4 +344,60 @@ public void noEventStream(@Context SseEventSink eventSink) throws Exception {
}
eventSink.close();
}

@GET
@Path("/bigmsg")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void bigEventMsg(@Context SseEventSink sink) throws IOException, URISyntaxException
{
if (sink == null)
{
throw new IllegalStateException("No client connected.");
}
this.eventSink = sink;
ExecutorService service = (ExecutorService) servletContext
.getAttribute(ExecutorServletContextListener.TEST_EXECUTOR);
java.io.InputStream inputStream = SseResource.class.getResourceAsStream("bigmsg.json");
String bigMsg = toString(new InputStreamReader(inputStream));
service.execute(new Thread()
{
public void run()
{
if (!eventSink.isClosed() && sending)
{
try
{
synchronized (openLock)
{
eventSink.send(sse.newEvent(bigMsg));
}
Thread.sleep(200);
}
catch (final InterruptedException e)
{
logger.error(e.getMessage(), e);
}

}
}
});
}

public static String toString(final Reader input) throws IOException {

final char[] buffer = new char[2048];
StringBuilder strBuilder = new StringBuilder();
try (Reader r = input) {
int n = r.read(buffer);
while (-1 != n) {
if (n == 0) {
throw new IOException("0 bytes read in violation of InputStream.read(byte[])");
}
strBuilder.append(buffer, 0, n);
n = r.read(buffer);
}
return strBuilder.toString();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.jboss.resteasy.test.providers.sse;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -40,6 +43,8 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import com.fasterxml.jackson.databind.ObjectMapper;

@RunWith(Arquillian.class)
@RunAsClient
public class SseTest
Expand All @@ -52,14 +57,16 @@ public static Archive<?> deploy()
{
WebArchive war = TestUtil.prepareArchive(SseTest.class.getSimpleName());
war.addClass(SseTest.class);
war.addAsResource("org/jboss/resteasy/test/providers/sse/bigmsg.json", "org/jboss/resteasy/test/providers/sse/bigmsg.json");
war.addAsWebInfResource("org/jboss/resteasy/test/providers/sse/web.xml", "web.xml");
war.addAsWebResource("org/jboss/resteasy/test/providers/sse/index.html", "index.html");
war.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
war.addAsManifestResource(PermissionUtil.createPermissionsXmlAsset(
new RuntimePermission("modifyThread")
), "permissions.xml");
return TestUtil.finishContainerPrepare(war, null, SseApplication.class, GreenHouse.class, SseResource.class,
AnotherSseResource.class, EscapingSseResource.class, ExecutorServletContextListener.class);
AnotherSseResource.class, EscapingSseResource.class, ExecutorServletContextListener.class);

}

private String generateURL(String path)
Expand Down Expand Up @@ -559,7 +566,45 @@ public void testNoContent() throws Exception
Assert.assertTrue("error is not expected", errors.get() == 0);
client.close();
}

//Test for RESTEASY-2689 which is reported in quarkus: https://github.com/quarkusio/quarkus/issues/11824
@Test
@InSequence(14)
public void testBigMessage() throws Exception
{
final CountDownLatch latch = new CountDownLatch(1);
final AtomicInteger errors = new AtomicInteger(0);
final List<String> results = new ArrayList<String>();
Client client = ClientBuilder.newBuilder().build();
WebTarget target = client.target(generateURL("/service/server-sent-events/bigmsg"));
SseEventSource msgEventSource = SseEventSource.target(target).build();
try (SseEventSource eventSource = msgEventSource)
{
Assert.assertEquals(SseEventSourceImpl.class, eventSource.getClass());
eventSource.register(event -> {
results.add(event.readData());
latch.countDown();
}, ex -> {
errors.incrementAndGet();
logger.error(ex.getMessage(), ex);
throw new RuntimeException(ex);
}) ;
eventSource.open();
boolean waitResult = latch.await(30, TimeUnit.SECONDS);
Assert.assertEquals(0, errors.get());
Assert.assertTrue("Waiting for event to be delivered has timed out.", waitResult);
}
Assert.assertFalse("SseEventSource is not closed", msgEventSource.isOpen());
Assert.assertTrue("1 messages are expected, but is : " + results.size(), results.size() == 1);
java.nio.file.Path filepath= Paths.get(SseTest.class.getResource("bigmsg.json").toURI());
String bigMsg = new String(Files.readAllBytes(filepath));
ObjectMapper om = new ObjectMapper();
@SuppressWarnings("unchecked")
Map<String, Object> m1 = (Map<String, Object>)(om.readValue(bigMsg, Map.class));
@SuppressWarnings("unchecked")
Map<String, Object> m2 = (Map<String, Object>)(om.readValue(results.get(0), Map.class));
Assert.assertTrue("Unexpceted big size message", m1.equals(m2));
client.close();
}
// @Test
// //This will open a browser and test with html sse client
// public void testHtmlSse() throws Exception
Expand Down
Loading

0 comments on commit beebf5d

Please sign in to comment.