| 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.assertTrue; |
| 30 |
|
| 31 |
import java.nio.charset.StandardCharsets; |
| 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.Modified; |
| 39 |
import org.apache.qpid.proton.amqp.messaging.Released; |
| 40 |
import org.apache.qpid.proton.amqp.messaging.Source; |
| 41 |
import org.apache.qpid.proton.amqp.messaging.Target; |
| 42 |
import org.apache.qpid.proton.amqp.transport.ReceiverSettleMode; |
| 43 |
import org.apache.qpid.proton.amqp.transport.SenderSettleMode; |
| 44 |
import org.apache.qpid.proton.engine.Delivery; |
| 45 |
import org.apache.qpid.proton.engine.Sender; |
| 46 |
import org.apache.qpid.proton.message.Message; |
| 47 |
import org.junit.Test; |
| 48 |
|
| 49 |
public class DefaultDeliveryStateTest extends EngineTestBase |
| 50 |
{ |
| 51 |
private static final Logger LOGGER = Logger.getLogger(DefaultDeliveryStateTest.class.getName()); |
| 52 |
|
| 53 |
private static final int BUFFER_SIZE = 4096; |
| 54 |
|
| 55 |
private final String _sourceAddress = getServer().containerId + "-link1-source"; |
| 56 |
|
| 57 |
@Test |
| 58 |
public void testDefaultDeliveryState() throws Exception |
| 59 |
{ |
| 60 |
LOGGER.fine(bold("======== About to create transports")); |
| 61 |
|
| 62 |
getClient().transport = Proton.transport(); |
| 63 |
ProtocolTracerEnabler.setProtocolTracer(getClient().transport, TestLoggingHelper.CLIENT_PREFIX); |
| 64 |
|
| 65 |
getServer().transport = Proton.transport(); |
| 66 |
ProtocolTracerEnabler.setProtocolTracer(getServer().transport, " " + TestLoggingHelper.SERVER_PREFIX); |
| 67 |
|
| 68 |
doOutputInputCycle(); |
| 69 |
|
| 70 |
getClient().connection = Proton.connection(); |
| 71 |
getClient().transport.bind(getClient().connection); |
| 72 |
|
| 73 |
getServer().connection = Proton.connection(); |
| 74 |
getServer().transport.bind(getServer().connection); |
| 75 |
|
| 76 |
LOGGER.fine(bold("======== About to open connections")); |
| 77 |
getClient().connection.open(); |
| 78 |
getServer().connection.open(); |
| 79 |
|
| 80 |
doOutputInputCycle(); |
| 81 |
|
| 82 |
LOGGER.fine(bold("======== About to open sessions")); |
| 83 |
getClient().session = getClient().connection.session(); |
| 84 |
getClient().session.open(); |
| 85 |
|
| 86 |
pumpClientToServer(); |
| 87 |
|
| 88 |
getServer().session = getServer().connection.sessionHead(of(UNINITIALIZED), of(ACTIVE)); |
| 89 |
assertEndpointState(getServer().session, UNINITIALIZED, ACTIVE); |
| 90 |
|
| 91 |
getServer().session.open(); |
| 92 |
assertEndpointState(getServer().session, ACTIVE, ACTIVE); |
| 93 |
|
| 94 |
pumpServerToClient(); |
| 95 |
assertEndpointState(getClient().session, ACTIVE, ACTIVE); |
| 96 |
|
| 97 |
LOGGER.fine(bold("======== About to create reciever")); |
| 98 |
|
| 99 |
getClient().source = new Source(); |
| 100 |
getClient().source.setAddress(_sourceAddress); |
| 101 |
|
| 102 |
getClient().target = new Target(); |
| 103 |
getClient().target.setAddress(null); |
| 104 |
|
| 105 |
getClient().receiver = getClient().session.receiver("link1"); |
| 106 |
getClient().receiver.setTarget(getClient().target); |
| 107 |
getClient().receiver.setSource(getClient().source); |
| 108 |
|
| 109 |
getClient().receiver.setReceiverSettleMode(ReceiverSettleMode.FIRST); |
| 110 |
getClient().receiver.setSenderSettleMode(SenderSettleMode.UNSETTLED); |
| 111 |
|
| 112 |
assertEndpointState(getClient().receiver, UNINITIALIZED, UNINITIALIZED); |
| 113 |
|
| 114 |
getClient().receiver.open(); |
| 115 |
assertEndpointState(getClient().receiver, ACTIVE, UNINITIALIZED); |
| 116 |
|
| 117 |
pumpClientToServer(); |
| 118 |
|
| 119 |
LOGGER.fine(bold("======== About to set up implicitly created sender")); |
| 120 |
|
| 121 |
getServer().sender = (Sender) getServer().connection.linkHead(of(UNINITIALIZED), of(ACTIVE)); |
| 122 |
|
| 123 |
getServer().sender.setReceiverSettleMode(getServer().sender.getRemoteReceiverSettleMode()); |
| 124 |
getServer().sender.setSenderSettleMode(getServer().sender.getRemoteSenderSettleMode()); |
| 125 |
|
| 126 |
org.apache.qpid.proton.amqp.transport.Source serverRemoteSource = getServer().sender.getRemoteSource(); |
| 127 |
getServer().sender.setSource(serverRemoteSource); |
| 128 |
|
| 129 |
assertEndpointState(getServer().sender, UNINITIALIZED, ACTIVE); |
| 130 |
getServer().sender.open(); |
| 131 |
|
| 132 |
assertEndpointState(getServer().sender, ACTIVE, ACTIVE); |
| 133 |
|
| 134 |
pumpServerToClient(); |
| 135 |
|
| 136 |
assertEndpointState(getClient().receiver, ACTIVE, ACTIVE); |
| 137 |
|
| 138 |
int messagCount = 3; |
| 139 |
getClient().receiver.flow(messagCount); |
| 140 |
|
| 141 |
pumpClientToServer(); |
| 142 |
|
| 143 |
LOGGER.fine(bold("======== About to create messages and send to the client")); |
| 144 |
|
| 145 |
Delivery serverDelivery1 = sendMessageToClient("delivery1", "Msg1"); |
| 146 |
Delivery serverDelivery2 = sendMessageToClient("delivery2", "Msg2"); |
| 147 |
Delivery serverDelivery3 = sendMessageToClient("delivery3", "Msg3"); |
| 148 |
|
| 149 |
pumpServerToClient(); |
| 150 |
|
| 151 |
assertNull(serverDelivery1.getLocalState()); |
| 152 |
assertNull(serverDelivery2.getLocalState()); |
| 153 |
assertNull(serverDelivery3.getLocalState()); |
| 154 |
|
| 155 |
assertNull(serverDelivery1.getRemoteState()); |
| 156 |
assertNull(serverDelivery2.getRemoteState()); |
| 157 |
assertNull(serverDelivery3.getRemoteState()); |
| 158 |
|
| 159 |
LOGGER.fine(bold("======== About to process the messages on the client")); |
| 160 |
|
| 161 |
Delivery clientDelivery1 = receiveMessageFromServer("delivery1", "Msg1"); |
| 162 |
Delivery clientDelivery2 = receiveMessageFromServer("delivery2", "Msg2"); |
| 163 |
Delivery clientDelivery3 = receiveMessageFromServer("delivery3", "Msg3"); |
| 164 |
|
| 165 |
// Give them some default state |
| 166 |
clientDelivery1.setDefaultDeliveryState(Released.getInstance()); |
| 167 |
clientDelivery2.setDefaultDeliveryState(Released.getInstance()); |
| 168 |
clientDelivery3.setDefaultDeliveryState(Released.getInstance()); |
| 169 |
|
| 170 |
assertEquals(Released.getInstance(), clientDelivery1.getDefaultDeliveryState()); |
| 171 |
assertEquals(Released.getInstance(), clientDelivery2.getDefaultDeliveryState()); |
| 172 |
assertEquals(Released.getInstance(), clientDelivery3.getDefaultDeliveryState()); |
| 173 |
|
| 174 |
// Check the default state doesn't influence the actual state |
| 175 |
assertNull(clientDelivery1.getLocalState()); |
| 176 |
assertNull(clientDelivery2.getLocalState()); |
| 177 |
assertNull(clientDelivery3.getLocalState()); |
| 178 |
|
| 179 |
assertNull(clientDelivery1.getRemoteState()); |
| 180 |
assertNull(clientDelivery2.getRemoteState()); |
| 181 |
assertNull(clientDelivery3.getRemoteState()); |
| 182 |
|
| 183 |
// Accept one for real, update default on another, leave last untouched |
| 184 |
clientDelivery1.disposition(Accepted.getInstance()); |
| 185 |
clientDelivery2.setDefaultDeliveryState(new Modified()); |
| 186 |
|
| 187 |
// Confirm default and actual states have or have not changed as expected |
| 188 |
assertEquals(Released.getInstance(), clientDelivery1.getDefaultDeliveryState()); |
| 189 |
assertTrue(clientDelivery2.getDefaultDeliveryState() instanceof Modified); |
| 190 |
assertEquals(Released.getInstance(), clientDelivery3.getDefaultDeliveryState()); |
| 191 |
|
| 192 |
assertEquals(Accepted.getInstance(), clientDelivery1.getLocalState()); |
| 193 |
assertNull(clientDelivery2.getLocalState()); |
| 194 |
assertNull(clientDelivery3.getLocalState()); |
| 195 |
|
| 196 |
// Verify the server gets intended state changes |
| 197 |
pumpClientToServer(); |
| 198 |
|
| 199 |
assertEquals(Accepted.getInstance(), serverDelivery1.getRemoteState()); |
| 200 |
assertNull(serverDelivery2.getRemoteState()); |
| 201 |
assertNull(serverDelivery3.getRemoteState()); |
| 202 |
|
| 203 |
// Confirm server sees the default states for second and third |
| 204 |
// messages when they get settled during link free |
| 205 |
getClient().receiver.close(); |
| 206 |
assertEndpointState(getClient().receiver, CLOSED, ACTIVE); |
| 207 |
|
| 208 |
pumpClientToServer(); |
| 209 |
|
| 210 |
assertEndpointState(getServer().sender, ACTIVE, CLOSED); |
| 211 |
getServer().sender.close(); |
| 212 |
|
| 213 |
assertEndpointState(getServer().sender, CLOSED, CLOSED); |
| 214 |
|
| 215 |
pumpServerToClient(); |
| 216 |
|
| 217 |
getClient().receiver.free(); |
| 218 |
|
| 219 |
assertEndpointState(getClient().receiver, CLOSED, CLOSED); |
| 220 |
|
| 221 |
pumpClientToServer(); |
| 222 |
|
| 223 |
assertEquals(Accepted.getInstance(), serverDelivery1.getRemoteState()); |
| 224 |
assertTrue(serverDelivery2.getRemoteState() instanceof Modified); |
| 225 |
assertEquals(Released.getInstance(), serverDelivery3.getRemoteState()); |
| 226 |
} |
| 227 |
|
| 228 |
private Delivery receiveMessageFromServer(String deliveryTag, String messageContent) |
| 229 |
{ |
| 230 |
Delivery delivery = getClient().connection.getWorkHead(); |
| 231 |
|
| 232 |
assertTrue(Arrays.equals(deliveryTag.getBytes(StandardCharsets.UTF_8), delivery.getTag())); |
| 233 |
assertEquals("The received delivery should be on our receiver", |
| 234 |
getClient().receiver, delivery.getLink()); |
| 235 |
|
| 236 |
assertNull(delivery.getLocalState()); |
| 237 |
assertNull(delivery.getRemoteState()); |
| 238 |
|
| 239 |
assertFalse(delivery.isPartial()); |
| 240 |
assertTrue(delivery.isReadable()); |
| 241 |
|
| 242 |
byte[] received = new byte[BUFFER_SIZE]; |
| 243 |
int len = getClient().receiver.recv(received, 0, BUFFER_SIZE); |
| 244 |
|
| 245 |
assertTrue("given array was too small", len < BUFFER_SIZE); |
| 246 |
|
| 247 |
Message m = Proton.message(); |
| 248 |
m.decode(received, 0, len); |
| 249 |
|
| 250 |
Object messageBody = ((AmqpValue)m.getBody()).getValue(); |
| 251 |
assertEquals("Unexpected message content", messageContent, messageBody); |
| 252 |
|
| 253 |
boolean receiverAdvanced = getClient().receiver.advance(); |
| 254 |
assertTrue("receiver has not advanced", receiverAdvanced); |
| 255 |
|
| 256 |
return delivery; |
| 257 |
} |
| 258 |
|
| 259 |
private Delivery sendMessageToClient(String deliveryTag, String messageBody) |
| 260 |
{ |
| 261 |
byte[] tag = deliveryTag.getBytes(StandardCharsets.UTF_8); |
| 262 |
|
| 263 |
Message m = Proton.message(); |
| 264 |
m.setBody(new AmqpValue(messageBody)); |
| 265 |
|
| 266 |
byte[] encoded = new byte[BUFFER_SIZE]; |
| 267 |
int len = m.encode(encoded, 0, BUFFER_SIZE); |
| 268 |
|
| 269 |
assertTrue("given array was too small", len < BUFFER_SIZE); |
| 270 |
|
| 271 |
Delivery serverDelivery = getServer().sender.delivery(tag); |
| 272 |
int sent = getServer().sender.send(encoded, 0, len); |
| 273 |
|
| 274 |
assertEquals("sender unable to send all data at once as assumed for simplicity", len, sent); |
| 275 |
|
| 276 |
boolean senderAdvanced = getServer().sender.advance(); |
| 277 |
assertTrue("sender has not advanced", senderAdvanced); |
| 278 |
|
| 279 |
return serverDelivery; |
| 280 |
} |
| 281 |
} |