protected ByteArrayOutputStream main(final String... args) throws Exception {
   final ByteArrayOutputStream out = new ByteArrayOutputStream();
   final ByteArrayOutputStream err = new ByteArrayOutputStream();
   final CliMain main = new CliMain(new PrintStream(out), new PrintStream(err), args);
   main.run();
   return out;
 }
Beispiel #2
0
  @Test
  public void test() throws Exception {
    startDefaultMaster();
    startDefaultAgent(testHost());
    awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);

    // Create job
    final JobId jobId =
        createJob(
            testJobName,
            testJobVersion,
            BUSYBOX,
            IDLE_COMMAND,
            ImmutableMap.of(
                "FOO", "4711",
                "BAR", "deadbeef"));

    // deploy
    deployJob(jobId, testHost());

    final String[] commands =
        new String[] {"watch", "-z", masterEndpoint(), "--no-log-setup", jobId.toString()};

    final AtomicBoolean success = new AtomicBoolean(false);
    final List<String> outputLines = Lists.newArrayList();

    final long deadline = System.currentTimeMillis() + SECONDS.toMillis(LONG_WAIT_SECONDS);

    final String testHost = testHost();
    final String abbreviatedTestHost;
    if (testHost.length() > 10) {
      abbreviatedTestHost = testHost.substring(0, 10);
    } else {
      abbreviatedTestHost = testHost;
    }

    final OutputStream out =
        new OutputStream() {
          int counter = 0;
          final byte[] lineBuffer = new byte[8192];

          @Override
          public void write(int b) throws IOException {
            if (System.currentTimeMillis() > deadline) {
              throw new IOException("timed out trying to succeed");
            }
            lineBuffer[counter] = (byte) b;
            counter++;

            if (b != 10) {
              return;
            }

            final String line =
                Charsets.UTF_8.decode(ByteBuffer.wrap(lineBuffer, 0, counter)).toString();
            outputLines.add(line);
            counter = 0;

            if (line.contains(abbreviatedTestHost) && !line.contains("UNKNOWN")) {
              success.set(true);
              throw new IOException("output closed");
            }
          }
        };
    final CliMain main =
        new CliMain(new PrintStream(out), new PrintStream(new ByteArrayOutputStream()), commands);
    main.run();
    assertTrue(
        "Should have stopped the stream due to success: got\n" + Joiner.on("").join(outputLines),
        success.get());
  }