Search in sources :

Example 1 with MemoryPersistence

use of org.eclipse.paho.client.mqttv3.persist.MemoryPersistence in project azure-iot-sdk-java by Azure.

the class MqttTest method constructorThrowsExceptionIfMqttAsyncClientFails.

/*
    **Tests_SRS_Mqtt_25_045: [**The constructor throws IOException if MqttException is thrown and doesn't instantiate this instance.**]**
     */
@Test(expected = IOException.class)
public void constructorThrowsExceptionIfMqttAsyncClientFails() throws IOException, MqttException {
    Mqtt mockMqtt = null;
    try {
        //arrange
        new NonStrictExpectations() {

            {
                new MemoryPersistence();
                result = mockMemoryPersistence;
                new MqttAsyncClient(serverUri, clientId, mockMemoryPersistence);
                result = mockMqttException;
            }
        };
        //act
        mockMqtt = instantiateMqtt(true);
    } finally {
        testCleanUp(mockMqtt);
    }
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) MemoryPersistence(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) Test(org.junit.Test)

Example 2 with MemoryPersistence

use of org.eclipse.paho.client.mqttv3.persist.MemoryPersistence in project azure-iot-sdk-java by Azure.

the class MqttTest method connectionLostAttemptsToReconnect.

/*
    **Tests_SRS_Mqtt_25_026: [**The function shall notify all its concrete classes by calling abstract method onReconnect at the entry of the function**]**
     */
/*
    **Tests_SRS_Mqtt_25_029: [**The function shall notify all its concrete classes by calling abstract method onReconnectComplete at the exit of the function**]**
     */
@Test
public void connectionLostAttemptsToReconnect() throws IOException, MqttException {
    //arrange
    Mqtt mockMqtt = null;
    Throwable t = new Throwable();
    try {
        new StrictExpectations() {

            {
                new MemoryPersistence();
                result = mockMemoryPersistence;
                new MqttAsyncClient(serverUri, clientId, mockMemoryPersistence);
                result = mockMqttAsyncClient;
                mockMqttAsyncClient.setCallback((Mqtt) any);
                new MqttConnectOptions();
                result = mockMqttConnectionOptions;
                mockMqttConnectionOptions.setKeepAliveInterval(anyInt);
                mockMqttConnectionOptions.setCleanSession(anyBoolean);
                mockMqttConnectionOptions.setMqttVersion(anyInt);
                mockMqttConnectionOptions.setUserName(anyString);
                mockMqttConnectionOptions.setPassword(password.toCharArray());
                mockMqttConnectionOptions.setSocketFactory(mockIotHubSSLContext.getIotHubSSlContext().getSocketFactory());
                mockMqttAsyncClient.isConnected();
                result = false;
                mockMqttAsyncClient.isConnected();
                result = false;
                mockMqttAsyncClient.connect(mockMqttConnectionOptions);
                result = mockMqttToken;
                mockMqttToken.waitForCompletion();
                mockMqttAsyncClient.isConnected();
                result = true;
            }
        };
        //act
        try {
            mockMqtt = instantiateMqtt(true);
            mockMqtt.connectionLost(t);
        } catch (Exception e) {
            System.out.print("Completed throwing exception - " + e.getCause() + e.getMessage());
        }
    } finally {
        testCleanUp(mockMqtt);
    }
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) MemoryPersistence(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) IOException(java.io.IOException) InvalidParameterException(java.security.InvalidParameterException) Test(org.junit.Test)

Example 3 with MemoryPersistence

use of org.eclipse.paho.client.mqttv3.persist.MemoryPersistence in project azure-iot-sdk-java by Azure.

the class MqttTest method connectionLostAttemptsToReconnectAgainIfConnectFails.

/*
    **Tests_SRS_Mqtt_25_027: [**The function shall attempt to reconnect to the IoTHub in a loop with exponential backoff until it succeeds**]**
     */
