| 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.CLOSED; |
| 24 |
import static org.apache.qpid.proton.engine.EndpointState.UNINITIALIZED; |
| 25 |
import static org.apache.qpid.proton.systemtests.TestLoggingHelper.bold; |
| 26 |
import static org.junit.Assert.assertEquals; |
| 27 |
import static org.junit.Assert.assertFalse; |
| 28 |
import static org.junit.Assert.assertNull; |
| 29 |
import static org.junit.Assert.assertSame; |
| 30 |
import static org.junit.Assert.assertTrue; |
| 31 |
|
| 32 |
import java.util.Arrays; |
| 33 |
import java.util.logging.Logger; |
| 34 |
|
| 35 |
import org.apache.qpid.proton.Proton; |
| 36 |
import org.apache.qpid.proton.amqp.messaging.Accepted; |
| 37 |
import org.apache.qpid.proton.amqp.messaging.AmqpValue; |
| 38 |
import org.apache.qpid.proton.amqp.messaging.Section; |
| 39 |
import org.apache.qpid.proton.amqp.messaging.Source; |
| 40 |
import org.apache.qpid.proton.amqp.messaging.Target; |
| 41 |
import org.apache.qpid.proton.amqp.transport.ReceiverSettleMode; |
| 42 |
import org.apache.qpid.proton.amqp.transport.SenderSettleMode; |
| 43 |
import org.apache.qpid.proton.engine.Delivery; |
| 44 |
import org.apache.qpid.proton.engine.Receiver; |
| 45 |
import org.apache.qpid.proton.message.Message; |
| 46 |
import org.junit.Ignore; |
| 47 |
import org.junit.Test; |
| 48 |
|
| 49 |
/** |
| 50 |
* Simple example to illustrate the use of the Engine and Message APIs. |
| 51 |
* |
| 52 |
* Implemented as a JUnit test for convenience, although the main purpose is to educate the reader |
| 53 |
* rather than test the code. |
| 54 |
* |
| 55 |
* To see the protocol trace, add the following line to test/resources/logging.properties: |
| 56 |
* |
| 57 |
* org.apache.qpid.proton.logging.LoggingProtocolTracer.sent.level = ALL |
| 58 |
* |
| 59 |
* and to see the byte level trace, add the following: |
| 60 |
* |
| 61 |
* org.apache.qpid.proton.systemtests.ProtonEngineExampleTest.level = ALL |
| 62 |
* |
| 63 |
* Does not illustrate use of the Messenger API. |
| 64 |
*/ |
| 65 |
public class ProtonEngineExampleTest extends EngineTestBase |
| 66 |
{ |
| 67 |
private static final Logger LOGGER = Logger.getLogger(ProtonEngineExampleTest.class.getName()); |
| 68 |
|
| 69 |
private static final int BUFFER_SIZE = 4096; |
| 70 |
|
| 71 |
private final String _targetAddress = getServer().containerId + "-link1-target"; |
| 72 |
|
| 73 |
@Test |
| 74 |
public void test() throws Exception |
| 75 |
{ |
| 76 |
LOGGER.fine(bold("======== About to create transports")); |
| 77 |
|
| 78 |
getClient().transport = Proton.transport(); |
| 79 |
ProtocolTracerEnabler.setProtocolTracer(getClient().transport, TestLoggingHelper.CLIENT_PREFIX); |
| 80 |
|
| 81 |
getServer().transport = Proton.transport(); |
| 82 |
ProtocolTracerEnabler.setProtocolTracer(getServer().transport, " " + TestLoggingHelper.SERVER_PREFIX); |
| 83 |
|
| 84 |
doOutputInputCycle(); |
| 85 |
|
| 86 |
getClient().connection = Proton.connection(); |
| 87 |
getClient().transport.bind(getClient().connection); |
| 88 |
|
| 89 |
getServer().connection = Proton.connection(); |
| 90 |
getServer().transport.bind(getServer().connection); |
| 91 |
|
| 92 |
LOGGER.fine(bold("======== About to open connections")); |
| 93 |
getClient().connection.open(); |
| 94 |
getServer().connection.open(); |
| 95 |
|
| 96 |
doOutputInputCycle(); |
| 97 |
|
| 98 |
LOGGER.fine(bold("======== About to open sessions")); |
| 99 |
getClient().session = getClient().connection.session(); |
| 100 |
getClient().session.open(); |
| 101 |
|
| 102 |
pumpClientToServer(); |
| 103 |
|
| 104 |
getServer().session = getServer().connection.sessionHead(of(UNINITIALIZED), of(ACTIVE)); |
| 105 |
assertEndpointState(getServer().session, UNINITIALIZED, ACTIVE); |
| 106 |
|
| 107 |
getServer().session.open(); |
| 108 |
assertEndpointState(getServer().session, ACTIVE, ACTIVE); |
| 109 |
|
| 110 |
pumpServerToClient(); |
| 111 |
assertEndpointState(getClient().session, ACTIVE, ACTIVE); |
| 112 |
|
| 113 |
LOGGER.fine(bold("======== About to create sender")); |
| 114 |
|
| 115 |
getClient().source = new Source(); |
| 116 |
getClient().source.setAddress(null); |
| 117 |
|
| 118 |
getClient().target = new Target(); |
| 119 |
getClient().target.setAddress(_targetAddress); |
| 120 |
|
| 121 |
getClient().sender = getClient().session.sender("link1"); |
| 122 |
getClient().sender.setTarget(getClient().target); |
| 123 |
getClient().sender.setSource(getClient().source); |
| 124 |
// Exactly once delivery semantics |
| 125 |
getClient().sender.setSenderSettleMode(SenderSettleMode.UNSETTLED); |
| 126 |
getClient().sender.setReceiverSettleMode(ReceiverSettleMode.SECOND); |
| 127 |
|
| 128 |
assertEndpointState(getClient().sender, UNINITIALIZED, UNINITIALIZED); |
| 129 |
|
| 130 |
getClient().sender.open(); |
| 131 |
assertEndpointState(getClient().sender, ACTIVE, UNINITIALIZED); |
| 132 |
|
| 133 |
pumpClientToServer(); |
| 134 |
|
| 135 |
LOGGER.fine(bold("======== About to set up implicitly created receiver")); |
| 136 |
|
| 137 |
// A real application would be interested in more states than simply ACTIVE, as there |
| 138 |
// exists the possibility that the link could have moved to another state already e.g. CLOSED. |
| 139 |
// (See pipelining). |
| 140 |
getServer().receiver = (Receiver) getServer().connection.linkHead(of(UNINITIALIZED), of(ACTIVE)); |
| 141 |
// Accept the settlement modes suggested by the client |
| 142 |
getServer().receiver.setSenderSettleMode(getServer().receiver.getRemoteSenderSettleMode()); |
| 143 |
getServer().receiver.setReceiverSettleMode(getServer().receiver.getRemoteReceiverSettleMode()); |
| 144 |
|
| 145 |
org.apache.qpid.proton.amqp.transport.Target serverRemoteTarget = getServer().receiver.getRemoteTarget(); |
| 146 |
assertTerminusEquals(getClient().target, serverRemoteTarget); |
| 147 |
|
| 148 |
getServer().receiver.setTarget(applicationDeriveTarget(serverRemoteTarget)); |
| 149 |
|
| 150 |
assertEndpointState(getServer().receiver, UNINITIALIZED, ACTIVE); |
| 151 |
getServer().receiver.open(); |
| 152 |
|
| 153 |
assertEndpointState(getServer().receiver, ACTIVE, ACTIVE); |
| 154 |
|
| 155 |
pumpServerToClient(); |
| 156 |
assertEndpointState(getClient().sender, ACTIVE, ACTIVE); |
| 157 |
|
| 158 |
getServer().receiver.flow(1); |
| 159 |
pumpServerToClient(); |
| 160 |
|
| 161 |
LOGGER.fine(bold("======== About to create a message and send it to the server")); |
| 162 |
|
| 163 |
getClient().message = Proton.message(); |
| 164 |
Section messageBody = new AmqpValue("Hello"); |
| 165 |
getClient().message.setBody(messageBody); |
| 166 |
getClient().messageData = new byte[BUFFER_SIZE]; |
| 167 |
int lengthOfEncodedMessage = getClient().message.encode(getClient().messageData, 0, BUFFER_SIZE); |
| 168 |
getTestLoggingHelper().prettyPrint(TestLoggingHelper.MESSAGE_PREFIX, Arrays.copyOf(getClient().messageData, lengthOfEncodedMessage)); |
| 169 |
|
| 170 |
byte[] deliveryTag = "delivery1".getBytes(); |
| 171 |
getClient().delivery = getClient().sender.delivery(deliveryTag); |
| 172 |
int numberOfBytesAcceptedBySender = getClient().sender.send(getClient().messageData, 0, lengthOfEncodedMessage); |
| 173 |
assertEquals("For simplicity, assume the sender can accept all the data", |
| 174 |
lengthOfEncodedMessage, numberOfBytesAcceptedBySender); |
| 175 |
|
| 176 |
assertNull(getClient().delivery.getLocalState()); |
| 177 |
|
| 178 |
boolean senderAdvanced = getClient().sender.advance(); |
| 179 |
assertTrue("sender has not advanced", senderAdvanced); |
| 180 |
|
| 181 |
pumpClientToServer(); |
| 182 |
|
| 183 |
LOGGER.fine(bold("======== About to process the message on the server")); |
| 184 |
|
| 185 |
getServer().delivery = getServer().connection.getWorkHead(); |
| 186 |
assertEquals("The received delivery should be on our receiver", |
| 187 |
getServer().receiver, getServer().delivery.getLink()); |
| 188 |
assertNull(getServer().delivery.getLocalState()); |
| 189 |
assertNull(getServer().delivery.getRemoteState()); |
| 190 |
|
| 191 |
assertFalse(getServer().delivery.isPartial()); |
| 192 |
assertTrue(getServer().delivery.isReadable()); |
| 193 |
|
| 194 |
getServer().messageData = new byte[BUFFER_SIZE]; |
| 195 |
int numberOfBytesProducedByReceiver = getServer().receiver.recv(getServer().messageData, 0, BUFFER_SIZE); |
| 196 |
assertEquals(numberOfBytesAcceptedBySender, numberOfBytesProducedByReceiver); |
| 197 |
|
| 198 |
getServer().message = Proton.message(); |
| 199 |
getServer().message.decode(getServer().messageData, 0, numberOfBytesProducedByReceiver); |
| 200 |
|
| 201 |
boolean messageProcessed = applicationProcessMessage(getServer().message); |
| 202 |
assertTrue(messageProcessed); |
| 203 |
|
| 204 |
getServer().delivery.disposition(Accepted.getInstance()); |
| 205 |
assertEquals(Accepted.getInstance(), getServer().delivery.getLocalState()); |
| 206 |
|
| 207 |
pumpServerToClient(); |
| 208 |
assertEquals(Accepted.getInstance(), getClient().delivery.getRemoteState()); |
| 209 |
|
| 210 |
LOGGER.fine(bold("======== About to accept and settle the message on the client")); |
| 211 |
|
| 212 |
Delivery clientDelivery = getClient().connection.getWorkHead(); |
| 213 |
assertEquals(getClient().delivery, clientDelivery); |
| 214 |
assertTrue(clientDelivery.isUpdated()); |
| 215 |
assertEquals(getClient().sender, clientDelivery.getLink()); |
| 216 |
clientDelivery.disposition(clientDelivery.getRemoteState()); |
| 217 |
assertEquals(Accepted.getInstance(), getClient().delivery.getLocalState()); |
| 218 |
|
| 219 |
clientDelivery.settle(); |
| 220 |
assertNull("Now we've settled, the delivery should no longer be in the work list", getClient().connection.getWorkHead()); |
| 221 |
|
| 222 |
pumpClientToServer(); |
| 223 |
|
| 224 |
LOGGER.fine(bold("======== About to settle the message on the server")); |
| 225 |
|
| 226 |
assertEquals(Accepted.getInstance(), getServer().delivery.getRemoteState()); |
| 227 |
Delivery serverDelivery = getServer().connection.getWorkHead(); |
| 228 |
assertEquals(getServer().delivery, serverDelivery); |
| 229 |
assertTrue(serverDelivery.isUpdated()); |
| 230 |
assertTrue("Client should have already settled", serverDelivery.remotelySettled()); |
| 231 |
serverDelivery.settle(); |
| 232 |
assertTrue(serverDelivery.isSettled()); |
| 233 |
assertNull("Now we've settled, the delivery should no longer be in the work list", getServer().connection.getWorkHead()); |
| 234 |
|
| 235 |
// Increment the receiver's credit so its ready for another message. |
| 236 |
// When using proton-c, this call is required in order to generate a Flow frame |
| 237 |
// (proton-j sends one even without it to eagerly restore the session incoming window). |
| 238 |
getServer().receiver.flow(1); |
| 239 |
pumpServerToClient(); |
| 240 |
|
| 241 |
LOGGER.fine(bold("======== About to close client's sender")); |
| 242 |
|
| 243 |
getClient().sender.close(); |
| 244 |
|
| 245 |
pumpClientToServer(); |
| 246 |
|
| 247 |
LOGGER.fine(bold("======== Server about to process client's link closure")); |
| 248 |
|
| 249 |
assertSame(getServer().receiver, getServer().connection.linkHead(of(ACTIVE), of(CLOSED))); |
| 250 |
getServer().receiver.close(); |
| 251 |
|
| 252 |
pumpServerToClient(); |
| 253 |
|
| 254 |
LOGGER.fine(bold("======== About to close client's session")); |
| 255 |
|
| 256 |
getClient().session.close(); |
| 257 |
|
| 258 |
pumpClientToServer(); |
| 259 |
|
| 260 |
LOGGER.fine(bold("======== Server about to process client's session closure")); |
| 261 |
|
| 262 |
assertSame(getServer().session, getServer().connection.sessionHead(of(ACTIVE), of(CLOSED))); |
| 263 |
getServer().session.close(); |
| 264 |
|
| 265 |
pumpServerToClient(); |
| 266 |
|
| 267 |
LOGGER.fine(bold("======== About to close client's connection")); |
| 268 |
|
| 269 |
getClient().connection.close(); |
| 270 |
|
| 271 |
pumpClientToServer(); |
| 272 |
|
| 273 |
LOGGER.fine(bold("======== Server about to process client's connection closure")); |
| 274 |
|
| 275 |
assertEquals(CLOSED, getServer().connection.getRemoteState()); |
| 276 |
getServer().connection.close(); |
| 277 |
|
| 278 |
pumpServerToClient(); |
| 279 |
|
| 280 |
LOGGER.fine(bold("======== Checking client has nothing more to pump")); |
| 281 |
|
| 282 |
assertClientHasNothingToOutput(); |
| 283 |
|
| 284 |
LOGGER.fine(bold("======== Done!")); |
| 285 |
} |
| 286 |
|
| 287 |
@Ignore("This test does not have a fix yet") |
| 288 |
@Test |
| 289 |
public void testPROTON_1017() throws Exception |
| 290 |
{ |
| 291 |
LOGGER.fine(bold("======== About to create transports")); |
| 292 |
|
| 293 |
getClient().transport = Proton.transport(); |
| 294 |
ProtocolTracerEnabler.setProtocolTracer(getClient().transport, TestLoggingHelper.CLIENT_PREFIX); |
| 295 |
|
| 296 |
getServer().transport = Proton.transport(); |
| 297 |
ProtocolTracerEnabler.setProtocolTracer(getServer().transport, " " + TestLoggingHelper.SERVER_PREFIX); |
| 298 |
|
| 299 |
doOutputInputCycle(); |
| 300 |
|
| 301 |
getClient().connection = Proton.connection(); |
| 302 |
getClient().transport.bind(getClient().connection); |
| 303 |
|
| 304 |
getServer().connection = Proton.connection(); |
| 305 |
getServer().transport.bind(getServer().connection); |
| 306 |
|
| 307 |
LOGGER.fine(bold("======== About to open connections")); |
| 308 |
getClient().connection.open(); |
| 309 |
getServer().connection.open(); |
| 310 |
|
| 311 |
doOutputInputCycle(); |
| 312 |
|
| 313 |
LOGGER.fine(bold("======== About to open and close client session")); |
| 314 |
getClient().session = getClient().connection.session(); |
| 315 |
getClient().session.open(); |
| 316 |
getClient().session.close(); |
| 317 |
pumpClientToServer(); |
| 318 |
|
| 319 |
getServer().session = getServer().connection.sessionHead(of(UNINITIALIZED), of(CLOSED)); |
| 320 |
assertEndpointState(getServer().session, UNINITIALIZED, CLOSED); |
| 321 |
|
| 322 |
getServer().session.open(); |
| 323 |
assertEndpointState(getServer().session, ACTIVE, CLOSED); |
| 324 |
|
| 325 |
getServer().session.close(); |
| 326 |
assertEndpointState(getServer().session, CLOSED, CLOSED); |
| 327 |
|
| 328 |
pumpServerToClient(); |
| 329 |
assertEndpointState(getClient().session, CLOSED, CLOSED); |
| 330 |
|
| 331 |
LOGGER.fine(bold("======== About to close client's connection")); |
| 332 |
|
| 333 |
getClient().connection.close(); |
| 334 |
|
| 335 |
pumpClientToServer(); |
| 336 |
|
| 337 |
LOGGER.fine(bold("======== Server about to process client's connection closure")); |
| 338 |
|
| 339 |
assertEquals(CLOSED, getServer().connection.getRemoteState()); |
| 340 |
getServer().connection.close(); |
| 341 |
|
| 342 |
pumpServerToClient(); |
| 343 |
|
| 344 |
LOGGER.fine(bold("======== Checking client has nothing more to pump")); |
| 345 |
|
| 346 |
assertClientHasNothingToOutput(); |
| 347 |
|
| 348 |
LOGGER.fine(bold("======== Done!")); |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* Simulates creating a local terminus using the properties supplied by the remote link endpoint. |
| 353 |
* |
| 354 |
* In a broker you'd usually overlay serverRemoteTarget (eg its filter properties) onto |
| 355 |
* an existing object (which eg contains whether it's a queue or a topic), creating a new one from that |
| 356 |
* overlay. Also if this is link recovery then you'd fetch the unsettled map too. |
| 357 |
*/ |
| 358 |
private org.apache.qpid.proton.amqp.transport.Target applicationDeriveTarget(org.apache.qpid.proton.amqp.transport.Target serverRemoteTarget) |
| 359 |
{ |
| 360 |
return serverRemoteTarget; |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* Simulates processing a message. |
| 365 |
*/ |
| 366 |
private boolean applicationProcessMessage(Message message) |
| 367 |
{ |
| 368 |
Object messageBody = ((AmqpValue)message.getBody()).getValue(); |
| 369 |
return "Hello".equals(messageBody); |
| 370 |
} |
| 371 |
} |