Skip to content

Instantly share code, notes, and snippets.

@bdelbosc
Last active November 9, 2017 17:48
Show Gist options
  • Save bdelbosc/0a240b1afa6cef54a20ba80d587af3dd to your computer and use it in GitHub Desktop.
Save bdelbosc/0a240b1afa6cef54a20ba80d587af3dd to your computer and use it in GitHub Desktop.
testRandom fails since 4.6.25 including 4.6.41, output:
75071733366784: msg0
75071733366785: msg1
75071733366786: msg2
75071733366787: msg3
75071733366788: msg4
75071733366789: msg5
75071733366790: msg6
75071733366791: msg7
75071733366792: msg8
75071733366793: msg9
----------
75071733366785: msg1
75071733366786: msg2 next
75071733366786: msg3
org.junit.ComparisonFailure:
Expected :msg2
Actual :msg3
testSimple fails since CQ 4.6.25, this is a duplicate as Chronicle-Queue/issues/393 and is fixed in 4.6.38
org.junit.ComparisonFailure:
Expected :msg2
Actual :msg4
at org.nuxeo.lib.stream.tests.TestLibChronicleRegression.testSimple(TestLibChronicleRegression.java:85)
/*
* (C) Copyright 2017 Nuxeo SA (http://nuxeo.com/) and others.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Contributors:
* bdelbosc
*/
package org.nuxeo.lib.stream.tests;
import static org.junit.Assert.assertEquals;
import java.io.File;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import net.openhft.chronicle.queue.ChronicleQueue;
import net.openhft.chronicle.queue.ExcerptAppender;
import net.openhft.chronicle.queue.ExcerptTailer;
import net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder;
/**
* Unit Test to learn the Chronicle Queue lib.
*
* @since 9.3
*/
public class TestLibChronicleRegression {
@Rule
public TemporaryFolder folder = new TemporaryFolder();
@Test
public void testSimple() throws Exception {
File cqFolder = folder.newFolder("cq");
try (ChronicleQueue queue = SingleChronicleQueueBuilder.binary(cqFolder).build()) {
// create a queue and add some excerpts
ExcerptAppender app = queue.acquireAppender();
for (int i =0; i< 10; i++) {
final String msg = "msg" + i;
app.writeDocument(w -> w.write("message").object(msg));
}
ExcerptTailer tailer = queue.createTailer().toStart();
final String[] ret = new String[1];
// read a msg
tailer.readDocument(w -> ret[0] = (String) w.read("message").object());
assertEquals("msg0", ret[0]);
// read another one
tailer.readDocument(w -> ret[0] = (String) w.read("message").object());
assertEquals("msg1", ret[0]);
long pos2 = tailer.index();
tailer.readDocument(w -> ret[0] = (String) w.read("message").object());
assertEquals("msg2", ret[0]);
tailer.readDocument(w -> ret[0] = (String) w.read("message").object());
assertEquals("msg3", ret[0]);
// go back to pos2
tailer.moveToIndex(pos2);
// read a third doc
tailer.readDocument(w -> ret[0] = (String) w.read("message").object());
assertEquals("msg2", ret[0]);
tailer.readDocument(w -> ret[0] = (String) w.read("message").object());
assertEquals("msg3", ret[0]);
// again
tailer.moveToIndex(pos2);
tailer.readDocument(w -> ret[0] = (String) w.read("message").object());
// this return msg4 with chronicle queue 190a05c
assertEquals("msg2", ret[0]);
tailer.readDocument(w -> ret[0] = (String) w.read("message").object());
assertEquals("msg3", ret[0]);
}
}
}
@Test
public void testRandomMove() throws Exception {
int nbMessages = 10;
int nbTests = 100;
Map<Long, String> messages = new HashMap<>(nbMessages);
File cqFolder = folder.newFolder("cq");
try (ChronicleQueue queue = SingleChronicleQueueBuilder.binary(cqFolder).build()) {
// create a queue and add some excerpts
ExcerptAppender app = queue.acquireAppender();
for (int i =0; i < nbMessages; i++) {
final String message = "msg" + i;
app.writeDocument(w -> w.write("message").object(message));
if (i < nbMessages + 1) {
messages.put(app.lastIndexAppended(), message);
System.out.println(app.lastIndexAppended() + ": " + message);
}
}
System.out.println("----------");
Random random = new Random();
List<Long> offsets = new ArrayList<>(messages.keySet());
ExcerptTailer tailer = queue.createTailer().toStart();
String[] ret = new String[1];
for (int i=0; i < nbTests; i++) {
long offset = offsets.get(random.nextInt(messages.keySet().size()));
tailer.moveToIndex(offset);
tailer.readDocument(w -> ret[0] = (String) w.read("message").object());
System.out.println(offset + ": " + ret[0]);
assertEquals(messages.get(offset), ret[0]);
long index = tailer.index();
tailer.readDocument(w -> ret[0] = (String) w.read("message").object());
System.out.println(index + ": " + ret[0] + " next");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment