@@ -55,22 +55,27 @@ public static Iterable<Object[]> data() {
55
55
56
56
Function <Vertx , HttpServer > http1ServerFactory = (v ) -> Providers .http1Server (v , INBOUND_LIMIT , OUTBOUND_LIMIT );
57
57
Function <Vertx , HttpServer > http2ServerFactory = (v ) -> Providers .http2Server (v , INBOUND_LIMIT , OUTBOUND_LIMIT );
58
+ Function <Vertx , HttpServer > http1NonTrafficShapedServerFactory = (v ) -> Providers .http1Server (v , 0 , 0 );
59
+ Function <Vertx , HttpServer > http2NonTrafficShapedServerFactory = (v ) -> Providers .http1Server (v , 0 , 0 );
58
60
Function <Vertx , HttpClient > http1ClientFactory = (v ) -> v .createHttpClient ();
59
61
Function <Vertx , HttpClient > http2ClientFactory = (v ) -> v .createHttpClient (createHttp2ClientOptions ());
60
62
61
63
return Arrays .asList (new Object [][] {
62
- { 1.1 , http1ServerFactory , http1ClientFactory },
63
- { 2.0 , http2ServerFactory , http2ClientFactory }
64
+ { 1.1 , http1ServerFactory , http1ClientFactory , http1NonTrafficShapedServerFactory },
65
+ { 2.0 , http2ServerFactory , http2ClientFactory , http2NonTrafficShapedServerFactory }
64
66
});
65
67
}
66
68
67
69
private Function <Vertx , HttpServer > serverFactory ;
68
70
private Function <Vertx , HttpClient > clientFactory ;
71
+ private Function <Vertx , HttpServer > nonTrafficShapedServerFactory ;
69
72
70
73
public HttpBandwidthLimitingTest (double protoVersion , Function <Vertx , HttpServer > serverFactory ,
71
- Function <Vertx , HttpClient > clientFactory ) {
74
+ Function <Vertx , HttpClient > clientFactory ,
75
+ Function <Vertx , HttpServer > nonTrafficShapedServerFactory ) {
72
76
this .serverFactory = serverFactory ;
73
77
this .clientFactory = clientFactory ;
78
+ this .nonTrafficShapedServerFactory = nonTrafficShapedServerFactory ;
74
79
}
75
80
76
81
@ Before
@@ -199,6 +204,63 @@ public void start(Promise<Void> startPromise) {
199
204
Assert .assertTrue (elapsedMillis > expectedTimeMillis (totalReceivedLength .get (), OUTBOUND_LIMIT )); // because there are simultaneous 2 requests
200
205
}
201
206
207
+ @ Test
208
+ public void testDynamicOutboundRateUpdate () throws Exception {
209
+ Buffer expectedBuffer = TestUtils .randomBuffer (TEST_CONTENT_SIZE );
210
+
211
+ HttpServer testServer = serverFactory .apply (vertx );
212
+ testServer .requestHandler (HANDLERS .bufferRead (expectedBuffer ));
213
+ startServer (testServer );
214
+
215
+ // update outbound rate to twice the limit
216
+ TrafficShapingOptions trafficOptions = new TrafficShapingOptions ()
217
+ .setInboundGlobalBandwidth (INBOUND_LIMIT ) // unchanged
218
+ .setOutboundGlobalBandwidth (2 * OUTBOUND_LIMIT );
219
+ testServer .updateTrafficShapingOptions (trafficOptions );
220
+
221
+ long startTime = System .nanoTime ();
222
+ HttpClient testClient = clientFactory .apply (vertx );
223
+ read (expectedBuffer , testServer , testClient );
224
+ await ();
225
+ long elapsedMillis = TimeUnit .NANOSECONDS .toMillis (System .nanoTime () - startTime );
226
+
227
+ Assert .assertTrue (elapsedMillis < expectedUpperBoundTimeMillis (TEST_CONTENT_SIZE , OUTBOUND_LIMIT ));
228
+ }
229
+
230
+ @ Test
231
+ public void testDynamicInboundRateUpdate () throws Exception {
232
+ Buffer expectedBuffer = TestUtils .randomBuffer ((TEST_CONTENT_SIZE ));
233
+
234
+ HttpServer testServer = serverFactory .apply (vertx );
235
+ testServer .requestHandler (HANDLERS .bufferWrite (expectedBuffer ));
236
+ startServer (testServer );
237
+
238
+ // update inbound rate to twice the limit
239
+ TrafficShapingOptions trafficOptions = new TrafficShapingOptions ()
240
+ .setOutboundGlobalBandwidth (OUTBOUND_LIMIT ) // unchanged
241
+ .setInboundGlobalBandwidth (2 * INBOUND_LIMIT );
242
+ testServer .updateTrafficShapingOptions (trafficOptions );
243
+
244
+ long startTime = System .nanoTime ();
245
+ HttpClient testClient = clientFactory .apply (vertx );
246
+ write (expectedBuffer , testServer , testClient );
247
+ await ();
248
+ long elapsedMillis = TimeUnit .NANOSECONDS .toMillis (System .nanoTime () - startTime );
249
+
250
+ Assert .assertTrue (elapsedMillis < expectedUpperBoundTimeMillis (TEST_CONTENT_SIZE , INBOUND_LIMIT ));
251
+ }
252
+
253
+ @ Test (expected = IllegalStateException .class )
254
+ public void testRateUpdateWhenServerStartedWithoutTrafficShaping () {
255
+ HttpServer testServer = nonTrafficShapedServerFactory .apply (vertx );
256
+
257
+ // update inbound rate to twice the limit
258
+ TrafficShapingOptions trafficOptions = new TrafficShapingOptions ()
259
+ .setOutboundGlobalBandwidth (OUTBOUND_LIMIT )
260
+ .setInboundGlobalBandwidth (2 * INBOUND_LIMIT );
261
+ testServer .updateTrafficShapingOptions (trafficOptions );
262
+ }
263
+
202
264
/**
203
265
* The throttling takes a while to kick in so the expected time cannot be strict especially
204
266
* for small data sizes in these tests.
@@ -211,6 +273,10 @@ private long expectedTimeMillis(long size, int rate) {
211
273
return (long ) (TimeUnit .MILLISECONDS .convert (( size / rate ), TimeUnit .SECONDS ) * 0.5 ); // multiplied by 0.5 to be more tolerant of time pauses during CI runs
212
274
}
213
275
276
+ private long expectedUpperBoundTimeMillis (long size , int rate ) {
277
+ return TimeUnit .MILLISECONDS .convert (( size / rate ), TimeUnit .SECONDS ); // Since existing rate will be upperbound, runs should complete by this time
278
+ }
279
+
214
280
private void read (Buffer expected , HttpServer server , HttpClient client ) {
215
281
client .request (HttpMethod .GET , server .actualPort (), DEFAULT_HTTP_HOST ,"/buffer-read" )
216
282
.compose (req -> req .send ()
@@ -280,19 +346,25 @@ static class Providers {
280
346
private static HttpServer http1Server (Vertx vertx , int inboundLimit , int outboundLimit ) {
281
347
HttpServerOptions options = new HttpServerOptions ()
282
348
.setHost (DEFAULT_HTTP_HOST )
283
- .setPort (DEFAULT_HTTP_PORT )
284
- .setTrafficShapingOptions (new TrafficShapingOptions ()
285
- .setInboundGlobalBandwidth (inboundLimit )
286
- .setOutboundGlobalBandwidth (outboundLimit ));
349
+ .setPort (DEFAULT_HTTP_PORT );
350
+
351
+ if (inboundLimit != 0 || outboundLimit != 0 ) {
352
+ options .setTrafficShapingOptions (new TrafficShapingOptions ()
353
+ .setInboundGlobalBandwidth (inboundLimit )
354
+ .setOutboundGlobalBandwidth (outboundLimit ));
355
+ }
287
356
288
357
return vertx .createHttpServer (options );
289
358
}
290
359
291
360
private static HttpServer http2Server (Vertx vertx , int inboundLimit , int outboundLimit ) {
292
- HttpServerOptions options = createHttp2ServerOptions (DEFAULT_HTTP_PORT , DEFAULT_HTTP_HOST )
293
- .setTrafficShapingOptions (new TrafficShapingOptions ()
294
- .setInboundGlobalBandwidth (inboundLimit )
295
- .setOutboundGlobalBandwidth (outboundLimit ));
361
+ HttpServerOptions options = createHttp2ServerOptions (DEFAULT_HTTP_PORT , DEFAULT_HTTP_HOST );
362
+
363
+ if (inboundLimit != 0 || outboundLimit != 0 ) {
364
+ options .setTrafficShapingOptions (new TrafficShapingOptions ()
365
+ .setInboundGlobalBandwidth (inboundLimit )
366
+ .setOutboundGlobalBandwidth (outboundLimit ));
367
+ }
296
368
297
369
return vertx .createHttpServer (options );
298
370
}
0 commit comments