else if (wasConnected && !connected.get())
System.err.println("BayeuxClient unconnected");
}
});
String channelName = "/test";
ClientSessionChannel channel = client.getChannel(channelName);
channel.addListener(new ClientSessionChannel.MessageListener()
{
public void onMessage(ClientSessionChannel channel, Message message)
{
if (!message.isSuccessful())
publishLatch.get().countDown();
}
});
client.handshake();
assertTrue(connectLatch.await(5, TimeUnit.SECONDS));
// Wait for a second connect to be issued
Thread.sleep(1000);
// Add some margin since the session is swept every 'sweepIntervalMs'
long networkDown = maxInterval + 3 * sweepInterval;
client.setNetworkDown(networkDown);
// Publish, it must succeed
publishLatch.set(new CountDownLatch(1));
channel.publish(new HashMap());
assertTrue(publishLatch.get().await(5, TimeUnit.SECONDS));
// Wait for the connect to return
// We already slept a bit before, so we are sure that the connect returned
Thread.sleep(timeout);
// Now we are simulating the network is down
// Be sure we are disconnected
assertFalse(connected.get());
// Another publish, it must fail
publishLatch.set(new CountDownLatch(1));
channel.publish(new HashMap());
assertTrue(publishLatch.get().await(5, TimeUnit.SECONDS));
// Sleep to allow the next connect to be issued
Thread.sleep(networkDown);
// Now another connect has been sent (delayed by 'networkDown' ms)
// but the server expired the client, so we handshake again
assertTrue(handshakeLatch.await(5, TimeUnit.SECONDS));
// We should be able to publish now
publishLatch.set(new CountDownLatch(1));
channel.publish(new HashMap());
assertFalse(publishLatch.get().await(1, TimeUnit.SECONDS));
disconnectBayeuxClient(client);
}