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

Commit 307ffda

Browse files
author
Marek Potociar
committed
Fix for JERSEY-2223 and similar issue in server-async example test.
- Server async & connector timeout unit tests updated. - Minor changes in unit tests in attempt to resolve intermittent Hudson build failures caused mainly by newly added Jetty connector tests and async example tests. Change-Id: I94848a895a674d0f93133b4e4c5f9398baf8d687 Signed-off-by: Marek Potociar <[email protected]>
1 parent 6bd5179 commit 307ffda

File tree

6 files changed

+51
-48
lines changed

6 files changed

+51
-48
lines changed

connectors/apache-connector/src/test/java/org/glassfish/jersey/apache/connector/TimeoutTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@
5555
import org.glassfish.jersey.test.JerseyTest;
5656

5757
import org.junit.Test;
58+
import static org.hamcrest.CoreMatchers.instanceOf;
5859
import static org.junit.Assert.assertEquals;
59-
import static org.junit.Assert.fail;
60+
import static org.junit.Assert.assertThat;
6061

6162
/**
6263
* @author Martin Matula (martin.matula at oracle.com)
@@ -109,10 +110,8 @@ public void testSlow() {
109110
try {
110111
target("test/timeout").request().get();
111112
} catch (ProcessingException e) {
112-
if (!(e.getCause() instanceof SocketTimeoutException)) {
113-
e.printStackTrace();
114-
fail();
115-
}
113+
assertThat("Unexpected processing exception cause",
114+
e.getCause(), instanceOf(SocketTimeoutException.class));
116115
}
117116
}
118117
}

connectors/grizzly-connector/src/test/java/org/glassfish/jersey/grizzly/connector/TimeoutTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@
5353
import org.glassfish.jersey.test.JerseyTest;
5454

5555
import org.junit.Test;
56+
import static org.hamcrest.CoreMatchers.instanceOf;
5657
import static org.junit.Assert.assertEquals;
57-
import static org.junit.Assert.fail;
58+
import static org.junit.Assert.assertThat;
5859

5960
/**
6061
* @author Martin Matula (martin.matula at oracle.com)
@@ -102,10 +103,8 @@ public void testSlow() {
102103
try {
103104
target("test/timeout").request().get();
104105
} catch (ProcessingException e) {
105-
if (!(e.getCause() instanceof TimeoutException)) {
106-
e.printStackTrace();
107-
fail();
108-
}
106+
assertThat("Unexpected processing exception cause",
107+
e.getCause(), instanceOf(TimeoutException.class));
109108
}
110109
}
111110
}

connectors/jetty-connector/pom.xml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,34 +65,24 @@
6565
<artifactId>jetty-client</artifactId>
6666
</dependency>
6767

68-
<dependency>
69-
<groupId>org.glassfish.jersey.core</groupId>
70-
<artifactId>jersey-client</artifactId>
71-
<version>${project.version}</version>
72-
<type>jar</type>
73-
</dependency>
74-
7568
<dependency>
7669
<groupId>org.glassfish.jersey.containers</groupId>
7770
<artifactId>jersey-container-jetty-http</artifactId>
7871
<version>${project.version}</version>
7972
<scope>test</scope>
8073
</dependency>
81-
8274
<dependency>
8375
<groupId>org.glassfish.jersey.media</groupId>
8476
<artifactId>jersey-media-json-jackson</artifactId>
8577
<version>${jersey.version}</version>
8678
<scope>test</scope>
8779
</dependency>
88-
8980
<dependency>
9081
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
9182
<artifactId>jersey-test-framework-provider-jetty</artifactId>
9283
<version>${project.version}</version>
9384
<scope>test</scope>
9485
</dependency>
95-
9686
</dependencies>
9787

9888
<build>

connectors/jetty-connector/src/test/java/org/glassfish/jersey/jetty/connector/TimeoutTest.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
*/
4040
package org.glassfish.jersey.jetty.connector;
4141

42+
import java.net.NoRouteToHostException;
4243
import java.net.SocketTimeoutException;
4344
import java.net.URI;
4445
import java.net.URISyntaxException;
@@ -61,8 +62,10 @@
6162
import org.glassfish.jersey.test.JerseyTest;
6263

