/**
   * Validate streams generate the same output.
   *
   * @param expIn Expected input stream.
   * @param actIn Actual input stream.
   * @param expSize Expected size of the streams.
   * @param seek Seek to use async position-based reading or {@code null} to use simple continuous
   *     reading.
   * @throws IOException In case of any IO exception.
   */
  private void assertEqualStreams(
      InputStream expIn, GridGgfsInputStream actIn, @Nullable Long expSize, @Nullable Long seek)
      throws IOException {
    if (seek != null) expIn.skip(seek);

    int bufSize = 2345;
    byte buf1[] = new byte[bufSize];
    byte buf2[] = new byte[bufSize];
    long pos = 0;

    long start = System.currentTimeMillis();

    while (true) {
      int read = (int) Math.min(bufSize, expSize - pos);

      int i1;

      if (seek == null) i1 = actIn.read(buf1, 0, read);
      else if (seek % 2 == 0) i1 = actIn.read(pos + seek, buf1, 0, read);
      else {
        i1 = read;

        actIn.readFully(pos + seek, buf1, 0, read);
      }

      // Read at least 0 byte, but don't read more then 'i1' or 'read'.
      int i2 = expIn.read(buf2, 0, Math.max(0, Math.min(i1, read)));

      if (i1 != i2) {
        fail(
            "Expects the same data [read="
                + read
                + ", pos="
                + pos
                + ", seek="
                + seek
                + ", i1="
                + i1
                + ", i2="
                + i2
                + ']');
      }

      if (i1 == -1) break; // EOF

      // i1 == bufSize => compare buffers.
      // i1 <  bufSize => Compare part of buffers, rest of buffers are equal from previous
      // iteration.
      assertTrue(
          "Expects the same data [read="
              + read
              + ", pos="
              + pos
              + ", seek="
              + seek
              + ", i1="
              + i1
              + ", i2="
              + i2
              + ']',
          Arrays.equals(buf1, buf2));

      if (read == 0) break; // Nothing more to read.

      pos += i1;
    }

    if (expSize != null) assertEquals(expSize.longValue(), pos);

    long time = System.currentTimeMillis() - start;

    if (time != 0 && log.isInfoEnabled()) {
      log.info(
          String.format(
              "Streams were compared in continuous reading " + "[size=%7d, rate=%3.1f MB/sec]",
              expSize, expSize * 1000. / time / 1024 / 1024));
    }
  }
Exemplo n.º 2
0
  /** {@inheritDoc} */
  @Override
  public <T> T holdcc(long timeout) {
    if (ctx != null) {
      if (job == null) job = ctx.job().activeJob(jobId);

      // Completed?
      if (job != null) {
        if (timeout > 0 && !job.isDone()) {
          final long endTime = System.currentTimeMillis() + timeout;

          // Overflow.
          if (endTime > 0) {
            ctx.timeout()
                .addTimeoutObject(
                    new GridTimeoutObject() {
                      private final GridUuid id = GridUuid.randomUuid();

                      @Override
                      public GridUuid timeoutId() {
                        return id;
                      }

                      @Override
                      public long endTime() {
                        return endTime;
                      }

                      @Override
                      public void onTimeout() {
                        callcc();
                      }
                    });
          }
        }

        job.hold();
      }
    }

    return null;
  }