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