-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
111 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/test/java/io/logz/guice/jersey/ServerSentEventsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.logz.guice.jersey; | ||
|
||
import io.logz.guice.jersey.resources.SseResource; | ||
import io.logz.guice.jersey.supplier.JerseyServerSupplier; | ||
import org.glassfish.jersey.server.ResourceConfig; | ||
import org.junit.Test; | ||
|
||
import javax.ws.rs.client.Entity; | ||
import javax.ws.rs.sse.SseEventSource; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; | ||
|
||
public class ServerSentEventsTest { | ||
|
||
@Test | ||
public void testEventSource() throws Exception { | ||
ResourceConfig resourceConfig = new ResourceConfig().registerClasses(SseResource.class); | ||
JerseyServerSupplier.createServerAndTest(resourceConfig, target -> { | ||
CountDownLatch latch = new CountDownLatch(1); | ||
AtomicReference<String> receivedMessage = new AtomicReference<>(); | ||
SseEventSource eventSource = SseEventSource.target(target.path(SseResource.PATH)).build(); | ||
eventSource.register((inboundEvent) -> { | ||
receivedMessage.set(inboundEvent.readData()); | ||
latch.countDown(); | ||
}); | ||
|
||
eventSource.open(); | ||
target.path(SseResource.PATH).request().post(Entity.text("message")); | ||
|
||
try { | ||
assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue() | ||
.withFailMessage("Waiting for message to be delivered has timed out."); | ||
} finally { | ||
eventSource.close(); | ||
} | ||
|
||
assertThat(receivedMessage.get()).isEqualTo("message") | ||
.withFailMessage("Unexpected SSE event data value."); | ||
}); | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
src/test/java/io/logz/guice/jersey/resources/SseResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.logz.guice.jersey.resources; | ||
|
||
import javax.ws.rs.DELETE; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.sse.Sse; | ||
import javax.ws.rs.sse.SseEventSink; | ||
import java.io.IOException; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
@Path(SseResource.PATH) | ||
public class SseResource { | ||
|
||
public static final String PATH = "sse"; | ||
|
||
private static volatile SseEventSink eventSink = null; | ||
|
||
@GET | ||
@Produces(MediaType.SERVER_SENT_EVENTS) | ||
public void getMessageQueue(@Context SseEventSink sink) { | ||
eventSink = requireNonNull(sink); | ||
} | ||
|
||
@POST | ||
public void addMessage(final String message, @Context Sse sse) throws IOException { | ||
final SseEventSink localSink = eventSink; | ||
if (localSink != null) { | ||
localSink.send(sse.newEventBuilder().name("custom-message").data(String.class, message).build()); | ||
} | ||
} | ||
|
||
@DELETE | ||
public void close() throws IOException { | ||
final SseEventSink localSink = eventSink; | ||
if (localSink != null) { | ||
eventSink.close(); | ||
} | ||
eventSink = null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters