log.info("client is connected");
assertTrue(client.getInvoker() instanceof BisocketClientInvoker);
String callbackLocatorURI1 = getTransport() + "://" + host + ":1";
config.put(Bisocket.IS_CALLBACK_SERVER, "true");
Connector callbackConnector1 = new Connector(callbackLocatorURI1, config);
callbackConnector1.start();
String callbackLocatorURI2 = getTransport() + "://" + host + ":2";
Connector callbackConnector2 = new Connector(callbackLocatorURI2, config);
callbackConnector2.start();
DelayedCallbackHandler callbackHandler1 = new DelayedCallbackHandler();
client.addListener(callbackHandler1, new InvokerLocator(callbackLocatorURI1));
DelayedCallbackHandler callbackHandler2 = new DelayedCallbackHandler();
client.addListener(callbackHandler2, new InvokerLocator(callbackLocatorURI2));
assertEquals(2, invocationHandler.callbackHandlers.size());
log.info("clients added callback handler");
Iterator it = invocationHandler.callbackHandlers.iterator();
ServerInvokerCallbackHandler serverInvokerCallbackHandler1 = (ServerInvokerCallbackHandler) it.next();
ServerInvokerCallbackHandler serverInvokerCallbackHandler2 = (ServerInvokerCallbackHandler) it.next();
Client callbackClient1 = serverInvokerCallbackHandler1.getCallbackClient();
Client callbackClient2 = serverInvokerCallbackHandler2.getCallbackClient();
assertNotSame(callbackClient1, callbackClient2);
ClientInvoker clientInvoker1 = callbackClient1.getInvoker();
assertTrue(clientInvoker1 instanceof BisocketClientInvoker);
ClientInvoker clientInvoker2 = callbackClient2.getInvoker();
assertTrue(clientInvoker2 instanceof BisocketClientInvoker);
assertNotSame(clientInvoker1, clientInvoker2);
Field field = MicroSocketClientInvoker.class.getDeclaredField("pool");
field.setAccessible(true);
List pool1 = (List) field.get(clientInvoker1);
List pool2 = (List) field.get(clientInvoker2);
// field = MicroSocketClientInvoker.class.getDeclaredField("usedPooled");
// field.setAccessible(true);
assertEquals(0, callbackHandler1.callbackCounter);
assertEquals(0, callbackHandler2.callbackCounter);
assertEquals(0, pool1.size());
assertEquals(0, pool2.size());
// Long usedPooled1 = (Long) field.get(clientInvoker1);
// Long usedPooled2 = (Long) field.get(clientInvoker2);
// assertEquals(0, usedPooled1.longValue());
// assertEquals(0, usedPooled2.longValue());
assertEquals(0, ((MicroSocketClientInvoker) clientInvoker1).getNumberOfUsedConnections());
assertEquals(0, ((MicroSocketClientInvoker) clientInvoker2).getNumberOfUsedConnections());
client.invokeOneway(CALLBACK_TEST);
client.invokeOneway(CALLBACK_TEST);
Thread.sleep(500);
int count1 = callbackHandler1.callbackCounter;
int count2 = callbackHandler2.callbackCounter;
assertTrue(count1 == 2 && count2 == 0 || count1 == 0 && count2 == 2);
if (count1 == 0)
{
Object temp = callbackHandler1;
callbackHandler1 = callbackHandler2;
callbackHandler2 = (DelayedCallbackHandler) temp;;
}
assertEquals(2, callbackHandler1.callbackCounter);
assertEquals(0, callbackHandler2.callbackCounter);
assertEquals(0, pool1.size());
assertEquals(0, pool2.size());
// usedPooled1 = (Long) field.get(clientInvoker1);
// usedPooled2 = (Long) field.get(clientInvoker2);
// assertEquals(2, usedPooled1.longValue());
// assertEquals(0, usedPooled2.longValue());
assertEquals(2, ((MicroSocketClientInvoker) clientInvoker1).getNumberOfUsedConnections());
assertEquals(0, ((MicroSocketClientInvoker) clientInvoker2).getNumberOfUsedConnections());
Thread.sleep(2000);
assertEquals(2, callbackHandler1.callbackCounter);
assertEquals(2, callbackHandler2.callbackCounter);
assertEquals(2, pool1.size());
assertEquals(0, pool2.size());
// usedPooled1 = (Long) field.get(clientInvoker1);
// usedPooled2 = (Long) field.get(clientInvoker2);
// assertEquals(0, usedPooled1.intValue());
// assertEquals(2, usedPooled2.intValue());
assertEquals(0, ((MicroSocketClientInvoker) clientInvoker1).getNumberOfUsedConnections());
assertEquals(2, ((MicroSocketClientInvoker) clientInvoker2).getNumberOfUsedConnections());
Thread.sleep(2000);
assertEquals(2, callbackHandler1.callbackCounter);
assertEquals(2, callbackHandler2.callbackCounter);
assertEquals(2, pool1.size());
assertEquals(2, pool2.size());
// usedPooled1 = (Long) field.get(clientInvoker1);
// usedPooled2 = (Long) field.get(clientInvoker2);
// assertEquals(0, usedPooled1.intValue());
// assertEquals(0, usedPooled2.intValue());
assertEquals(0, ((MicroSocketClientInvoker) clientInvoker1).getNumberOfUsedConnections());
assertEquals(0, ((MicroSocketClientInvoker) clientInvoker2).getNumberOfUsedConnections());
client.removeListener(callbackHandler1);
client.removeListener(callbackHandler2);
client.disconnect();
callbackConnector1.stop();
callbackConnector2.stop();
}