Search in sources :

Example 11 with Pipe

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

the class PipeSingleTemplateDecimalTest method simpleWriteReadThreaded.

@Test
public void simpleWriteReadThreaded() {
    // this ring is 2^7 eg 128
    final byte primaryRingSizeInBits = 8;
    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) {
                break;
            }
            readTestValue(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 12 with Pipe

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

the class PipeSingleTemplateTest method simpleBytesWriteReadThreaded.

@Test
public void simpleBytesWriteReadThreaded() {
    // this ring is 2^7 eg 128
    final byte primaryRingSizeInBits = 7;
    final byte byteRingSizeInBits = 16;
    final Pipe ring = new Pipe(new PipeConfig(RawDataSchema.instance, 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() {
            populateRingBufferWithBytes(ring, varDataMax, testSize);
        }
    });
    t.start();
    // now read the data back
    byte[] target = new byte[varDataMax];
    int BYTE_LOC = FieldReferenceOffsetManager.lookupFieldLocator("ByteArray", FRAG_LOC, FROM);
    int k = testSize;
    while (k > 1) {
        // 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--;
            // would use this method rarely to determine if fragment starts new message
            assertTrue(PipeReader.isNewMessage(ring));
            // when we only have 1 message type this would not normally be called
            assertEquals(0, PipeReader.getMsgIdx(ring));
            int expectedLength = (varDataMax * k) / testSize;
            // read bytes as normal code would do
            int actualLength = PipeReader.readBytes(ring, BYTE_LOC, target, 0);
            assertEquals(expectedLength, actualLength);
        } 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 13 with Pipe

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

the class PipeSingleTemplateTest method simpleBytesWriteRead.

@Test
public void simpleBytesWriteRead() {
    // this ring is 2^7 eg 128
    byte primaryRingSizeInBits = 7;
    byte byteRingSizeInBits = 16;
    Pipe ring = new Pipe(new PipeConfig(RawDataSchema.instance, 1 << primaryRingSizeInBits, 1 << byteRingSizeInBits));
    ring.initBuffers();
    int messageSize = FROM.fragDataSize[FRAG_LOC];
    int varDataMax = (ring.blobMask / (ring.slabMask >> 1)) / messageSize;
    int testSize = (1 << primaryRingSizeInBits) / messageSize;
    populateRingBufferWithBytes(ring, varDataMax, testSize);
    // now read the data back
    int BYTE_LOC = FieldReferenceOffsetManager.lookupFieldLocator("ByteArray", FRAG_LOC, FROM);
    byte[] target = new byte[varDataMax];
    int k = testSize;
    while (PipeReader.tryReadFragment(ring)) {
        if (PipeReader.isNewMessage(ring)) {
            assertEquals(0, PipeReader.getMsgIdx(ring));
            int expectedLength = (varDataMax * (--k)) / testSize;
            // read bytes as normal code would do
            int actualLength = PipeReader.readBytes(ring, BYTE_LOC, target, 0);
            assertEquals(expectedLength, actualLength);
        }
    }
}
Also used : PipeConfig(com.ociweb.pronghorn.pipe.PipeConfig) Pipe(com.ociweb.pronghorn.pipe.Pipe) Test(org.junit.Test)

Example 14 with Pipe

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

the class StreamingConsumerTest method matchingTestPositive.

@Test
public void matchingTestPositive() {
    Pipe<MessageSchemaDynamic> ring1 = new Pipe<MessageSchemaDynamic>(new PipeConfig<MessageSchemaDynamic>(new MessageSchemaDynamic(FROM), 1 << primaryRingSizeInBits, 1 << byteRingSizeInBits));
    Pipe<MessageSchemaDynamic> ring2 = new Pipe<MessageSchemaDynamic>(new PipeConfig<MessageSchemaDynamic>(new MessageSchemaDynamic(FROM), 1 << primaryRingSizeInBits, 1 << byteRingSizeInBits));
    ring1.initBuffers();
    ring2.initBuffers();
    int commonSeed = 100;
    StreamingWriteVisitorGenerator swvg1 = new StreamingWriteVisitorGenerator(FROM, new Random(commonSeed), 30, 30);
    StreamingVisitorWriter svw1 = new StreamingVisitorWriter(ring1, swvg1);
    StreamingWriteVisitorGenerator swvg2 = new StreamingWriteVisitorGenerator(FROM, new Random(commonSeed), 30, 30);
    StreamingVisitorWriter svw2 = new StreamingVisitorWriter(ring2, swvg2);
    svw1.startup();
    svw2.startup();
    svw1.run();
    svw2.run();
    svw1.run();
    svw2.run();
    // confirm that both rings contain the exact same thing
    assertTrue(Arrays.equals(Pipe.primaryBuffer(ring1), Pipe.primaryBuffer(ring2)));
    assertTrue(Arrays.equals(Pipe.byteBuffer(ring1), Pipe.byteBuffer(ring2)));
    // now use matcher to confirm the same.
    StreamingReadVisitorMatcher srvm = new StreamingReadVisitorMatcher(ring1);
    // new StreamingReadVisitorDebugDelegate(srvm) );
    StreamingVisitorReader svr = new StreamingVisitorReader(ring2, srvm);
    svr.startup();
    try {
        svr.run();
    } catch (Throwable t) {
        t.printStackTrace();
        fail(t.getMessage());
    }
    svr.shutdown();
    svw1.shutdown();
    svw2.shutdown();
}
Also used : StreamingWriteVisitorGenerator(com.ociweb.pronghorn.pipe.stream.StreamingWriteVisitorGenerator) StreamingVisitorWriter(com.ociweb.pronghorn.pipe.stream.StreamingVisitorWriter) Random(java.util.Random) StreamingVisitorReader(com.ociweb.pronghorn.pipe.stream.StreamingVisitorReader) MessageSchemaDynamic(com.ociweb.pronghorn.pipe.MessageSchemaDynamic) StreamingReadVisitorMatcher(com.ociweb.pronghorn.pipe.stream.StreamingReadVisitorMatcher) Pipe(com.ociweb.pronghorn.pipe.Pipe) Test(org.junit.Test)

Example 15 with Pipe

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

the class StreamingConsumerTest method sequenceFragmentWriteRead.

@Test
public void sequenceFragmentWriteRead() {
    Pipe<MessageSchemaDynamic> ring = new Pipe<MessageSchemaDynamic>(new PipeConfig<MessageSchemaDynamic>(new MessageSchemaDynamic(FROM), 1 << primaryRingSizeInBits, 1 << byteRingSizeInBits));
    ring.initBuffers();
    int testSize = 5;
    // in this method we write two sequence members but only record the count after writing the members
    populateRingBufferWithSequence(ring, testSize);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(baos);
    StreamingReadVisitor visitor = new StreamingReadVisitorToJSON(ps);
    // new StreamingReadVisitorDebugDelegate(visitor) );
    StreamingVisitorReader reader = new StreamingVisitorReader(ring, visitor);
    // ring is fully populated so we should not need to call this run again
    while (Pipe.contentRemaining(ring) > 0) {
        reader.run();
    }
    ps.close();
    String results = new String(baos.toByteArray());
    // spot check the produced JSON
    assertTrue(results, results.indexOf("\"TruckId\":10") > 0);
    assertTrue(results, results.indexOf("{\"AThing\":7}") > 0);
    assertTrue(results, results.indexOf("{\"JustOneMoreQuestion\":42}") > 0);
}
Also used : PrintStream(java.io.PrintStream) StreamingReadVisitorToJSON(com.ociweb.pronghorn.pipe.stream.StreamingReadVisitorToJSON) StreamingReadVisitor(com.ociweb.pronghorn.pipe.stream.StreamingReadVisitor) StreamingVisitorReader(com.ociweb.pronghorn.pipe.stream.StreamingVisitorReader) MessageSchemaDynamic(com.ociweb.pronghorn.pipe.MessageSchemaDynamic) Pipe(com.ociweb.pronghorn.pipe.Pipe) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

Pipe (com.ociweb.pronghorn.pipe.Pipe)42 PipeConfig (com.ociweb.pronghorn.pipe.PipeConfig)19 Test (org.junit.Test)18 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 Ignore (org.junit.Ignore)7 MessageSchemaDynamic (com.ociweb.pronghorn.pipe.MessageSchemaDynamic)5 IOException (java.io.IOException)5 StreamingVisitorReader (com.ociweb.pronghorn.pipe.stream.StreamingVisitorReader)4 StreamingVisitorWriter (com.ociweb.pronghorn.pipe.stream.StreamingVisitorWriter)4 StreamingWriteVisitorGenerator (com.ociweb.pronghorn.pipe.stream.StreamingWriteVisitorGenerator)4 Random (java.util.Random)4 RingInputStream (com.ociweb.pronghorn.pipe.stream.RingInputStream)3 JSONVisitor (com.ociweb.pronghorn.util.parse.JSONVisitor)3 JSONVisitorCapture (com.ociweb.pronghorn.util.parse.JSONVisitorCapture)3 NetPayloadSchema (com.ociweb.pronghorn.network.schema.NetPayloadSchema)2 DataOutputBlobWriter (com.ociweb.pronghorn.pipe.DataOutputBlobWriter)2 RawDataSchema (com.ociweb.pronghorn.pipe.RawDataSchema)2 RingOutputStream (com.ociweb.pronghorn.pipe.stream.RingOutputStream)2 StreamingReadVisitor (com.ociweb.pronghorn.pipe.stream.StreamingReadVisitor)2 StreamingReadVisitorMatcher (com.ociweb.pronghorn.pipe.stream.StreamingReadVisitorMatcher)2