Search in sources :

Example 1 with MqttClientPool

use of org.eclipse.kapua.transport.mqtt.pooling.MqttClientPool in project kapua by eclipse.

the class MqttClientPoolTest method testPoolBorrowMax.

/**
 * Ignoring this test for a while. We should fix the build in the first place and then use embedded ActiveMQ
 * broker for tests.
 */
@Ignore
@Test
public void testPoolBorrowMax() throws Exception {
    // 
    // Get pool
    MqttClientPool transportPool = MqttClientPool.getInstance();
    // 
    // Test max borrow clients
    MqttClientPoolSetting setting = MqttClientPoolSetting.getInstance();
    long maxClients = setting.getLong(MqttClientPoolSettingKeys.CLIENT_POOL_SIZE_TOTAL_MAX);
    List<MqttClient> mqttClients = new ArrayList<>();
    for (int i = 0; i < maxClients; i++) {
        mqttClients.add(transportPool.borrowObject());
    }
    // Verify borrowed clients
    for (MqttClient t : mqttClients) {
        assertNotNull("mqttClient", t);
        assertTrue("mqttClient.isConnected", t.isConnected());
    }
    assertEquals("numActiveClients", maxClients, transportPool.getNumActive());
    // Ask one more transport client
    try {
        mqttClients.add(transportPool.borrowObject());
        fail("Should have thrown " + NoSuchElementException.class.getName() + " exception");
    } catch (NoSuchElementException nsee) {
    // All ok
    } catch (Exception e) {
        assertEquals("Should have thrown " + NoSuchElementException.class.getName() + " exception", NoSuchElementException.class, e.getClass());
    }
    // 
    // Return clients
    Iterator<MqttClient> transportClientIterator = mqttClients.iterator();
    while (transportClientIterator.hasNext()) {
        transportPool.returnObject(transportClientIterator.next());
    }
    assertEquals("numActiveClients", 0, transportPool.getNumActive());
}
Also used : MqttClient(org.eclipse.kapua.transport.mqtt.MqttClient) ArrayList(java.util.ArrayList) MqttClientPoolSetting(org.eclipse.kapua.transport.mqtt.pooling.setting.MqttClientPoolSetting) MqttClientPool(org.eclipse.kapua.transport.mqtt.pooling.MqttClientPool) NoSuchElementException(java.util.NoSuchElementException) NoSuchElementException(java.util.NoSuchElementException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with MqttClientPool

use of org.eclipse.kapua.transport.mqtt.pooling.MqttClientPool in project kapua by eclipse.

the class MqttClientPoolTest method testPoolBorrow.

/**
 * Ignoring this test for a while. We should fix the build in the first place and then use embedded ActiveMQ
 * broker for tests.
 */
@Ignore
@Test
public void testPoolBorrow() throws Exception {
    // 
    // Get pool
    MqttClientPool transportPool = MqttClientPool.getInstance();
    // 
    // Borrow client
    MqttClient mqttClient = null;
    try {
        mqttClient = transportPool.borrowObject();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    // 
    // Verify client
    assertNotNull("mqttClient", mqttClient);
    assertTrue("mqttClient.isConnected", mqttClient.isConnected());
    // Return client
    try {
        transportPool.returnObject(mqttClient);
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : MqttClient(org.eclipse.kapua.transport.mqtt.MqttClient) MqttClientPool(org.eclipse.kapua.transport.mqtt.pooling.MqttClientPool) NoSuchElementException(java.util.NoSuchElementException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

NoSuchElementException (java.util.NoSuchElementException)2 MqttClient (org.eclipse.kapua.transport.mqtt.MqttClient)2 MqttClientPool (org.eclipse.kapua.transport.mqtt.pooling.MqttClientPool)2 Ignore (org.junit.Ignore)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 MqttClientPoolSetting (org.eclipse.kapua.transport.mqtt.pooling.setting.MqttClientPoolSetting)1