Search in sources :

Example 16 with Pipe

use of java.nio.channels.Pipe 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 17 with Pipe

use of java.nio.channels.Pipe in project robovm by robovm.

the class PipeTest method test_sink.

/**
	 * @tests java.nio.channels.Pipe#sink()
	 */
public void test_sink() throws IOException {
    Pipe pipe = Pipe.open();
    SinkChannel sink = pipe.sink();
    assertTrue(sink.isBlocking());
}
Also used : Pipe(java.nio.channels.Pipe) SinkChannel(java.nio.channels.Pipe.SinkChannel)

Example 18 with Pipe

use of java.nio.channels.Pipe in project robovm by robovm.

the class PipeTest method test_source.

/**
	 * @tests java.nio.channels.Pipe#source()
	 */
public void test_source() throws IOException {
    Pipe pipe = Pipe.open();
    SourceChannel source = pipe.source();
    assertTrue(source.isBlocking());
}
Also used : SourceChannel(java.nio.channels.Pipe.SourceChannel) Pipe(java.nio.channels.Pipe)

Example 19 with Pipe

use of java.nio.channels.Pipe in project robovm by robovm.

the class SelectorTest method test_cancelledKeys.

/**
     * This test cancels a key while selecting to verify that the cancelled
     * key set is processed both before and after the call to the underlying
     * operating system.
     */
public void test_cancelledKeys() throws Exception {
    final AtomicReference<Throwable> failure = new AtomicReference<Throwable>();
    final AtomicBoolean complete = new AtomicBoolean();
    final Pipe pipe = Pipe.open();
    pipe.source().configureBlocking(false);
    final SelectionKey key = pipe.source().register(selector, SelectionKey.OP_READ);
    Thread thread = new Thread() {

        public void run() {
            try {
                // make sure to call key.cancel() while the main thread is selecting
                Thread.sleep(500);
                key.cancel();
                assertFalse(key.isValid());
                // unblock select()
                pipe.sink().write(ByteBuffer.allocate(4));
            } catch (Throwable e) {
                failure.set(e);
            } finally {
                complete.set(true);
            }
        }
    };
    assertTrue(key.isValid());
    thread.start();
    do {
        // blocks
        assertEquals(0, selector.select(5000));
        assertEquals(0, selector.selectedKeys().size());
    } while (// avoid spurious interrupts
    !complete.get());
    assertFalse(key.isValid());
    thread.join();
    assertNull(failure.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SelectionKey(java.nio.channels.SelectionKey) AtomicReference(java.util.concurrent.atomic.AtomicReference) Pipe(java.nio.channels.Pipe)

Example 20 with Pipe

use of java.nio.channels.Pipe in project robovm by robovm.

the class SourceChannelTest method test_read_$LByteBufferII.

/**
     * @tests java.nio.channels.Pipe.SourceChannel#read(ByteBuffer[], int, int)
     */
public void test_read_$LByteBufferII() throws IOException {
    ByteBuffer[] bufArray = { buffer, positionedBuffer };
    boolean[] sinkBlockingMode = { true, true, false, false };
    boolean[] sourceBlockingMode = { true, false, true, false };
    for (int i = 0; i < sinkBlockingMode.length; ++i) {
        Pipe pipe = Pipe.open();
        sink = pipe.sink();
        source = pipe.source();
        sink.configureBlocking(sinkBlockingMode[i]);
        source.configureBlocking(sourceBlockingMode[i]);
        buffer.position(0);
        positionedBuffer.position(BUFFER_SIZE);
        try {
            sink.write(bufArray);
            // invoke close to ensure all data will be sent out
            sink.close();
            // read until EOF is meet or readBufArray is full.
            ByteBuffer[] readBufArray = { ByteBuffer.allocate(BUFFER_SIZE), ByteBuffer.allocate(BUFFER_SIZE) };
            long totalCount = 0;
            do {
                long count = source.read(readBufArray, 0, 2);
                if (count < 0) {
                    break;
                }
                if (0 == count && BUFFER_SIZE == readBufArray[1].position()) {
                    // source.read returns 0 because readBufArray is full
                    break;
                }
                totalCount += count;
            } while (totalCount != 10);
            // assert read result
            for (ByteBuffer readBuf : readBufArray) {
                // RI may fail because of its bug implementation
                assertEquals(BUFFER_SIZE, readBuf.position());
                assertEquals("bytes", new String(readBuf.array(), ISO8859_1));
            }
        } finally {
            sink.close();
            source.close();
        }
    }
}
Also used : Pipe(java.nio.channels.Pipe) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Pipe (java.nio.channels.Pipe)21 Test (org.junit.Test)7 File (java.io.File)6 GcsPath (org.apache.beam.sdk.util.gcsfs.GcsPath)6 FileNotFoundException (java.io.FileNotFoundException)5 DataflowPackage (com.google.api.services.dataflow.model.DataflowPackage)3 DbusEventAppender (com.linkedin.databus.core.test.DbusEventAppender)3 DbusEventBufferReader (com.linkedin.databus.core.test.DbusEventBufferReader)3 DbusEventGenerator (com.linkedin.databus.core.test.DbusEventGenerator)3 UncaughtExceptionTrackingThread (com.linkedin.databus.core.util.UncaughtExceptionTrackingThread)3 ArrayList (java.util.ArrayList)3 Vector (java.util.Vector)3 Matchers.anyString (org.mockito.Matchers.anyString)3 DatabusV2ConsumerRegistration (com.linkedin.databus.client.consumer.DatabusV2ConsumerRegistration)2 MultiConsumerCallback (com.linkedin.databus.client.consumer.MultiConsumerCallback)2 SelectingDatabusCombinedConsumer (com.linkedin.databus.client.consumer.SelectingDatabusCombinedConsumer)2 StreamConsumerCallbackFactory (com.linkedin.databus.client.consumer.StreamConsumerCallbackFactory)2 ConsumerCallbackStats (com.linkedin.databus.client.pub.mbean.ConsumerCallbackStats)2 UnifiedClientStats (com.linkedin.databus.client.pub.mbean.UnifiedClientStats)2 Checkpoint (com.linkedin.databus.core.Checkpoint)2