| 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 |
package org.apache.qpid.proton.systemtests; |
| 20 |
|
| 21 |
import static java.util.EnumSet.of; |
| 22 |
import static org.apache.qpid.proton.engine.EndpointState.ACTIVE; |
| 23 |
import static org.apache.qpid.proton.engine.EndpointState.UNINITIALIZED; |
| 24 |
import static org.apache.qpid.proton.systemtests.TestLoggingHelper.bold; |
| 25 |
import static org.junit.Assert.assertEquals; |
| 26 |
import static org.junit.Assert.assertFalse; |
| 27 |
import static org.junit.Assert.assertNull; |
| 28 |
import static org.junit.Assert.assertTrue; |
| 29 |
|
| 30 |
import java.nio.charset.StandardCharsets; |
| 31 |
import java.util.Arrays; |
| 32 |
import java.util.Random; |
| 33 |
import java.util.logging.Logger; |
| 34 |
|
| 35 |
import org.apache.qpid.proton.Proton; |
| 36 |
import org.apache.qpid.proton.amqp.UnsignedInteger; |
| 37 |
import org.apache.qpid.proton.amqp.messaging.AmqpValue; |
| 38 |
import org.apache.qpid.proton.amqp.messaging.Source; |
| 39 |
import org.apache.qpid.proton.amqp.messaging.Target; |
| 40 |
import org.apache.qpid.proton.amqp.transport.ReceiverSettleMode; |
| 41 |
import org.apache.qpid.proton.amqp.transport.SenderSettleMode; |
| 42 |
import org.apache.qpid.proton.engine.Delivery; |
| 43 |
import org.apache.qpid.proton.engine.Sender; |
| 44 |
import org.apache.qpid.proton.message.Message; |
| 45 |
import org.junit.Test; |
| 46 |
|
| 47 |
public class DeliveryTest extends EngineTestBase |
| 48 |
{ |
| 49 |
private static final Logger LOGGER = Logger.getLogger(DeliveryTest.class.getName()); |
| 50 |
|
| 51 |
private static final int BUFFER_SIZE = 4096; |
| 52 |
|
| 53 |
private final String _sourceAddress = getServer().containerId + "-link1-source"; |
| 54 |
|
| 55 |
@Test |
| 56 |
public void testMessageFormat() throws Exception |
| 57 |
{ |
| 58 |
LOGGER.fine(bold("======== About to create transports")); |
| 59 |
|
| 60 |
getClient().transport = Proton.transport(); |
| 61 |
ProtocolTracerEnabler.setProtocolTracer(getClient().transport, TestLoggingHelper.CLIENT_PREFIX); |
| 62 |
|
| 63 |
getServer().transport = Proton.transport(); |
| 64 |
ProtocolTracerEnabler.setProtocolTracer(getServer().transport, " " + TestLoggingHelper.SERVER_PREFIX); |
| 65 |
|
| 66 |
doOutputInputCycle(); |
| 67 |
|
| 68 |
getClient().connection = Proton.connection(); |
| 69 |
getClient().transport.bind(getClient().connection); |
| 70 |
|
| 71 |
getServer().connection = Proton.connection(); |
| 72 |
getServer().transport.bind(getServer().connection); |
| 73 |
|
| 74 |
LOGGER.fine(bold("======== About to open connections")); |
| 75 |
getClient().connection.open(); |
| 76 |
getServer().connection.open(); |
| 77 |
|
| 78 |
doOutputInputCycle(); |
| 79 |
|
| 80 |
LOGGER.fine(bold("======== About to open sessions")); |
| 81 |
getClient().session = getClient().connection.session(); |
| 82 |
getClient().session.open(); |
| 83 |
|
| 84 |
pumpClientToServer(); |
| 85 |
|
| 86 |
getServer().session = getServer().connection.sessionHead(of(UNINITIALIZED), of(ACTIVE)); |
| 87 |
assertEndpointState(getServer().session, UNINITIALIZED, ACTIVE); |
| 88 |
|
| 89 |
getServer().session.open(); |
| 90 |
assertEndpointState(getServer().session, ACTIVE, ACTIVE); |
| 91 |
|
| 92 |
pumpServerToClient(); |
| 93 |
assertEndpointState(getClient().session, ACTIVE, ACTIVE); |
| 94 |
|
| 95 |
LOGGER.fine(bold("======== About to create reciever")); |
| 96 |
|
| 97 |
getClient().source = new Source(); |
| 98 |
getClient().source.setAddress(_sourceAddress); |
| 99 |
|
| 100 |
getClient().target = new Target(); |
| 101 |
getClient().target.setAddress(null); |
| 102 |
|
| 103 |
getClient().receiver = getClient().session.receiver("link1"); |
| 104 |
getClient().receiver.setTarget(getClient().target); |
| 105 |
getClient().receiver.setSource(getClient().source); |
| 106 |
|
| 107 |
getClient().receiver.setReceiverSettleMode(ReceiverSettleMode.FIRST); |
| 108 |
getClient().receiver.setSenderSettleMode(SenderSettleMode.UNSETTLED); |
| 109 |
|
| 110 |
assertEndpointState(getClient().receiver, UNINITIALIZED, UNINITIALIZED); |
| 111 |
|
| 112 |
getClient().receiver.open(); |
| 113 |
assertEndpointState(getClient().receiver, ACTIVE, UNINITIALIZED); |
| 114 |
|
| 115 |
pumpClientToServer(); |
| 116 |
|
| 117 |
LOGGER.fine(bold("======== About to set up implicitly created sender")); |
| 118 |
|
| 119 |
getServer().sender = (Sender) getServer().connection.linkHead(of(UNINITIALIZED), of(ACTIVE)); |
| 120 |
|
| 121 |
getServer().sender.setReceiverSettleMode(getServer().sender.getRemoteReceiverSettleMode()); |
| 122 |
getServer().sender.setSenderSettleMode(getServer().sender.getRemoteSenderSettleMode()); |
| 123 |
|
| 124 |
org.apache.qpid.proton.amqp.transport.Source serverRemoteSource = getServer().sender.getRemoteSource(); |
| 125 |
getServer().sender.setSource(serverRemoteSource); |
| 126 |
|
| 127 |
assertEndpointState(getServer().sender, UNINITIALIZED, ACTIVE); |
| 128 |
getServer().sender.open(); |
| 129 |
|
| 130 |
assertEndpointState(getServer().sender, ACTIVE, ACTIVE); |
| 131 |
|
| 132 |
pumpServerToClient(); |
| 133 |
|
| 134 |
assertEndpointState(getClient().receiver, ACTIVE, ACTIVE); |
| 135 |
|
| 136 |
int messagCount = 4; |
| 137 |
getClient().receiver.flow(messagCount); |
| 138 |
|
| 139 |
pumpClientToServer(); |
| 140 |
|
| 141 |
LOGGER.fine(bold("======== About to create messages and send to the client")); |
| 142 |
|
| 143 |
sendMessageToClient("delivery1", "Msg1", null); // Don't set it, so it should be defaulted |
| 144 |
sendMessageToClient("delivery2", "Msg2", 0); // Explicitly set it to the default |
| 145 |
sendMessageToClient("delivery3", "Msg3", 1); |
| 146 |
sendMessageToClient("delivery4", "Msg4", UnsignedInteger.MAX_VALUE.intValue()); // Limit |
| 147 |
|
| 148 |
pumpServerToClient(); |
| 149 |
|
| 150 |
LOGGER.fine(bold("======== About to process the messages on the client")); |
| 151 |
|
| 152 |
Delivery clientDelivery1 = receiveMessageFromServer("delivery1", "Msg1"); |
| 153 |
Delivery clientDelivery2 = receiveMessageFromServer("delivery2", "Msg2"); |
| 154 |
Delivery clientDelivery3 = receiveMessageFromServer("delivery3", "Msg3"); |
| 155 |
Delivery clientDelivery4 = receiveMessageFromServer("delivery4", "Msg4"); |
| 156 |
|
| 157 |
// Verify the message format is as expected |
| 158 |
assertEquals("Unexpected message format", 0, clientDelivery1.getMessageFormat()); |
| 159 |
assertEquals("Unexpected message format", 0, clientDelivery2.getMessageFormat()); |
| 160 |
assertEquals("Unexpected message format", 1, clientDelivery3.getMessageFormat()); |
| 161 |
assertEquals("Unexpected message format", UnsignedInteger.MAX_VALUE.intValue(), clientDelivery4.getMessageFormat()); |
| 162 |
} |
| 163 |
|
| 164 |
@Test |
| 165 |
public void testSendReceiveLargeMessage() throws Exception |
| 166 |
{ |
| 167 |
doTestSendReceiveLargeMessage(-1, -1); |
| 168 |
} |
| 169 |
|
| 170 |
@Test |
| 171 |
public void testSendReceiveLargeMessageWithFrameSizeLimits() throws Exception |
| 172 |
{ |
| 173 |
doTestSendReceiveLargeMessage(128 * 1024, 128 * 1024); |
| 174 |
} |
| 175 |
|
| 176 |
private void doTestSendReceiveLargeMessage(int maxFrameSize, int maxOutboundFrameSize) throws Exception |
| 177 |
{ |
| 178 |
LOGGER.fine(bold("======== About to create transports")); |
| 179 |
|
| 180 |
getClient().transport = Proton.transport(); |
| 181 |
if (maxFrameSize > 0) { |
| 182 |
getClient().transport.setMaxFrameSize(maxFrameSize); |
| 183 |
} |
| 184 |
if (maxOutboundFrameSize > 0) { |
| 185 |
getClient().transport.setOutboundFrameSizeLimit(maxOutboundFrameSize); |
| 186 |
} |
| 187 |
ProtocolTracerEnabler.setProtocolTracer(getClient().transport, TestLoggingHelper.CLIENT_PREFIX); |
| 188 |
|
| 189 |
getServer().transport = Proton.transport(); |
| 190 |
if (maxFrameSize > 0) { |
| 191 |
getServer().transport.setMaxFrameSize(maxFrameSize); |
| 192 |
} |
| 193 |
if (maxOutboundFrameSize > 0) { |
| 194 |
getServer().transport.setOutboundFrameSizeLimit(maxOutboundFrameSize); |
| 195 |
} |
| 196 |
ProtocolTracerEnabler.setProtocolTracer(getServer().transport, " " + TestLoggingHelper.SERVER_PREFIX); |
| 197 |
|
| 198 |
doOutputInputCycle(); |
| 199 |
|
| 200 |
getClient().connection = Proton.connection(); |
| 201 |
getClient().transport.bind(getClient().connection); |
| 202 |
|
| 203 |
getServer().connection = Proton.connection(); |
| 204 |
getServer().transport.bind(getServer().connection); |
| 205 |
|
| 206 |
LOGGER.fine(bold("======== About to open connections")); |
| 207 |
getClient().connection.open(); |
| 208 |
getServer().connection.open(); |
| 209 |
|
| 210 |
doOutputInputCycle(); |
| 211 |
|
| 212 |
LOGGER.fine(bold("======== About to open sessions")); |
| 213 |
getClient().session = getClient().connection.session(); |
| 214 |
getClient().session.open(); |
| 215 |
|
| 216 |
pumpClientToServer(); |
| 217 |
|
| 218 |
getServer().session = getServer().connection.sessionHead(of(UNINITIALIZED), of(ACTIVE)); |
| 219 |
assertEndpointState(getServer().session, UNINITIALIZED, ACTIVE); |
| 220 |
|
| 221 |
getServer().session.open(); |
| 222 |
assertEndpointState(getServer().session, ACTIVE, ACTIVE); |
| 223 |
|
| 224 |
pumpServerToClient(); |
| 225 |
assertEndpointState(getClient().session, ACTIVE, ACTIVE); |
| 226 |
|
| 227 |
LOGGER.fine(bold("======== About to create reciever")); |
| 228 |
|
| 229 |
getClient().source = new Source(); |
| 230 |
getClient().source.setAddress(_sourceAddress); |
| 231 |
|
| 232 |
getClient().target = new Target(); |
| 233 |
getClient().target.setAddress(null); |
| 234 |
|
| 235 |
getClient().receiver = getClient().session.receiver("link1"); |
| 236 |
getClient().receiver.setTarget(getClient().target); |
| 237 |
getClient().receiver.setSource(getClient().source); |
| 238 |
|
| 239 |
getClient().receiver.setReceiverSettleMode(ReceiverSettleMode.FIRST); |
| 240 |
getClient().receiver.setSenderSettleMode(SenderSettleMode.UNSETTLED); |
| 241 |
|
| 242 |
assertEndpointState(getClient().receiver, UNINITIALIZED, UNINITIALIZED); |
| 243 |
|
| 244 |
getClient().receiver.open(); |
| 245 |
assertEndpointState(getClient().receiver, ACTIVE, UNINITIALIZED); |
| 246 |
|
| 247 |
pumpClientToServer(); |
| 248 |
|
| 249 |
LOGGER.fine(bold("======== About to set up implicitly created sender")); |
| 250 |
|
| 251 |
getServer().sender = (Sender) getServer().connection.linkHead(of(UNINITIALIZED), of(ACTIVE)); |
| 252 |
|
| 253 |
getServer().sender.setReceiverSettleMode(getServer().sender.getRemoteReceiverSettleMode()); |
| 254 |
getServer().sender.setSenderSettleMode(getServer().sender.getRemoteSenderSettleMode()); |
| 255 |
|
| 256 |
org.apache.qpid.proton.amqp.transport.Source serverRemoteSource = getServer().sender.getRemoteSource(); |
| 257 |
getServer().sender.setSource(serverRemoteSource); |
| 258 |
|
| 259 |
assertEndpointState(getServer().sender, UNINITIALIZED, ACTIVE); |
| 260 |
getServer().sender.open(); |
| 261 |
|
| 262 |
assertEndpointState(getServer().sender, ACTIVE, ACTIVE); |
| 263 |
|
| 264 |
pumpServerToClient(); |
| 265 |
|
| 266 |
assertEndpointState(getClient().receiver, ACTIVE, ACTIVE); |
| 267 |
|
| 268 |
getClient().receiver.flow(1); |
| 269 |
|
| 270 |
pumpClientToServer(); |
| 271 |
|
| 272 |
String messageContent = createMessageContent(256 * 1024); |
| 273 |
|
| 274 |
LOGGER.fine(bold("======== About to create messages and send to the client")); |
| 275 |
|
| 276 |
sendMessageToClient("delivery1", messageContent, 0); // Explicitly set it to the default |
| 277 |
|
| 278 |
pumpAllServerPendingToClient(); |
| 279 |
|
| 280 |
LOGGER.fine(bold("======== About to process the messages on the client")); |
| 281 |
|
| 282 |
Delivery clientDelivery = receiveMessageFromServer("delivery1", messageContent); |
| 283 |
|
| 284 |
// Verify the message format is as expected |
| 285 |
assertEquals("Unexpected message format", 0, clientDelivery.getMessageFormat()); |
| 286 |
} |
| 287 |
|
| 288 |
private Delivery receiveMessageFromServer(String deliveryTag, String messageContent) |
| 289 |
{ |
| 290 |
Delivery delivery = getClient().connection.getWorkHead(); |
| 291 |
|
| 292 |
assertTrue(Arrays.equals(deliveryTag.getBytes(StandardCharsets.UTF_8), delivery.getTag())); |
| 293 |
assertEquals("The received delivery should be on our receiver", |
| 294 |
getClient().receiver, delivery.getLink()); |
| 295 |
|
| 296 |
assertNull(delivery.getLocalState()); |
| 297 |
assertNull(delivery.getRemoteState()); |
| 298 |
|
| 299 |
assertFalse(delivery.isPartial()); |
| 300 |
assertTrue(delivery.isReadable()); |
| 301 |
|
| 302 |
final int BUFFER_SIZE = messageContent.length() * 4; |
| 303 |
byte[] received = new byte[BUFFER_SIZE]; |
| 304 |
int len = getClient().receiver.recv(received, 0, BUFFER_SIZE); |
| 305 |
|
| 306 |
assertTrue("given array was too small", len < BUFFER_SIZE); |
| 307 |
|
| 308 |
Message m = Proton.message(); |
| 309 |
m.decode(received, 0, len); |
| 310 |
|
| 311 |
Object messageBody = ((AmqpValue)m.getBody()).getValue(); |
| 312 |
assertEquals("Unexpected message content", messageContent, messageBody); |
| 313 |
|
| 314 |
boolean receiverAdvanced = getClient().receiver.advance(); |
| 315 |
assertTrue("receiver has not advanced", receiverAdvanced); |
| 316 |
|
| 317 |
return delivery; |
| 318 |
} |
| 319 |
|
| 320 |
private static String createMessageContent(int length) { |
| 321 |
Random rand = new Random(System.currentTimeMillis()); |
| 322 |
|
| 323 |
byte[] payload = new byte[length]; |
| 324 |
for (int i = 0; i < length; i++) { |
| 325 |
payload[i] = (byte) (64 + 1 + rand.nextInt(9)); |
| 326 |
} |
| 327 |
|
| 328 |
return new String(payload, StandardCharsets.UTF_8); |
| 329 |
} |
| 330 |
|
| 331 |
private Delivery sendMessageToClient(String deliveryTag, String messageContent, Integer messageFormat) |
| 332 |
{ |
| 333 |
byte[] tag = deliveryTag.getBytes(StandardCharsets.UTF_8); |
| 334 |
|
| 335 |
Message m = Proton.message(); |
| 336 |
m.setBody(new AmqpValue(messageContent)); |
| 337 |
|
| 338 |
final int BUFFER_SIZE = messageContent.length() * 4; |
| 339 |
byte[] encoded = new byte[BUFFER_SIZE]; |
| 340 |
int len = m.encode(encoded, 0, BUFFER_SIZE); |
| 341 |
|
| 342 |
assertTrue("given array was too small", len <= BUFFER_SIZE); |
| 343 |
|
| 344 |
Delivery serverDelivery = getServer().sender.delivery(tag); |
| 345 |
|
| 346 |
// Verify the default format of 0 is in place |
| 347 |
assertEquals("Unexpected message format", 0, serverDelivery.getMessageFormat()); |
| 348 |
|
| 349 |
// Set the message format explicitly if given, or leave it at the default |
| 350 |
if(messageFormat != null) { |
| 351 |
serverDelivery.setMessageFormat(messageFormat); |
| 352 |
} |
| 353 |
|
| 354 |
int sent = getServer().sender.send(encoded, 0, len); |
| 355 |
|
| 356 |
assertEquals("sender unable to send all data at once as assumed for simplicity", len, sent); |
| 357 |
|
| 358 |
boolean senderAdvanced = getServer().sender.advance(); |
| 359 |
assertTrue("sender has not advanced", senderAdvanced); |
| 360 |
|
| 361 |
return serverDelivery; |
| 362 |
} |
| 363 |
} |