6364
import org.junit.Test;
65+
import static org.hamcrest.CoreMatchers.anyOf;
66+
import static org.hamcrest.CoreMatchers.instanceOf;
6467
import static org.junit.Assert.assertEquals;
65-
import static org.junit.Assert.fail;
68+
import static org.junit.Assert.assertThat;
6669

6770
/**
6871
* @author Martin Matula (martin.matula at oracle.com)
@@ -119,12 +122,11 @@ public void testSlow() {
119122
try {
120123
t.path("test/timeout").request().get();
121124
} catch (ProcessingException e) {
122-
if (!(e.getCause() instanceof TimeoutException)) {
123-
e.printStackTrace();
124-
fail();
125-
}
125+
assertThat("Unexpected processing exception cause",
126+
e.getCause(), instanceOf(TimeoutException.class));
127+
} finally {
128+
c.close();
126129
}
127-
c.close();
128130
}
129131

130132
@Test
@@ -137,11 +139,11 @@ public void testUnknownHost() throws URISyntaxException {
137139
try {
138140
target.request().get();
139141
} catch (ProcessingException e) {
140-
if (!(e.getCause().getCause() instanceof SocketTimeoutException)) {
141-
e.printStackTrace();
142-
fail();
143-
}
142+
assertThat("Unexpected processing exception cause",
143+
e.getCause().getCause(),
144+
anyOf(instanceOf(SocketTimeoutException.class), instanceOf(NoRouteToHostException.class)));
145+
} finally {
146+
c.close();
144147
}
145-
c.close();
146148
}
147149
}

connectors/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,10 @@
8484
<artifactId>junit</artifactId>
8585
<scope>test</scope>
8686
</dependency>
87+
<dependency>
88+
<groupId>org.hamcrest</groupId>
89+
<artifactId>hamcrest-library</artifactId>
90+
<scope>test</scope>
91+
</dependency>
8792
</dependencies>
8893
</project>

examples/server-async/src/test/java/org/glassfish/jersey/examples/server/async/AsyncResourceTest.java

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@
3939
*/
4040
package org.glassfish.jersey.examples.server.async;
4141

42+
import java.util.ArrayList;
4243
import java.util.Collection;
44+
import java.util.Collections;
45+
import java.util.Comparator;
4346
import java.util.LinkedList;
4447
import java.util.List;
4548
import java.util.Map;
@@ -96,7 +99,7 @@ private void executeChatTest(final WebTarget resourceTarget, final String expect
9699
final Object sequentialPostLock = new Object();
97100

98101
final ExecutorService executor = Executors.newCachedThreadPool(
99-
new ThreadFactoryBuilder().setNameFormat("async-resource-test-%d").build());
102+
new ThreadFactoryBuilder().setNameFormat("async-resource-test-%02d").build());
100103

