Search in sources :

Example 86 with LinkedBlockingQueue

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);
}
Also used : AmqpsIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection) AmqpsTransport(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) AmqpsMessage(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage) Test(org.junit.Test)

Example 87 with LinkedBlockingQueue

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();
}
Also used : Expectations(mockit.Expectations) AmqpSendHandler(com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSendHandler) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) IotHubServiceClientProtocol(com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol) AmqpResponseVerification(com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpResponseVerification) Test(org.junit.Test)

Example 88 with LinkedBlockingQueue

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);
}
Also used : Expectations(mockit.Expectations) Message(org.apache.qpid.proton.message.Message) AmqpSendHandler(com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSendHandler) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) IotHubServiceClientProtocol(com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol) Test(org.junit.Test)

Example 89 with LinkedBlockingQueue

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();
}
Also used : Expectations(mockit.Expectations) AmqpSendHandler(com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSendHandler) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) IotHubServiceClientProtocol(com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol) AmqpResponseVerification(com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpResponseVerification) Test(org.junit.Test)

Example 90 with LinkedBlockingQueue

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);
}
Also used : Disposable(com.intellij.openapi.Disposable) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) InvocationTargetException(java.lang.reflect.InvocationTargetException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Alarm(com.intellij.util.Alarm) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)259 Test (org.junit.Test)91 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)64 IOException (java.io.IOException)26 ArrayList (java.util.ArrayList)23 Emitter (io.socket.emitter.Emitter)19 JSONObject (org.json.JSONObject)19 CountDownLatch (java.util.concurrent.CountDownLatch)18 ThreadFactory (java.util.concurrent.ThreadFactory)16 ExecutorService (java.util.concurrent.ExecutorService)14 BlockingQueue (java.util.concurrent.BlockingQueue)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 List (java.util.List)12 URI (java.net.URI)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 Intent (android.content.Intent)9 HashMap (java.util.HashMap)9 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)8 Map (java.util.Map)8 UUID (java.util.UUID)8