config.put(InvokerLocator.FORCE_REMOTE, "true");
config.put(Bisocket.IS_CALLBACK_SERVER, "true");
config.put(Bisocket.PING_FREQUENCY, TEST_PING_FREQUENCY_STRING);
addExtraClientConfig(config);
Client client = new Client(serverLocator, config);
client.connect();
log.info("client is connected");
assertTrue(client.getInvoker() instanceof BisocketClientInvoker);
int CALLBACK_HANDLER_COUNT = 5;
SimpleCallbackHandler[] callbackHandlers = new SimpleCallbackHandler[CALLBACK_HANDLER_COUNT];
for (int i = 0; i < CALLBACK_HANDLER_COUNT; i++)
{
callbackHandlers[i] = new SimpleCallbackHandler();
client.addListener(callbackHandlers[i], new HashMap());
}
client.invoke(CALLBACK_TEST);
for (int i = 0; i < CALLBACK_HANDLER_COUNT; i++)
{
assertEquals(1, callbackHandlers[i].callbackCounter);
}
// Static fields.
Field field = BisocketServerInvoker.class.getDeclaredField("listenerIdToServerInvokerMap");
field.setAccessible(true);
Map listenerIdToServerInvokerMap = (Map) field.get(null);
field = BisocketClientInvoker.class.getDeclaredField("listenerIdToClientInvokerMap");
field.setAccessible(true);
Map listenerIdToClientInvokerMap = (Map) field.get(null);
field = BisocketClientInvoker.class.getDeclaredField("listenerIdToCallbackClientInvokerMap");
field.setAccessible(true);
Map listenerIdToCallbackClientInvokerMap = (Map) field.get(null);
field = BisocketClientInvoker.class.getDeclaredField("listenerIdToSocketsMap");
field.setAccessible(true);
Map listenerIdToSocketsMap = (Map) field.get(null);
// Non-static fields.
field = Client.class.getDeclaredField("callbackConnectors");
field.setAccessible(true);
Map callbackConnectors = (Map) field.get(client);
assertEquals(CALLBACK_HANDLER_COUNT, callbackConnectors.size());
ServerInvoker[] serverInvokers = new ServerInvoker[CALLBACK_HANDLER_COUNT];
Map[] listenerIdToInvokerLocatorMaps = new Map[CALLBACK_HANDLER_COUNT];
Map[] controlConnectionThreadMaps = new Map[CALLBACK_HANDLER_COUNT];
int i = 0;
Iterator it = callbackConnectors.values().iterator();
while (it.hasNext())
{
Set set = (Set) it.next();
assertEquals(1, set.size());
Connector c = (Connector) set.iterator().next();
serverInvokers[i] = c.getServerInvoker();
assertTrue(serverInvokers[i] instanceof BisocketServerInvoker);
field = BisocketServerInvoker.class.getDeclaredField("listenerIdToInvokerLocatorMap");
field.setAccessible(true);
listenerIdToInvokerLocatorMaps[i] = (Map) field.get(serverInvokers[i]);
field = BisocketServerInvoker.class.getDeclaredField("controlConnectionThreadMap");
field.setAccessible(true);
controlConnectionThreadMaps[i] = (Map) field.get(serverInvokers[i]);
i++;
}
assertEquals(CALLBACK_HANDLER_COUNT, listenerIdToServerInvokerMap.size());
assertEquals(CALLBACK_HANDLER_COUNT, listenerIdToClientInvokerMap.size());
assertEquals(CALLBACK_HANDLER_COUNT, listenerIdToCallbackClientInvokerMap.size());
assertEquals(CALLBACK_HANDLER_COUNT, listenerIdToSocketsMap.size());
for (int j = 0; j < CALLBACK_HANDLER_COUNT; j++)
{
assertEquals(1, listenerIdToInvokerLocatorMaps[j].size());
assertEquals(1, controlConnectionThreadMaps[j].size());
field = BisocketServerInvoker.class.getDeclaredField("controlMonitorTimerTask");
field.setAccessible(true);
assertNotNull(field.get(serverInvokers[j]));
}
for (int j = 0; j < CALLBACK_HANDLER_COUNT; j++)
{
client.removeListener(callbackHandlers[j]);
}
assertEquals(0, listenerIdToServerInvokerMap.size());
assertEquals(0, listenerIdToClientInvokerMap.size());
assertEquals(0, listenerIdToCallbackClientInvokerMap.size());
assertEquals(0, listenerIdToSocketsMap.size());
Class[] classes = BisocketServerInvoker.class.getDeclaredClasses();
Class controlMonitorTimerTaskClass = null;
for (int j = 0; j < classes.length; j++)
{
log.info(classes[j]);
String fqn = classes[j].getName();
String className = fqn.substring(fqn.lastIndexOf('.') + 1);
log.info(className);
if ("BisocketServerInvoker$ControlMonitorTimerTask".equals(className))
{
controlMonitorTimerTaskClass = classes[j];
break;
}
}
assertNotNull(controlMonitorTimerTaskClass);
for (int j = 0; j < CALLBACK_HANDLER_COUNT; j++)
{
assertEquals("server invoker: " + j, 0, listenerIdToInvokerLocatorMaps[j].size());
assertEquals("server invoker: " + j, 0, controlConnectionThreadMaps[j].size());
field = BisocketServerInvoker.class.getDeclaredField("controlMonitorTimerTask");
field.setAccessible(true);
Object controlMonitorTimerTask = field.get(serverInvokers[j]);
field = controlMonitorTimerTaskClass.getDeclaredField("listenerIdToInvokerLocatorMap");
field.setAccessible(true);
assertNull("server invoker: " + j, field.get(controlMonitorTimerTask));
field = controlMonitorTimerTaskClass.getDeclaredField("controlConnectionThreadMap");
field.setAccessible(true);
assertNull("server invoker: " + j, field.get(controlMonitorTimerTask));
}
client.disconnect();
}