Search in sources :

Example 1 with BytesRingBuffer

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Δ());
        }
    }
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) BytesRingBuffer(net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 2 with BytesRingBuffer

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Δ());
    }
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) BytesRingBuffer(net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer)

Example 3 with BytesRingBuffer

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());
    }
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) NativeBytesStore(net.openhft.chronicle.bytes.NativeBytesStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) BytesRingBuffer(net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 4 with BytesRingBuffer

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));
    }
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) NativeBytesStore(net.openhft.chronicle.bytes.NativeBytesStore) ExecutorService(java.util.concurrent.ExecutorService) BytesRingBuffer(net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 5 with BytesRingBuffer

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Δ());
        }
    }
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) BytesRingBuffer(net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Bytes (net.openhft.chronicle.bytes.Bytes)9 BytesRingBuffer (net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer)9 NativeBytesStore (net.openhft.chronicle.bytes.NativeBytesStore)4 ByteBuffer (java.nio.ByteBuffer)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ExecutorService (java.util.concurrent.ExecutorService)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 File (java.io.File)1 Executors (java.util.concurrent.Executors)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 ChronicleQueueBuilder (net.openhft.chronicle.queue.ChronicleQueueBuilder)1 org.junit (org.junit)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.fail (org.junit.Assert.fail)1 Test (org.junit.Test)1