23
23
import static org .mockito .Mockito .atLeast ;
24
24
import static org .mockito .Mockito .times ;
25
25
26
+ import com .google .api .gax .grpc .BundlingSettings ;
26
27
import com .google .cloud .pubsub .Publisher .Builder ;
27
28
import com .google .common .base .Optional ;
28
29
import com .google .common .util .concurrent .ListenableFuture ;
@@ -99,9 +100,13 @@ public static void tearDownClass() throws Exception {
99
100
public void testPublishByDuration () throws Exception {
100
101
Publisher publisher =
101
102
getTestPublisherBuilder ()
102
- .setMaxBundleDuration (Duration .standardSeconds (5 ))
103
103
// To demonstrate that reaching duration will trigger publish
104
- .setMaxBundleMessages (10 )
104
+ .setBundlingSettings (
105
+ Publisher .DEFAULT_BUNDLING_SETTINGS
106
+ .toBuilder ()
107
+ .setDelayThreshold (Duration .standardSeconds (5 ))
108
+ .setElementCountThreshold (10 )
109
+ .build ())
105
110
.build ();
106
111
107
112
testPublisherServiceImpl .addPublishResponse (
@@ -127,8 +132,12 @@ public void testPublishByDuration() throws Exception {
127
132
public void testPublishByNumBundledMessages () throws Exception {
128
133
Publisher publisher =
129
134
getTestPublisherBuilder ()
130
- .setMaxBundleDuration (Duration .standardSeconds (100 ))
131
- .setMaxBundleMessages (2 )
135
+ .setBundlingSettings (
136
+ Publisher .DEFAULT_BUNDLING_SETTINGS
137
+ .toBuilder ()
138
+ .setElementCountThreshold (2 )
139
+ .setDelayThreshold (Duration .standardSeconds (100 ))
140
+ .build ())
132
141
.build ();
133
142
134
143
testPublisherServiceImpl
@@ -162,8 +171,12 @@ public void testPublishByNumBundledMessages() throws Exception {
162
171
public void testSinglePublishByNumBytes () throws Exception {
163
172
Publisher publisher =
164
173
getTestPublisherBuilder ()
165
- .setMaxBundleDuration (Duration .standardSeconds (100 ))
166
- .setMaxBundleMessages (2 )
174
+ .setBundlingSettings (
175
+ Publisher .DEFAULT_BUNDLING_SETTINGS
176
+ .toBuilder ()
177
+ .setElementCountThreshold (2 )
178
+ .setDelayThreshold (Duration .standardSeconds (100 ))
179
+ .build ())
167
180
.build ();
168
181
169
182
testPublisherServiceImpl
@@ -192,9 +205,13 @@ public void testSinglePublishByNumBytes() throws Exception {
192
205
public void testPublishMixedSizeAndDuration () throws Exception {
193
206
Publisher publisher =
194
207
getTestPublisherBuilder ()
195
- .setMaxBundleDuration (Duration .standardSeconds (5 ))
196
208
// To demonstrate that reaching duration will trigger publish
197
- .setMaxBundleMessages (2 )
209
+ .setBundlingSettings (
210
+ Publisher .DEFAULT_BUNDLING_SETTINGS
211
+ .toBuilder ()
212
+ .setElementCountThreshold (2 )
213
+ .setDelayThreshold (Duration .standardSeconds (5 ))
214
+ .build ())
198
215
.build ();
199
216
200
217
testPublisherServiceImpl .addPublishResponse (
@@ -237,8 +254,12 @@ public void testPublishFailureRetries() throws Exception {
237
254
Publisher publisher =
238
255
getTestPublisherBuilder ()
239
256
.setExecutor (Executors .newSingleThreadScheduledExecutor ())
240
- .setMaxBundleDuration (Duration .standardSeconds (5 ))
241
- .setMaxBundleMessages (1 )
257
+ .setBundlingSettings (
258
+ Publisher .DEFAULT_BUNDLING_SETTINGS
259
+ .toBuilder ()
260
+ .setElementCountThreshold (1 )
261
+ .setDelayThreshold (Duration .standardSeconds (5 ))
262
+ .build ())
242
263
.build (); // To demonstrate that reaching duration will trigger publish
243
264
244
265
ListenableFuture <String > publishFuture1 = sendTestMessage (publisher , "A" );
@@ -258,8 +279,12 @@ public void testPublishFailureRetries_exceededsRetryDuration() throws Exception
258
279
getTestPublisherBuilder ()
259
280
.setExecutor (Executors .newSingleThreadScheduledExecutor ())
260
281
.setSendBundleDeadline (Duration .standardSeconds (10 ))
261
- .setMaxBundleDuration (Duration .standardSeconds (5 ))
262
- .setMaxBundleMessages (1 )
282
+ .setBundlingSettings (
283
+ Publisher .DEFAULT_BUNDLING_SETTINGS
284
+ .toBuilder ()
285
+ .setElementCountThreshold (1 )
286
+ .setDelayThreshold (Duration .standardSeconds (5 ))
287
+ .build ())
263
288
.build (); // To demonstrate that reaching duration will trigger publish
264
289
265
290
ListenableFuture <String > publishFuture1 = sendTestMessage (publisher , "A" );
@@ -283,8 +308,12 @@ public void testPublishFailureRetries_nonRetryableFailsImmediately() throws Exce
283
308
getTestPublisherBuilder ()
284
309
.setExecutor (Executors .newSingleThreadScheduledExecutor ())
285
310
.setSendBundleDeadline (Duration .standardSeconds (10 ))
286
- .setMaxBundleDuration (Duration .standardSeconds (5 ))
287
- .setMaxBundleMessages (1 )
311
+ .setBundlingSettings (
312
+ Publisher .DEFAULT_BUNDLING_SETTINGS
313
+ .toBuilder ()
314
+ .setElementCountThreshold (1 )
315
+ .setDelayThreshold (Duration .standardSeconds (5 ))
316
+ .build ())
288
317
.build (); // To demonstrate that reaching duration will trigger publish
289
318
290
319
ListenableFuture <String > publishFuture1 = sendTestMessage (publisher , "A" );
@@ -309,9 +338,12 @@ public void testPublisherGetters() throws Exception {
309
338
builder .setCredentials (credentials );
310
339
builder .setExecutor (executor );
311
340
builder .setFailOnFlowControlLimits (true );
312
- builder .setMaxBundleBytes (10 );
313
- builder .setMaxBundleDuration (new Duration (11 ));
314
- builder .setMaxBundleMessages (12 );
341
+ builder .setBundlingSettings (
342
+ BundlingSettings .newBuilder ()
343
+ .setRequestByteThreshold (10 )
344
+ .setDelayThreshold (new Duration (11 ))
345
+ .setElementCountThreshold (12 )
346
+ .build ());
315
347
builder .setMaxOutstandingBytes (13 );
316
348
builder .setMaxOutstandingMessages (14 );
317
349
builder .setRequestTimeout (new Duration (15 ));
@@ -334,9 +366,14 @@ public void testBuilderParametersAndDefaults() {
334
366
assertEquals (Optional .absent (), builder .channelBuilder );
335
367
assertEquals (Optional .absent (), builder .executor );
336
368
assertFalse (builder .failOnFlowControlLimits );
337
- assertEquals (Publisher .DEFAULT_MAX_BUNDLE_BYTES , builder .maxBundleBytes );
338
- assertEquals (Publisher .DEFAULT_MAX_BUNDLE_DURATION , builder .maxBundleDuration );
339
- assertEquals (Publisher .DEFAULT_MAX_BUNDLE_MESSAGES , builder .maxBundleMessages );
369
+ assertEquals (
370
+ Publisher .DEFAULT_MAX_BUNDLE_BYTES ,
371
+ builder .bundlingSettings .getRequestByteThreshold ().longValue ());
372
+ assertEquals (
373
+ Publisher .DEFAULT_MAX_BUNDLE_DURATION , builder .bundlingSettings .getDelayThreshold ());
374
+ assertEquals (
375
+ Publisher .DEFAULT_MAX_BUNDLE_MESSAGES ,
376
+ builder .bundlingSettings .getElementCountThreshold ().longValue ());
340
377
assertEquals (Optional .absent (), builder .maxOutstandingBytes );
341
378
assertEquals (Optional .absent (), builder .maxOutstandingMessages );
342
379
assertEquals (Publisher .DEFAULT_REQUEST_TIMEOUT , builder .requestTimeout );
@@ -369,41 +406,66 @@ public void testBuilderInvalidArguments() {
369
406
// Expected
370
407
}
371
408
try {
372
- builder .setMaxBundleBytes (0 );
409
+ builder .setBundlingSettings (
410
+ Publisher .DEFAULT_BUNDLING_SETTINGS .toBuilder ().setRequestByteThreshold (null ).build ());
411
+ fail ("Should have thrown an NullPointerException" );
412
+ } catch (NullPointerException expected ) {
413
+ // Expected
414
+ }
415
+ try {
416
+ builder .setBundlingSettings (
417
+ Publisher .DEFAULT_BUNDLING_SETTINGS .toBuilder ().setRequestByteThreshold (0 ).build ());
373
418
fail ("Should have thrown an IllegalArgumentException" );
374
419
} catch (IllegalArgumentException expected ) {
375
420
// Expected
376
421
}
377
422
try {
378
- builder .setMaxBundleBytes (-1 );
423
+ builder .setBundlingSettings (
424
+ Publisher .DEFAULT_BUNDLING_SETTINGS .toBuilder ().setRequestByteThreshold (-1 ).build ());
379
425
fail ("Should have thrown an IllegalArgumentException" );
380
426
} catch (IllegalArgumentException expected ) {
381
427
// Expected
382
428
}
383
429
384
- builder .setMaxBundleDuration (new Duration (1 ));
430
+ builder .setBundlingSettings (
431
+ Publisher .DEFAULT_BUNDLING_SETTINGS .toBuilder ().setDelayThreshold (new Duration (1 )).build ());
385
432
try {
386
- builder .setMaxBundleDuration (null );
387
- fail ("Should have thrown an IllegalArgumentException" );
433
+ builder .setBundlingSettings (
434
+ Publisher .DEFAULT_BUNDLING_SETTINGS .toBuilder ().setDelayThreshold (null ).build ());
435
+ fail ("Should have thrown an NullPointerException" );
388
436
} catch (NullPointerException expected ) {
389
437
// Expected
390
438
}
391
439
try {
392
- builder .setMaxBundleDuration (new Duration (-1 ));
440
+ builder .setBundlingSettings (
441
+ Publisher .DEFAULT_BUNDLING_SETTINGS
442
+ .toBuilder ()
443
+ .setDelayThreshold (new Duration (-1 ))
444
+ .build ());
393
445
fail ("Should have thrown an IllegalArgumentException" );
394
446
} catch (IllegalArgumentException expected ) {
395
447
// Expected
396
448
}
397
449
398
- builder .setMaxBundleMessages (1 );
450
+ builder .setBundlingSettings (
451
+ Publisher .DEFAULT_BUNDLING_SETTINGS .toBuilder ().setElementCountThreshold (1 ).build ());
452
+ try {
453
+ builder .setBundlingSettings (
454
+ Publisher .DEFAULT_BUNDLING_SETTINGS .toBuilder ().setElementCountThreshold (null ).build ());
455
+ fail ("Should have thrown an NullPointerException" );
456
+ } catch (NullPointerException expected ) {
457
+ // Expected
458
+ }
399
459
try {
400
- builder .setMaxBundleMessages (0 );
460
+ builder .setBundlingSettings (
461
+ Publisher .DEFAULT_BUNDLING_SETTINGS .toBuilder ().setElementCountThreshold (0 ).build ());
401
462
fail ("Should have thrown an IllegalArgumentException" );
402
463
} catch (IllegalArgumentException expected ) {
403
464
// Expected
404
465
}
405
466
try {
406
- builder .setMaxBundleMessages (-1 );
467
+ builder .setBundlingSettings (
468
+ Publisher .DEFAULT_BUNDLING_SETTINGS .toBuilder ().setElementCountThreshold (-1 ).build ());
407
469
fail ("Should have thrown an IllegalArgumentException" );
408
470
} catch (IllegalArgumentException expected ) {
409
471
// Expected
0 commit comments