Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jersey update to version 2.32 #2406

Merged
merged 3 commits into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<version.lib.jboss.transaction-spi>7.6.0.Final</version.lib.jboss.transaction-spi>
<version.lib.jboss.transaction-api>1.0.0.Final</version.lib.jboss.transaction-api>
<version.lib.jedis>3.1.0</version.lib.jedis>
<version.lib.jersey>2.31</version.lib.jersey>
<version.lib.jersey>2.32</version.lib.jersey>
<version.lib.jsonb-api>1.0.2</version.lib.jsonb-api>
<version.lib.jsonp-api>1.1.6</version.lib.jsonp-api>
<version.lib.jsonp-impl>1.1.6</version.lib.jsonp-impl>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,9 @@

import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.Flow;
import java.util.concurrent.TimeUnit;

import javax.ws.rs.GET;
Expand All @@ -33,10 +36,13 @@
import javax.ws.rs.sse.SseEventSink;
import javax.ws.rs.sse.SseEventSource;

import io.helidon.common.reactive.Multi;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.fail;

/**
Expand All @@ -45,7 +51,8 @@
class ServerSseTest {
private static Client client;

private CompletableFuture<Void> connClosedFuture = new CompletableFuture<>();
private final CompletableFuture<Void> connClosedFuture = new CompletableFuture<>();
private final CompletableFuture<Void> multiTestFuture = new CompletableFuture<>();

@BeforeAll
static void initClass() {
Expand All @@ -59,25 +66,38 @@ static void destroyClass() {

@Test
void testSse() throws Exception {
innerTest("test1", connClosedFuture);
}

@Test
void testSseMulti() throws Exception {
innerTest("test2", multiTestFuture);
}

private void innerTest(String endpoint, CompletableFuture<Void> future) throws InterruptedException {
Server server = Server.builder()
.addApplication("/", new TestApplication1())
.build();
server.start();

try {
// Set up SSE event source
WebTarget target = client.target("http://localhost:" + server.port()).path("test1").path("sse");
WebTarget target = client.target("http://localhost:" + server.port()).path(endpoint).path("sse");
SseEventSource sseEventSource = SseEventSource.target(target).build();
sseEventSource.register(event -> System.out.println(event.readData(String.class)));
CountDownLatch count = new CountDownLatch(4);
sseEventSource.register(event -> {
event.readData(String.class);
count.countDown();
});

// Open SSE source for a few millis and then close it
sseEventSource.open();
Thread.sleep(200);
assertThat("Await method should have not timeout", count.await(250, TimeUnit.MILLISECONDS));
sseEventSource.close();

// Wait for server to detect connection closed
try {
connClosedFuture.get(2000, TimeUnit.MILLISECONDS);
future.get(2000, TimeUnit.MILLISECONDS);
} catch (Exception e) {
fail("Closing of SSE connection not detected!");
}
Expand All @@ -89,7 +109,7 @@ void testSse() throws Exception {
private final class TestApplication1 extends Application {
@Override
public Set<Object> getSingletons() {
return Set.of(new TestResource1());
return Set.of(new TestResource1(), new TestResource2());
}
}

Expand All @@ -100,18 +120,34 @@ public final class TestResource1 {
@Produces(MediaType.SERVER_SENT_EVENTS)
public void listenToEvents(@Context SseEventSink eventSink, @Context Sse sse) {
while (true) {
eventSink.send(sse.newEvent("hello")).thenAccept(t -> {
if (t != null) {
System.out.println(t);
connClosedFuture.complete(null);
}
});
try {
Thread.sleep(50);
eventSink.send(sse.newEvent("hello")).thenAccept(t -> {
if (t != null) {
System.out.println(t);
connClosedFuture.complete(null);
}
});
TimeUnit.MILLISECONDS.sleep(5);
} catch (InterruptedException e) {
// falls through
} catch (IllegalStateException e) {
//https://github.com/oracle/helidon/issues/1290
connClosedFuture.complete(null);
}
}
}
}

@Path("/test2")
public final class TestResource2 {
@GET
@Path("sse")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void listenToEvents(@Context Flow.Subscriber<String> sub, @Context Sse sse) {
Multi.interval(5, TimeUnit.MILLISECONDS, Executors.newScheduledThreadPool(1))
.map(String::valueOf)
.onCancel(() -> multiTestFuture.complete(null))
.subscribe(sub);
}
}
}