/
CreateAtIndexTest.java
executable file
·113 lines (87 loc) · 3.93 KB
/
CreateAtIndexTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package net.openhft.chronicle.queue;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.core.OS;
import net.openhft.chronicle.core.io.IORuntimeException;
import net.openhft.chronicle.core.io.IOTools;
import net.openhft.chronicle.queue.impl.single.SingleChronicleQueue;
import net.openhft.chronicle.queue.impl.single.SingleChronicleQueueExcerpts.InternalAppender;
import net.openhft.chronicle.wire.DocumentContext;
import org.junit.Assert;
import org.junit.Test;
import static net.openhft.chronicle.queue.RollCycles.TEST_DAILY;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
/**
* @author Rob Austin.
*/
public class CreateAtIndexTest {
@Test
public void testWriteBytesWithIndex() throws Exception {
String tmp = OS.TARGET + "/CreateAtIndexTest-" + System.nanoTime();
try (SingleChronicleQueue queue = ChronicleQueueBuilder.single(tmp)
.rollCycle(TEST_DAILY).build()) {
InternalAppender appender = (InternalAppender) queue.acquireAppender();
appender.writeBytes(0x421d00000000L, Bytes.from("hello world"));
appender.writeBytes(0x421d00000001L, Bytes.from("hello world"));
}
// try again and fail.
try (SingleChronicleQueue queue = ChronicleQueueBuilder.single(tmp).build()) {
InternalAppender appender = (InternalAppender) queue.acquireAppender();
// try {
appender.writeBytes(0x421d00000000L, Bytes.from("hello world"));
// fail();
// } catch (IllegalStateException e) {
// assertEquals("Unable to move to index 421d00000000 as the index already exists",
// e.getMessage());
// }
}
// try too far
try (SingleChronicleQueue queue = ChronicleQueueBuilder.single(tmp).build()) {
InternalAppender appender = (InternalAppender) queue.acquireAppender();
try {
appender.writeBytes(0x421d00000003L, Bytes.from("hello world"));
fail();
} catch (IllegalStateException e) {
assertEquals("Unable to move to index 421d00000003 beyond the end of the queue",
e.getMessage());
}
}
try (SingleChronicleQueue queue = ChronicleQueueBuilder.single(tmp).build()) {
InternalAppender appender = (InternalAppender) queue.acquireAppender();
appender.writeBytes(0x421d00000002L, Bytes.from("hello world"));
appender.writeBytes(0x421d00000003L, Bytes.from("hello world"));
}
try {
IOTools.deleteDirWithFiles(tmp, 2);
} catch (IORuntimeException ignored) {
}
}
@Test
public void testWrittenAndReadIndexesAreTheSameOfTheFirstExcerpt() throws Exception {
String tmp = OS.TARGET + "/CreateAtIndexTest-" + System.nanoTime();
long expected = 0;
try (SingleChronicleQueue queue = ChronicleQueueBuilder.single(tmp).build()) {
ExcerptAppender appender = queue.acquireAppender();
try (DocumentContext dc = appender.writingDocument()) {
dc.wire().write().text("some-data");
expected = dc.index();
Assert.assertTrue(expected > 0);
}
appender.lastIndexAppended();
ExcerptTailer tailer = queue.createTailer();
try (DocumentContext dc = tailer.readingDocument()) {
String text = dc.wire().read().text();
{
long actualIndex = dc.index();
Assert.assertTrue(actualIndex > 0);
Assert.assertEquals(expected, actualIndex);
}
{
long actualIndex = tailer.index();
Assert.assertTrue(actualIndex > 0);
Assert.assertEquals(expected, actualIndex);
}
}
}
}
}