Search in sources :

Example 1 with SseEventSource

use of javax.ws.rs.sse.SseEventSource in project jersey by jersey.

the class BroadcasterTest method test.

@Test
public void test() throws InterruptedException {
    SseEventSource eventSourceA = SseEventSource.target(target().path("sse/events")).build();
    List<String> resultsA1 = new ArrayList<>();
    List<String> resultsA2 = new ArrayList<>();
    CountDownLatch a1Latch = new CountDownLatch(5);
    CountDownLatch a2Latch = new CountDownLatch(5);
    eventSourceA.subscribe((event) -> {
        resultsA1.add(event.readData());
        a1Latch.countDown();
    });
    eventSourceA.subscribe((event) -> {
        resultsA2.add(event.readData());
        a2Latch.countDown();
    });
    eventSourceA.open();
    target().path("sse/push/firstBroadcast").request().get(String.class);
    SseEventSource eventSourceB = SseEventSource.target(target().path("sse/events")).build();
    List<String> resultsB1 = new ArrayList<>();
    List<String> resultsB2 = new ArrayList<>();
    CountDownLatch b1Latch = new CountDownLatch(4);
    CountDownLatch b2Latch = new CountDownLatch(4);
    eventSourceB.subscribe((event) -> {
        resultsB1.add(event.readData());
        b1Latch.countDown();
    });
    eventSourceB.subscribe((event) -> {
        resultsB2.add(event.readData());
        b2Latch.countDown();
    });
    eventSourceB.open();
    target().path("sse/push/secondBroadcast").request().get(String.class);
    Assert.assertTrue("Waiting for resultsA1 to be complete failed.", a1Latch.await(3000, TimeUnit.MILLISECONDS));
    Assert.assertTrue("Waiting for resultsA2 to be complete failed.", a2Latch.await(3000, TimeUnit.MILLISECONDS));
    Assert.assertTrue("Waiting for resultsB1 to be complete failed.", b1Latch.await(3000, TimeUnit.MILLISECONDS));
    Assert.assertTrue("Waiting for resultsB2 to be complete failed.", b2Latch.await(3000, TimeUnit.MILLISECONDS));
    Assert.assertTrue(txLatch.await(5000, TimeUnit.MILLISECONDS));
    // Event1, Event2, Event3, firstBroadcast, secondBroadcast
    Assert.assertEquals("resultsA1 does not contain 5 elements.", 5, resultsA1.size());
    Assert.assertEquals("resultsA2 does not contain 5 elements.", 5, resultsA2.size());
    Assert.assertTrue("resultsA1 does not contain expected data", resultsA1.get(0).equals("Event1") && resultsA1.get(1).equals("Event2") && resultsA1.get(2).equals("Event3") && resultsA1.get(3).equals("firstBroadcast") && resultsA1.get(4).equals("secondBroadcast"));
    Assert.assertTrue("resultsA2 does not contain expected data", resultsA2.get(0).equals("Event1") && resultsA2.get(1).equals("Event2") && resultsA2.get(2).equals("Event3") && resultsA2.get(3).equals("firstBroadcast") && resultsA2.get(4).equals("secondBroadcast"));
    Assert.assertEquals("resultsB1 does not contain 4 elements.", 4, resultsB1.size());
    Assert.assertEquals("resultsB2 does not contain 4 elements.", 4, resultsB2.size());
    Assert.assertTrue("resultsB1 does not contain expected data", resultsB1.get(0).equals("Event1") && resultsB1.get(1).equals("Event2") && resultsB1.get(2).equals("Event3") && resultsB1.get(3).equals("secondBroadcast"));
    Assert.assertTrue("resultsB2 does not contain expected data", resultsB2.get(0).equals("Event1") && resultsB2.get(1).equals("Event2") && resultsB2.get(2).equals("Event3") && resultsB2.get(3).equals("secondBroadcast"));
    target().path("sse/close").request().get();
    Assert.assertTrue(closeLatch.await(3000, TimeUnit.MILLISECONDS));
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 2 with SseEventSource

use of javax.ws.rs.sse.SseEventSource in project jersey by jersey.

the class SseEventSinkToEventSourceTest method testWithEventSource.

@Test
public void testWithEventSource() throws InterruptedException {
    transmitLatch = new CountDownLatch(2 * MSG_COUNT);
    final WebTarget endpoint = target().path("events");
    final SseEventSource eventSource = SseEventSource.target(endpoint).named("my-sse-eventsource1").build();
    final CountDownLatch count1 = new CountDownLatch(3 * MSG_COUNT);
    final CountDownLatch count2 = new CountDownLatch(3 * MSG_COUNT);
    eventSource.subscribe(new InboundHandler("consumer1", count1));
    eventSource.subscribe(new InboundHandler("consumer2", count2));
    eventSource.open();
    final boolean sent = transmitLatch.await(5 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS);
    Assert.assertTrue("Awaiting for SSE message has timeout. Not all message were sent.", sent);
    final boolean handled2 = count2.await(5 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS);
    Assert.assertTrue("Awaiting for SSE message has timeout. Not all message were handled by eventSource2.", handled2);
    final boolean handled1 = count1.await(5 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS);
    Assert.assertTrue("Awaiting for SSE message has timeout. Not all message were handled by eventSource1.", handled1);
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) WebTarget(javax.ws.rs.client.WebTarget) CountDownLatch(java.util.concurrent.CountDownLatch) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 3 with SseEventSource

use of javax.ws.rs.sse.SseEventSource in project meecrowave by apache.

the class SSETest method sse.

@Test
public void sse() throws MalformedURLException, InterruptedException {
    final Client client = ClientBuilder.newBuilder().build();
    WebTarget base = client.target(String.format("http://localhost:%d", CONTAINER.getConfiguration().getHttpPort()));
    // An exception is not thrown on a 404 response but that is not a Meecrowave issue.
    try (final SseEventSource eventSource = SseEventSource.target(base.path("/rs/news/update")).build()) {
        CountDownLatch cdl = new CountDownLatch(5);
        eventSource.register(sse -> {
            JsonObject data = sse.readData(JsonObject.class, MediaType.APPLICATION_JSON_TYPE);
            assertNotNull(data);
            cdl.countDown();
        }, e -> {
            e.printStackTrace();
            fail(e.getMessage());
        });
        eventSource.open();
        assertTrue(cdl.await(20, TimeUnit.SECONDS));
        assertTrue(eventSource.close(5, TimeUnit.SECONDS));
    }
    client.close();
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) JsonObject(javax.json.JsonObject) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with SseEventSource

use of javax.ws.rs.sse.SseEventSource in project cxf by apache.

the class StatsClient method main.

public static void main(String[] args) throws InterruptedException {
    final WebTarget target = ClientBuilder.newClient().register(JacksonJsonProvider.class).target("http://localhost:8686/rest/stats/sse");
    try (SseEventSource eventSource = SseEventSource.target(target).build()) {
        eventSource.register(StatsClient::print, System.out::println);
        eventSource.open();
        // Give the SSE stream 5 seconds to collect all events
        Thread.sleep(5000);
    }
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) WebTarget(javax.ws.rs.client.WebTarget)

Example 5 with SseEventSource

use of javax.ws.rs.sse.SseEventSource in project cxf by apache.

the class SseEventSourceImplTest method testNoReconnectAndNotAuthorized.

@Test
public void testNoReconnectAndNotAuthorized() throws InterruptedException, IOException {
    try (SseEventSource eventSource = withNoReconnect(Type.EVENT_NOT_AUTHORIZED)) {
        eventSource.open();
        assertThat(eventSource.isOpen(), equalTo(false));
        assertThat(errors.size(), equalTo(1));
        // Allow the event processor to pull for events (150ms)
        Thread.sleep(150L);
    }
    await().atMost(Duration.ofMillis(500L)).untilAsserted(() -> assertThat(errors.size(), equalTo(1)));
    assertThat(events.size(), equalTo(0));
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) Test(org.junit.Test)

Aggregations

SseEventSource (javax.ws.rs.sse.SseEventSource)39 Test (org.junit.Test)34 WebTarget (javax.ws.rs.client.WebTarget)13 ArrayList (java.util.ArrayList)11 CountDownLatch (java.util.concurrent.CountDownLatch)4 JerseyTest (org.glassfish.jersey.test.JerseyTest)3 JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 JsonObject (javax.json.JsonObject)1 Client (javax.ws.rs.client.Client)1 Response (javax.ws.rs.core.Response)1 InboundSseEvent (javax.ws.rs.sse.InboundSseEvent)1 Builder (javax.ws.rs.sse.SseEventSource.Builder)1 Book (org.apache.cxf.systest.jaxrs.resources.Book)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 Test (org.junit.jupiter.api.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1