-
Notifications
You must be signed in to change notification settings - Fork 567
Expand file tree
/
Copy pathCreateAtIndexTest.java
More file actions
146 lines (116 loc) · 5 KB
/
Copy pathCreateAtIndexTest.java
File metadata and controls
146 lines (116 loc) · 5 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Copyright 2013-2025 chronicle.software; SPDX-License-Identifier: Apache-2.0
*/
package net.openhft.chronicle.queue;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.core.annotation.RequiredForClient;
import net.openhft.chronicle.core.io.IORuntimeException;
import net.openhft.chronicle.core.io.IOTools;
import net.openhft.chronicle.queue.impl.single.IllegalIndexException;
import net.openhft.chronicle.queue.impl.single.InternalAppender;
import net.openhft.chronicle.wire.DocumentContext;
import org.junit.Assert;
import org.junit.Test;
import java.io.File;
import static net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder.single;
import static net.openhft.chronicle.queue.rollcycles.TestRollCycles.TEST_DAILY;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
@RequiredForClient
public class CreateAtIndexTest extends QueueTestCommon {
@Test
public void
testWriteBytesWithIndex() {
final Bytes<?> HELLO_WORLD = Bytes.from("hello world");
File tmp = getTmpDir();
try (ChronicleQueue queue = single(tmp).testBlockSize().rollCycle(TEST_DAILY).build();
InternalAppender appender = (InternalAppender) queue.createAppender()) {
appender.writeBytes(0x421d00000000L, HELLO_WORLD);
appender.writeBytes(0x421d00000001L, HELLO_WORLD);
}
try (ChronicleQueue queue = single(tmp)
.testBlockSize()
.build();
InternalAppender appender = (InternalAppender) queue.createAppender()) {
String before = queue.dump();
appender.writeBytes(0x421d00000000L, HELLO_WORLD);
String after = queue.dump();
// ignore benign metadata deltas from the appender's first-write EOF normalisation
assertEquals(cleanDump(before), cleanDump(after));
}
/*
TODO FIX
if (Jvm.isAssertEnabled()) {
try (ChronicleQueue queue = single(tmp)
.testBlockSize()
.build()) {
InternalAppender appender = (InternalAppender) queue.acquireAppender();
String before = queue.dump();
try {
appender.writeBytes(0x421d00000000L, Bytes.from("hellooooo world"));
fail();
} catch (IllegalStateException e) {
// expected
}
String after = queue.dump();
assertEquals(before, after);
}
}
*/
// try too far
try (ChronicleQueue queue = single(tmp)
.testBlockSize()
.build();
InternalAppender appender = (InternalAppender) queue.createAppender()) {
IllegalIndexException e = assertThrows(IllegalIndexException.class, () -> appender.writeBytes(0x421d00000003L, HELLO_WORLD));
assertEquals("Index provided is after the next index in the queue, provided index = 421d00000003, last index in queue = 421d00000001", e.getMessage());
}
try (ChronicleQueue queue = single(tmp)
.testBlockSize()
.build();
InternalAppender appender = (InternalAppender) queue.createAppender()) {
appender.writeBytes(0x421d00000002L, HELLO_WORLD);
appender.writeBytes(0x421d00000003L, HELLO_WORLD);
}
try {
IOTools.deleteDirWithFiles(tmp, 2);
} catch (IORuntimeException ignored) {
}
}
private static String cleanDump(String dump) {
return dump
.replaceAll("# \\d+ bytes remaining", "# NN bytes remaining")
.replaceAll("modCount: (\\d+)", "modCount: 00")
.replaceAll("# position: \\d+, header: \\d+\\R--- !!data #binary\\RnormalisedEOFsTo: \\d+\\R", "");
}
@Test
public void testWrittenAndReadIndexesAreTheSameOfTheFirstExcerpt() {
File tmp = getTmpDir();
long expected;
try (ChronicleQueue queue = single(tmp)
.testBlockSize()
.build();
ExcerptAppender appender = queue.createAppender()) {
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()) {
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);
}
}
}
}
}