@Test public void simpleWriteRead() { byte primaryRingSizeInBits = 9; byte byteRingSizeInBits = 18; Pipe ring = new Pipe(new PipeConfig(primaryRingSizeInBits, byteRingSizeInBits, null, FROM)); ring.initBuffers(); int messageSize = FROM.fragDataSize[FRAG_LOC]; int varDataMax = (ring.byteMask / (ring.mask >> 1)) / messageSize; int testSize = ((1 << primaryRingSizeInBits) / messageSize) - 1; // room for EOF writeTestValue(ring, varDataMax, testSize); // now read the data back int FIELD_LOC = FieldReferenceOffsetManager.lookupFieldLocator(SINGLE_MESSAGE_NAMES[0], FRAG_LOC, FROM); int k = testSize; while (PipeReader.tryReadFragment(ring)) { --k; assertTrue(PipeReader.isNewMessage(ring)); int messageIdx = PipeReader.getMsgIdx(ring); if (messageIdx < 0) { return; } testReadValue(ring, varDataMax, testSize, FIELD_LOC, k, messageIdx); } }
@Test public void simpleWriteReadThreaded() { final byte primaryRingSizeInBits = 7; // this ring is 2^7 eg 128 final byte byteRingSizeInBits = 16; final Pipe ring = new Pipe(new PipeConfig(primaryRingSizeInBits, byteRingSizeInBits, null, FROM)); ring.initBuffers(); final int messageSize = FROM.fragDataSize[FRAG_LOC]; final int varDataMax = (ring.byteMask / (ring.mask >> 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) { // This is the example code that one would normally use. // 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 k--; // count down all the expected messages so we stop this test at the right time 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(); } } }
@Before public void createTestFile() { rb = new Pipe(new PipeConfig((byte) 21, (byte) 7, null, FieldReferenceOffsetManager.RAW_BYTES)); rb.initBuffers(); try { // build a bunch of data int i = 1 << 26; byte[] temp = new byte[i]; while (--i >= 0) { if ((i & 0x3F) == 0) { temp[i] = '\n'; } else { if ((((i + 1) & 0x7) == 0) && (((i + 9) & 0x1F) == 0)) { temp[i] = '\\'; } else { if ((i & 0x7) == 0) { temp[i] = ','; } else { temp[i] = (byte) ('0' + (int) (0x1F & i)); } } } } // System.err.println(new String(temp)); // write the data to the file File f = File.createTempFile(this.getClass().getSimpleName(), "test"); f.deleteOnExit(); // but do keep it arround while we do our test FileOutputStream out = new FileOutputStream(f); out.write(temp); out.close(); testFile = f; } catch (IOException e) { e.printStackTrace(); fail(); } }