use of com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection in project azure-iot-sdk-java by Azure.
the class AmqpsTransportTest method messageSentBuffersPreviouslySentMessageIfNotSuccessfullyDelivered.
// Tests_SRS_AMQPSTRANSPORT_15_031: [If the message was not delivered successfully, it is buffered to be sent again.]
@Test
public void messageSentBuffersPreviouslySentMessageIfNotSuccessfullyDelivered() throws IOException {
new NonStrictExpectations() {
{
new AmqpsIotHubConnection(mockConfig, false);
result = mockConnection;
}
};
AmqpsTransport transport = new AmqpsTransport(mockConfig, false);
transport.open();
Map<Integer, IotHubOutboundPacket> inProgressMessages = new ConcurrentHashMap<>();
inProgressMessages.put(1, new IotHubOutboundPacket(new Message(), mockIotHubEventCallback, new Object()));
inProgressMessages.put(2, new IotHubOutboundPacket(new Message(), mockIotHubEventCallback, new Object()));
Deencapsulation.setField(transport, "inProgressMessages", inProgressMessages);
transport.messageSent(1, false);
new Verifications() {
{
new IotHubCallbackPacket(IotHubStatusCode.OK_EMPTY, (IotHubEventCallback) any, any);
times = 0;
}
};
Queue<IotHubOutboundPacket> waitingMessages = Deencapsulation.getField(transport, "waitingMessages");
Queue<IotHubCallbackPacket> callbackList = Deencapsulation.getField(transport, "callbackList");
Assert.assertTrue(inProgressMessages.size() == 1);
Assert.assertTrue(waitingMessages.size() == 1);
Assert.assertTrue(callbackList.size() == 0);
}
use of com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection in project azure-iot-sdk-java by Azure.
the class AmqpsIotHubConnectionTest method onLinkInitReceive.
// Tests_SRS_AMQPSIOTHUBCONNECTION_14_046: [If the link is the Receiver link, the event handler shall create a new Source (Proton) object using the receiver endpoint address member variable.]
// Tests_SRS_AMQPSIOTHUBCONNECTION_14_047: [If the link is the Receiver link, the event handler shall set its source to the created Source (Proton) object.]
@Test
public void onLinkInitReceive() throws IOException {
baseExpectations();
new NonStrictExpectations() {
{
mockEvent.getLink();
result = mockReceiver;
mockReceiver.getName();
result = "receiver";
new Source();
result = mockSource;
mockSource.setAddress(anyString);
mockReceiver.setSource(mockSource);
}
};
final AmqpsIotHubConnection connection = new AmqpsIotHubConnection(mockConfig, false);
connection.onLinkInit(mockEvent);
new Verifications() {
{
mockEvent.getLink();
times = 1;
mockReceiver.getName();
times = 1;
new Source();
times = 1;
mockSource.setAddress(anyString);
times = 1;
mockReceiver.setSource((org.apache.qpid.proton.amqp.transport.Source) any);
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection in project azure-iot-sdk-java by Azure.
the class AmqpsIotHubConnectionTest method onLinkRemoteClose.
// Tests_SRS_AMQPSIOTHUBCONNECTION_15_042 [The event handler shall attempt to reconnect to the IoTHub.]
@Test
public void onLinkRemoteClose() throws IOException {
baseExpectations();
new NonStrictExpectations() {
{
mockEvent.getLink();
result = mockSender;
mockSender.getName();
result = "sender";
mockServerListener.connectionLost();
}
};
final AmqpsIotHubConnection connection = new AmqpsIotHubConnection(mockConfig, false);
final Boolean[] openAsyncCalled = { false };
final Boolean[] closeAsyncCalled = { false };
new MockUp<AmqpsIotHubConnection>() {
@Mock
void openAsync() {
openAsyncCalled[0] = true;
Deencapsulation.setField(connection, "state", State.OPEN);
}
@Mock
void closeAsync() {
closeAsyncCalled[0] = true;
Deencapsulation.setField(connection, "state", State.CLOSED);
}
};
connection.addListener(mockServerListener);
connection.onLinkRemoteClose(mockEvent);
assertEquals(true, closeAsyncCalled[0]);
assertEquals(false, openAsyncCalled[0]);
new Verifications() {
{
mockEvent.getLink();
times = 1;
mockSender.getName();
times = 1;
mockServerListener.connectionLost();
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection in project azure-iot-sdk-java by Azure.
the class AmqpsIotHubConnectionTest method sendMessage.
// Tests_SRS_AMQPSIOTHUBCONNECTION_15_016: [The function shall encode the message and copy the contents to the byte buffer.]
// Tests_SRS_AMQPSIOTHUBCONNECTION_15_017: [The function shall set the delivery tag for the sender.]
// Tests_SRS_AMQPSIOTHUBCONNECTION_15_018: [The function shall attempt to send the message using the sender link.]
// Tests_SRS_AMQPSIOTHUBCONNECTION_15_019: [The function shall advance the sender link.]
// Tests_SRS_AMQPSIOTHUBCONNECTION_15_020: [The function shall set the delivery hash to the value returned by the sender link.]
// Tests_SRS_AMQPSIOTHUBCONNECTION_15_021: [The function shall return the delivery hash.]
@Test
public void sendMessage() throws IOException {
baseExpectations();
new NonStrictExpectations() {
{
mockProtonMessage.encode((byte[]) any, anyInt, anyInt);
mockSender.delivery((byte[]) any);
result = mockDelivery;
}
};
final AmqpsIotHubConnection connection = new AmqpsIotHubConnection(mockConfig, false);
Deencapsulation.setField(connection, "state", State.OPEN);
Deencapsulation.setField(connection, "linkCredit", 100);
Deencapsulation.setField(connection, "sender", mockSender);
Integer expectedDeliveryHash = mockDelivery.hashCode();
Integer actualDeliveryHash = connection.sendMessage(mockProtonMessage);
assertEquals(expectedDeliveryHash, actualDeliveryHash);
new Verifications() {
{
mockProtonMessage.encode((byte[]) any, anyInt, anyInt);
times = 1;
mockSender.delivery((byte[]) any);
times = 1;
mockSender.send((byte[]) any, anyInt, anyInt);
times = 1;
mockSender.advance();
times = 1;
mockDelivery.hashCode();
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection in project azure-iot-sdk-java by Azure.
the class AmqpsIotHubConnectionTest method sendMessageFreesDeliveryIfSendFails.
@Test
public void sendMessageFreesDeliveryIfSendFails() throws IOException {
baseExpectations();
new NonStrictExpectations() {
{
mockProtonMessage.encode((byte[]) any, anyInt, anyInt);
mockSender.delivery((byte[]) any);
result = mockDelivery;
mockSender.send((byte[]) any, anyInt, anyInt);
result = new Exception();
}
};
final AmqpsIotHubConnection connection = new AmqpsIotHubConnection(mockConfig, false);
Deencapsulation.setField(connection, "state", State.OPEN);
Deencapsulation.setField(connection, "linkCredit", 100);
Deencapsulation.setField(connection, "sender", mockSender);
Integer expectedDeliveryHash = -1;
Integer actualDeliveryHash = connection.sendMessage(mockProtonMessage);
assertEquals(expectedDeliveryHash, actualDeliveryHash);
new Verifications() {
{
mockProtonMessage.encode((byte[]) any, anyInt, anyInt);
times = 1;
mockSender.delivery((byte[]) any);
times = 1;
mockSender.send((byte[]) any, anyInt, anyInt);
times = 1;
mockSender.advance();
times = 1;
mockDelivery.free();
times = 1;
}
};
}
Aggregations