|
75 | 75 | import static com.azure.core.util.tracing.Tracer.HOST_NAME_KEY; |
76 | 76 | import static com.azure.core.util.tracing.Tracer.PARENT_TRACE_CONTEXT_KEY; |
77 | 77 | import static com.azure.core.util.tracing.Tracer.SPAN_CONTEXT_KEY; |
| 78 | +import static com.azure.messaging.servicebus.ServiceBusSenderAsyncClient.MAX_BATCH_SIZE_BYTES; |
78 | 79 | import static com.azure.messaging.servicebus.ServiceBusSenderAsyncClient.MAX_MESSAGE_LENGTH_BYTES; |
79 | 80 | import static org.junit.jupiter.api.Assertions.assertEquals; |
80 | 81 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
@@ -302,6 +303,105 @@ void createsMessageBatchWithSize(boolean isV2) { |
302 | 303 | }).expectComplete().verify(DEFAULT_TIMEOUT); |
303 | 304 | } |
304 | 305 |
|
| 306 | + /** |
| 307 | + * Verifies that the batch max size is capped at MAX_BATCH_SIZE_BYTES (1 MB) when the link reports a larger size. |
| 308 | + * This simulates a Premium partitioned namespace where the link advertises up to 100 MB per-message. |
| 309 | + */ |
| 310 | + @ParameterizedTest |
| 311 | + @MethodSource("selectStack") |
| 312 | + void createBatchCappedAtMaxBatchSizeWhenLinkReportsLargerSize(boolean isV2) { |
| 313 | + // Arrange |
| 314 | + arrangeIfV2(isV2); |
| 315 | + int largeLinkSize = 100 * 1024 * 1024; // 100 MB |
| 316 | + |
| 317 | + final AmqpSendLink link = mock(AmqpSendLink.class); |
| 318 | + when(link.getLinkSize()).thenReturn(Mono.just(largeLinkSize)); |
| 319 | + |
| 320 | + when(connection.createSendLink(eq(ENTITY_NAME), eq(ENTITY_NAME), any(AmqpRetryOptions.class), isNull(), |
| 321 | + eq(CLIENT_IDENTIFIER))).thenReturn(Mono.just(link)); |
| 322 | + |
| 323 | + // Act & Assert |
| 324 | + StepVerifier.create(sender.createMessageBatch()).assertNext(batch -> { |
| 325 | + Assertions.assertEquals(MAX_BATCH_SIZE_BYTES, batch.getMaxSizeInBytes()); |
| 326 | + }).expectComplete().verify(DEFAULT_TIMEOUT); |
| 327 | + } |
| 328 | + |
| 329 | + /** |
| 330 | + * Verifies that the batch max size uses the link size when it is smaller than MAX_BATCH_SIZE_BYTES (1 MB). |
| 331 | + * This simulates a Standard namespace where the link advertises 256 KB. |
| 332 | + */ |
| 333 | + @ParameterizedTest |
| 334 | + @MethodSource("selectStack") |
| 335 | + void createBatchUsesLinkSizeWhenSmallerThanMaxBatchSize(boolean isV2) { |
| 336 | + // Arrange |
| 337 | + arrangeIfV2(isV2); |
| 338 | + int smallLinkSize = 256 * 1024; // 256 KB |
| 339 | + |
| 340 | + final AmqpSendLink link = mock(AmqpSendLink.class); |
| 341 | + when(link.getLinkSize()).thenReturn(Mono.just(smallLinkSize)); |
| 342 | + |
| 343 | + when(connection.createSendLink(eq(ENTITY_NAME), eq(ENTITY_NAME), any(AmqpRetryOptions.class), isNull(), |
| 344 | + eq(CLIENT_IDENTIFIER))).thenReturn(Mono.just(link)); |
| 345 | + |
| 346 | + // Act & Assert |
| 347 | + StepVerifier.create(sender.createMessageBatch()).assertNext(batch -> { |
| 348 | + Assertions.assertEquals(smallLinkSize, batch.getMaxSizeInBytes()); |
| 349 | + }).expectComplete().verify(DEFAULT_TIMEOUT); |
| 350 | + } |
| 351 | + |
| 352 | + /** |
| 353 | + * Verifies that user-specified maxSize exceeding the effective 1 MB cap throws an error. |
| 354 | + */ |
| 355 | + @ParameterizedTest |
| 356 | + @MethodSource("selectStack") |
| 357 | + void createBatchWithOptionsExceedingMaxBatchSizeCapThrowsError(boolean isV2) { |
| 358 | + // Arrange |
| 359 | + arrangeIfV2(isV2); |
| 360 | + int largeLinkSize = 100 * 1024 * 1024; // 100 MB |
| 361 | + int requestedBatchSize = 2 * 1024 * 1024; // 2 MB - exceeds 1 MB cap |
| 362 | + |
| 363 | + final AmqpSendLink link = mock(AmqpSendLink.class); |
| 364 | + when(link.getLinkSize()).thenReturn(Mono.just(largeLinkSize)); |
| 365 | + |
| 366 | + when(connection.createSendLink(eq(ENTITY_NAME), eq(ENTITY_NAME), any(AmqpRetryOptions.class), isNull(), |
| 367 | + eq(CLIENT_IDENTIFIER))).thenReturn(Mono.just(link)); |
| 368 | + |
| 369 | + final CreateMessageBatchOptions options |
| 370 | + = new CreateMessageBatchOptions().setMaximumSizeInBytes(requestedBatchSize); |
| 371 | + |
| 372 | + // Act & Assert |
| 373 | + // The IllegalArgumentException from createMessageBatch is wrapped by mapError into ServiceBusException. |
| 374 | + StepVerifier.create(sender.createMessageBatch(options)) |
| 375 | + .expectError(ServiceBusException.class) |
| 376 | + .verify(DEFAULT_TIMEOUT); |
| 377 | + } |
| 378 | + |
| 379 | + /** |
| 380 | + * Verifies that user-specified maxSize smaller than the 1 MB cap is respected. |
| 381 | + */ |
| 382 | + @ParameterizedTest |
| 383 | + @MethodSource("selectStack") |
| 384 | + void createBatchWithOptionsSmallerThanMaxBatchSizeCapIsRespected(boolean isV2) { |
| 385 | + // Arrange |
| 386 | + arrangeIfV2(isV2); |
| 387 | + int largeLinkSize = 100 * 1024 * 1024; // 100 MB |
| 388 | + int requestedBatchSize = 500 * 1024; // 500 KB |
| 389 | + |
| 390 | + final AmqpSendLink link = mock(AmqpSendLink.class); |
| 391 | + when(link.getLinkSize()).thenReturn(Mono.just(largeLinkSize)); |
| 392 | + |
| 393 | + when(connection.createSendLink(eq(ENTITY_NAME), eq(ENTITY_NAME), any(AmqpRetryOptions.class), isNull(), |
| 394 | + eq(CLIENT_IDENTIFIER))).thenReturn(Mono.just(link)); |
| 395 | + |
| 396 | + final CreateMessageBatchOptions options |
| 397 | + = new CreateMessageBatchOptions().setMaximumSizeInBytes(requestedBatchSize); |
| 398 | + |
| 399 | + // Act & Assert |
| 400 | + StepVerifier.create(sender.createMessageBatch(options)).assertNext(batch -> { |
| 401 | + Assertions.assertEquals(requestedBatchSize, batch.getMaxSizeInBytes()); |
| 402 | + }).expectComplete().verify(DEFAULT_TIMEOUT); |
| 403 | + } |
| 404 | + |
305 | 405 | @ParameterizedTest |
306 | 406 | @MethodSource("selectStack") |
307 | 407 | void scheduleMessageSizeTooBig(boolean isV2) { |
|
0 commit comments