Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit 74d6c2c

Browse files
author
Petr Janouch
committed
Adding a test for empty SSE events
Change-Id: I8810067d96d421f562aa7cabab16549eb9ef1ffc
1 parent ed55856 commit 74d6c2c

File tree

1 file changed

+183
-0
lines changed

1 file changed

+183
-0
lines changed
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
41+
package org.glassfish.jersey.media.sse;
42+
43+
import java.io.UnsupportedEncodingException;
44+
import java.util.ArrayList;
45+
import java.util.LinkedList;
46+
import java.util.List;
47+
import java.util.concurrent.CountDownLatch;
48+
import java.util.concurrent.TimeUnit;
49+
50+
import javax.ws.rs.GET;
51+
import javax.ws.rs.Path;
52+
import javax.ws.rs.Produces;
53+
import javax.ws.rs.client.WebTarget;
54+
import javax.ws.rs.core.Application;
55+
56+
import org.glassfish.jersey.client.ClientConfig;
57+
import org.glassfish.jersey.server.ResourceConfig;
58+
import org.glassfish.jersey.test.JerseyTest;
59+
60+
import org.junit.Test;
61+
import static org.junit.Assert.assertTrue;
62+
63+
/**
64+
* Tests handling of empty SSE events.
65+
*/
66+
public class EmptyEventsTest extends JerseyTest {
67+
68+
@Override
69+
protected Application configure() {
70+
return new ResourceConfig(SseTestResource.class, SseFeature.class);
71+
}
72+
73+
@Override
74+
protected void configureClient(ClientConfig config) {
75+
config.register(SseFeature.class);
76+
}
77+
78+
/**
79+
* Tests a situation when 2 non-empty SSE events are separated with an empty one.
80+
*/
81+
@Test
82+
public void test1EmptyEvent() throws InterruptedException {
83+
doTest("sse/1");
84+
}
85+
86+
/**
87+
* Tests a situation when 2 non-empty SSE events are separated with 2 empty ones.
88+
*/
89+
@Test
90+
public void test2EmptyEvents() throws InterruptedException {
91+
doTest("sse/2");
92+
}
93+
94+
/**
95+
* Tests a situation when 2 non-empty SSE events are separated with 3 empty ones.
96+
*/
97+
@Test
98+
public void test3EmptyEvents() throws InterruptedException {
99+
doTest("sse/3");
100+
}
101+
102+
private void doTest(String target) throws InterruptedException {
103+
List<String> receivedNames = new ArrayList<>();
104+
List<String> receivedData = new LinkedList<>();
105+
106+
WebTarget sseTarget = target(target);
107+
CountDownLatch latch = new CountDownLatch(2);
108+
new EventSource(sseTarget) {
109+
@Override
110+
public void onEvent(InboundEvent inboundEvent) {
111+
receivedNames.add(inboundEvent.getName());
112+
try {
113+
receivedData.add(new String(inboundEvent.getRawData(), "ASCII"));
114+
} catch (UnsupportedEncodingException e) {
115+
e.printStackTrace();
116+
}
117+
118+
latch.countDown();
119+
}
120+
};
121+
122+
assertTrue(latch.await(5, TimeUnit.SECONDS));
123+
assertTrue(receivedNames.contains("e1"));
124+
assertTrue(receivedNames.contains("e2"));
125+
assertTrue(receivedData.contains("d1"));
126+
assertTrue(receivedData.contains("d2"));
127+
}
128+
129+
@Path("/sse")
130+
public static class SseTestResource {
131+
132+
@GET
133+
@Path("/1")
134+
@Produces("text/event-stream;charset=utf-8")
135+
public String send1EmptyEvent() {
136+
return "event: e1\r\n"
137+
+ "data: d1\r\n"
138+
+ "\r\n"
139+
// end of e1
140+
+ "\r\n"
141+
// end of an empty event
142+
+ "event: e2\r\n"
143+
+ "data: d2\r\n"
144+
+ "\r\n";
145+
}
146+
147+
@GET
148+
@Path("/2")
149+
@Produces("text/event-stream;charset=utf-8")
150+
public String send2EmptyEvents() {
151+
return "event: e1\r\n"
152+
+ "data: d1\r\n"
153+
+ "\r\n"
154+
// end of e1
155+
+ "\r\n"
156+
// end of an empty event
157+
+ "\r\n"
158+
// end of an empty event
159+
+ "event: e2\r\n"
160+
+ "data: d2\r\n"
161+
+ "\r\n";
162+
}
163+
164+
@GET
165+
@Path("/3")
166+
@Produces("text/event-stream;charset=utf-8")
167+
public String send3EmptyEvents() {
168+
return "event: e1\r\n"
169+
+ "data: d1\r\n"
170+
+ "\r\n"
171+
// end of e1
172+
+ "\r\n"
173+
// end of an empty event
174+
+ "\r\n"
175+
// end of an empty event
176+
+ "\r\n"
177+
// end of an empty event
178+
+ "event: e2\r\n"
179+
+ "data: d2\r\n"
180+
+ "\r\n";
181+
}
182+
}
183+
}

0 commit comments

Comments
 (0)