Search in sources :

Example 16 with PipeConfig

use of com.ociweb.pronghorn.pipe.PipeConfig in project PronghornPipes by oci-pronghorn.

the class PipeSingleTemplateFloatTest method simpleWriteReadThreaded.

@Test
public void simpleWriteReadThreaded() {
    // this ring is 2^7 eg 128
    final byte primaryRingSizeInBits = 7;
    final byte byteRingSizeInBits = 16;
    final Pipe ring = new Pipe(new PipeConfig(new MessageSchemaDynamic(FROM), 1 << primaryRingSizeInBits, 1 << byteRingSizeInBits));
    ring.initBuffers();
    final int messageSize = FROM.fragDataSize[FRAG_LOC];
    final int varDataMax = (ring.blobMask / (ring.slabMask >> 1)) / messageSize;
    final int testSize = (1 << primaryRingSizeInBits) / messageSize;
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            writeTestValue(ring, varDataMax, testSize);
        }
    });
    t.start();
    // now read the data back
    int FIELD_LOC = FieldReferenceOffsetManager.lookupFieldLocator(SINGLE_MESSAGE_NAMES[0], FRAG_LOC, FROM);
    int k = testSize;
    while (k > 0) {
        // System.err.println("content "+ring.contentRemaining(ring));
        if (PipeReader.tryReadFragment(ring)) {
            // this method releases old messages as needed and moves pointer up to the next fragment
            // count down all the expected messages so we stop this test at the right time
            k--;
            assertTrue(PipeReader.isNewMessage(ring));
            int messageIdx = PipeReader.getMsgIdx(ring);
            if (messageIdx < 0) {
                return;
            }
            testReadValue(ring, varDataMax, testSize, FIELD_LOC, k, messageIdx);
        } else {
            // unable to read so at this point
            // we can do other work and try again soon
            Thread.yield();
        }
    }
}
Also used : PipeConfig(com.ociweb.pronghorn.pipe.PipeConfig) Pipe(com.ociweb.pronghorn.pipe.Pipe) Test(org.junit.Test)

Example 17 with PipeConfig

use of com.ociweb.pronghorn.pipe.PipeConfig in project PronghornPipes by oci-pronghorn.

the class RingStreamsTest method testRingToRingInputStream.

// TODO: RingStreams needs to be deleted and no longer used.
@Ignore
public void testRingToRingInputStream() {
    Pipe testRing = new Pipe(new PipeConfig(RawDataSchema.instance, 1 << 5, 1 << 13));
    testRing.initBuffers();
    int blockSize = testRing.maxVarLen;
    RingInputStream ringInputStream = new RingInputStream(testRing);
    Pipe targetRing = new Pipe(new PipeConfig(RawDataSchema.instance, 1 << 5, 1 << 13));
    targetRing.initBuffers();
    int testSize = 3000;
    byte[] testData = new byte[testSize];
    int testIdx = 0;
    while (testIdx < testSize) {
        assertEquals(0, Pipe.contentRemaining(targetRing));
        // Write data into the the ring buffer
        RingStreams.writeBytesToRing(testData, 0, testIdx, testRing, blockSize);
        RingStreams.writeEOF(testRing);
        // Here we are reading from one ring and writing to another ring going through an InputStream
        try {
            RingStreams.readFromInputStream(ringInputStream, targetRing);
            RingStreams.writeEOF(targetRing);
        } catch (IOException e) {
            e.printStackTrace();
            fail();
        }
        // Now read the data off the target ring to confirm it matches
        ByteArrayOutputStream baost = new ByteArrayOutputStream();
        try {
            RingStreams.writeToOutputStream(targetRing, baost);
        } catch (IOException e) {
            e.printStackTrace();
            fail();
        }
        assertTrue("len:" + testIdx, Arrays.equals(Arrays.copyOfRange(testData, 0, testIdx), baost.toByteArray()));
        testData[testIdx] = (byte) (testIdx & 0xFF);
        testIdx++;
    }
}
Also used : PipeConfig(com.ociweb.pronghorn.pipe.PipeConfig) RingInputStream(com.ociweb.pronghorn.pipe.stream.RingInputStream) Pipe(com.ociweb.pronghorn.pipe.Pipe) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Ignore(org.junit.Ignore)

Example 18 with PipeConfig

use of com.ociweb.pronghorn.pipe.PipeConfig in project PronghornPipes by oci-pronghorn.

the class RingStreamsTest method testRingToRingOutputStream.

