metadata.put(Bisocket.IS_CALLBACK_SERVER, "true");
client.addListener(callbackHandler, metadata);
// Stop PingTimerTask on server. This allows the possibility that the Timer
// will be replaced.
BisocketServerInvoker serverInvoker = (BisocketServerInvoker) connector.getServerInvoker();
Field field = ServerInvoker.class.getDeclaredField("callbackHandlers");
field.setAccessible(true);
Map callbackHandlers = (Map) field.get(serverInvoker);
assertEquals(1, callbackHandlers.size());
ServerInvokerCallbackHandler serverInvokerCallbackHandler;
serverInvokerCallbackHandler = (ServerInvokerCallbackHandler) callbackHandlers.values().iterator().next();
field = ServerInvokerCallbackHandler.class.getDeclaredField("callBackClient");
field.setAccessible(true);
Client callbackClient = (Client) field.get(serverInvokerCallbackHandler);
assertNotNull(callbackClient);
BisocketClientInvoker callbackClientInvoker = (BisocketClientInvoker) callbackClient.getInvoker();
field = BisocketClientInvoker.class.getDeclaredField("pingTimerTask");
field.setAccessible(true);
TimerTask pingTimerTask1 = (TimerTask) field.get(callbackClientInvoker);
BisocketClientInvoker ci = (BisocketClientInvoker) client.getInvoker();
assertNotNull(pingTimerTask1);
pingTimerTask1.cancel();
// Cause recreation of PingTimerTask on server. If call below to
// createControlConnection() doesn't throw an exception, then the test passes.
Thread.sleep(waitPeriod);
field = Client.class.getDeclaredField("callbackConnectors");
field.setAccessible(true);
Map callbackConnectors = (Map) field.get(client);
assertEquals(1, callbackConnectors.size());
Set connectors = (Set) callbackConnectors.values().iterator().next();
assertEquals(1, connectors.size());
Connector callbackConnector = (Connector) connectors.iterator().next();
BisocketServerInvoker callbackInvoker = (BisocketServerInvoker) callbackConnector.getServerInvoker();
field = BisocketServerInvoker.class.getDeclaredField("controlConnectionThreadMap");
field.setAccessible(true);
Map controlConnectionThreadMap = (Map) field.get(callbackInvoker);
assertEquals(1, controlConnectionThreadMap.size());
Thread controlConnectionThread = (Thread) controlConnectionThreadMap.values().iterator().next();
Class[] classes = BisocketServerInvoker.class.getDeclaredClasses();
Class ControlConnectionThreadClass = null;
for (int i = 0; i < classes.length; i++)
{
if (classes[i].toString().indexOf("ControlConnectionThread") != -1)
{
ControlConnectionThreadClass = classes[i];
break;
}
}
assertNotNull(ControlConnectionThreadClass);
field = ControlConnectionThreadClass.getDeclaredField("listenerId");
field.setAccessible(true);
String listenerId = (String) field.get(controlConnectionThread);
assertNotNull(listenerId);
callbackInvoker.createControlConnection(listenerId, false);
Thread.sleep(2000);
// Get PingTimerTask and verify it has changed.
field = BisocketClientInvoker.class.getDeclaredField("pingTimerTask");
field.setAccessible(true);