/**
  * Try to connect to the host controller.
  *
  * @throws IOException
  */
 public void connect() throws IOException {
   connect(30, TimeUnit.SECONDS);
 }
  private void testReconnect(ReconnectTestScenario[] scenarios) throws Exception {
    int initialisedScenarios = -1;
    try {
      DomainClient masterClient = domainMasterLifecycleUtil.getDomainClient();
      DomainClient slaveClient = domainSlaveLifecycleUtil.getDomainClient();
      for (int i = 0; i < scenarios.length; i++) {
        initialisedScenarios = i;
        scenarios[i].setUpDomain(testSupport, masterClient, slaveClient);
      }

      for (ReconnectTestScenario scenario : scenarios) {
        scenario.testOnInitialStartup(masterClient, slaveClient);
      }

      // Restart the DC as admin-only
      ModelNode restartAdminOnly =
          Util.createEmptyOperation("reload", PathAddress.pathAddress(HOST, "master"));
      restartAdminOnly.get("admin-only").set(true);
      domainMasterLifecycleUtil.executeAwaitConnectionClosed(restartAdminOnly);
      domainMasterLifecycleUtil.connect();
      domainMasterLifecycleUtil.awaitHostController(System.currentTimeMillis());
      masterClient = domainMasterLifecycleUtil.createDomainClient();

      for (ReconnectTestScenario scenario : scenarios) {
        scenario.testWhileMasterInAdminOnly(masterClient, slaveClient);
      }

      // Restart the DC as normal
      restartAdminOnly.get("admin-only").set(false);
      domainMasterLifecycleUtil.executeAwaitConnectionClosed(restartAdminOnly);
      domainMasterLifecycleUtil.connect();
      domainMasterLifecycleUtil.awaitHostController(System.currentTimeMillis());
      masterClient = domainMasterLifecycleUtil.createDomainClient();

      // Wait for the slave to reconnect, look for the slave in the list of hosts
      long end = System.currentTimeMillis() + 20 * ADJUSTED_SECOND;
      boolean slaveReconnected = false;
      do {
        Thread.sleep(1 * ADJUSTED_SECOND);
        slaveReconnected = checkSlaveReconnected(masterClient);
      } while (!slaveReconnected && System.currentTimeMillis() < end);

      // Wait for master servers to come up
      end = System.currentTimeMillis() + 60 * ADJUSTED_SECOND;
      boolean serversUp = false;
      do {
        Thread.sleep(1 * ADJUSTED_SECOND);
        serversUp = checkHostServersStarted(masterClient, "master");
      } while (!serversUp && System.currentTimeMillis() < end);

      for (ReconnectTestScenario scenario : scenarios) {
        scenario.testAfterReconnect(masterClient, slaveClient);
      }
    } finally {
      for (int i = initialisedScenarios; i >= 0; i--) {
        scenarios[i].tearDownDomain(
            domainMasterLifecycleUtil.getDomainClient(),
            domainSlaveLifecycleUtil.getDomainClient());
      }
    }
  }