assert !agent1.getClientCommandSender().isSending() : "we should have been able to turn off the InitializeCallback/connectAgent should therefore not have triggered our listener and started the sender";
assert agent2.getClientCommandSender().isSending() : "Should have already started the sender";
// our canary-in-the-mine is the sending mode, the sender goes into sending mode when it detects the server
IdentifyCommand command = new IdentifyCommand();
CommandResponse response;
response = agent2.getClientCommandSender().sendSynch(command);
assert response.isSuccessful() : "agent2 should have been able to send to agent1: " + response;
assert agent1.getClientCommandSender().isSending() : "agent2 message should have started agent1 sender";
// now let's stop the agent1 sender and try again, just to make sure the command listeners work repeatedly
agent1.getClientCommandSender().stopSending(false);
assert !agent1.getClientCommandSender().isSending() : "Should not be in sending mode again";
assert agent2.getClientCommandSender().isSending() : "Should still be started";
response = agent2.getClientCommandSender().sendSynch(command);
assert response.isSuccessful() : "agent2 should have been able to send to agent1: " + response;
assert agent1.getClientCommandSender().isSending() : "agent2 message should have re-started agent1 sender again";
// now let's entirely shutdown agent1 and try again, just to make sure the command listeners can be rebuilt
agent1.shutdown();
assert agent1.getClientCommandSender() == null : "agent1 should be completely shutdown";
assert agent2.getClientCommandSender().isSending() : "Should still be started";
agent1.start();
agent1.getClientCommandSender().getRemoteCommunicator().setInitializeCallback(null);
assert agent1.getClientCommandSender() != null : "agent1 should be started again";
assert !agent1.getClientCommandSender().isSending() : "Should still not yet be in sending mode again";
assert agent2.getClientCommandSender().isSending() : "Should still be started";
response = agent2.getClientCommandSender().sendSynch(command);
assert response.isSuccessful() : "agent2 should have been able to send to agent1: " + response;
assert agent1.getClientCommandSender().isSending() : "agent2 message should have re-started agent1 sender again";
} finally {
if (agent1 != null) {
agent1.shutdown();
}