@Test
public void connectionLostAttemptsToReconnectAgainIfConnectFails() throws IOException, MqttException {
    //arrange
    Mqtt mockMqtt = null;
    Throwable t = new Throwable();
    try {
        new StrictExpectations() {

            {
                new MemoryPersistence();
                result = mockMemoryPersistence;
                new MqttAsyncClient(serverUri, clientId, mockMemoryPersistence);
                result = mockMqttAsyncClient;
                mockMqttAsyncClient.setCallback((Mqtt) any);
                new MqttConnectOptions();
                result = mockMqttConnectionOptions;
                mockMqttConnectionOptions.setKeepAliveInterval(anyInt);
                mockMqttConnectionOptions.setCleanSession(anyBoolean);
                mockMqttConnectionOptions.setMqttVersion(anyInt);
                mockMqttConnectionOptions.setUserName(anyString);
                mockMqttConnectionOptions.setPassword(password.toCharArray());
                mockMqttConnectionOptions.setSocketFactory(mockIotHubSSLContext.getIotHubSSlContext().getSocketFactory());
                mockMqttAsyncClient.isConnected();
                result = false;
                mockMqttAsyncClient.isConnected();
                result = false;
                mockMqttAsyncClient.connect(mockMqttConnectionOptions);
                result = mockMqttException;
                mockMqttAsyncClient.isConnected();
                result = false;
                mockMqttAsyncClient.isConnected();
                result = false;
                mockMqttAsyncClient.connect(mockMqttConnectionOptions);
                result = mockMqttToken;
                mockMqttToken.waitForCompletion();
                mockMqttAsyncClient.isConnected();
                result = true;
            }
        };
        //act
        try {
            mockMqtt = instantiateMqtt(true);
            mockMqtt.connectionLost(t);
        } catch (Exception e) {
            System.out.print("Completed throwing exception - " + e.getCause() + e.getMessage());
        }
    } finally {
        testCleanUp(mockMqtt);
    }
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) MemoryPersistence(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) IOException(java.io.IOException) InvalidParameterException(java.security.InvalidParameterException) Test(org.junit.Test)

Example 4 with MemoryPersistence

use of org.eclipse.paho.client.mqttv3.persist.MemoryPersistence in project ignite by apache.

the class IgniteMqttStreamerTest method beforeTest.

/**
     * @throws Exception If failed.
     */
@Before
@SuppressWarnings("unchecked")
public void beforeTest() throws Exception {
    grid().<Integer, String>getOrCreateCache(defaultCacheConfiguration());
    // find an available local port
    try (ServerSocket ss = new ServerSocket(0)) {
        port = ss.getLocalPort();
    }
    // create the broker
    broker = new BrokerService();
    broker.setDeleteAllMessagesOnStartup(true);
    broker.setPersistent(false);
    broker.setPersistenceAdapter(null);
    broker.setPersistenceFactory(null);
    PolicyMap plcMap = new PolicyMap();
    PolicyEntry plc = new PolicyEntry();
    plc.setQueuePrefetch(1);
    broker.setDestinationPolicy(plcMap);
    broker.getDestinationPolicy().setDefaultEntry(plc);
    broker.setSchedulerSupport(false);
    // add the MQTT transport connector to the broker
    broker.addConnector("mqtt://localhost:" + port);
    broker.setStartAsync(false);
    broker.start(true);
    // create the broker URL
    brokerUrl = "tcp://localhost:" + port;
    // create the client and connect
    client = new MqttClient(brokerUrl, UUID.randomUUID().toString(), new MemoryPersistence());
    client.connect();
    // create mqtt streamer
    dataStreamer = grid().dataStreamer(DEFAULT_CACHE_NAME);
    streamer = createMqttStreamer(dataStreamer);
}
Also used : MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) PolicyMap(org.apache.activemq.broker.region.policy.PolicyMap) MemoryPersistence(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) ServerSocket(java.net.ServerSocket) BrokerService(org.apache.activemq.broker.BrokerService) PolicyEntry(org.apache.activemq.broker.region.policy.PolicyEntry) Before(org.junit.Before)

Aggregations

MemoryPersistence (org.eclipse.paho.client.mqttv3.persist.MemoryPersistence)4 Mqtt (com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 InvalidParameterException (java.security.InvalidParameterException)2 ServerSocket (java.net.ServerSocket)1 BrokerService (org.apache.activemq.broker.BrokerService)1 PolicyEntry (org.apache.activemq.broker.region.policy.PolicyEntry)1 PolicyMap (org.apache.activemq.broker.region.policy.PolicyMap)1 MqttClient (org.eclipse.paho.client.mqttv3.MqttClient)1 Before (org.junit.Before)1