Search in sources :

Example 1 with TestingThread

use of org.apache.hadoop.test.MultithreadedTestUtil.TestingThread in project hadoop by apache.

the class TestMultithreadedTestUtil method testThreadThrowsCheckedException.

@Test
public void testThreadThrowsCheckedException() throws Exception {
    TestContext ctx = new TestContext();
    ctx.addThread(new TestingThread(ctx) {

        @Override
        public void doWork() throws Exception {
            throw new IOException("my ioe");
        }
    });
    ctx.startThreads();
    long st = Time.now();
    try {
        ctx.waitFor(30000);
        fail("waitFor did not throw");
    } catch (RuntimeException rte) {
        // expected
        assertEquals("my ioe", rte.getCause().getMessage());
    }
    long et = Time.now();
    // Test shouldn't have waited the full 30 seconds, since
    // the thread throws faster than that
    assertTrue("Test took " + (et - st) + "ms", et - st < 5000);
}
Also used : TestContext(org.apache.hadoop.test.MultithreadedTestUtil.TestContext) TestingThread(org.apache.hadoop.test.MultithreadedTestUtil.TestingThread) IOException(java.io.IOException) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with TestingThread

use of org.apache.hadoop.test.MultithreadedTestUtil.TestingThread in project hadoop by apache.

the class TestSocketIOWithTimeout method testSocketIOWithTimeout.

@Test
public void testSocketIOWithTimeout() throws Exception {
    // first open pipe:
    Pipe pipe = Pipe.open();
    Pipe.SourceChannel source = pipe.source();
    Pipe.SinkChannel sink = pipe.sink();
    try {
        final InputStream in = new SocketInputStream(source, TIMEOUT);
        OutputStream out = new SocketOutputStream(sink, TIMEOUT);
        byte[] writeBytes = TEST_STRING.getBytes();
        byte[] readBytes = new byte[writeBytes.length];
        byte byteWithHighBit = (byte) 0x80;
        out.write(writeBytes);
        out.write(byteWithHighBit);
        doIO(null, out, TIMEOUT);
        in.read(readBytes);
        assertTrue(Arrays.equals(writeBytes, readBytes));
        assertEquals(byteWithHighBit & 0xff, in.read());
        doIO(in, null, TIMEOUT);
        // Change timeout on the read side.
        ((SocketInputStream) in).setTimeout(TIMEOUT * 2);
        doIO(in, null, TIMEOUT * 2);
        /*
       * Verify that it handles interrupted threads properly.
       * Use a large timeout and expect the thread to return quickly
       * upon interruption.
       */
        ((SocketInputStream) in).setTimeout(0);
        TestingThread thread = new TestingThread(ctx) {

            @Override
            public void doWork() throws Exception {
                try {
                    in.read();
                    fail("Did not fail with interrupt");
                } catch (InterruptedIOException ste) {
                    LOG.info("Got expection while reading as expected : " + ste.getMessage());
                }
            }
        };
        ctx.addThread(thread);
        ctx.startThreads();
        // If the thread is interrupted before it calls read()
        // then it throws ClosedByInterruptException due to
        // some Java quirk. Waiting for it to call read()
        // gets it into select(), so we get the expected
        // InterruptedIOException.
        Thread.sleep(1000);
        thread.interrupt();
        ctx.stop();
        //make sure the channels are still open
        assertTrue(source.isOpen());
        assertTrue(sink.isOpen());
        // larger buffers in doIO.  Nothing helped the situation though.
        if (!Shell.WINDOWS) {
            try {
                out.write(1);
                fail("Did not throw");
            } catch (IOException ioe) {
                GenericTestUtils.assertExceptionContains("stream is closed", ioe);
            }
        }
        out.close();
        assertFalse(sink.isOpen());
        // close sink and expect -1 from source.read()
        assertEquals(-1, in.read());
        // make sure close() closes the underlying channel.
        in.close();
        assertFalse(source.isOpen());
    } finally {
        if (source != null) {
            source.close();
        }
        if (sink != null) {
            sink.close();
        }
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) TestingThread(org.apache.hadoop.test.MultithreadedTestUtil.TestingThread) Pipe(java.nio.channels.Pipe) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) Test(org.junit.Test)

Example 3 with TestingThread

use of org.apache.hadoop.test.MultithreadedTestUtil.TestingThread in project hadoop by apache.

the class TestMultithreadedTestUtil method testThreadFails.

@Test
public void testThreadFails() throws Exception {
    TestContext ctx = new TestContext();
    ctx.addThread(new TestingThread(ctx) {

        @Override
        public void doWork() throws Exception {
            fail(FAIL_MSG);
        }
    });
    ctx.startThreads();
    long st = Time.now();
    try {
        ctx.waitFor(30000);
        fail("waitFor did not throw");
    } catch (RuntimeException rte) {
        // expected
        assertEquals(FAIL_MSG, rte.getCause().getMessage());
    }
    long et = Time.now();
    // Test shouldn't have waited the full 30 seconds, since
    // the thread throws faster than that
    assertTrue("Test took " + (et - st) + "ms", et - st < 5000);
}
Also used : TestContext(org.apache.hadoop.test.MultithreadedTestUtil.TestContext) TestingThread(org.apache.hadoop.test.MultithreadedTestUtil.TestingThread) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with TestingThread

use of org.apache.hadoop.test.MultithreadedTestUtil.TestingThread in project hadoop by apache.

the class TestMultithreadedTestUtil method testNoErrors.

@Test
public void testNoErrors() throws Exception {
    final AtomicInteger threadsRun = new AtomicInteger();
    TestContext ctx = new TestContext();
    for (int i = 0; i < 3; i++) {
        ctx.addThread(new TestingThread(ctx) {

            @Override
            public void doWork() throws Exception {
                threadsRun.incrementAndGet();
            }
        });
    }
    assertEquals(0, threadsRun.get());
    ctx.startThreads();
    long st = Time.now();
    ctx.waitFor(30000);
    long et = Time.now();
    // All threads should have run
    assertEquals(3, threadsRun.get());
    // Test shouldn't have waited the full 30 seconds, since
    // the threads exited faster than that.
    assertTrue("Test took " + (et - st) + "ms", et - st < 5000);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestContext(org.apache.hadoop.test.MultithreadedTestUtil.TestContext) TestingThread(org.apache.hadoop.test.MultithreadedTestUtil.TestingThread) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)4 TestingThread (org.apache.hadoop.test.MultithreadedTestUtil.TestingThread)4 Test (org.junit.Test)4 TestContext (org.apache.hadoop.test.MultithreadedTestUtil.TestContext)3 InputStream (java.io.InputStream)1 InterruptedIOException (java.io.InterruptedIOException)1 OutputStream (java.io.OutputStream)1 Pipe (java.nio.channels.Pipe)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1