org.eclipse.paho.client.mqttv3.TimerPingSender Java Examples

The following examples show how to use org.eclipse.paho.client.mqttv3.TimerPingSender. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MoquetteConnectivityTest.java    From rxmqtt with Apache License 2.0 4 votes vote down vote up
/**
* This test is unreliable because the subscriptions
* don't always arrive.
* Need to investigate why this is and reinstate.
*/

@Ignore
@Test
public void whenAutoReconnectAndCleanSessionFalseThenNoNeedToResubscribe() throws Throwable {
	
	// Create a latch and a way to count subscription receipts
	CountDownLatch latch = new CountDownLatch(2);
	List<SubscribeMessage> subscribes = new ArrayList<>();
    
    // Create client with re-connect and dirty sessions
    MqttConnectOptions options = new MqttConnectOptions();
    options.setAutomaticReconnect(true);
    options.setCleanSession(false);
    options.setKeepAliveInterval(1);
    options.setConnectionTimeout(1);
    
    // Topic
    String topic = "topical";
    
    // Create clients
    String proxyUrl = "tcp://" + PROXY_HOST + ":" + PROXY_PORT;
    MqttAsyncClient publisher = new MqttAsyncClient(proxyUrl, "test-publisher-id", new MemoryPersistence(), new TimerPingSender());
    MqttAsyncClient subscriber = new MqttAsyncClient(proxyUrl, "test-subscriber-id", new MemoryPersistence(), new TimerPingSender());
    ObservableMqttClient publisherObs = observableClient(publisher, options);
    ObservableMqttClient subscriberObs = observableClient(subscriber, options);
    
    // Connect, subscribe, publish test message
    publisherObs.connect().blockingAwait();
    subscriberObs.connect().blockingAwait();
    Assert.assertTrue(subscriberObs.isConnected());
    Assert.assertTrue(publisherObs.isConnected());
    subscriberObs.subscribe(topic, 1).subscribe(m -> {
    	System.out.println(m);
    	subscribes.add(m);
    	latch.countDown();
    });
    publisherObs.publish(topic, PublishMessage.create("Test Message 1".getBytes(), 1, false)).blockingGet();
    Assert.assertEquals(1, broker.getMessages().size());
    
    // Stop the proxy, wait, check status
    brokerProxy.disable();
    Thread.sleep(3000);
    Assert.assertFalse(publisherObs.isConnected());
    Assert.assertFalse(subscriberObs.isConnected());
 
    // Restart the broker, wait, check status
    brokerProxy.enable();
    Thread.sleep(10000);
    Assert.assertTrue(publisherObs.isConnected());
    Assert.assertTrue(subscriberObs.isConnected());
    
    // Publish and check for subscription
    publisherObs.publish(topic, PublishMessage.create("Test Message 2".getBytes(), 1, false)).blockingGet();
    Assert.assertEquals(2, broker.getMessages().size());
    
    // Check we got all the messages on the subscription
    latch.await(3, TimeUnit.SECONDS);	
    Assert.assertEquals(2, subscribes.size());
}