{
}
});
// First handshake fails
Message message = (Message)results.poll(10, TimeUnit.SECONDS);
Assert.assertNotNull(message);
Assert.assertEquals(Channel.META_HANDSHAKE, message.getChannel());
Assert.assertFalse(message.isSuccessful());
// Second handshake works
message = (Message)results.poll(10, TimeUnit.SECONDS);
Assert.assertNotNull(message);
Assert.assertEquals(Channel.META_HANDSHAKE, message.getChannel());
Assert.assertTrue(message.isSuccessful());
String id = client.getId();
Assert.assertNotNull(id);
boolean subscribe = false;
boolean connect = false;
for (int i = 0; i < 2; ++i)
{
message = (Message)results.poll(10, TimeUnit.SECONDS);
Assert.assertNotNull(message);
subscribe |= Channel.META_SUBSCRIBE.equals(message.getChannel());
connect |= Channel.META_CONNECT.equals(message.getChannel());
}
Assert.assertTrue(subscribe);
Assert.assertTrue(connect);
// Subscribe again
client.getChannel("/foo/bar").subscribe(new ClientSessionChannel.MessageListener()
{
public void onMessage(ClientSessionChannel channel, Message message)
{
}
});
// No second subscribe sent, be sure to wait less than the timeout
// otherwise we get a connect message
message = (Message)results.poll(5, TimeUnit.SECONDS);
Assert.assertNull(message);
client.disconnect();
boolean disconnect = false;
connect = false;
for (int i = 0; i < 2; ++i)
{
message = (Message)results.poll(10, TimeUnit.SECONDS);
Assert.assertNotNull(message);
disconnect |= Channel.META_DISCONNECT.equals(message.getChannel());
connect |= Channel.META_CONNECT.equals(message.getChannel());
}
Assert.assertTrue(disconnect);
Assert.assertTrue(connect);
Assert.assertTrue(client.waitFor(5000, BayeuxClient.State.DISCONNECTED));
// Rehandshake
client.handshake();
Assert.assertTrue(client.waitFor(5000, BayeuxClient.State.CONNECTED));
results.clear();
// Subscribe again
client.getChannel("/foo/bar").subscribe(new ClientSessionChannel.MessageListener()
{
public void onMessage(ClientSessionChannel channel, Message message)
{
}
});
// Subscribe is sent, skip the connect message if present
message = (Message)results.poll(10, TimeUnit.SECONDS);
Assert.assertNotNull(message);
if (Channel.META_CONNECT.equals(message.getChannel()))
message = (Message)results.poll(10, TimeUnit.SECONDS);
Assert.assertEquals(Channel.META_SUBSCRIBE, message.getChannel());
// Restart server
int port = connector.getLocalPort();
server.stop();
server.join();
Assert.assertTrue(client.waitFor(5000, BayeuxClient.State.UNCONNECTED));
connector.setPort(port);
server.start();
bayeux = (BayeuxServerImpl)context.getServletContext().getAttribute(BayeuxServer.ATTRIBUTE);
Assert.assertTrue(client.waitFor(5000, BayeuxClient.State.CONNECTED));
results.clear();
// Subscribe again
client.getChannel("/foo/bar").subscribe(new ClientSessionChannel.MessageListener()
{
public void onMessage(ClientSessionChannel channel, Message message)
{
}
});
// Subscribe is sent, skip the connect message if present
message = (Message)results.poll(10, TimeUnit.SECONDS);
Assert.assertNotNull(message);
if (Channel.META_CONNECT.equals(message.getChannel()))
message = (Message)results.poll(10, TimeUnit.SECONDS);
Assert.assertEquals(Channel.META_SUBSCRIBE, message.getChannel());
disconnectBayeuxClient(client);
}