Search in sources :

Example 1 with SSEResource

use of test.types.SSEResource in project aries-jax-rs-whiteboard by apache.

the class JaxrsTest method testSSEApplication.

@Test
public void testSSEApplication() throws InterruptedException, MalformedURLException, TimeoutException {
    AtomicInteger atomicInteger = new AtomicInteger();
    registerApplication(new TestSSEApplication(), JAX_RS_APPLICATION_BASE, "/sse");
    registerAddon(new SSEResource(), JAX_RS_APPLICATION_SELECT, "(" + JAX_RS_APPLICATION_BASE + "=/sse)");
    registerExtension(ContainerResponseFilter.class, (req, res) -> atomicInteger.incrementAndGet(), "Filter", JAX_RS_APPLICATION_SELECT, "(" + JAX_RS_APPLICATION_BASE + "=/sse)");
    SseEventSourceFactory sseFactory = createSseFactory();
    SseEventSource source1 = sseFactory.newSource(createDefaultTarget().path("/sse").path("/subscribe"));
    SseEventSource source2 = sseFactory.newSource(createDefaultTarget().path("/sse").path("/subscribe"));
    ArrayList<String> source1Events = new ArrayList<>();
    ArrayList<String> source2Events = new ArrayList<>();
    Phaser phaser = new Phaser(2);
    source1.register(event -> {
        source1Events.add(event.readData(String.class));
        phaser.arrive();
    });
    source2.register(event -> {
        source2Events.add(event.readData(String.class));
        phaser.arrive();
    });
    source1.open();
    source2.open();
    phaser.awaitAdvanceInterruptibly(0, 10, TimeUnit.SECONDS);
    // The filter IS invoked on the subscribe method
    assertEquals(2, atomicInteger.get());
    WebTarget broadcast = createDefaultTarget().path("/sse").path("/broadcast");
    broadcast.request().post(Entity.entity("message", MediaType.TEXT_PLAIN_TYPE));
    phaser.awaitAdvanceInterruptibly(1, 10, TimeUnit.SECONDS);
    assertEquals(Arrays.asList("welcome", "message"), source1Events);
    assertEquals(Arrays.asList("welcome", "message"), source2Events);
    source2.close();
    phaser.arrive();
    atomicInteger.set(0);
    broadcast.request().post(Entity.entity("another message", MediaType.TEXT_PLAIN_TYPE));
    phaser.awaitAdvanceInterruptibly(2, 10, TimeUnit.SECONDS);
    assertEquals(Arrays.asList("welcome", "message", "another message"), source1Events);
    assertEquals(Arrays.asList("welcome", "message"), source2Events);
    source1.close();
    // The filter IS invoked when broadcasting events
    assertEquals(1, atomicInteger.get());
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestSSEApplication(test.types.TestSSEApplication) SseEventSourceFactory(org.osgi.service.jaxrs.client.SseEventSourceFactory) ArrayList(java.util.ArrayList) WebTarget(javax.ws.rs.client.WebTarget) SSEResource(test.types.SSEResource) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 WebTarget (javax.ws.rs.client.WebTarget)1 SseEventSource (javax.ws.rs.sse.SseEventSource)1 Test (org.junit.Test)1 SseEventSourceFactory (org.osgi.service.jaxrs.client.SseEventSourceFactory)1 SSEResource (test.types.SSEResource)1 TestSSEApplication (test.types.TestSSEApplication)1