use of net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer in project Chronicle-Queue by OpenHFT.
the class ZipBytesRingBufferTest method testZipAndAppend.
@Test
public void testZipAndAppend() {
File file = null;
try {
NativeBytesStore allocate = NativeBytesStore.nativeStoreWithFixedCapacity(1024);
NativeBytesStore msgBytes = NativeBytesStore.nativeStoreWithFixedCapacity(150);
net.openhft.chronicle.bytes.Bytes message = msgBytes.bytes();
message.writeUTFΔ("Hello World");
message.flip();
file = File.createTempFile("chronicle", "q");
DirectChronicleQueue chronicle = (DirectChronicleQueue) new ChronicleQueueBuilder(file.getName()).build();
final long writeAddress = getHeader((SingleChronicleQueue) chronicle).getWriteByte();
final BytesRingBuffer ring = new BytesRingBuffer(allocate.bytes());
final ZippedDocumentAppender zippedDocumentAppender = new ZippedDocumentAppender(ring, chronicle);
zippedDocumentAppender.append(message);
long initialValue = chronicle.firstBytes();
AtomicLong offset = new AtomicLong(initialValue);
while (lastWrite((SingleChronicleQueue) chronicle) == writeAddress) {
// wait for data to be written ( via another thread )
}
// read the data from chronicle into actual
Bytes actual = NativeBytesStore.nativeStoreWithFixedCapacity(100).bytes();
chronicle.readDocument(offset, actual);
// "Hello World" zipped should be 12 chars
Assert.assertEquals(12, actual.flip().remaining());
} finally {
if (file != null)
file.delete();
}
}
use of net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer in project Chronicle-Queue by OpenHFT.
the class BytesRingBufferTest method testFlowAroundSingleThreadedWriteDifferentSizeBuffers.
@Test
public void testFlowAroundSingleThreadedWriteDifferentSizeBuffers() {
try (NativeBytesStore<Void> nativeStore = NativeBytesStore.nativeStoreWithFixedCapacity(150)) {
System.out.println(nativeStore.realCapacity());
System.out.println(nativeStore.capacity());
System.out.println(nativeStore.limit());
assert !nativeStore.isElastic();
Bytes<Void> bytes = nativeStore.bytes();
System.out.println(bytes);
for (int j = 23 + 34; j < 100; j++) {
final BytesRingBuffer bytesRingBuffer = new BytesRingBuffer(nativeStore.bytes());
for (int i = 0; i < 50; i++) {
bytesRingBuffer.offer(data());
String actual = bytesRingBuffer.take(maxSize -> input.clear()).readUTFΔ();
assertEquals(EXPECTED, actual);
}
}
}
}
use of net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer in project Chronicle-Queue by OpenHFT.
the class BytesRingBufferTest method testWriteAndRead.
@Test
public void testWriteAndRead() {
try (NativeBytesStore<Void> nativeStore = NativeBytesStore.nativeStoreWithFixedCapacity(150)) {
assert nativeStore.isNative();
final BytesRingBuffer bytesRingBuffer = new BytesRingBuffer(nativeStore.bytes());
data();
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 testPollWithNoData.
@Test
public void testPollWithNoData() {
try (NativeBytesStore<Void> nativeStore = NativeBytesStore.nativeStoreWithFixedCapacity(150)) {
assert nativeStore.isNative();
final BytesRingBuffer bytesRingBuffer = new BytesRingBuffer(nativeStore.bytes());
Bytes actual = bytesRingBuffer.poll(maxSize -> input.clear());
assertEquals(null, actual);
}
}
Aggregations