101104
final Map<Integer, String> postResponses = new ConcurrentHashMap<Integer, String>();
102105
final Map<Integer, String> getResponses = new ConcurrentHashMap<Integer, String>();
@@ -127,7 +130,7 @@ private void post() {
127130
attemptCounter++;
128131
try {
129132
final String response = resourceTarget.request()
130-
.post(Entity.text("" + requestId), String.class);
133+
.post(Entity.text(String.format("%02d", requestId)), String.class);
131134
postResponses.put(requestId, response);
132135
break;
133136
} catch (Throwable t) {
@@ -206,29 +209,27 @@ private void get() {
206209

207210
StringBuilder messageBuilder = new StringBuilder("POST responses received: ").append(postResponses.size()).append("\n");
208211
for (Map.Entry<Integer, String> postResponseEntry : postResponses.entrySet()) {
209-
messageBuilder.append("POST response for message ")
210-
.append(postResponseEntry.getKey()).append(": ")
212+
messageBuilder.append(String.format("POST response for message %02d: ", postResponseEntry.getKey()))
211213
.append(postResponseEntry.getValue()).append('\n');
212214
}
213215
messageBuilder.append('\n');
214216
messageBuilder.append("GET responses received: ").append(getResponses.size()).append("\n");
215217
for (Map.Entry<Integer, String> getResponseEntry : getResponses.entrySet()) {
216-
messageBuilder.append("GET response for message ")
217-
.append(getResponseEntry.getKey()).append(": ")
218+
messageBuilder.append(String.format("GET response for message %02d: ", getResponseEntry.getKey()))
218219
.append(getResponseEntry.getValue()).append('\n');
219220
}
220221
LOGGER.info(messageBuilder.toString());
221222

222223
for (Map.Entry<Integer, String> postResponseEntry : postResponses.entrySet()) {
223224
assertEquals(
224-
"Unexpected POST notification response for message " + postResponseEntry.getKey(),
225+
String.format("Unexpected POST notification response for message %02d", postResponseEntry.getKey()),
225226
expectedPostResponse, postResponseEntry.getValue());
226227
}
227228

228229
final List<Integer> lost = new LinkedList<Integer>();
229230
final Collection<String> getResponseValues = getResponses.values();
230231
for (int i = 0; i < MAX_MESSAGES; i++) {
231-
if (!getResponseValues.contains("" + i)) {
232+
if (!getResponseValues.contains(String.format("%02d", i))) {
232233
lost.add(i);
233234
}
234235
}
@@ -245,13 +246,13 @@ public void testLongRunningResource() throws InterruptedException {
245246
final String expectedResponse = SimpleLongRunningResource.NOTIFICATION_RESPONSE;
246247

247248
final int MAX_MESSAGES = 100;
248-
final int LATCH_WAIT_TIMEOUT = 10;
249+
final int LATCH_WAIT_TIMEOUT = 25;
249250
final boolean debugMode = false;
250251
final boolean sequentialGet = false;
251252
final Object sequentialGetLock = new Object();
252253

253254
final ExecutorService executor = Executors.newCachedThreadPool(
254-
new ThreadFactoryBuilder().setNameFormat("async-resource-test-%d").build());
255+
new ThreadFactoryBuilder().setNameFormat("async-resource-test-%02d").build());
255256

256257
final Map<Integer, String> getResponses = new ConcurrentHashMap<Integer, String>();
257258

@@ -312,22 +313,29 @@ private void get() {
312313
executor.shutdownNow();
313314
}
314315

315-
StringBuilder messageBuilder = new StringBuilder("GET responses received: ").append(getResponses.size()).append("\n");
316-
for (Map.Entry<Integer, String> getResponseEntry : getResponses.entrySet()) {
317-
messageBuilder.append("GET response for message ")
318-
.append(getResponseEntry.getKey()).append(": ")
316+
final ArrayList<Map.Entry<Integer, String>> responseEntryList =
317+
new ArrayList<Map.Entry<Integer, String>>(getResponses.entrySet());
318+
Collections.sort(responseEntryList,
319+
new Comparator<Map.Entry<Integer, String>>() {
320+
@Override
321+
public int compare(Map.Entry<Integer, String> o1, Map.Entry<Integer, String> o2) {
322+
return o1.getKey().compareTo(o2.getKey());
323+
}
324+
});
325+
StringBuilder messageBuilder = new StringBuilder("GET responses received: ").append(responseEntryList.size()).append("\n");
326+
for (Map.Entry<Integer, String> getResponseEntry : responseEntryList) {
327+
messageBuilder.append(String.format("GET response for message %02d: ", getResponseEntry.getKey()))
319328
.append(getResponseEntry.getValue()).append('\n');
320329
}
321330
LOGGER.info(messageBuilder.toString());
322331

323-
for (Map.Entry<Integer, String> entry : getResponses.entrySet()) {
332+
for (Map.Entry<Integer, String> entry : responseEntryList) {
324333
assertEquals(
325-
"Unexpected GET notification response for message " + entry.getKey(),
334+
String.format("Unexpected GET notification response for message %02d", entry.getKey()),
326335
expectedResponse, entry.getValue());
327336
}
328337
assertEquals(MAX_MESSAGES, getResponses.size());
329338
}
330339

331340

332-
333341
}

0 commit comments

Comments
 (0)