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,26 @@ 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
+
88
109
@ Override
89
110
protected void configureClient (ClientConfig config ) {
111
+ connectionManager = new PoolingHttpClientConnectionManager ();
112
+ config .property (ApacheClientProperties .CONNECTION_MANAGER , connectionManager );
90
113
config .connectorProvider (new ApacheConnectorProvider ());
91
114
}
92
115
@@ -118,5 +141,12 @@ public String sendEvent() {
118
141
public ChunkedOutput <String > get () {
119
142
return output ;
120
143
}
144
+
145
+ @ GET
146
+ @ Path ("get" )
147
+ @ Produces (MediaType .TEXT_PLAIN )
148
+ public String getString () {
149
+ return "OK" ;
150
+ }
121
151
}
122
152
}
0 commit comments