25
25
import javax .ws .rs .client .WebTarget ;
26
26
import javax .ws .rs .core .Application ;
27
27
import javax .ws .rs .core .MediaType ;
28
-
28
+ import javax . ws . rs . core . Response ;
29
29
import javax .inject .Singleton ;
30
30
31
+ import org .apache .http .impl .conn .PoolingHttpClientConnectionManager ;
32
+
31
33
import org .glassfish .jersey .client .ClientConfig ;
34
+ import org .glassfish .jersey .client .ClientProperties ;
32
35
import org .glassfish .jersey .server .ChunkedOutput ;
33
36
import org .glassfish .jersey .server .ResourceConfig ;
34
37
import org .glassfish .jersey .test .JerseyTest ;
40
43
* @author Petr Janouch (petr.janouch at oracle.com)
41
44
*/
42
45
public class StreamingTest extends JerseyTest {
46
+ private PoolingHttpClientConnectionManager connectionManager ;
43
47
44
48
/**
45
49
* Test that a data stream can be terminated from the client side.
46
50
*/
47
51
@ Test
48
52
public void clientCloseTest () throws IOException {
49
53
// start streaming
50
- InputStream inputStream = target ().path ("/streamingEndpoint" ).request ().get (InputStream .class );
54
+ InputStream inputStream = target ().path ("/streamingEndpoint" ).request ()
55
+ .property (ClientProperties .READ_TIMEOUT , 1_000 ).get (InputStream .class );
51
56
52
57
WebTarget sendTarget = target ().path ("/streamingEndpoint/send" );
53
58
// trigger sending 'A' to the stream; OK is sent if everything on the server was OK
@@ -61,8 +66,35 @@ public void clientCloseTest() throws IOException {
61
66
assertEquals ("NOK" , sendTarget .request ().get ().readEntity (String .class ));
62
67
}
63
68
69
+ /**
70
+ * Tests that closing a response after completely reading the entity reuses the connection
71
+ */
72
+ @ Test
73
+ public void reuseConnectionTest () throws IOException {
74
+ Response response = target ().path ("/streamingEndpoint/get" ).request ().get ();
75
+ InputStream is = response .readEntity (InputStream .class );
76
+ byte [] buf = new byte [8192 ];
77
+ is .read (buf );
78
+ is .close ();
79
+ response .close ();
80
+
81
+ assertEquals (1 , connectionManager .getTotalStats ().getAvailable ());
82
+ assertEquals (0 , connectionManager .getTotalStats ().getLeased ());
83
+ }
84
+
85
+ /**
86
+ * Tests that closing a request without reading the entity does not throw an exception.
87
+ */
88
+ @ Test
89
+ public void clientCloseThrowsNoExceptionTest () throws IOException {
90
+ Response response = target ().path ("/streamingEndpoint/get" ).request ().get ();
91
+ response .close ();
92
+ }
93
+
64
94
@ Override
65
95
protected void configureClient (ClientConfig config ) {
96
+ connectionManager = new PoolingHttpClientConnectionManager ();
97
+ config .property (ApacheClientProperties .CONNECTION_MANAGER , connectionManager );
66
98
config .connectorProvider (new ApacheConnectorProvider ());
67
99
}
68
100
@@ -94,5 +126,12 @@ public String sendEvent() {
94
126
public ChunkedOutput <String > get () {
95
127
return output ;
96
128
}
129
+
130
+ @ GET
131
+ @ Path ("get" )
132
+ @ Produces (MediaType .TEXT_PLAIN )
133
+ public String getString () {
134
+ return "OK" ;
135
+ }
97
136
}
98
137
}
0 commit comments