Beispiel #1
0
  /** This method returns once the "startedAfterString" has been seen. */
  protected Runner startWithArgs(String[] args, String startedAfterString, RunTypes type) {
    LOG.info("Running with args {}", Arrays.toString(args));

    outContent = new ByteArrayOutputStream();
    errContent = new ByteArrayOutputStream();
    System.setOut(new PrintStream(outContent));
    System.setErr(new PrintStream(errContent));

    final int START_TIMEOUT_SECONDS = 60;

    Runner runner = new Runner(args, type);
    runner.setName("Frontend (CLI/YARN Client) runner thread (startWithArgs()).");
    runner.start();

    for (int second = 0; second < START_TIMEOUT_SECONDS; second++) {
      sleep(1000);
      // check output for correct TaskManager startup.
      if (outContent.toString().contains(startedAfterString)
          || errContent.toString().contains(startedAfterString)) {
        LOG.info("Found expected output in redirected streams");
        return runner;
      }
      // check if thread died
      if (!runner.isAlive()) {
        sendOutput();
        Assert.fail(
            "Runner thread died before the test was finished. Return value = "
                + runner.getReturnValue());
      }
    }

    sendOutput();
    Assert.fail(
        "During the timeout period of "
            + START_TIMEOUT_SECONDS
            + " seconds the "
            + "expected string did not show up");
    return null;
  }
Beispiel #2
0
  /**
   * The test has been passed once the "terminateAfterString" has been seen.
   *
   * @param args Command line arguments for the runner
   * @param terminateAfterString the runner is searching the stdout and stderr for this string. as
   *     soon as it appears, the test has passed
   * @param failOnPatterns The runner is searching stdout and stderr for the pattern (regexp)
   *     specified here. If one appears, the test has failed
   * @param type Set the type of the runner
   * @param returnCode Expected return code from the runner.
   * @param checkLogForTerminateString If true, the runner checks also the log4j logger for the
   *     terminate string
   */
  protected void runWithArgs(
      String[] args,
      String terminateAfterString,
      String[] failOnPatterns,
      RunTypes type,
      int returnCode,
      boolean checkLogForTerminateString) {
    LOG.info("Running with args {}", Arrays.toString(args));

    outContent = new ByteArrayOutputStream();
    errContent = new ByteArrayOutputStream();
    System.setOut(new PrintStream(outContent));
    System.setErr(new PrintStream(errContent));

    // we wait for at most three minutes
    final int START_TIMEOUT_SECONDS = 180;
    final long deadline = System.currentTimeMillis() + (START_TIMEOUT_SECONDS * 1000);

    Runner runner = new Runner(args, type);
    runner.start();

    boolean expectedStringSeen = false;
    boolean testPassedFromLog4j = false;
    do {
      sleep(1000);
      String outContentString = outContent.toString();
      String errContentString = errContent.toString();
      if (failOnPatterns != null) {
        for (String failOnString : failOnPatterns) {
          Pattern pattern = Pattern.compile(failOnString);
          if (pattern.matcher(outContentString).find()
              || pattern.matcher(errContentString).find()) {
            LOG.warn("Failing test. Output contained illegal string '" + failOnString + "'");
            sendOutput();
            // stopping runner.
            runner.sendStop();
            Assert.fail("Output contained illegal string '" + failOnString + "'");
          }
        }
      }
      // check output for the expected terminateAfterString.
      if (checkLogForTerminateString) {
        LoggingEvent matchedEvent = UtilsTest.getEventContainingString(terminateAfterString);
        if (matchedEvent != null) {
          testPassedFromLog4j = true;
          LOG.info("Found expected output in logging event {}", matchedEvent);
        }
      }

      if (outContentString.contains(terminateAfterString)
          || errContentString.contains(terminateAfterString)
          || testPassedFromLog4j) {
        expectedStringSeen = true;
        LOG.info("Found expected output in redirected streams");
        // send "stop" command to command line interface
        LOG.info("RunWithArgs: request runner to stop");
        runner.sendStop();
        // wait for the thread to stop
        try {
          runner.join(30000);
        } catch (InterruptedException e) {
          LOG.warn("Interrupted while stopping runner", e);
        }
        LOG.warn("RunWithArgs runner stopped.");
      } else {
        // check if thread died
        if (!runner.isAlive()) {
          if (runner.getReturnValue() != 0) {
            Assert.fail(
                "Runner thread died before the test was finished. Return value = "
                    + runner.getReturnValue());
          } else {
            LOG.info("Runner stopped earlier than expected with return value = 0");
          }
          // leave loop: the runner died, so we can not expect new strings to show up.
          break;
        }
      }
    } while (!expectedStringSeen && System.currentTimeMillis() < deadline);

    sendOutput();
    Assert.assertTrue(
        "During the timeout period of "
            + START_TIMEOUT_SECONDS
            + " seconds the "
            + "expected string did not show up",
        expectedStringSeen);

    // check for 0 return code
    Assert.assertEquals("Expected return value", returnCode, runner.getReturnValue());
    LOG.info("Test was successful");
  }