use of java.util.concurrent.LinkedBlockingQueue in project azure-iot-sdk-java by Azure.
the class AmqpsTransportTest method handleMessageClearsReceivedMessagesListIfNoCallbackIsDefined.
// Tests_SRS_AMQPSTRANSPORT_15_025: [If no callback is defined, the list of received messages is cleared.]
@Test
public void handleMessageClearsReceivedMessagesListIfNoCallbackIsDefined() throws IOException {
new NonStrictExpectations() {
{
new AmqpsIotHubConnection(mockConfig, false);
result = mockConnection;
mockConfig.getMessageCallback();
result = null;
}
};
AmqpsTransport transport = new AmqpsTransport(mockConfig, false);
transport.open();
Queue<AmqpsMessage> receivedMessages = new LinkedBlockingQueue<>();
receivedMessages.add(mockAmqpsMessage);
receivedMessages.add(mockAmqpsMessage);
Deencapsulation.setField(transport, "receivedMessages", receivedMessages);
transport.handleMessage();
Queue<AmqpsMessage> receivedTransportMessages = Deencapsulation.getField(transport, "receivedMessages");
Assert.assertTrue(receivedTransportMessages.size() == 0);
}
use of java.util.concurrent.LinkedBlockingQueue in project azure-iot-sdk-java by Azure.
the class AmqpSendHandlerTest method sendComplete_throws_exception_if_found.
//Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_25_031: [** The event handler shall get the exception from the response and throw is it is not null **]**
@Test(expected = IotHubException.class)
public void sendComplete_throws_exception_if_found(@Mocked final AmqpResponseVerification mockedVerification, @Mocked final IotHubException mockedIotHubException) throws IotHubException, IOException {
// Arrange
String hostName = "aaa";
String userName = "bbb";
String sasToken = "ccc";
IotHubServiceClientProtocol iotHubServiceClientProtocol = IotHubServiceClientProtocol.AMQPS;
AmqpSendHandler amqpSendHandler = new AmqpSendHandler(hostName, userName, sasToken, iotHubServiceClientProtocol);
Queue<AmqpResponseVerification> testsendStatusQueue = new LinkedBlockingQueue<>();
testsendStatusQueue.add(mockedVerification);
Deencapsulation.setField(amqpSendHandler, "sendStatusQueue", testsendStatusQueue);
// Assert
new Expectations() {
{
mockedVerification.getException();
result = mockedIotHubException;
}
};
// Act
amqpSendHandler.sendComplete();
}
use of java.util.concurrent.LinkedBlockingQueue in project azure-iot-sdk-java by Azure.
the class AmqpSendHandlerTest method onLinkFlowBufferOverflow_call_flow_ok.
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_017: [The event handler shall get the Sender (Proton) object from the link]
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_018: [The event handler shall encode the message and copy to the byte buffer]
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_019: [The event handler shall set the delivery tag on the Sender (Proton) object]
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_020: [The event handler shall send the encoded bytes]
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_021: [The event handler shall close the Sender, Session and Connection]
@Test
public void onLinkFlowBufferOverflow_call_flow_ok() throws UnsupportedEncodingException {
// Arrange
String hostName = "aaa";
String userName = "bbb";
String sasToken = "ccc";
String hostAddr = hostName + ":5671";
String deviceId = "deviceId";
String content = "abcdefghijklmnopqrst";
com.microsoft.azure.sdk.iot.service.Message iotMessage = new com.microsoft.azure.sdk.iot.service.Message(content);
String endpoint = "/messages/devicebound";
exceptionCount = 0;
IotHubServiceClientProtocol iotHubServiceClientProtocol = IotHubServiceClientProtocol.AMQPS;
createProtonObjects();
AmqpSendHandler amqpSendHandler = new AmqpSendHandler(hostName, userName, sasToken, iotHubServiceClientProtocol);
Queue<Message> testMessagesToBeSent = new LinkedBlockingQueue<>();
testMessagesToBeSent.add(messageWithException);
Deencapsulation.setField(amqpSendHandler, "messagesToBeSent", testMessagesToBeSent);
// Assert
new Expectations() {
{
link = event.getLink();
link.getCredit();
}
};
// Act
amqpSendHandler.onLinkFlow(event);
}
use of java.util.concurrent.LinkedBlockingQueue in project azure-iot-sdk-java by Azure.
the class AmqpSendHandlerTest method sendComplete_flow_OK.
/*
Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_25_029: [** The event handler shall check the status queue to get the response for the sent message **]**
Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_25_030: [** The event handler shall remove the response from the queue **]**
Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_25_031: [** The event handler shall get the exception from the response and throw is it is not null **]**
*/
@Test
public void sendComplete_flow_OK(@Mocked final AmqpResponseVerification mockedVerification) throws IotHubException, IOException {
// Arrange
String hostName = "aaa";
String userName = "bbb";
String sasToken = "ccc";
IotHubServiceClientProtocol iotHubServiceClientProtocol = IotHubServiceClientProtocol.AMQPS;
AmqpSendHandler amqpSendHandler = new AmqpSendHandler(hostName, userName, sasToken, iotHubServiceClientProtocol);
Queue<AmqpResponseVerification> testsendStatusQueue = new LinkedBlockingQueue<>();
testsendStatusQueue.add(mockedVerification);
Deencapsulation.setField(amqpSendHandler, "sendStatusQueue", testsendStatusQueue);
// Assert
new Expectations() {
{
mockedVerification.getException();
result = null;
}
};
// Act
amqpSendHandler.sendComplete();
}
use of java.util.concurrent.LinkedBlockingQueue in project intellij-community by JetBrains.
the class EDTGuard method create.
@NotNull
public static <T, O extends Watchable> T create(@NotNull final O target, final ProcessHandler process) {
final Pair<LinkedBlockingQueue<Call>, LinkedBlockingQueue<Call.Result>> queue = Pair.create(new LinkedBlockingQueue<Call>(10), new LinkedBlockingQueue<Call.Result>());
final Thread thread = new Thread("Async Invocation Thread for " + process) {
@Override
public void run() {
try {
while (!Thread.currentThread().isInterrupted()) {
final Call call = queue.first.take();
if (call != null) {
queue.second.offer(call.invoke());
}
}
} catch (InterruptedException e) {
// break
}
}
};
thread.start();
final AtomicBoolean ref = new AtomicBoolean();
final Disposable d = new Disposable() {
boolean disposed;
@Override
public void dispose() {
if (!disposed) {
disposed = true;
ref.set(true);
thread.interrupt();
}
}
};
process.addProcessListener(new ProcessAdapter() {
@Override
public void processTerminated(ProcessEvent event) {
synchronized (d) {
Disposer.dispose(d);
}
}
@Override
public void processWillTerminate(ProcessEvent event, boolean willBeDestroyed) {
if (!willBeDestroyed) {
synchronized (d) {
Disposer.dispose(d);
}
}
}
});
final Alarm alarm = new Alarm(Alarm.ThreadToUse.POOLED_THREAD, d);
final Alarm alarm2 = new Alarm(Alarm.ThreadToUse.POOLED_THREAD, alarm);
final Runnable watchdog = () -> ref.set(true);
final Runnable ping = new Runnable() {
@Override
public void run() {
synchronized (d) {
if (alarm.isDisposed()) {
return;
}
alarm2.addRequest(watchdog, 200);
try {
ref.set(!target.ping());
} catch (Exception e) {
ref.set(true);
} finally {
alarm2.cancelRequest(watchdog);
alarm.addRequest(this, 500);
}
}
}
};
alarm.addRequest(ping, 500);
final EDTGuard guard = new EDTGuard(target, queue, ref);
final ClassLoader classLoader = target.getClass().getClassLoader();
final Class<?>[] interfaces = target.getClass().getInterfaces();
//noinspection unchecked
return (T) Proxy.newProxyInstance(classLoader, interfaces, guard);
}
Aggregations