-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathMappedFileTest.java
More file actions
307 lines (254 loc) · 11 KB
/
Copy pathMappedFileTest.java
File metadata and controls
307 lines (254 loc) · 11 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* Copyright (c) 2016-2022 chronicle.software
*
* https://chronicle.software
*
* 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.
*/
package net.openhft.chronicle.bytes;
import net.openhft.chronicle.core.Jvm;
import net.openhft.chronicle.core.OS;
import net.openhft.chronicle.core.io.IOTools;
import net.openhft.chronicle.core.io.ReferenceOwner;
import org.jetbrains.annotations.NotNull;
import org.junit.After;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.*;
import java.nio.BufferUnderflowException;
import java.nio.file.Files;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeFalse;
public class MappedFileTest extends BytesTestCommon {
@Rule
public final TemporaryFolder tmpDir = new TemporaryFolder();
@org.junit.jupiter.api.Test
void testWarmup() {
try {
MappedFile.warmup();
} catch (Throwable t) {
fail(t.getMessage());
}
}
@Test
public void shouldReleaseReferenceWhenNewStoreIsAcquired()
throws IOException {
final File file = tmpDir.newFile();
// this is what it will end up as
final long chunkSize = OS.mapAlign(64);
final ReferenceOwner test = ReferenceOwner.temporary("test");
try (final MappedFile mappedFile = MappedFile.mappedFile(file, 64, 0)) {
final MappedBytesStore first = mappedFile.acquireByteStore(test, 1);
final int expected = MappedFile.RETAIN ? 2 : 1;
assertEquals(expected, first.refCount());
final MappedBytesStore second = mappedFile.acquireByteStore(test, 1 + chunkSize);
assertEquals(expected, first.refCount());
assertEquals(expected, second.refCount());
final MappedBytesStore third = mappedFile.acquireByteStore(test, 1 + chunkSize + chunkSize);
assertEquals(expected, first.refCount());
assertEquals(expected, second.refCount());
assertEquals(expected, third.refCount());
third.release(test);
second.release(test);
first.release(test);
}
}
@Test
public void testReferenceCounts()
throws IOException {
/* assumeFalse(Jvm.isMacArm());*/
final File tmp = IOTools.createTempFile("testReferenceCounts");
final int chunkSize;
if (OS.isWindows()) {
chunkSize = 64 << 10;
} else if (Jvm.isMacArm()) {
chunkSize = 16 << 10;
} else
chunkSize = 4 << 10;
try (MappedFile mf = MappedFile.mappedFile(tmp, chunkSize, 0)) {
assertEquals("refCount: 1", mf.referenceCounts());
final ReferenceOwner test = ReferenceOwner.temporary("test");
final MappedBytesStore bs = mf.acquireByteStore(test, chunkSize + (1 << 10));
try {
assertEquals(chunkSize, bs.start());
assertEquals(chunkSize * 2, bs.capacity());
final Bytes<?> bytes = bs.bytesForRead();
assertNotNull(bytes.toString()); // show it doesn't blow up.
assertNotNull(bs.toString()); // show it doesn't blow up.
assertEquals(chunkSize, bytes.start());
assertEquals(0L, bs.readLong(chunkSize + (1 << 10)));
assertEquals(0L, bytes.readLong(chunkSize + (1 << 10)));
Assert.assertFalse(bs.inside(chunkSize - (1 << 10)));
Assert.assertFalse(bs.inside(chunkSize - 1));
Assert.assertTrue(bs.inside(chunkSize));
Assert.assertTrue(bs.inside(chunkSize * 2 - 1));
Assert.assertFalse(bs.inside(chunkSize * 2));
try {
bytes.readLong(chunkSize - (1 << 10));
Assert.fail();
} catch (BufferUnderflowException e) {
// expected
}
try {
bytes.readLong(chunkSize * 2 + (1 << 10));
Assert.fail();
} catch (BufferUnderflowException e) {
// expected
}
assertEquals(1, mf.refCount());
final int expected = MappedFile.RETAIN ? 2 : 1;
assertEquals(expected + 1, bs.refCount());
assertEquals("refCount: 1, 0, " + (expected + 1), mf.referenceCounts());
final BytesStore<?, ?> bs2 = mf.acquireByteStore(test, chunkSize + (1 << 10), bs);
assertSame(bs, bs2);
assertEquals(expected + 1, bs2.refCount());
assertEquals("refCount: 1, 0, " + (expected + 1), mf.referenceCounts());
bytes.releaseLast();
assertEquals(expected, bs2.refCount());
assertEquals("refCount: 1, 0, " + expected, mf.referenceCounts());
} finally {
bs.release(test);
}
}
}
@Test
public void largeReadOnlyFile()
throws IOException {
assumeFalse(Runtime.getRuntime().maxMemory() < Integer.MAX_VALUE || OS.isWindows());
final File file = Files.createTempFile("largeReadOnlyFile", "deleteme").toFile();
file.deleteOnExit();
try (MappedBytes bytes = MappedBytes.mappedBytes(file, 1 << 30, OS.pageSize())) {
bytes.writeLong(3L << 30, 0x12345678); // make the file 3 GB.
}
try (MappedBytes bytes = MappedBytes.readOnly(file)) {
Assert.assertEquals(0x12345678L, bytes.readLong(3L << 30));
}
}
@Test
public void largeReadOnlyFileSingle()
throws IOException {
assumeFalse(OS.isWindows());
assumeFalse(Runtime.getRuntime().maxMemory() < Integer.MAX_VALUE);
final File file = Files.createTempFile("largeReadOnlyFile", "deleteme").toFile();
file.deleteOnExit();
try (MappedBytes bytes = MappedBytes.singleMappedBytes(file, 4L << 30)) {
bytes.writeLong(3L << 30, 0x12345678); // make the file 3 GB.
}
try (MappedBytes bytes = MappedBytes.singleMappedBytes(file, 4L << 30, false)) {
Assert.assertEquals(0x12345678L, bytes.readLong(3L << 30));
}
}
@Test
public void interrupted()
throws FileNotFoundException {
Thread.currentThread().interrupt();
final String filename = IOTools.createTempFile("interrupted").getAbsolutePath();
try (MappedFile mf = MappedFile.mappedFile(filename, 64 << 10, 0)) {
mf.actualSize();
assertTrue(Thread.currentThread().isInterrupted());
}
}
@Test
public void testCreateMappedFile()
throws IOException {
final File file = IOTools.createTempFile("mappedFile");
final MappedFile mappedFile = MappedFile.mappedFile(file, 1024, 256, 256, false);
try {
final MappedFile mappedFile2 = MappedFile.mappedFile(file, 1024, 256, 256, false);
mappedFile2.releaseLast();
assertTrue(mappedFile2.isClosed());
} finally {
mappedFile.releaseLast();
}
assertTrue(mappedFile.isClosed());
}
@Test
public void testReadOnlyOpen()
throws IOException {
assumeFalse(OS.isWindows());
String text = "Some text to put in this file. yay!\n";
@NotNull File file = Files.createTempFile("readOnlyOpenFile", "deleteme").toFile();
// write some stuff to a file so it exits using stock java APIs
@NotNull OutputStreamWriter outWrite = new OutputStreamWriter(new FileOutputStream(file));
outWrite.append(text);
outWrite.flush();
outWrite.close();
byte[] tmp = new byte[1024 * 16];
// Open and read the file w/ a MappedBytes to show it's readable
try (@NotNull MappedBytes mapBuf = MappedBytes.readOnly(file)) {
mapBuf.readLimit(file.length());
int readLen = mapBuf.read(tmp, 0, tmp.length);
assertEquals(text, new String(tmp, 0, readLen));
}
// open up the same file via a mapped file
try (@NotNull MappedFile mapFile = MappedFile.mappedFile(file, OS.pageSize() * 16L, OS.pageSize(), true)) {
// this throws a exception as of v2.20.9. it shouldn't
ReferenceOwner temp = ReferenceOwner.temporary("TEMP");
@NotNull Bytes<?> buf = mapFile.acquireBytesForRead(temp, 0);
buf.readLimit(file.length());
int readLen = buf.read(tmp, 0, tmp.length);
assertEquals(text, new String(tmp, 0, readLen));
buf.releaseLast(temp);
}
file.deleteOnExit();
}
@Test
public void testSlicePositions()
throws IOException {
assumeFalse(OS.isWindows());
final long chunkSize = OS.pageSize() * 64;
final long overlapSize = OS.pageSize();
// in the first chunk.
final Long firstPos = chunkSize / 2;
// in the first overlap
final Long secondPos = chunkSize + (overlapSize / 2);
// span the overlap and next chunk
final Long thirdPos = chunkSize + overlapSize - 4;
// need a file to read
final File inFile = Files.createTempFile("testSlicePositions-inFile", "deleteme").toFile();
inFile.deleteOnExit();
try (MappedBytes bytes = MappedBytes.mappedBytes(inFile, chunkSize)) {
// write in the middle of two different chunks
bytes.writeLong(firstPos, 12345);
bytes.writeLong(secondPos, 54321);
bytes.writeLong(thirdPos, 87654);
// set the file length
bytes.writeLong(chunkSize * 4, 1010101);
}
try (MappedBytes root = MappedBytes.mappedBytes(inFile, chunkSize, overlapSize, true)) {
// BUG 1 : must read first, or exception
if(false) {
assertEquals(12345, root.readLong(firstPos));
}
Bytes<Void> slice = root.bytesForRead();
slice.readPositionRemaining(secondPos, 8);
// BUG 2 : readPosion fails, PositionRemaining works
if (false) {
root.readPositionRemaining(thirdPos, 8);
} else {
root.readPosition(thirdPos);
}
long rootPos = root.readPosition();
Assert.assertEquals(54321, slice.readLong());
Assert.assertEquals(rootPos, root.readPosition());
assertEquals(87654, root.readLong());
slice.releaseLast();
}
}
@After
public void clearInterrupt() {
Thread.interrupted();
}
}