| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
* |
| 19 |
*/ |
| 20 |
package org.apache.qpid.proton.engine.impl; |
| 21 |
|
| 22 |
import static org.junit.Assert.assertArrayEquals; |
| 23 |
import static org.junit.Assert.assertEquals; |
| 24 |
import static org.junit.Assert.assertFalse; |
| 25 |
import static org.junit.Assert.assertNotNull; |
| 26 |
import static org.junit.Assert.assertNotSame; |
| 27 |
import static org.junit.Assert.assertSame; |
| 28 |
import static org.junit.Assert.assertTrue; |
| 29 |
|
| 30 |
import java.nio.ByteBuffer; |
| 31 |
import java.nio.charset.StandardCharsets; |
| 32 |
|
| 33 |
import org.apache.qpid.proton.amqp.Binary; |
| 34 |
import org.apache.qpid.proton.codec.CompositeReadableBuffer; |
| 35 |
import org.apache.qpid.proton.codec.ReadableBuffer; |
| 36 |
import org.apache.qpid.proton.codec.WritableBuffer; |
| 37 |
import org.apache.qpid.proton.codec.WritableBuffer.ByteBufferWrapper; |
| 38 |
import org.apache.qpid.proton.engine.Delivery; |
| 39 |
import org.apache.qpid.proton.engine.Record; |
| 40 |
import org.junit.Test; |
| 41 |
import org.mockito.Mockito; |
| 42 |
|
| 43 |
public class DeliveryImplTest |
| 44 |
{ |
| 45 |
//----- Test for toString ------------------------------------------------// |
| 46 |
|
| 47 |
@Test |
| 48 |
public void testToStringOnEmptyDelivery() throws Exception |
| 49 |
{ |
| 50 |
// Check that no NPE gets thrown when no data in delivery. |
| 51 |
DeliveryImpl delivery = new DeliveryImpl(null, Mockito.mock(LinkImpl.class), null); |
| 52 |
assertNotNull(delivery.toString()); |
| 53 |
} |
| 54 |
|
| 55 |
//----- Tests for message format handling --------------------------------// |
| 56 |
|
| 57 |
@Test |
| 58 |
public void testDefaultMessageFormat() throws Exception |
| 59 |
{ |
| 60 |
DeliveryImpl delivery = new DeliveryImpl(null, Mockito.mock(LinkImpl.class), null); |
| 61 |
assertEquals("Unexpected value", 0L, DeliveryImpl.DEFAULT_MESSAGE_FORMAT); |
| 62 |
assertEquals("Unexpected message format", DeliveryImpl.DEFAULT_MESSAGE_FORMAT, delivery.getMessageFormat()); |
| 63 |
} |
| 64 |
|
| 65 |
@Test |
| 66 |
public void testSetGetMessageFormat() throws Exception |
| 67 |
{ |
| 68 |
DeliveryImpl delivery = new DeliveryImpl(null, Mockito.mock(LinkImpl.class), null); |
| 69 |
|
| 70 |
// lowest value and default |
| 71 |
int newFormat = 0; |
| 72 |
delivery.setMessageFormat(newFormat); |
| 73 |
assertEquals("Unexpected message format", newFormat, delivery.getMessageFormat()); |
| 74 |
|
| 75 |
newFormat = 123456; |
| 76 |
delivery.setMessageFormat(newFormat); |
| 77 |
assertEquals("Unexpected message format", newFormat, delivery.getMessageFormat()); |
| 78 |
|
| 79 |
// Highest value |
| 80 |
newFormat = (1 << 32) - 1; |
| 81 |
delivery.setMessageFormat(newFormat); |
| 82 |
assertEquals("Unexpected message format", newFormat, delivery.getMessageFormat()); |
| 83 |
} |
| 84 |
|
| 85 |
@Test |
| 86 |
public void testAttachmentsNonNull() throws Exception |
| 87 |
{ |
| 88 |
DeliveryImpl delivery = new DeliveryImpl(null, Mockito.mock(LinkImpl.class), null); |
| 89 |
|
| 90 |
assertNotNull("Expected attachments to be non-null", delivery.attachments()); |
| 91 |
} |
| 92 |
|
| 93 |
@Test |
| 94 |
public void testAttachmentsReturnsSameRecordOnSuccessiveCalls() throws Exception |
| 95 |
{ |
| 96 |
DeliveryImpl delivery = new DeliveryImpl(null, Mockito.mock(LinkImpl.class), null); |
| 97 |
|
| 98 |
Record attachments = delivery.attachments(); |
| 99 |
Record attachments2 = delivery.attachments(); |
| 100 |
assertSame("Expected to get the same attachments", attachments, attachments2); |
| 101 |
} |
| 102 |
|
| 103 |
@Test |
| 104 |
public void testAvailable() throws Exception |
| 105 |
{ |
| 106 |
// Set up a delivery with some data |
| 107 |
byte[] myData = "myData".getBytes(StandardCharsets.UTF_8); |
| 108 |
|
| 109 |
DeliveryImpl deliveyImpl = new DeliveryImpl(null, Mockito.mock(LinkImpl.class), null); |
| 110 |
deliveyImpl.append(myData); |
| 111 |
|
| 112 |
Delivery delivery = deliveyImpl; |
| 113 |
|
| 114 |
// Check the full data is available |
| 115 |
assertNotNull("expected the delivery to be present", delivery); |
| 116 |
assertEquals("unexpectd available count", myData.length, delivery.available()); |
| 117 |
|
| 118 |
// Extract some of the data as the receiver link will, check available gets reduced accordingly. |
| 119 |
int partLength = 2; |
| 120 |
int remainderLength = myData.length - partLength; |
| 121 |
assertTrue(partLength < myData.length); |
| 122 |
|
| 123 |
byte[] myRecievedData1 = new byte[partLength]; |
| 124 |
|
| 125 |
int received = deliveyImpl.recv(myRecievedData1, 0, myRecievedData1.length); |
| 126 |
assertEquals("Unexpected data length received", partLength, received); |
| 127 |
assertEquals("Unexpected data length available", remainderLength, delivery.available()); |
| 128 |
|
| 129 |
// Extract remainder of the data as the receiver link will, check available hits 0. |
| 130 |
byte[] myRecievedData2 = new byte[remainderLength]; |
| 131 |
|
| 132 |
received = deliveyImpl.recv(myRecievedData2, 0, remainderLength); |
| 133 |
assertEquals("Unexpected data length received", remainderLength, received); |
| 134 |
assertEquals("Expected no data to remain available", 0, delivery.available()); |
| 135 |
} |
| 136 |
|
| 137 |
@Test |
| 138 |
public void testAvailableWhenEmpty() throws Exception |
| 139 |
{ |
| 140 |
DeliveryImpl delivery = new DeliveryImpl(null, Mockito.mock(LinkImpl.class), null); |
| 141 |
assertEquals(0, delivery.available()); |
| 142 |
} |
| 143 |
|
| 144 |
//----- Tests for getters of internal Data -------------------------------// |
| 145 |
|
| 146 |
@Test |
| 147 |
public void testGetDataOnEmptyDelivery() throws Exception |
| 148 |
{ |
| 149 |
DeliveryImpl delivery = new DeliveryImpl(null, Mockito.mock(LinkImpl.class), null); |
| 150 |
|
| 151 |
assertNotNull(delivery.getData()); |
| 152 |
assertFalse(delivery.getData().hasRemaining()); |
| 153 |
} |
| 154 |
|
| 155 |
@Test |
| 156 |
public void testGetDataLengthOnEmptyDelivery() throws Exception |
| 157 |
{ |
| 158 |
DeliveryImpl delivery = new DeliveryImpl(null, Mockito.mock(LinkImpl.class), null); |
| 159 |
|
| 160 |
assertEquals(0, delivery.getDataLength()); |
| 161 |
} |
| 162 |
|
| 163 |
//----- Tests for Append of Data -----------------------------------------// |
| 164 |
|
| 165 |
@Test |
| 166 |
public void testAppendArraysToBuffer() throws Exception |
| 167 |
{ |
| 168 |
DeliveryImpl delivery = new DeliveryImpl(null, Mockito.mock(LinkImpl.class), null); |
| 169 |
|
| 170 |
byte[] data1 = new byte[] { 0, 1, 2, 3, 4, 5 }; |
| 171 |
byte[] data2 = new byte[] { 6, 7, 8, 9, 10, 11 }; |
| 172 |
|
| 173 |
delivery.append(data1); |
| 174 |
delivery.append(data2); |
| 175 |
|
| 176 |
assertEquals(data1.length + data2.length, delivery.getDataLength()); |
| 177 |
assertNotNull(delivery.getData()); |
| 178 |
assertEquals(data1.length + data2.length, delivery.getData().remaining()); |
| 179 |
} |
| 180 |
|
| 181 |
@Test |
| 182 |
public void testAppendBinaryToBuffer() throws Exception |
| 183 |
{ |
| 184 |
DeliveryImpl delivery = new DeliveryImpl(null, Mockito.mock(LinkImpl.class), null); |
| 185 |
|
| 186 |
byte[] data1 = new byte[] { 0, 1, 2, 3, 4, 5 }; |
| 187 |
byte[] data2 = new byte[] { 6, 7, 8, 9, 10, 11 }; |
| 188 |
|
| 189 |
Binary binary1 = new Binary(data1); |
| 190 |
Binary binary2 = new Binary(data2); |
| 191 |
|
| 192 |
delivery.append(binary1); |
| 193 |
delivery.append(binary2); |
| 194 |
|
| 195 |
assertEquals(data1.length + data2.length, delivery.getDataLength()); |
| 196 |
assertNotNull(delivery.getData()); |
| 197 |
assertEquals(data1.length + data2.length, delivery.getData().remaining()); |
| 198 |
} |
| 199 |
|
| 200 |
@Test |
| 201 |
public void testAppendBinaryWithOffsetsToBuffer() throws Exception |
| 202 |
{ |
| 203 |
DeliveryImpl delivery = new DeliveryImpl(null, Mockito.mock(LinkImpl.class), null); |
| 204 |
|
| 205 |
byte[] data1 = new byte[] { 0, 1, 2, 3, 4, 5 }; |
| 206 |
byte[] data2 = new byte[] { 6, 7, 8, 9, 10, 11 }; |
| 207 |
|
| 208 |
Binary binary1 = new Binary(data1, 1, 2); |
| 209 |
Binary binary2 = new Binary(data2, 0, 4); |
| 210 |
|
| 211 |
delivery.append(binary1); |
| 212 |
delivery.append(binary2); |
| 213 |
|
| 214 |
assertEquals(binary1.getLength() + binary2.getLength(), delivery.getDataLength()); |
| 215 |
assertNotNull(delivery.getData()); |
| 216 |
assertEquals(binary1.getLength() + binary2.getLength(), delivery.getData().remaining()); |
| 217 |
} |
| 218 |
|
| 219 |
//----- Tests for recv all data ------------------------------------------// |
| 220 |
|
| 221 |
@Test |
| 222 |
public void testRecvAllAsReadableBufferWhenEmpty() throws Exception |
| 223 |
{ |
| 224 |
DeliveryImpl delivery = new DeliveryImpl(null, Mockito.mock(LinkImpl.class), null); |
| 225 |
|
| 226 |
ReadableBuffer payload = delivery.recv(); |
| 227 |
|
| 228 |
assertNotNull(payload); |
| 229 |
assertEquals(0, payload.remaining()); |
| 230 |
} |
| 231 |
|
| 232 |
@Test |
| 233 |
public void testRecvAllAsReadableBuffer() throws Exception |
| 234 |
{ |
| 235 |
DeliveryImpl delivery = new DeliveryImpl(null, Mockito.mock(LinkImpl.class), null); |
| 236 |
|
| 237 |
byte[] data1 = new byte[] { 0, 1, 2, 3, 4, 5 }; |
| 238 |
byte[] data2 = new byte[] { 6, 7, 8, 9, 10, 11 }; |
| 239 |
|
| 240 |
int legnth = data1.length + data2.length; |
| 241 |
|
| 242 |
Binary binary1 = new Binary(data1); |
| 243 |
Binary binary2 = new Binary(data2); |
| 244 |
|
| 245 |
delivery.append(binary1); |
| 246 |
delivery.append(binary2); |
| 247 |
|
| 248 |
ReadableBuffer payload = delivery.recv(); |
| 249 |
assertTrue(payload instanceof CompositeReadableBuffer); |
| 250 |
CompositeReadableBuffer composite = (CompositeReadableBuffer) payload; |
| 251 |
assertEquals(2, composite.getArrays().size()); |
| 252 |
|
| 253 |
assertNotNull(payload); |
| 254 |
assertEquals(legnth, payload.remaining()); |
| 255 |
assertEquals(0, payload.get(0)); |
| 256 |
assertEquals(11, payload.get(11)); |
| 257 |
} |
| 258 |
|
| 259 |
//----- Tests for recv array data ----------------------------------------// |
| 260 |
|
| 261 |
@Test |
| 262 |
public void testRecvArrayWithEmptyDelivery() throws Exception |
| 263 |
{ |
| 264 |
DeliveryImpl delivery = new DeliveryImpl(null, Mockito.mock(LinkImpl.class), null); |
| 265 |
byte[] received = new byte[5]; |
| 266 |
assertEquals(0, delivery.recv(received, 0, received.length)); |
| 267 |
} |
| 268 |
|
| 269 |
@Test |
| 270 |
public void testRecvArrayWhenIncomingIsOneArray() throws Exception |
| 271 |
{ |
| 272 |
DeliveryImpl delivery = new DeliveryImpl(null, Mockito.mock(LinkImpl.class), null); |
| 273 |
|
| 274 |
byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; |
| 275 |
|
| 276 |
int length = data.length; |
| 277 |
|
| 278 |
delivery.append(data); |
| 279 |
|
| 280 |
assertEquals(length, delivery.available()); |
| 281 |
|
| 282 |
byte[] received = new byte[length]; |
| 283 |
|
| 284 |
assertEquals(length, delivery.recv(received, 0, length)); |
| 285 |
|
| 286 |
for (int i = 0; i < length; ++i) { |
| 287 |
assertEquals(received[i], data[i]); |
| 288 |
} |
| 289 |
|
| 290 |
assertEquals(0, delivery.recv(received, 0, length)); |
| 291 |
} |
| 292 |
|
| 293 |
@Test |
| 294 |
public void testRecvArrayWhenIncomingIsSplitArrays() throws Exception |
| 295 |
{ |
| 296 |
DeliveryImpl delivery = new DeliveryImpl(null, Mockito.mock(LinkImpl.class), null); |
| 297 |
|
| 298 |
byte[] data1 = new byte[] { 0, 1, 2, 3, 4, 5 }; |
| 299 |
byte[] data2 = new byte[] { 6, 7, 8, 9, 10, 11 }; |
| 300 |
|
| 301 |
int length = data1.length + data2.length; |
| 302 |
|
| 303 |
Binary binary1 = new Binary(data1); |
| 304 |
Binary binary2 = new Binary(data2); |
| 305 |
|
| 306 |
delivery.append(binary1); |
| 307 |
delivery.append(binary2); |
| 308 |
|
| 309 |
assertEquals(length, delivery.available()); |
| 310 |
|
| 311 |
byte[] received = new byte[length]; |
| 312 |
|
| 313 |
assertEquals(length, delivery.recv(received, 0, length)); |
| 314 |
|
| 315 |
for (int i = 0; i < data1.length; ++i) { |
| 316 |
assertEquals(received[i], data1[i]); |
| 317 |
} |
| 318 |
|
| 319 |
int offset = data1.length; |
| 320 |
|
| 321 |
for (int i = 0; i < data2.length; ++i) { |
| 322 |
assertEquals(received[i + offset], data2[i]); |
| 323 |
} |
| 324 |
|
| 325 |
assertEquals(0, delivery.recv(received, 0, length)); |
| 326 |
} |
| 327 |
|
| 328 |
//----- Tests for recv WritableBuffer ------------------------------------// |
| 329 |
|
| 330 |
@Test |
| 331 |
public void testRecvWriteableBufferWithEmptyDelivery() throws Exception |
| 332 |
{ |
| 333 |
DeliveryImpl delivery = new DeliveryImpl(null, Mockito.mock(LinkImpl.class), null); |
| 334 |
WritableBuffer buffer = WritableBuffer.ByteBufferWrapper.allocate(5); |
| 335 |
assertEquals(0, delivery.recv(buffer)); |
| 336 |
} |
| 337 |
|
| 338 |
@Test |
| 339 |
public void testRecvWritableWhenIncomingIsOneArray() throws Exception |
| 340 |
{ |
| 341 |
DeliveryImpl delivery = new DeliveryImpl(null, Mockito.mock(LinkImpl.class), null); |
| 342 |
|
| 343 |
byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; |
| 344 |
|
| 345 |
int length = data.length; |
| 346 |
|
| 347 |
delivery.append(data); |
| 348 |
|
| 349 |
assertEquals(length, delivery.available()); |
| 350 |
|
| 351 |
ByteBufferWrapper buffer = WritableBuffer.ByteBufferWrapper.allocate(length); |
| 352 |
|
| 353 |
assertEquals(length, delivery.recv(buffer)); |
| 354 |
|
| 355 |
ByteBuffer received = buffer.byteBuffer(); |
| 356 |
|
| 357 |
for (int i = 0; i < length; ++i) { |
| 358 |
assertEquals(received.get(i), data[i]); |
| 359 |
} |
| 360 |
|
| 361 |
assertEquals(0, delivery.recv(WritableBuffer.ByteBufferWrapper.allocate(length))); |
| 362 |
} |
| 363 |
|
| 364 |
@Test |
| 365 |
public void testRecvWritableWhenIncomingIsSplitArrays() throws Exception |
| 366 |
{ |
| 367 |
DeliveryImpl delivery = new DeliveryImpl(null, Mockito.mock(LinkImpl.class), null); |
| 368 |
|
| 369 |
byte[] data1 = new byte[] { 0, 1, 2, 3, 4, 5 }; |
| 370 |
byte[] data2 = new byte[] { 6, 7, 8, 9, 10, 11 }; |
| 371 |
|
| 372 |
int length = data1.length + data2.length; |
| 373 |
|
| 374 |
delivery.append(data1); |
| 375 |
delivery.append(data2); |
| 376 |
|
| 377 |
assertEquals(length, delivery.available()); |
| 378 |
|
| 379 |
ByteBufferWrapper buffer = WritableBuffer.ByteBufferWrapper.allocate(length); |
| 380 |
|
| 381 |
assertEquals(length, delivery.recv(buffer)); |
| 382 |
|
| 383 |
ByteBuffer received = buffer.byteBuffer(); |
| 384 |
|
| 385 |
for (int i = 0; i < data1.length; ++i) { |
| 386 |
assertEquals(received.get(i), data1[i]); |
| 387 |
} |
| 388 |
|
| 389 |
int offset = data1.length; |
| 390 |
|
| 391 |
for (int i = 0; i < data2.length; ++i) { |
| 392 |
assertEquals(received.get(i + offset), data2[i]); |
| 393 |
} |
| 394 |
|
| 395 |
assertEquals(0, delivery.recv(WritableBuffer.ByteBufferWrapper.allocate(length))); |
| 396 |
} |
| 397 |
|
| 398 |
//----- Test send with byte arrays ---------------------------------------// |
| 399 |
|
| 400 |
@Test |
| 401 |
public void testSendSingleByteArray() throws Exception |
| 402 |
{ |
| 403 |
DeliveryImpl delivery = createSenderDelivery(); |
| 404 |
|
| 405 |
byte[] data = new byte[] { 0, 1, 2, 3, 4, 5 }; |
| 406 |
|
| 407 |
delivery.send(data, 0, data.length); |
| 408 |
|
| 409 |
assertEquals(data.length, delivery.pending()); |
| 410 |
assertEquals(data.length, delivery.getData().remaining()); |
| 411 |
|
| 412 |
CompositeReadableBuffer composite = (CompositeReadableBuffer) delivery.getData(); |
| 413 |
|
| 414 |
assertNotSame(data, composite.array()); |
| 415 |
assertArrayEquals(data, composite.array()); |
| 416 |
} |
| 417 |
|
| 418 |
@Test |
| 419 |
public void testSendMultipleByteArrays() throws Exception |
| 420 |
{ |
| 421 |
DeliveryImpl delivery = createSenderDelivery(); |
| 422 |
|
| 423 |
byte[] data1 = new byte[] { 0, 1, 2, 3, 4, 5 }; |
| 424 |
byte[] data2 = new byte[] { 6, 7, 8, 9, 10, 11 }; |
| 425 |
|
| 426 |
int length = data1.length + data2.length; |
| 427 |
|
| 428 |
delivery.send(data1, 0, data1.length); |
| 429 |
delivery.send(data2, 0, data2.length); |
| 430 |
|
| 431 |
assertEquals(length, delivery.pending()); |
| 432 |
assertEquals(length, delivery.getData().remaining()); |
| 433 |
|
| 434 |
CompositeReadableBuffer composite = (CompositeReadableBuffer) delivery.getData(); |
| 435 |
|
| 436 |
assertNotSame(data1, composite.getArrays().get(0)); |
| 437 |
assertNotSame(data2, composite.getArrays().get(1)); |
| 438 |
|
| 439 |
assertArrayEquals(data1, composite.getArrays().get(0)); |
| 440 |
assertArrayEquals(data2, composite.getArrays().get(1)); |
| 441 |
} |
| 442 |
|
| 443 |
//----- Test send with ReadableBuffer ------------------------------------// |
| 444 |
|
| 445 |
@Test |
| 446 |
public void testSendSingleReadableBuffer() throws Exception |
| 447 |
{ |
| 448 |
DeliveryImpl delivery = createSenderDelivery(); |
| 449 |
|
| 450 |
byte[] data = new byte[] { 0, 1, 2, 3, 4, 5 }; |
| 451 |
ReadableBuffer buffer = ReadableBuffer.ByteBufferReader.wrap(data); |
| 452 |
|
| 453 |
delivery.send(buffer); |
| 454 |
|
| 455 |
assertEquals(data.length, delivery.pending()); |
| 456 |
assertEquals(data.length, delivery.getData().remaining()); |
| 457 |
|
| 458 |
CompositeReadableBuffer composite = (CompositeReadableBuffer) delivery.getData(); |
| 459 |
|
| 460 |
assertNotSame(data, composite.array()); |
| 461 |
assertArrayEquals(data, composite.array()); |
| 462 |
} |
| 463 |
|
| 464 |
@Test |
| 465 |
public void testSendSingleReadableBufferWithPartialContent() throws Exception |
| 466 |
{ |
| 467 |
DeliveryImpl delivery = createSenderDelivery(); |
| 468 |
|
| 469 |
byte[] data = new byte[] { 0, 1, 2, 3, 4, 5 }; |
| 470 |
byte[] expected = new byte[] { 3, 4, 5 }; |
| 471 |
ReadableBuffer buffer = ReadableBuffer.ByteBufferReader.wrap(data); |
| 472 |
// Now move the position forward so we only send some of the data |
| 473 |
buffer.position(3); |
| 474 |
|
| 475 |
delivery.send(buffer); |
| 476 |
|
| 477 |
assertEquals(expected.length, delivery.pending()); |
| 478 |
assertEquals(expected.length, delivery.getData().remaining()); |
| 479 |
|
| 480 |
CompositeReadableBuffer composite = (CompositeReadableBuffer) delivery.getData(); |
| 481 |
|
| 482 |
assertNotSame(data, composite.array()); |
| 483 |
assertNotSame(expected, composite.array()); |
| 484 |
assertArrayEquals(expected, composite.array()); |
| 485 |
} |
| 486 |
|
| 487 |
@Test |
| 488 |
public void testSendSingleReadableBufferWithOffsetAndPartialContent() throws Exception |
| 489 |
{ |
| 490 |
DeliveryImpl delivery = createSenderDelivery(); |
| 491 |
|
| 492 |
byte[] bytes = new byte[] { 0, 1, 2, 3, 4, 5 }; |
| 493 |
ByteBuffer data = ByteBuffer.wrap(bytes, 0, 5); // Wrap, miss out the last byte from array |
| 494 |
data.position(1); // Now move the position forward |
| 495 |
ByteBuffer dataSlice = data.slice(); // Now slice, causing us to have an array offset at start of array |
| 496 |
|
| 497 |
byte[] expected = new byte[] { 2, 3, 4 }; |
| 498 |
|
| 499 |
ReadableBuffer buffer = ReadableBuffer.ByteBufferReader.wrap(dataSlice); |
| 500 |
// Now move the position forward so we only send some of the data |
| 501 |
buffer.position(1); |
| 502 |
|
| 503 |
assertEquals("Unexpected array offset", 1, buffer.arrayOffset()); |
| 504 |
assertEquals("Unexpected remaining", 3, buffer.remaining()); |
| 505 |
|
| 506 |
delivery.send(buffer); |
| 507 |
|
| 508 |
assertEquals(expected.length, delivery.pending()); |
| 509 |
assertEquals(expected.length, delivery.getData().remaining()); |
| 510 |
|
| 511 |
CompositeReadableBuffer composite = (CompositeReadableBuffer) delivery.getData(); |
| 512 |
|
| 513 |
assertNotSame(bytes, composite.array()); |
| 514 |
assertNotSame(expected, composite.array()); |
| 515 |
assertArrayEquals(expected, composite.array()); |
| 516 |
} |
| 517 |
|
| 518 |
@Test |
| 519 |
public void testSendMultipleReadableBuffers() throws Exception |
| 520 |
{ |
| 521 |
DeliveryImpl delivery = createSenderDelivery(); |
| 522 |
|
| 523 |
byte[] data1 = new byte[] { 0, 1, 2, 3, 4, 5 }; |
| 524 |
byte[] data2 = new byte[] { 6, 7, 8, 9, 10, 11 }; |
| 525 |
|
| 526 |
ReadableBuffer buffer1 = ReadableBuffer.ByteBufferReader.wrap(data1); |
| 527 |
ReadableBuffer buffer2 = ReadableBuffer.ByteBufferReader.wrap(data2); |
| 528 |
|
| 529 |
int length = data1.length + data2.length; |
| 530 |
|
| 531 |
delivery.send(buffer1); |
| 532 |
delivery.send(buffer2); |
| 533 |
|
| 534 |
assertEquals(length, delivery.pending()); |
| 535 |
assertEquals(length, delivery.getData().remaining()); |
| 536 |
|
| 537 |
CompositeReadableBuffer composite = (CompositeReadableBuffer) delivery.getData(); |
| 538 |
|
| 539 |
assertNotSame(data1, composite.getArrays().get(0)); |
| 540 |
assertNotSame(data2, composite.getArrays().get(1)); |
| 541 |
|
| 542 |
assertArrayEquals(data1, composite.getArrays().get(0)); |
| 543 |
assertArrayEquals(data2, composite.getArrays().get(1)); |
| 544 |
} |
| 545 |
|
| 546 |
//----- Test send with ReadableBuffer ------------------------------------// |
| 547 |
|
| 548 |
@Test |
| 549 |
public void testSendNoCopySingleReadableBuffer() throws Exception |
| 550 |
{ |
| 551 |
DeliveryImpl delivery = createSenderDelivery(); |
| 552 |
|
| 553 |
byte[] data = new byte[] { 0, 1, 2, 3, 4, 5 }; |
| 554 |
ReadableBuffer buffer = ReadableBuffer.ByteBufferReader.wrap(data); |
| 555 |
|
| 556 |
delivery.sendNoCopy(buffer); |
| 557 |
|
| 558 |
assertEquals(data.length, delivery.pending()); |
| 559 |
assertEquals(data.length, delivery.getData().remaining()); |
| 560 |
|
| 561 |
assertSame(buffer, delivery.getData()); |
| 562 |
|
| 563 |
assertSame(data, delivery.getData().array()); |
| 564 |
assertArrayEquals(data, delivery.getData().array()); |
| 565 |
} |
| 566 |
|
| 567 |
@Test |
| 568 |
public void testSendNoCopySingleReadableBufferWhenPreviousBufferWasConsumed() throws Exception |
| 569 |
{ |
| 570 |
DeliveryImpl delivery = createSenderDelivery(); |
| 571 |
|
| 572 |
byte[] data1 = new byte[] { 0, 1, 2, 3, 4, 5 }; |
| 573 |
ReadableBuffer buffer1 = ReadableBuffer.ByteBufferReader.wrap(data1); |
| 574 |
|
| 575 |
byte[] data2 = new byte[] { 0, 1, 2, 3, 4, 5, 6 }; |
| 576 |
ReadableBuffer buffer2 = ReadableBuffer.ByteBufferReader.wrap(data2); |
| 577 |
|
| 578 |
delivery.sendNoCopy(buffer1); |
| 579 |
|
| 580 |
assertEquals(data1.length, delivery.pending()); |
| 581 |
assertEquals(data1.length, delivery.getData().remaining()); |
| 582 |
|
| 583 |
assertSame(buffer1, delivery.getData()); |
| 584 |
|
| 585 |
assertSame(data1, delivery.getData().array()); |
| 586 |
assertArrayEquals(data1, delivery.getData().array()); |
| 587 |
|
| 588 |
delivery.getData().position(delivery.getDataLength()); |
| 589 |
|
| 590 |
delivery.sendNoCopy(buffer2); |
| 591 |
|
| 592 |
assertEquals(data2.length, delivery.pending()); |
| 593 |
assertEquals(data2.length, delivery.getData().remaining()); |
| 594 |
|
| 595 |
assertSame(buffer2, delivery.getData()); |
| 596 |
|
| 597 |
assertSame(data2, delivery.getData().array()); |
| 598 |
assertArrayEquals(data2, delivery.getData().array()); |
| 599 |
} |
| 600 |
|
| 601 |
@Test |
| 602 |
public void testSendNoCopyMultipleReadableBuffers() throws Exception |
| 603 |
{ |
| 604 |
DeliveryImpl delivery = createSenderDelivery(); |
| 605 |
|
| 606 |
byte[] data1 = new byte[] { 0, 1, 2, 3, 4, 5 }; |
| 607 |
byte[] data2 = new byte[] { 6, 7, 8, 9, 10, 11 }; |
| 608 |
|
| 609 |
ReadableBuffer buffer1 = ReadableBuffer.ByteBufferReader.wrap(data1); |
| 610 |
ReadableBuffer buffer2 = ReadableBuffer.ByteBufferReader.wrap(data2); |
| 611 |
|
| 612 |
int length = data1.length + data2.length; |
| 613 |
|
| 614 |
delivery.sendNoCopy(buffer1); |
| 615 |
delivery.sendNoCopy(buffer2); |
| 616 |
|
| 617 |
assertEquals(length, delivery.pending()); |
| 618 |
assertEquals(length, delivery.getData().remaining()); |
| 619 |
|
| 620 |
// The Delivery had to copy because it doesn't aggregate buffers only arrays |
| 621 |
|
| 622 |
CompositeReadableBuffer composite = (CompositeReadableBuffer) delivery.getData(); |
| 623 |
|
| 624 |
assertNotSame(data1, composite.getArrays().get(0)); |
| 625 |
assertNotSame(data2, composite.getArrays().get(1)); |
| 626 |
|
| 627 |
assertArrayEquals(data1, composite.getArrays().get(0)); |
| 628 |
assertArrayEquals(data2, composite.getArrays().get(1)); |
| 629 |
|
| 630 |
byte[] data3 = new byte[] { 12, 13, 14 }; |
| 631 |
ReadableBuffer buffer3 = ReadableBuffer.ByteBufferReader.wrap(data3); |
| 632 |
|
| 633 |
length += data3.length; |
| 634 |
|
| 635 |
delivery.sendNoCopy(buffer3); |
| 636 |
|
| 637 |
assertEquals(length, delivery.pending()); |
| 638 |
assertEquals(length, delivery.getData().remaining()); |
| 639 |
|
| 640 |
assertSame(composite, delivery.getData()); |
| 641 |
|
| 642 |
assertNotSame(data1, composite.getArrays().get(0)); |
| 643 |
assertNotSame(data2, composite.getArrays().get(1)); |
| 644 |
assertNotSame(data3, composite.getArrays().get(2)); |
| 645 |
|
| 646 |
assertArrayEquals(data1, composite.getArrays().get(0)); |
| 647 |
assertArrayEquals(data2, composite.getArrays().get(1)); |
| 648 |
assertArrayEquals(data3, composite.getArrays().get(2)); |
| 649 |
} |
| 650 |
|
| 651 |
//----- Tests for afterSend cleanup --------------------------------------// |
| 652 |
|
| 653 |
@Test |
| 654 |
public void testAfterSendOnEmptyDelivery() throws Exception |
| 655 |
{ |
| 656 |
DeliveryImpl delivery = createSenderDelivery(); |
| 657 |
|
| 658 |
ReadableBuffer sendBuffer = delivery.getData(); |
| 659 |
|
| 660 |
delivery.afterSend(); |
| 661 |
|
| 662 |
assertSame(sendBuffer, delivery.getData()); |
| 663 |
} |
| 664 |
|
| 665 |
@Test |
| 666 |
public void testAfterSendPreservesInteralBufferWhenEmpty() throws Exception |
| 667 |
{ |
| 668 |
DeliveryImpl delivery = createSenderDelivery(); |
| 669 |
|
| 670 |
byte[] data = new byte[] { 0, 1, 2, 3, 4, 5 }; |
| 671 |
ReadableBuffer buffer = ReadableBuffer.ByteBufferReader.wrap(data); |
| 672 |
|
| 673 |
delivery.send(buffer); |
| 674 |
|
| 675 |
assertEquals(data.length, delivery.pending()); |
| 676 |
assertEquals(data.length, delivery.getData().remaining()); |
| 677 |
|
| 678 |
CompositeReadableBuffer composite = (CompositeReadableBuffer) delivery.getData(); |
| 679 |
|
| 680 |
assertNotSame(data, composite.array()); |
| 681 |
assertArrayEquals(data, composite.array()); |
| 682 |
|
| 683 |
delivery.getData().position(delivery.getData().limit()); |
| 684 |
delivery.afterSend(); |
| 685 |
|
| 686 |
assertSame(composite, delivery.getData()); |
| 687 |
} |
| 688 |
|
| 689 |
@Test |
| 690 |
public void testAfterSendNoCopyClearsExternalReadableBuffer() throws Exception |
| 691 |
{ |
| 692 |
DeliveryImpl delivery = createSenderDelivery(); |
| 693 |
|
| 694 |
byte[] data = new byte[] { 0, 1, 2, 3, 4, 5 }; |
| 695 |
ReadableBuffer buffer = ReadableBuffer.ByteBufferReader.wrap(data); |
| 696 |
|
| 697 |
delivery.sendNoCopy(buffer); |
| 698 |
|
| 699 |
ReadableBuffer sendBuffer = delivery.getData(); |
| 700 |
|
| 701 |
assertEquals(data.length, sendBuffer.remaining()); |
| 702 |
assertSame(buffer, sendBuffer); |
| 703 |
|
| 704 |
sendBuffer.position(sendBuffer.limit()); |
| 705 |
|
| 706 |
delivery.afterSend(); |
| 707 |
|
| 708 |
assertNotSame(buffer, delivery.getData()); |
| 709 |
} |
| 710 |
|
| 711 |
@Test |
| 712 |
public void testAfterSendNoCopyPreservesExternalReadableBufferIfNotDrained() throws Exception |
| 713 |
{ |
| 714 |
DeliveryImpl delivery = createSenderDelivery(); |
| 715 |
|
| 716 |
byte[] data = new byte[] { 0, 1, 2, 3, 4, 5 }; |
| 717 |
ReadableBuffer buffer = ReadableBuffer.ByteBufferReader.wrap(data); |
| 718 |
|
| 719 |
delivery.sendNoCopy(buffer); |
| 720 |
|
| 721 |
ReadableBuffer sendBuffer = delivery.getData(); |
| 722 |
|
| 723 |
assertEquals(data.length, sendBuffer.remaining()); |
| 724 |
assertSame(buffer, sendBuffer); |
| 725 |
|
| 726 |
sendBuffer.position(sendBuffer.limit() - 1); |
| 727 |
|
| 728 |
delivery.afterSend(); |
| 729 |
|
| 730 |
assertSame(buffer, delivery.getData()); |
| 731 |
} |
| 732 |
|
| 733 |
//------------------------------------------------------------------------// |
| 734 |
|
| 735 |
private DeliveryImpl createSenderDelivery() { |
| 736 |
LinkImpl link = Mockito.mock(SenderImpl.class); |
| 737 |
ConnectionImpl connection = Mockito.mock(ConnectionImpl.class); |
| 738 |
SessionImpl session = Mockito.mock(SessionImpl.class); |
| 739 |
|
| 740 |
Mockito.when(link.getSession()).thenReturn(session); |
| 741 |
Mockito.when(link.getConnectionImpl()).thenReturn(connection); |
| 742 |
|
| 743 |
return new DeliveryImpl(null, link, null); |
| 744 |
} |
| 745 |
} |