use of net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer in project Chronicle-Queue by OpenHFT.
the class BytesRingBufferTest method testWriteAndRead3SingleThreadedWrite.
@Test
public void testWriteAndRead3SingleThreadedWrite() {
try (NativeBytesStore<Void> nativeStore = NativeBytesStore.nativeStoreWithFixedCapacity(24)) {
final BytesRingBuffer bytesRingBuffer = new BytesRingBuffer(nativeStore.bytes());
for (int i = 0; i < 100; i++) {
bytesRingBuffer.offer(data());
Bytes bytes = bytesRingBuffer.take(maxSize -> {
Bytes<ByteBuffer> clear = input.clear();
return clear;
});
assertEquals(EXPECTED, bytes.readUTFΔ());
}
}
}
use of net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer in project Chronicle-Queue by OpenHFT.
the class BytesRingBufferTest method testSimpledSingleThreadedWriteRead.
@Test
public void testSimpledSingleThreadedWriteRead() {
try (NativeBytesStore<Void> nativeStore = NativeBytesStore.nativeStoreWithFixedCapacity(150)) {
final BytesRingBuffer bytesRingBuffer = new BytesRingBuffer(nativeStore.bytes());
bytesRingBuffer.offer(data());
Bytes actual = bytesRingBuffer.take(maxSize -> input.clear());
assertEquals(EXPECTED, actual.readUTFΔ());
}
}
use of net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer in project Chronicle-Queue by OpenHFT.
the class BytesRingBufferTest method testMultiThreadedWithIntValues.
// @Ignore("works in lang-bytes, appears to be a visibility issue that can be fixed by adding
// a" +
// " synchronized to ringbuffer.poll() and ringbuffer.offer()")
@Test
public void testMultiThreadedWithIntValues() {
try (NativeBytesStore allocate = NativeBytesStore.nativeStoreWithFixedCapacity(1000)) {
final BytesRingBuffer bytesRingBuffer = new BytesRingBuffer(allocate.bytes());
AtomicInteger counter = new AtomicInteger();
// writer
int iterations = 20_000;
{
ExecutorService executorService = Executors.newFixedThreadPool(2);
for (int i = 0; i < iterations; i++) {
final int j = i;
executorService.submit(() -> {
try (NativeBytesStore allocate2 = NativeBytesStore.nativeStoreWithFixedCapacity(iterations)) {
final Bytes out = allocate2.bytes();
out.clear();
out.writeInt(j);
counter.addAndGet(j);
out.flip();
boolean offer;
do {
offer = bytesRingBuffer.offer(out);
} while (!offer);
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
CountDownLatch count = new CountDownLatch(iterations);
// reader
{
ExecutorService executorService = Executors.newSingleThreadExecutor();
for (int i = 0; i < iterations; i++) {
executorService.submit(() -> {
try {
try (NativeBytesStore allocate3 = NativeBytesStore.nativeStoreWithFixedCapacity(25)) {
final Bytes bytes = allocate3.bytes();
Bytes result = null;
do {
try {
result = bytesRingBuffer.poll(maxsize -> bytes);
} catch (InterruptedException e) {
return;
}
} while (result == null);
int value = result.readInt();
counter.addAndGet(-value);
count.countDown();
} catch (Error e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
Assert.assertTrue(count.await(5000, TimeUnit.SECONDS));
Assert.assertEquals(0, counter.get());
}
}
use of net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer in project Chronicle-Queue by OpenHFT.
the class BytesRingBufferTest method testMultiThreadedCheckAllEntriesReturnedAreValidText.
// @Ignore("works in lang-bytes")
@Test
public void testMultiThreadedCheckAllEntriesReturnedAreValidText() throws InterruptedException {
System.out.println("Hello World");
try (NativeBytesStore allocate = NativeBytesStore.nativeStoreWithFixedCapacity(1000)) {
final BytesRingBuffer bytesRingBuffer = new BytesRingBuffer(allocate.bytes());
// writer
final int iterations = 100_000;
{
ExecutorService executorService = Executors.newFixedThreadPool(2);
for (int i = 0; i < iterations; i++) {
final int j = i;
executorService.submit(() -> {
try (NativeBytesStore<Void> nativeStore = NativeBytesStore.nativeStoreWithFixedCapacity(iterations)) {
final Bytes out = nativeStore.bytes();
String expected = EXPECTED_VALUE + j;
out.clear();
out.writeUTFΔ(expected);
out.flip();
boolean offer;
do {
offer = bytesRingBuffer.offer(out);
} while (!offer);
System.out.println("+");
} catch (Throwable e) {
e.printStackTrace();
}
});
}
}
CountDownLatch count = new CountDownLatch(iterations);
System.out.println(count);
// reader
{
ExecutorService executorService = Executors.newSingleThreadExecutor();
for (int i = 0; i < iterations; i++) {
executorService.submit(() -> {
try (NativeBytesStore<Void> nativeStore = NativeBytesStore.nativeStoreWithFixedCapacity(25)) {
Bytes bytes = nativeStore.bytes();
Bytes result = null;
do {
try {
result = bytesRingBuffer.poll(maxSize -> bytes);
} catch (InterruptedException e) {
return;
}
} while (result == null);
result.clear();
String actual = result.readUTFΔ();
if (actual.startsWith(EXPECTED_VALUE))
count.countDown();
System.out.println("-");
} catch (Throwable e) {
e.printStackTrace();
}
});
}
}
Assert.assertTrue(count.await(5000, TimeUnit.SECONDS));
}
}
use of net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer in project Chronicle-Queue by OpenHFT.
the class BytesRingBufferTest method testWrite3read3SingleThreadedWrite.
@Test
public void testWrite3read3SingleThreadedWrite() {
try (NativeBytesStore<Void> nativeStore = NativeBytesStore.nativeStoreWithFixedCapacity(150)) {
final BytesRingBuffer bytesRingBuffer = new BytesRingBuffer(nativeStore.bytes());
assert nativeStore.bytes().capacity() < (1 << 12);
for (int i = 0; i < 100; i++) {
bytesRingBuffer.offer(data());
// bytesRingBuffer.offer(data());
// bytesRingBuffer.offer(data());
// assertEquals(EXPECTED, bytesRingBuffer.take(maxSize -> input.clear()).readUTFΔ());
Bytes bytes = bytesRingBuffer.take(maxSize -> {
Bytes<ByteBuffer> clear = input.clear();
return clear;
});
try {
String s = bytes.readUTFΔ();
} catch (AssertionError e) {
System.out.println(Bytes.toHex(bytes));
}
// System.out.println("hex="+Bytes.toHex(bytes));
// assertEquals(EXPECTED, bytes.readUTFΔ());
}
}
}
Aggregations