49
49
import javax .ws .rs .client .WebTarget ;
50
50
import javax .ws .rs .core .Application ;
51
51
import javax .ws .rs .core .MediaType ;
52
-
52
+ import javax . ws . rs . core . Response ;
53
53
import javax .inject .Singleton ;
54
54
55
+ import org .apache .http .impl .conn .PoolingHttpClientConnectionManager ;
56
+
55
57
import org .glassfish .jersey .client .ClientConfig ;
58
+ import org .glassfish .jersey .client .ClientProperties ;
56
59
import org .glassfish .jersey .server .ChunkedOutput ;
57
60
import org .glassfish .jersey .server .ResourceConfig ;
58
61
import org .glassfish .jersey .test .JerseyTest ;
64
67
* @author Petr Janouch (petr.janouch at oracle.com)
65
68
*/
66
69
public class StreamingTest extends JerseyTest {
70
+ private PoolingHttpClientConnectionManager connectionManager ;
67
71
68
72
/**
69
73
* Test that a data stream can be terminated from the client side.
70
74
*/
71
75
@ Test
72
76
public void clientCloseTest () throws IOException {
73
77
// start streaming
74
- InputStream inputStream = target ().path ("/streamingEndpoint" ).request ().get (InputStream .class );
78
+ InputStream inputStream = target ().path ("/streamingEndpoint" ).request ()
79
+ .property (ClientProperties .READ_TIMEOUT , 1_000 ).get (InputStream .class );
75
80
76
81
WebTarget sendTarget = target ().path ("/streamingEndpoint/send" );
77
82
// trigger sending 'A' to the stream; OK is sent if everything on the server was OK
@@ -85,8 +90,35 @@ public void clientCloseTest() throws IOException {
85
90
assertEquals ("NOK" , sendTarget .request ().get ().readEntity (String .class ));
86
91
}
87
92
93
+ /**
94
+ * Tests that closing a response after completely reading the entity reuses the connection
95
+ */
96
+ @ Test
97
+ public void reuseConnectionTest () throws IOException {
98
+ Response response = target ().path ("/streamingEndpoint/get" ).request ().get ();
99
+ InputStream is = response .readEntity (InputStream .class );
100
+ byte [] buf = new byte [8192 ];
101
+ is .read (buf );
102
+ is .close ();
103
+ response .close ();
104
+
105
+ assertEquals (1 , connectionManager .getTotalStats ().getAvailable ());
106
+ assertEquals (0 , connectionManager .getTotalStats ().getLeased ());
107
+ }
108
+
109
+ /**
110
+ * Tests that closing a request without reading the entity does not throw an exception.
111
+ */
112
+ @ Test
113
+ public void clientCloseThrowsNoExceptionTest () throws IOException {
114
+ Response response = target ().path ("/streamingEndpoint/get" ).request ().get ();
115
+ response .close ();
116
+ }
117
+
88
118
@ Override
89
119
protected void configureClient (ClientConfig config ) {
120
+ connectionManager = new PoolingHttpClientConnectionManager ();
121
+ config .property (ApacheClientProperties .CONNECTION_MANAGER , connectionManager );
90
122
config .connectorProvider (new ApacheConnectorProvider ());
91
123
}
92
124
@@ -118,5 +150,12 @@ public String sendEvent() {
118
150
public ChunkedOutput <String > get () {
119
151
return output ;
120
152
}
153
+
154
+ @ GET
155
+ @ Path ("get" )
156
+ @ Produces (MediaType .TEXT_PLAIN )
157
+ public String getString () {
158
+ return "OK" ;
159
+ }
121
160
}
122
161
}
0 commit comments