/** Basic load balance testing. A random distribution is used amongst nodes for client now. */
  private void loadbalance(EJBDirectory directory, String deployment1, String deployment2)
      throws Exception {
    this.start(CONTAINER_1);
    this.deploy(CONTAINER_1, deployment1);
    this.start(CONTAINER_2);
    this.deploy(CONTAINER_2, deployment2);

    final ContextSelector<EJBClientContext> previousSelector =
        EJBClientContextSelector.setup(CLIENT_PROPERTIES);

    int numberOfServers = 2;
    int numberOfCalls = 40;
    // there will be at least 20% of calls processed by all servers
    double serversProccessedAtLeast = 0.2;

    try {
      StatelessRemoteHome home =
          directory.lookupHome(StatelessBean.class, StatelessRemoteHome.class);
      StatelessRemote bean = home.create();

      String node = bean.getNodeName();
      log.info("Node called : " + node);

      validateBalancing(bean, numberOfCalls, numberOfServers, serversProccessedAtLeast);

      Properties contextChangeProperties = new Properties();
      contextChangeProperties.put(REMOTE_PORT_PROPERTY_NAME, PORT_2.toString());
      contextChangeProperties.put(REMOTE_HOST_PROPERTY_NAME, HOST_2.toString());
      EJBClientContextSelector.setup(CLIENT_PROPERTIES, contextChangeProperties);

      bean = home.create();
      node = bean.getNodeName();
      log.info("Node called : " + node);

      validateBalancing(bean, numberOfCalls, numberOfServers, serversProccessedAtLeast);
    } finally {
      // reset the selector
      if (previousSelector != null) {
        EJBClientContext.setSelector(previousSelector);
      }
      // undeploy&shutdown the containers
      undeployAll();
      shutdownAll();
    }
  }
  private void doFailover(
      boolean isStop, EJBDirectory directory, String deployment1, String deployment2)
      throws Exception {
    this.start(CONTAINER_1);
    this.deploy(CONTAINER_1, deployment1);

    final ContextSelector<EJBClientContext> selector =
        EJBClientContextSelector.setup(CLIENT_PROPERTIES);

    try {
      StatelessRemoteHome home =
          directory.lookupHome(StatelessBean.class, StatelessRemoteHome.class);
      StatelessRemote bean = home.create();

      assertEquals(
          "The only " + NODES[0] + " is active. Bean had to be invoked on it but it wasn't.",
          NODES[0],
          bean.getNodeName());

      this.start(CONTAINER_2);
      this.deploy(CONTAINER_2, deployment2);

      if (isStop) {
        this.stop(CONTAINER_1);
      } else {
        this.undeploy(CONTAINER_1, deployment1);
      }

      assertEquals(
          "Only " + NODES[1] + " is active. The bean had to be invoked on it but it wasn't.",
          NODES[1],
          bean.getNodeName());
    } finally {
      // reset the selector
      if (selector != null) {
        EJBClientContext.setSelector(selector);
      }
      // need to have the container started to undeploy deployment afterwards
      this.start(CONTAINER_1);
      // shutdown the containers
      undeployAll();
      shutdownAll();
    }
  }