// TODO: RingStreams needs to be deleted and no longer used.
@Ignore
public void testRingToRingOutputStream() {
    Pipe testRing = new Pipe(new PipeConfig(RawDataSchema.instance, 1 << 5, 1 << 13));
    testRing.initBuffers();
    int blockSize = testRing.maxVarLen;
    Pipe targetRing = new Pipe(new PipeConfig(RawDataSchema.instance, 1 << 5, 1 << 13));
    targetRing.initBuffers();
    RingOutputStream ringOutputStream = new RingOutputStream(targetRing);
    int testBits = 11;
    int testSize = 1 << testBits;
    int testMask = testSize - 1;
    byte[] testData = new byte[testSize];
    int j = testSize;
    while (--j >= 0) {
        testData[j] = (byte) (j & 0xFF);
    }
    int testIdx = 0;
    int testTotal = testSize * 10;
    while (testIdx < testTotal) {
        int datLen = testIdx & testMask;
        assertEquals(0, Pipe.contentRemaining(testRing));
        assertEquals(0, Pipe.contentRemaining(targetRing));
        // Write data into the the ring buffer
        RingStreams.writeBytesToRing(testData, 0, datLen, testRing, blockSize);
        RingStreams.writeEOF(testRing);
        // Here we are reading from one ring and writing to another ring going through an OutputStream
        try {
            RingStreams.writeToOutputStream(testRing, ringOutputStream);
            RingStreams.writeEOF(targetRing);
        } catch (IOException e) {
            e.printStackTrace();
            fail();
        }
        // Now read the data off the target ring to confirm it matches
        ByteArrayOutputStream baost = new ByteArrayOutputStream();
        try {
            RingStreams.writeToOutputStream(targetRing, baost);
        } catch (IOException e) {
            e.printStackTrace();
            fail();
        }
        assertTrue("len:" + testIdx, Arrays.equals(Arrays.copyOfRange(testData, 0, datLen), baost.toByteArray()));
        testIdx++;
    }
}
Also used : PipeConfig(com.ociweb.pronghorn.pipe.PipeConfig) RingOutputStream(com.ociweb.pronghorn.pipe.stream.RingOutputStream) Pipe(com.ociweb.pronghorn.pipe.Pipe) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Ignore(org.junit.Ignore)

Example 19 with PipeConfig

use of com.ociweb.pronghorn.pipe.PipeConfig in project PronghornPipes by oci-pronghorn.

the class PipeSingleTemplateASCIITest method simpleBytesWriteRead.

@Test
public void simpleBytesWriteRead() {
    // this ring is 2^7 eg 128
    byte primaryRingSizeInBits = 7;
    byte byteRingSizeInBits = 16;
    Pipe<RawDataSchema> ring = new Pipe<RawDataSchema>(new PipeConfig(RawDataSchema.instance, 1 << primaryRingSizeInBits, 1 << byteRingSizeInBits));
    ring.initBuffers();
    int messageSize = RawDataSchema.FROM.fragDataSize[RawDataSchema.MSG_CHUNKEDSTREAM_1];
    int varDataMax = (ring.blobMask / (ring.slabMask >> 1)) / messageSize;
    int testSize = (1 << primaryRingSizeInBits) / messageSize;
    populateRingBufferWithASCII(ring, varDataMax, testSize);
    StringBuilder target = new StringBuilder();
    char[] target2 = new char[varDataMax];
    int k = testSize;
    while (PipeReader.tryReadFragment(ring)) {
        if (PipeReader.isNewMessage(ring)) {
            target.setLength(0);
            assertEquals(0, PipeReader.getMsgIdx(ring));
            int expectedLength = (varDataMax * (--k)) / testSize;
            String testString = buildTestString(expectedLength);
            if (0 == (k & 1)) {
                int actualLength = ((StringBuilder) PipeReader.readASCII(ring, RawDataSchema.MSG_CHUNKEDSTREAM_1_FIELD_BYTEARRAY_2, target)).length();
                assertEquals(expectedLength, actualLength);
                assertEquals(testString, target.toString());
            } else {
                int actualLength = PipeReader.readASCII(ring, RawDataSchema.MSG_CHUNKEDSTREAM_1_FIELD_BYTEARRAY_2, target2, 0);
                assertEquals(expectedLength, actualLength);
                assertTrue(testString + " vs " + new String(target2, 0, actualLength), Arrays.equals(testString.toCharArray(), Arrays.copyOfRange(target2, 0, actualLength)));
            }
        }
    }
}
Also used : PipeConfig(com.ociweb.pronghorn.pipe.PipeConfig) Pipe(com.ociweb.pronghorn.pipe.Pipe) Test(org.junit.Test)

Aggregations

Pipe (com.ociweb.pronghorn.pipe.Pipe)19 PipeConfig (com.ociweb.pronghorn.pipe.PipeConfig)19 Test (org.junit.Test)10 Ignore (org.junit.Ignore)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 IOException (java.io.IOException)5 RingInputStream (com.ociweb.pronghorn.pipe.stream.RingInputStream)3 RingOutputStream (com.ociweb.pronghorn.pipe.stream.RingOutputStream)2 DataOutputBlobWriter (com.ociweb.pronghorn.pipe.DataOutputBlobWriter)1 FieldReferenceOffsetManager (com.ociweb.pronghorn.pipe.FieldReferenceOffsetManager)1 MessageSchemaDynamic (com.ociweb.pronghorn.pipe.MessageSchemaDynamic)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1