|
30 | 30 | import static org.mockito.Mockito.when; |
31 | 31 |
|
32 | 32 | import java.nio.ByteBuffer; |
| 33 | +import java.nio.charset.StandardCharsets; |
33 | 34 | import java.util.HashMap; |
| 35 | +import java.util.concurrent.atomic.AtomicInteger; |
| 36 | + |
34 | 37 | import org.apache.activemq.artemis.core.message.LargeBodyReader; |
35 | 38 | import org.apache.activemq.artemis.core.persistence.impl.journal.LargeServerMessageImpl; |
36 | 39 | import org.apache.activemq.artemis.core.persistence.impl.nullpm.NullStorageManager; |
@@ -273,6 +276,173 @@ private void doTestMessageEncodingWrittenToDeliveryWithAnnotations(boolean deliv |
273 | 276 | verifyNoMoreInteractions(protonDelivery); |
274 | 277 | } |
275 | 278 |
|
| 279 | + @Test |
| 280 | + public void testMessageEncodingWrittenToDeliveryWithDeliveryAnnotationsThatExceedFrameSize() throws Exception { |
| 281 | + final byte[] headersBytes = new byte[4]; |
| 282 | + |
| 283 | + headersBytes[0] = 4; |
| 284 | + headersBytes[1] = 5; |
| 285 | + headersBytes[2] = 6; |
| 286 | + headersBytes[3] = 7; |
| 287 | + |
| 288 | + final byte[] payloadBytes = new byte[4]; |
| 289 | + |
| 290 | + payloadBytes[0] = 1; |
| 291 | + payloadBytes[1] = 2; |
| 292 | + payloadBytes[2] = 3; |
| 293 | + payloadBytes[3] = 4; |
| 294 | + |
| 295 | + final DeliveryAnnotations annotations = new DeliveryAnnotations(new HashMap<>()); |
| 296 | + |
| 297 | + annotations.getValue().put(Symbol.valueOf("a"), "a".repeat(1024)); |
| 298 | + annotations.getValue().put(Symbol.valueOf("b"), "b".repeat(1024)); |
| 299 | + annotations.getValue().put(Symbol.valueOf("c"), "c".repeat(1024)); |
| 300 | + annotations.getValue().put(Symbol.valueOf("d"), "d".repeat(1024)); |
| 301 | + annotations.getValue().put(Symbol.valueOf("e"), "e".repeat(1024)); |
| 302 | + |
| 303 | + doTestMessageEncodingForTunneledCoreLargeMessage(annotations, headersBytes, payloadBytes); |
| 304 | + } |
| 305 | + |
| 306 | + @Test |
| 307 | + public void testMessageEncodingWrittenToDeliveryWithCoreHeaderEncodingThatExceedsFrameSize() throws Exception { |
| 308 | + final byte[] headersBytes = "A".repeat(4097).getBytes(StandardCharsets.US_ASCII); |
| 309 | + final byte[] payloadBytes = new byte[4]; |
| 310 | + |
| 311 | + payloadBytes[0] = 1; |
| 312 | + payloadBytes[1] = 2; |
| 313 | + payloadBytes[2] = 3; |
| 314 | + payloadBytes[3] = 4; |
| 315 | + |
| 316 | + final DeliveryAnnotations annotations = new DeliveryAnnotations(new HashMap<>()); |
| 317 | + |
| 318 | + annotations.getValue().put(Symbol.valueOf("a"), "a"); |
| 319 | + annotations.getValue().put(Symbol.valueOf("b"), "b"); |
| 320 | + annotations.getValue().put(Symbol.valueOf("c"), "c"); |
| 321 | + annotations.getValue().put(Symbol.valueOf("d"), "d"); |
| 322 | + annotations.getValue().put(Symbol.valueOf("e"), "e"); |
| 323 | + |
| 324 | + doTestMessageEncodingForTunneledCoreLargeMessage(annotations, headersBytes, payloadBytes); |
| 325 | + } |
| 326 | + |
| 327 | + @Test |
| 328 | + public void testMessageEncodingWrittenToDeliveryWithBothDAandCoreHeadersExceedingFrameSize() throws Exception { |
| 329 | + final byte[] headersBytes = "A".repeat(4097).getBytes(StandardCharsets.US_ASCII); |
| 330 | + final byte[] payloadBytes = new byte[4]; |
| 331 | + |
| 332 | + payloadBytes[0] = 1; |
| 333 | + payloadBytes[1] = 2; |
| 334 | + payloadBytes[2] = 3; |
| 335 | + payloadBytes[3] = 4; |
| 336 | + |
| 337 | + final DeliveryAnnotations annotations = new DeliveryAnnotations(new HashMap<>()); |
| 338 | + |
| 339 | + annotations.getValue().put(Symbol.valueOf("a"), "a".repeat(1024)); |
| 340 | + annotations.getValue().put(Symbol.valueOf("b"), "b".repeat(1024)); |
| 341 | + annotations.getValue().put(Symbol.valueOf("c"), "c".repeat(1024)); |
| 342 | + annotations.getValue().put(Symbol.valueOf("d"), "d".repeat(1024)); |
| 343 | + annotations.getValue().put(Symbol.valueOf("e"), "e".repeat(1024)); |
| 344 | + |
| 345 | + doTestMessageEncodingForTunneledCoreLargeMessage(annotations, headersBytes, payloadBytes); |
| 346 | + } |
| 347 | + |
| 348 | + @Test |
| 349 | + public void testMessageEncodingWrittenToDeliveryWithAllSectionsExceedFrameSize() throws Exception { |
| 350 | + final byte[] headersBytes = "A".repeat(4097).getBytes(StandardCharsets.US_ASCII); |
| 351 | + final byte[] payloadBytes = "B".repeat(4097).getBytes(StandardCharsets.US_ASCII); |
| 352 | + final DeliveryAnnotations annotations = new DeliveryAnnotations(new HashMap<>()); |
| 353 | + |
| 354 | + annotations.getValue().put(Symbol.valueOf("a"), "a".repeat(1024)); |
| 355 | + annotations.getValue().put(Symbol.valueOf("b"), "b".repeat(1024)); |
| 356 | + annotations.getValue().put(Symbol.valueOf("c"), "c".repeat(1024)); |
| 357 | + annotations.getValue().put(Symbol.valueOf("d"), "d".repeat(1024)); |
| 358 | + annotations.getValue().put(Symbol.valueOf("e"), "e".repeat(1024)); |
| 359 | + |
| 360 | + doTestMessageEncodingForTunneledCoreLargeMessage(annotations, headersBytes, payloadBytes); |
| 361 | + } |
| 362 | + |
| 363 | + private void doTestMessageEncodingForTunneledCoreLargeMessage(DeliveryAnnotations annotations, byte[] headersBytes, byte[] payloadBytes) throws Exception { |
| 364 | + when(protonTransport.getOutboundFrameSizeLimit()).thenReturn(4096); |
| 365 | + |
| 366 | + AMQPTunneledCoreLargeMessageWriter writer = new AMQPTunneledCoreLargeMessageWriter(serverSender); |
| 367 | + |
| 368 | + writer.open(Mockito.mock(MessageReference.class)); |
| 369 | + |
| 370 | + final ByteBuf expectedEncoding = Unpooled.buffer(); |
| 371 | + final AtomicInteger payloadReaderPosition = new AtomicInteger(); |
| 372 | + |
| 373 | + writeDeliveryAnnotations(expectedEncoding, annotations); |
| 374 | + |
| 375 | + when(reference.getProtocolData(any())).thenReturn(annotations); |
| 376 | + |
| 377 | + writeDataSection(expectedEncoding, headersBytes); |
| 378 | + writeDataSection(expectedEncoding, payloadBytes); |
| 379 | + |
| 380 | + when(protonSender.getLocalState()).thenReturn(EndpointState.ACTIVE); |
| 381 | + when(protonDelivery.isPartial()).thenReturn(true); |
| 382 | + when(message.getHeadersAndPropertiesEncodeSize()).thenReturn(headersBytes.length); |
| 383 | + |
| 384 | + // Provides the simulated encoded core headers and properties |
| 385 | + doAnswer(invocation -> { |
| 386 | + final ByteBuf buffer = invocation.getArgument(0); |
| 387 | + |
| 388 | + buffer.writeBytes(headersBytes); |
| 389 | + |
| 390 | + return null; |
| 391 | + }).when(message).encodeHeadersAndProperties(any(ByteBuf.class)); |
| 392 | + |
| 393 | + when(bodyReader.getSize()).thenReturn((long) payloadBytes.length); |
| 394 | + |
| 395 | + final ByteBuf encodedByteBuf = Unpooled.buffer(); |
| 396 | + final NettyWritable encodedBytes = new NettyWritable(encodedByteBuf); |
| 397 | + |
| 398 | + // Answer back with the amount of writable bytes |
| 399 | + doAnswer(invocation -> { |
| 400 | + final ByteBuffer buffer = invocation.getArgument(0); |
| 401 | + |
| 402 | + final int readSize = Math.min(buffer.remaining(), payloadBytes.length - payloadReaderPosition.get()); |
| 403 | + |
| 404 | + buffer.put(payloadBytes, payloadReaderPosition.get(), readSize); |
| 405 | + |
| 406 | + payloadReaderPosition.addAndGet(readSize); |
| 407 | + |
| 408 | + return readSize; |
| 409 | + }).when(bodyReader).readInto(any()); |
| 410 | + |
| 411 | + // Capture the write for comparison, this avoid issues with released netty buffers |
| 412 | + doAnswer(invocation -> { |
| 413 | + final ReadableBuffer buffer = invocation.getArgument(0); |
| 414 | + |
| 415 | + encodedBytes.put(buffer); |
| 416 | + |
| 417 | + return null; |
| 418 | + }).when(protonSender).send(any()); |
| 419 | + |
| 420 | + try { |
| 421 | + writer.writeBytes(reference); |
| 422 | + } catch (IllegalStateException e) { |
| 423 | + fail("Should not throw as the delivery is completed so no data should be written."); |
| 424 | + } |
| 425 | + |
| 426 | + verify(message).usageUp(); |
| 427 | + verify(message).getLargeBodyReader(); |
| 428 | + verify(message).getHeadersAndPropertiesEncodeSize(); |
| 429 | + verify(message).encodeHeadersAndProperties(any(ByteBuf.class)); |
| 430 | + verify(reference).getMessage(); |
| 431 | + verify(reference).getProtocolData(any()); |
| 432 | + verify(protonSender).getSession(); |
| 433 | + verify(protonDelivery).getTag(); |
| 434 | + verify(protonSender, atLeastOnce()).getLocalState(); |
| 435 | + verify(protonSender, atLeastOnce()).send(any(ReadableBuffer.class)); |
| 436 | + |
| 437 | + assertTrue(encodedByteBuf.isReadable()); |
| 438 | + assertEquals(expectedEncoding.readableBytes(), encodedByteBuf.readableBytes()); |
| 439 | + assertEquals(expectedEncoding, encodedByteBuf); |
| 440 | + |
| 441 | + verifyNoMoreInteractions(message); |
| 442 | + verifyNoMoreInteractions(reference); |
| 443 | + verifyNoMoreInteractions(protonDelivery); |
| 444 | + } |
| 445 | + |
276 | 446 | @Test |
277 | 447 | public void testLargeMessageUsageLoweredOnCloseWhenWriteNotCompleted() throws Exception { |
278 | 448 | AMQPTunneledCoreLargeMessageWriter writer = new AMQPTunneledCoreLargeMessageWriter(serverSender); |
|
0 commit comments