Search in sources :

Example 21 with Random

use of java.util.Random in project flink by apache.

the class PagedViewsTest method testReadFully.

@Test
public void testReadFully() {
    int bufferSize = 100;
    byte[] expected = new byte[bufferSize];
    new Random().nextBytes(expected);
    TestOutputView outputView = new TestOutputView(bufferSize);
    try {
        outputView.write(expected);
    } catch (Exception e) {
        e.printStackTrace();
        fail("Unexpected exception: Could not write to TestOutputView.");
    }
    outputView.close();
    TestInputView inputView = new TestInputView(outputView.segments);
    byte[] buffer = new byte[bufferSize];
    try {
        inputView.readFully(buffer);
    } catch (IOException e) {
        e.printStackTrace();
        fail("Unexpected exception: Could not read TestInputView.");
    }
    assertEquals(inputView.getCurrentPositionInSegment(), bufferSize);
    assertArrayEquals(expected, buffer);
}
Also used : Random(java.util.Random) IOException(java.io.IOException) IOException(java.io.IOException) EOFException(java.io.EOFException) Test(org.junit.Test)

Example 22 with Random

use of java.util.Random in project flink by apache.

the class PagedViewsTest method testReadFullyWithNotEnoughData.

@Test
public void testReadFullyWithNotEnoughData() {
    int bufferSize = 100;
    int bytes2Write = 99;
    int segmentSize = 30;
    byte[] expected = new byte[bytes2Write];
    new Random().nextBytes(expected);
    TestOutputView outputView = new TestOutputView(segmentSize);
    try {
        outputView.write(expected);
    } catch (Exception e) {
        e.printStackTrace();
        fail("Unexpected exception: Could not write to TestOutputView.");
    }
    outputView.close();
    TestInputView inputView = new TestInputView(outputView.segments);
    byte[] buffer = new byte[bufferSize];
    boolean eofException = false;
    try {
        inputView.readFully(buffer);
    } catch (EOFException e) {
        //Expected exception
        eofException = true;
    } catch (IOException e) {
        e.printStackTrace();
        fail("Unexpected exception: Could not read TestInputView.");
    }
    assertTrue("EOFException should have occurred.", eofException);
    int bytesRead = 0;
    try {
        bytesRead = inputView.read(buffer);
    } catch (Exception e) {
        e.printStackTrace();
        fail("Unexpected exception: Could not read TestInputView.");
    }
    assertEquals(-1, bytesRead);
}
Also used : Random(java.util.Random) EOFException(java.io.EOFException) IOException(java.io.IOException) IOException(java.io.IOException) EOFException(java.io.EOFException) Test(org.junit.Test)

Example 23 with Random

use of java.util.Random in project flink by apache.

the class PagedViewsTest method testReadFullyWithOffset.

@Test
public void testReadFullyWithOffset() {
    int bufferSize = 100;
    int segmentSize = 30;
    byte[] expected = new byte[bufferSize];
    new Random().nextBytes(expected);
    TestOutputView outputView = new TestOutputView(segmentSize);
    try {
        outputView.write(expected);
    } catch (Exception e) {
        e.printStackTrace();
        fail("Unexpected exception: Could not write to TestOutputView.");
    }
    outputView.close();
    TestInputView inputView = new TestInputView(outputView.segments);
    byte[] buffer = new byte[2 * bufferSize];
    try {
        inputView.readFully(buffer, bufferSize, bufferSize);
    } catch (IOException e) {
        e.printStackTrace();
        fail("Unexpected exception: Could not read TestInputView.");
    }
    assertEquals(inputView.getCurrentPositionInSegment(), bufferSize % segmentSize);
    byte[] tempBuffer = new byte[bufferSize];
    System.arraycopy(buffer, bufferSize, tempBuffer, 0, bufferSize);
    assertArrayEquals(expected, tempBuffer);
}
Also used : Random(java.util.Random) IOException(java.io.IOException) IOException(java.io.IOException) EOFException(java.io.EOFException) Test(org.junit.Test)

