Skip to content

Commit cb8245c

Browse files
senivamjansupol
authored andcommitted
SSE tests fixed (#3870)
Signed-off-by: Maxim Nesen <[email protected]>
1 parent 62abe73 commit cb8245c

File tree

11 files changed

+150
-84
lines changed

11 files changed

+150
-84
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ cache:
1313
- .autoconf
1414
env:
1515
matrix:
16-
- TEST_SET="-Ptravis_main"
17-
- TEST_SET="-fn -Dtest=BroadcasterTest,ClientCloseTest,ResponseWriterOutputStreamTest -DfailIfNoTests=false"
16+
- TEST_SET="-Ptravis_e2e_skip"
17+
- TEST_SET="-Ptravis_e2e"
1818

1919
install: true
2020

media/sse/pom.xml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,6 @@
7575
<artifactId>maven-bundle-plugin</artifactId>
7676
<inherited>true</inherited>
7777
</plugin>
78-
<plugin>
79-
<groupId>org.apache.maven.plugins</groupId>
80-
<artifactId>maven-surefire-plugin</artifactId>
81-
<configuration>
82-
<forkCount>1</forkCount>
83-
<reuseForks>false</reuseForks>
84-
<enableAssertions>false</enableAssertions>
85-
<excludes>
86-
<exclude>${exclude.sse.tests}</exclude>
87-
</excludes>
88-
</configuration>
89-
</plugin>
9078
</plugins>
9179
</build>
9280

pom.xml

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,7 @@
765765
<properties>
766766
<release.tests.args>-Dskip.tests=true</release.tests.args>
767767
<skip.tests>true</skip.tests>
768+
<skip.e2e>true</skip.e2e>
768769
</properties>
769770
</profile>
770771
<profile>
@@ -1183,9 +1184,25 @@
11831184
11841185
the whole build is run as clean install but excludes several e2e tests because of the not grepped output
11851186
-->
1186-
<id>travis_main</id>
1187+
<id>travis_e2e_skip</id>
11871188
<properties>
1188-
<exclude.sse.tests>**/BroadcasterTest*,**/ClientCloseTest*,**/ResponseWriterOutputStreamTest*</exclude.sse.tests>
1189+
<skip.e2e>true</skip.e2e>
1190+
</properties>
1191+
</profile>
1192+
<profile>
1193+
<!--
1194+
Profile is aimed to run the build on travis
1195+
due to travis limitations for output (max 4MB) this profile is used to run e2e tests only.
1196+
1197+
the only thing which is happen using profile is run of e2e tests (with additional build)
1198+
everything is already build using travis_e2e_skip profile
1199+
1200+
the whole build is run as test -Ptravis_e2e
1201+
-->
1202+
<id>travis_e2e</id>
1203+
<properties>
1204+
<skip.e2e>false</skip.e2e>
1205+
<skip.tests>true</skip.tests>
11891206
</properties>
11901207
</profile>
11911208
</profiles>
@@ -1979,6 +1996,6 @@
19791996
<xerces.version>2.11.0</xerces.version>
19801997
<xmlunit.version>1.6</xmlunit.version>
19811998
<yasson.version>1.0.1</yasson.version>
1982-
<exclude.sse.tests>none</exclude.sse.tests>
1999+
<skip.e2e>false</skip.e2e>
19832000
</properties>
19842001
</project>

tests/e2e-client/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<forkCount>1</forkCount>
4242
<reuseForks>false</reuseForks>
4343
<enableAssertions>false</enableAssertions>
44+
<skipTests>${skip.e2e}</skipTests>
4445
</configuration>
4546
</plugin>
4647
</plugins>

tests/e2e-entity/pom.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@
4141
<forkCount>1</forkCount>
4242
<reuseForks>false</reuseForks>
4343
<enableAssertions>false</enableAssertions>
44-
<excludes>
45-
<exclude>${exclude.sse.tests}</exclude>
46-
</excludes>
44+
<skipTests>${skip.e2e}</skipTests>
4745
</configuration>
4846
</plugin>
4947
</plugins>

tests/e2e-entity/src/test/java/org/glassfish/jersey/tests/e2e/sse/BroadcasterTest.java

Lines changed: 91 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,29 @@
1616

1717
package org.glassfish.jersey.tests.e2e.sse;
1818

19-
import java.util.ArrayList;
20-
import java.util.List;
21-
import java.util.concurrent.CountDownLatch;
22-
import java.util.concurrent.TimeUnit;
19+
import org.glassfish.jersey.server.ResourceConfig;
20+
import org.glassfish.jersey.server.ServerProperties;
21+
import org.glassfish.jersey.test.JerseyTest;
22+
import org.junit.Assert;
23+
import org.junit.Test;
2324

25+
import javax.inject.Singleton;
2426
import javax.ws.rs.GET;
2527
import javax.ws.rs.Path;
2628
import javax.ws.rs.PathParam;
2729
import javax.ws.rs.Produces;
2830
import javax.ws.rs.core.Application;
2931
import javax.ws.rs.core.Context;
3032
import javax.ws.rs.core.MediaType;
33+
import javax.ws.rs.sse.OutboundSseEvent;
3134
import javax.ws.rs.sse.Sse;
3235
import javax.ws.rs.sse.SseBroadcaster;
3336
import javax.ws.rs.sse.SseEventSink;
3437
import javax.ws.rs.sse.SseEventSource;
35-
36-
import javax.inject.Singleton;
37-
38-
import org.glassfish.jersey.server.ResourceConfig;
39-
import org.glassfish.jersey.server.ServerProperties;
40-
import org.glassfish.jersey.test.JerseyTest;
41-
42-
import org.junit.Assert;
43-
import org.junit.Test;
38+
import java.util.ArrayList;
39+
import java.util.List;
40+
import java.util.concurrent.CountDownLatch;
41+
import java.util.concurrent.TimeUnit;
4442

4543
/**
4644
* JAX-RS {@link javax.ws.rs.sse.SseBroadcaster} test.
@@ -53,13 +51,14 @@ public class BroadcasterTest extends JerseyTest {
5351
static final CountDownLatch txLatch = new CountDownLatch(4);
5452
private static boolean isSingleton = false;
5553

56-
private static int LATCH_WAIT_TIMEOUT = 16000; //timeout to be waited for asynchronous events to be completed
54+
private static int ASYNC_WAIT_TIMEOUT = 1000; //timeout for asynchronous events to complete activities
5755

5856
@Path("sse")
5957
@Singleton
6058
public static class SseResource {
6159
private final Sse sse;
6260
private SseBroadcaster broadcaster;
61+
private OutboundSseEvent.Builder builder;
6362

6463
public SseResource(@Context final Sse sse) {
6564
this.sse = sse;
@@ -71,9 +70,10 @@ public SseResource(@Context final Sse sse) {
7170
@Path("events")
7271
public void getServerSentEvents(@Context final SseEventSink eventSink, @Context final Sse sse) {
7372
isSingleton = this.sse == sse;
74-
eventSink.send(sse.newEventBuilder().data("Event1").build());
75-
eventSink.send(sse.newEventBuilder().data("Event2").build());
76-
eventSink.send(sse.newEventBuilder().data("Event3").build());
73+
builder = sse.newEventBuilder();
74+
eventSink.send(builder.data("Event1").build());
75+
eventSink.send(builder.data("Event2").build());
76+
eventSink.send(builder.data("Event3").build());
7777
broadcaster.register(eventSink);
7878
broadcaster.onClose((subscriber) -> {
7979
if (subscriber == eventSink) {
@@ -84,9 +84,10 @@ public void getServerSentEvents(@Context final SseEventSink eventSink, @Context
8484
}
8585

8686
@Path("push/{msg}")
87+
@Produces(MediaType.SERVER_SENT_EVENTS)
8788
@GET
8889
public String pushMessage(@PathParam("msg") final String msg) {
89-
broadcaster.broadcast(sse.newEventBuilder().data(msg).build());
90+
broadcaster.broadcast(builder.data(msg).build());
9091
txLatch.countDown();
9192
return "Broadcasting message: " + msg;
9293
}
@@ -99,6 +100,55 @@ public String close() {
99100
}
100101
}
101102

103+
/**
104+
* Wrapper to hold results coming from events (including broadcast)
105+
*
106+
* @param <T> type of expected results
107+
*/
108+
public static class EventListWrapper<T> {
109+
private final List<T> data; //event results
110+
private final CountDownLatch eventCountDown; //count down delay for expected results
111+
private final CountDownLatch broadcastLag = new CountDownLatch(1); //broadcast lag
112+
// which shall be hold until thread is ready to process events from broadcast
113+
private static final int LAG_INTERVAL = 1000; //broadcast lag timeout - in milliseconds (1s)
114+
private static final int EXPECTED_REGULAR_EVENTS_COUNT = 3; //expected regular outbound events
115+
116+
public EventListWrapper(List<T> data, CountDownLatch eventCountDown) {
117+
this.data = data;
118+
this.eventCountDown = eventCountDown;
119+
}
120+
121+
public void add(T msg) {
122+
data.add(msg);
123+
eventCountDown.countDown();
124+
if (eventCountDown.getCount() == EXPECTED_REGULAR_EVENTS_COUNT) { //all regular events are received,
125+
//ready for broadcast
126+
broadcastLag.countDown();
127+
}
128+
}
129+
130+
public CountDownLatch getEventCountDown() {
131+
return eventCountDown;
132+
}
133+
134+
public T get(int pos) {
135+
return data.get(pos);
136+
}
137+
138+
public int size() {
139+
return data.size();
140+
}
141+
142+
/**
143+
* makes current thread to wait for predefined interval until broadcast is ready
144+
*
145+
* @throws InterruptedException in case of something went wrong
146+
*/
147+
public boolean waitBroadcast() throws InterruptedException {
148+
return broadcastLag.await(LAG_INTERVAL, TimeUnit.MILLISECONDS);
149+
}
150+
}
151+
102152
@Override
103153
protected Application configure() {
104154
final ResourceConfig rc = new ResourceConfig(SseResource.class);
@@ -108,50 +158,42 @@ protected Application configure() {
108158

109159
@Test
110160
public void test() throws InterruptedException {
111-
SseEventSource eventSourceA = SseEventSource.target(target().path("sse/events")).build();
112-
List<String> resultsA1 = new ArrayList<>();
113-
List<String> resultsA2 = new ArrayList<>();
114-
CountDownLatch a1Latch = new CountDownLatch(5);
115-
CountDownLatch a2Latch = new CountDownLatch(5);
116-
eventSourceA.register((event) -> {
117-
resultsA1.add(event.readData());
118-
a1Latch.countDown();
119-
});
120-
eventSourceA.register((event) -> {
121-
resultsA2.add(event.readData());
122-
a2Latch.countDown();
123-
});
161+
final SseEventSource eventSourceA = SseEventSource.target(target().path("sse/events")).build();
162+
final EventListWrapper<String> resultsA1 = new EventListWrapper(new ArrayList(), new CountDownLatch(5));
163+
final EventListWrapper<String> resultsA2 = new EventListWrapper(new ArrayList(), new CountDownLatch(5));
164+
165+
eventSourceA.register(event -> resultsA1.add(event.readData()));
166+
eventSourceA.register(event -> resultsA2.add(event.readData()));
124167
eventSourceA.open();
125168

169+
Assert.assertTrue(resultsA1.waitBroadcast()); //some delay is required to process consumer and producer
170+
Assert.assertTrue(resultsA2.waitBroadcast()); //some delay is required to process consumer and producer
171+
126172
target().path("sse/push/firstBroadcast").request().get(String.class);
127173

128174

129-
SseEventSource eventSourceB = SseEventSource.target(target().path("sse/events")).build();
130-
List<String> resultsB1 = new ArrayList<>();
131-
List<String> resultsB2 = new ArrayList<>();
132-
CountDownLatch b1Latch = new CountDownLatch(4);
133-
CountDownLatch b2Latch = new CountDownLatch(4);
134-
eventSourceB.register((event) -> {
135-
resultsB1.add(event.readData());
136-
b1Latch.countDown();
137-
});
138-
eventSourceB.register((event) -> {
139-
resultsB2.add(event.readData());
140-
b2Latch.countDown();
141-
});
175+
final SseEventSource eventSourceB = SseEventSource.target(target().path("sse/events")).build();
176+
final EventListWrapper<String> resultsB1 = new EventListWrapper(new ArrayList(), new CountDownLatch(4));
177+
final EventListWrapper<String> resultsB2 = new EventListWrapper(new ArrayList(), new CountDownLatch(4));
178+
179+
eventSourceB.register(event -> resultsB1.add(event.readData()));
180+
eventSourceB.register(event -> resultsB2.add(event.readData()));
142181
eventSourceB.open();
143182

183+
Assert.assertTrue(resultsB1.waitBroadcast()); //some delay is required to process consumer and producer
184+
Assert.assertTrue(resultsB2.waitBroadcast()); //some delay is required to process consumer and producer
185+
144186
target().path("sse/push/secondBroadcast").request().get(String.class);
145187

146188
Assert.assertTrue("Waiting for resultsA1 to be complete failed.",
147-
a1Latch.await(LATCH_WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
189+
resultsA1.getEventCountDown().await(ASYNC_WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
148190
Assert.assertTrue("Waiting for resultsA2 to be complete failed.",
149-
a2Latch.await(LATCH_WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
191+
resultsA2.getEventCountDown().await(ASYNC_WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
150192

151193
Assert.assertTrue("Waiting for resultsB1 to be complete failed.",
152-
b1Latch.await(LATCH_WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
194+
resultsB1.getEventCountDown().await(ASYNC_WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
153195
Assert.assertTrue("Waiting for resultsB2 to be complete failed.",
154-
b2Latch.await(LATCH_WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
196+
resultsB2.getEventCountDown().await(ASYNC_WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
155197

156198
Assert.assertTrue(txLatch.await(5000, TimeUnit.MILLISECONDS));
157199

@@ -186,7 +228,7 @@ public void test() throws InterruptedException {
186228
&& resultsB2.get(2).equals("Event3")
187229
&& resultsB2.get(3).equals("secondBroadcast"));
188230
target().path("sse/close").request().get();
189-
Assert.assertTrue(closeLatch.await(LATCH_WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
231+
closeLatch.await();
190232
Assert.assertTrue("Sse instances injected into resource and constructor differ. Sse should have been injected"
191233
+ "as a singleton", isSingleton);
192234
}

tests/e2e-server/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<forkCount>1</forkCount>
4242
<reuseForks>false</reuseForks>
4343
<enableAssertions>false</enableAssertions>
44+
<skipTests>${skip.e2e}</skipTests>
4445
</configuration>
4546
</plugin>
4647
</plugins>

tests/e2e-testng/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<forkCount>1</forkCount>
4646
<reuseForks>false</reuseForks>
4747
<enableAssertions>false</enableAssertions>
48+
<skipTests>${skip.e2e}</skipTests>
4849
</configuration>
4950
<dependencies>
5051
<dependency>

tests/e2e/pom.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@
4141
<forkCount>1</forkCount>
4242
<reuseForks>false</reuseForks>
4343
<enableAssertions>false</enableAssertions>
44-
<excludes>
45-
<exclude>${exclude.sse.tests}</exclude>
46-
</excludes>
44+
<skipTests>${skip.e2e}</skipTests>
4745
</configuration>
4846
</plugin>
4947
</plugins>

0 commit comments

Comments
 (0)