Example 24 with Random

use of java.util.Random in project flink by apache.

the class PagedViewsTest method testReadFullyAcrossSegments.

@Test
public void testReadFullyAcrossSegments() {
    int bufferSize = 100;
    int segmentSize = 30;
    byte[] expected = new byte[bufferSize];
    new Random().nextBytes(expected);
    TestOutputView outputView = new TestOutputView(segmentSize);
    try {
        outputView.write(expected);
    } catch (Exception e) {
        e.printStackTrace();
        fail("Unexpected exception: Could not write to TestOutputView.");
    }
    outputView.close();
    TestInputView inputView = new TestInputView(outputView.segments);
    byte[] buffer = new byte[bufferSize];
    try {
        inputView.readFully(buffer);
    } catch (IOException e) {
        e.printStackTrace();
        fail("Unexpected exception: Could not read TestInputView.");
    }
    assertEquals(inputView.getCurrentPositionInSegment(), bufferSize % segmentSize);
    assertArrayEquals(expected, buffer);
}
Also used : Random(java.util.Random) IOException(java.io.IOException) IOException(java.io.IOException) EOFException(java.io.EOFException) Test(org.junit.Test)

Example 25 with Random

use of java.util.Random in project flink by apache.

the class PagedViewsTest method testEmptyingInputView.

@Test
public void testEmptyingInputView() {
    int bufferSize = 100;
    int bytes2Write = 75;
    int segmentSize = 30;
    byte[] expected = new byte[bytes2Write];
    new Random().nextBytes(expected);
    TestOutputView outputView = new TestOutputView(segmentSize);
    try {
        outputView.write(expected);
    } catch (Exception e) {
        e.printStackTrace();
        fail("Unexpected exception: Could not write to TestOutputView.");
    }
    outputView.close();
    TestInputView inputView = new TestInputView(outputView.segments);
    byte[] buffer = new byte[bufferSize];
    int bytesRead = 0;
    try {
        bytesRead = inputView.read(buffer);
    } catch (IOException e) {
        e.printStackTrace();
        fail("Unexpected exception: Could not read TestInputView.");
    }
    assertEquals(bytes2Write, bytesRead);
    byte[] tempBuffer = new byte[bytesRead];
    System.arraycopy(buffer, 0, tempBuffer, 0, bytesRead);
    assertArrayEquals(expected, tempBuffer);
    try {
        bytesRead = inputView.read(buffer);
    } catch (IOException e) {
        e.printStackTrace();
        fail("Unexpected exception: Input view should be empty and thus return -1.");
    }
    assertEquals(-1, bytesRead);
    assertEquals(inputView.getCurrentPositionInSegment(), bytes2Write % segmentSize);
}
Also used : Random(java.util.Random) IOException(java.io.IOException) IOException(java.io.IOException) EOFException(java.io.EOFException) Test(org.junit.Test)

Aggregations

Random (java.util.Random)4728 Test (org.junit.Test)1273 ArrayList (java.util.ArrayList)602 IOException (java.io.IOException)313 HashMap (java.util.HashMap)242 File (java.io.File)209 List (java.util.List)154 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)151 ByteArrayInputStream (java.io.ByteArrayInputStream)134 HashSet (java.util.HashSet)129 ByteBuffer (java.nio.ByteBuffer)123 Test (org.testng.annotations.Test)121 Path (org.apache.hadoop.fs.Path)116 Map (java.util.Map)106 QuickTest (com.hazelcast.test.annotation.QuickTest)99 ParallelTest (com.hazelcast.test.annotation.ParallelTest)94 CountDownLatch (java.util.concurrent.CountDownLatch)93 Configuration (org.apache.hadoop.conf.Configuration)88 ByteArrayOutputStream (java.io.ByteArrayOutputStream)79 Before (org.junit.Before)78