Java源码示例:net.openhft.chronicle.core.io.IOTools

示例1
@Test(timeout = 5000L)
public void testRollWritesEOF() throws IOException {
    final File path = DirectoryUtils.tempDir(getClass().getName());
    try {
        path.mkdirs();
        final SetTimeProvider timeProvider = new SetTimeProvider();
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, -1);
        timeProvider.currentTimeMillis(cal.getTimeInMillis());
        createQueueAndWriteData(timeProvider, path);
        assertEquals(1, getNumberOfQueueFiles(path));

        // adjust time
        timeProvider.currentTimeMillis(System.currentTimeMillis());
        createQueueAndWriteData(timeProvider, path);
        assertEquals(2, getNumberOfQueueFiles(path));

        List<String> l = new LinkedList<>();
        new ChronicleReader().withMessageSink(l::add).withBasePath(path.toPath()).execute();
        // 2 entries per message
        assertEquals(4, l.size());
    } finally {
        IOTools.deleteDirWithFiles(path, 20);
    }
}
 
示例2
@Override
public void init(JLBH jlbh) {
    IOTools.deleteDirWithFiles("replica", 10);

    sourceQueue = single("replica").build();
    sinkQueue = single("replica").build();
    appender = sourceQueue.acquireAppender();
    tailer = sinkQueue.createTailer();
    new Thread(() -> {
        Datum datum2 = new Datum();
        while (true) {
            try (DocumentContext dc = tailer.readingDocument()) {
                if (dc.wire() == null)
                    continue;
                datum2.readMarshallable(dc.wire().bytes());
                jlbh.sample(System.nanoTime() - datum2.ts);
            }
        }
    }).start();
}
 
示例3
@Override
public void init(JLBH jlbh) {
    IOTools.deleteDirWithFiles("replica", 10);

    Byteable byteable = (Byteable) datum;
    long capacity = byteable.maxSize();
    byteable.bytesStore(NativeBytesStore.nativeStore(capacity), 0, capacity);
    datumBytes = ((Byteable) datum).bytesStore();
    datumWrite = datumBytes.bytesForWrite();

    sourceQueue = single("replica").build();
    sinkQueue = single("replica").build();
    appender = sourceQueue.acquireAppender();
    tailer = sinkQueue.createTailer();
    this.jlbh = jlbh;
}
 
示例4
@Test(expected = TimeoutException.class)
public void testDeadHeader() throws IOException {
    @NotNull File dir = DirectoryUtils.tempDir("testDeadHeader");

    dir.mkdirs();
    File file = new File(dir, "19700101" + SingleChronicleQueue.SUFFIX);
    file.createNewFile();
    @NotNull MappedBytes bytes = MappedBytes.mappedBytes(file, ChronicleQueue.TEST_BLOCK_SIZE);
    bytes.writeInt(Wires.NOT_COMPLETE | Wires.META_DATA);
    bytes.releaseLast();
    @Nullable ChronicleQueue queue = null;
    try {
        queue = binary(dir).timeoutMS(500L)
                .testBlockSize()
                .blockSize(ChronicleQueue.TEST_BLOCK_SIZE)
                .build();

        testQueue(queue);
    } finally {
        Closeable.closeQuietly(queue);
        IOTools.shallowDeleteDirWithFiles(dir.getAbsolutePath());
    }
}
 
示例5
public void run(String[] args) throws InterruptedException {
    File tmpDir = getTmpDir();
    SingleChronicleQueueBuilder builder = SingleChronicleQueueBuilder
            .fieldlessBinary(tmpDir)
            .blockSize(128 << 20);
    try (ChronicleQueue queue = builder
            .writeBufferMode(BUFFER_MODE)
            .readBufferMode(BufferMode.None)
            .build();
         ChronicleQueue queue2 = builder
                 .writeBufferMode(BufferMode.None)
                 .readBufferMode(BUFFER_MODE)
                 .build()) {

        runTest(queue, queue2);
    }
    IOTools.deleteDirWithFiles(tmpDir, 2);
}
 
示例6
public static void main(String[] args) {
    IOTools.deleteDirWithFiles("in", 2);
    IOTools.deleteDirWithFiles("out", 2);

    long events = 0, lastPrint = 0;
    try (ChronicleQueue queue = ChronicleQueue.singleBuilder("in").sourceId(1).build()) {
        try (ChronicleQueue queue2 = ChronicleQueue.singleBuilder("out").sourceId(2).build()) {

            Events out = queue2.methodWriterBuilder(Events.class).recordHistory(true).build();
            Events bridge = new BridgeEvents(out);
            MethodReader methodReader = queue.createTailer("bridge")
                    .methodReader(bridge);
            System.out.println("Started");
            long last = 0;
            while (running) {
                if (methodReader.readOne()) {
                    events++;
                } else {
                    long now = System.currentTimeMillis();
                    if (lastPrint != events && now > last + 250) {
                        System.out.println("events: " + events);
                        lastPrint = events;
                        last = now;
                    } else {
                        Thread.yield();
                    }
                }
            }
        }
    }
    System.out.println("... finished");
}
 
示例7
private void runTest(String id, Bytes msg) throws IOException {
    Path path = IOTools.createTempDirectory(id);
    try {
        CountDownLatch steady = new CountDownLatch(2);
        CountDownLatch go = new CountDownLatch(1);
        CountDownLatch done = new CountDownLatch(1);
        int n = 468;

        AtomicReference<Throwable> thr1 = useAppender(path, appender -> {
            appender.cycle();
            for (int i = 0; i < n; ++i)
                appender.writeBytes(msg);
            steady.countDown();
            await(go, "go");
            for (int i = 0; i < n; ++i)
                appender.writeBytes(msg);
        }, done);

        AtomicReference<Throwable> thr2 = useAppender(path, appender -> {
            steady.countDown();
            await(go, "go");
            int m = 2 * n;
            for (int i = 0; i < m; ++i)
                appender.cycle();
        }, done);

        await(steady, "steady");
        go.countDown();
        await(done, "done");

        assertNull(thr1.get());
        assertNull(thr2.get());
    } finally {
        DirectoryUtils.deleteDir(path.toFile());
    }
}
 
示例8
@Test
public void testWriteBytes() {
    File dir = DirectoryUtils.tempDir("WriteBytesTest");
    try (ChronicleQueue queue = binary(dir)
            .testBlockSize()
            .build()) {

        ExcerptAppender appender = queue.acquireAppender();
        ExcerptTailer tailer = queue.createTailer();

        outgoingMsgBytes[0] = 'A';
        outgoingBytes.write(outgoingMsgBytes);
        postOneMessage(appender);
        fetchOneMessage(tailer, incomingMsgBytes);
        System.out.println(new String(incomingMsgBytes));

        outgoingBytes.clear();

        outgoingMsgBytes[0] = 'A';
        outgoingMsgBytes[1] = 'B';
        outgoingBytes.write(outgoingMsgBytes);

        postOneMessage(appender);
        fetchOneMessage(tailer, incomingMsgBytes);
        System.out.println(new String(incomingMsgBytes));

    } finally {
        try {
            IOTools.deleteDirWithFiles(dir, 2);
        } catch (IORuntimeException e) {
            // ignored
        }
    }
}
 
示例9
@After
public void teardown() {
    try {
        IOTools.shallowDeleteDirWithFiles(chroniclePath);
    } catch (Exception e) {
        if (e instanceof AccessDeniedException && OS.isWindows())
            System.err.println(e);
        else
            throw e;
    }
}
 
示例10
@After
public void after() {
    try {
        IOTools.deleteDirWithFiles(path, 2);
    } catch (Exception ignored) {
    }
}
 
示例11
@Test(timeout = 5000L)
public void testRollWithoutEOFDoesntBlowup() throws IOException {
    final File path = DirectoryUtils.tempDir(getClass().getName());
    try {
        path.mkdirs();
        final SetTimeProvider timeProvider = new SetTimeProvider();
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, -1);
        timeProvider.currentTimeMillis(cal.getTimeInMillis());
        createQueueAndWriteData(timeProvider, path);
        assertEquals(1, getNumberOfQueueFiles(path));

        // adjust time
        timeProvider.currentTimeMillis(System.currentTimeMillis());
        createQueueAndWriteData(timeProvider, path);
        assertEquals(2, getNumberOfQueueFiles(path));

        Optional<Path> firstQueueFile = Files.list(path.toPath()).filter(p -> p.toString().endsWith(SUFFIX)).sorted().findFirst();

        assertTrue(firstQueueFile.isPresent());

        // remove EOF from first file
        removeEOF(firstQueueFile.get());

        List<String> l = new LinkedList<>();
        new ChronicleReader().withMessageSink(l::add).withBasePath(path.toPath()).execute();
        // 2 entries per message
        assertEquals(4, l.size());
    } finally {

        IOTools.deleteDirWithFiles(path, 20);
    }
}
 
示例12
@Test(timeout = 5000L)
public void testRollWithoutEOF() throws IOException {
    final File path = DirectoryUtils.tempDir(getClass().getName());
    try {
        path.mkdirs();
        final SetTimeProvider timeProvider = new SetTimeProvider();
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, -3);
        timeProvider.currentTimeMillis(cal.getTimeInMillis());
        createQueueAndWriteData(timeProvider, path);
        assertEquals(1, getNumberOfQueueFiles(path));

        // adjust time
        timeProvider.currentTimeMillis(System.currentTimeMillis());
        createQueueAndWriteData(timeProvider, path);
        assertEquals(2, getNumberOfQueueFiles(path));

        Optional<Path> firstQueueFile = Files.list(path.toPath()).filter(p -> p.toString().endsWith(SUFFIX)).sorted().findFirst();

        assertTrue(firstQueueFile.isPresent());

        // remove EOF from first file
        removeEOF(firstQueueFile.get());

        List<String> l = new LinkedList<>();
        new ChronicleReader().withMessageSink(l::add).withBasePath(path.toPath()).withReadOnly(false).execute();
        // 2 entries per message
        assertEquals(4, l.size());
    } finally {

        IOTools.deleteDirWithFiles(path, 20);
    }
}
 
示例13
public static void deleteDir(@NotNull File dir) {
    try {
        IOTools.deleteDirWithFiles(dir, 20);
    } catch (Throwable e) {
        e.printStackTrace();
    }
}
 
示例14
@Override
public void init(JLBH jlbh) {
    IOTools.deleteDirWithFiles("large", 3);

    sourceQueue = single("large").blockSize(1L << 30).build();
    sinkQueue = single("large").blockSize(1L << 30).build();
    appender = sourceQueue.acquireAppender();
    tailer = sinkQueue.createTailer();
    this.jlbh = jlbh;
}
 
示例15
@Test
public void state() throws IOException {
    // TODO FIX
    AbstractCloseable.disableCloseableTracing();

    assumeFalse(OS.isWindows());
    final Path dir = IOTools.createTempDirectory("openByAnyProcess");
    dir.toFile().mkdir();
    try {
        final File testFile = dir.resolve("tmpFile").toFile();
        Files.write(testFile.toPath(), "A".getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND);

        // The file is created but not open
        assertEquals(FileState.CLOSED, FileUtil.state(testFile));

        try (BufferedReader br = new BufferedReader(new FileReader(testFile))) {
            // The file is now held open
            assertEquals(FileState.OPEN, FileUtil.state(testFile));
        }

        // The file is now released again
        assertEquals(FileState.CLOSED, FileUtil.state(testFile));

    } finally {
        deleteDir(dir.toFile());
    }
}
 
示例16
public MoveToWrongIndexThenToEndTest() throws IOException {
    basePath = IOTools.createTempDirectory("MoveToWrongIndexThenToEndTest");
    basePath.toFile().deleteOnExit();

    queue = createChronicle(basePath);
    appender = queue.acquireAppender();
    outbound = Bytes.elasticByteBuffer();
}
 
示例17
@Test
public void shouldDetermineQueueDirectoryFromQueueFile() {
    final Path path = Paths.get(OS.USER_DIR, TEST_QUEUE_FILE);
    try (final ChronicleQueue queue =
                 ChronicleQueue.singleBuilder(path)
                         .testBlockSize()
                         .build()) {
        assertFalse(queue.createTailer().readingDocument().isPresent());
    } finally {
        IOTools.deleteDirWithFiles(path.toFile(), 20);
    }
}
 
示例18
@Test
public void testEmptyDirectory() {
    @NotNull File dir = new File(OS.TARGET, getClass().getSimpleName() + "-" + System.nanoTime());
    dir.mkdir();
    @NotNull RollingChronicleQueue queue = binary(dir).testBlockSize().build();
    assertEquals(Integer.MAX_VALUE, queue.firstCycle());
    assertEquals(Long.MAX_VALUE, queue.firstIndex());
    assertEquals(Integer.MIN_VALUE, queue.lastCycle());
    queue.close();

    IOTools.shallowDeleteDirWithFiles(dir.getAbsolutePath());
}
 
示例19
@Test(expected = TimeoutException.class)
public void testNoHeader() throws IOException {
    @NotNull File dir = new File(OS.TARGET + "/deleteme-" + System.nanoTime());
    dir.mkdir();

    File file = new File(dir, "19700101" + SingleChronicleQueue.SUFFIX);
    try (FileOutputStream fos = new FileOutputStream(file)) {
        byte[] bytes = new byte[1024];
        for (int i = 0; i < 128; i++) {
            fos.write(bytes);
        }
    }

    try (@NotNull ChronicleQueue queue = binary(dir)
            .rollCycle(RollCycles.TEST4_DAILY)
            .timeoutMS(500L)
            .testBlockSize()
            .build()) {
        testQueue(queue);
    } finally {
        try {
            IOTools.shallowDeleteDirWithFiles(dir.getAbsolutePath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 
示例20
@Test
public void tailerToEndIncreasesRefCount() throws NoSuchFieldException, IllegalAccessException {
    String path = OS.TARGET + "/toEndIncRefCount-" + System.nanoTime();
    IOTools.shallowDeleteDirWithFiles(path);

    SetTimeProvider time = new SetTimeProvider();
    long now = System.currentTimeMillis();
    time.currentTimeMillis(now);

    try (ChronicleQueue queue = SingleChronicleQueueBuilder.binary(path)
            .testBlockSize()
            .rollCycle(RollCycles.TEST_SECONDLY)
            .timeProvider(time)
            .build()) {

        final StoreAppender appender = (StoreAppender) queue.acquireAppender();
        Field storeF1 = StoreAppender.class.getDeclaredField("store");
        Jvm.setAccessible(storeF1);
        SingleChronicleQueueStore store1 = (SingleChronicleQueueStore) storeF1.get(appender);
        System.out.println(store1);

        appender.writeDocument(wire -> wire.write(() -> "msg").int32(1));

        final StoreTailer tailer = (StoreTailer) queue.createTailer();
        System.out.println(tailer);
        tailer.toEnd();
        System.out.println(tailer);

        Field storeF2 = StoreTailer.class.getDeclaredField("store");
        Jvm.setAccessible(storeF2);
        SingleChronicleQueueStore store2 = (SingleChronicleQueueStore) storeF2.get(tailer);

        // the reference count here is 1, the queue itself
        assertFalse(store2.isClosed());
    }
}
 
示例21
@Test
public void toEndBeforeWriteTest() {
    File baseDir = DirectoryUtils.tempDir("toEndBeforeWriteTest");
    IOTools.shallowDeleteDirWithFiles(baseDir);

    try (ChronicleQueue queue = SingleChronicleQueueBuilder.binary(baseDir)
            .testBlockSize()
            .build()) {
        checkOneFile(baseDir);

        // if this appender isn't created, the tailer toEnd doesn't cause a roll.
        @SuppressWarnings("unused")
        ExcerptAppender appender = queue.acquireAppender();
        checkOneFile(baseDir);

        ExcerptTailer tailer = queue.createTailer();
        checkOneFile(baseDir);

        ExcerptTailer tailer2 = queue.createTailer();
        checkOneFile(baseDir);

        tailer.toEnd();
        checkOneFile(baseDir);

        tailer2.toEnd();
        checkOneFile(baseDir);
    }
    System.gc();

    /*for (int i = 0; i < 10; i++) {
        final int j = i;
        appender.writeDocument(wire -> wire.write(() -> "msg").int32(j));
    }*/
    pathsToDelete.add(baseDir);
}
 
示例22
@Test
public void testWriteText() {
    File dir = DirectoryUtils.tempDir("testWriteText");
    try (ChronicleQueue queue = binary(dir)
            .testBlockSize()
            .build()) {

        ExcerptAppender appender = queue.acquireAppender();
        ExcerptTailer tailer = queue.createTailer();
        ExcerptTailer tailer2 = queue.createTailer();

        int runs = 1000;
        for (int i = 0; i < runs; i++)
            appender.writeText("" + i);
        for (int i = 0; i < runs; i++)
            assertEquals("" + i, tailer.readText());
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < runs; i++) {
            assertTrue(tailer2.readText(sb));
            assertEquals("" + i, sb.toString());
        }
    } finally {
        try {
            IOTools.deleteDirWithFiles(dir, 2);
        } catch (IORuntimeException e) {
            // ignored
        }
    }
}
 
示例23
@Test
public void testUsingPeekDocument() throws IOException {
    Path tempDir = null;
    try {
        tempDir = IOTools.createTempDirectory("ChronicleQueueLoggerTest");
        // Read back the data
        try (ChronicleQueue queue = SingleChronicleQueueBuilder.binary(tempDir).build()) {
            ExcerptTailer tailer = queue.createTailer();

            try (ChronicleQueue writeQueue = SingleChronicleQueueBuilder.binary(tempDir).build()) {
                ExcerptAppender appender = writeQueue.acquireAppender();

                try (DocumentContext dc = appender.writingDocument()) {
                    dc.wire().write("field1").int32(123534)
                            .write("field2").float64(123.423)
                            .write("time").int64(12053432432L);
                }

                try (DocumentContext dc = appender.writingDocument()) {
                    dc.wire().write("field1").int32(323242)
                            .write("field2").float64(543.1233)
                            .write("time").int64(12053432900L);
                }
            }

            assertEquals("field1: !int 123534\n" +
                            "field2: 123.423\n" +
                            "time: 12053432432\n",
                    read(tailer));

            assertEquals("field1: !int 323242\n" +
                            "field2: 543.1233\n" +
                            "time: 12053432900\n",
                    read(tailer));
        }
    } finally {
        if (tempDir != null) {
            IOTools.deleteDirWithFiles(tempDir.toFile(), 2);
        }
    }
}
 
示例24
@Before
public void setUp() throws IOException {
    tempQueueDir = IOTools.createTempDirectory("unitTestQueueDir");
}
 
示例25
@Test
    @Ignore("TODO FIX")
    public void testWithAsQueueService() {
        // acts as three processes in one test
        // process A writes to the HelloWorld interface.
        // process B read fromt he HelloWorld interface and writes to the
        String input = OS.TARGET + "/input-" + System.nanoTime();
        String output = OS.TARGET + "/output-" + System.nanoTime();

        HelloReplier replier = createMock(HelloReplier.class);
        replier.reply("Hello April");
        replier.reply("Hello June");
        replay(replier);

        ServiceWrapperBuilder<HelloReplier> builder = ServiceWrapperBuilder
                .serviceBuilder(input, output, HelloReplier.class, HelloWorldImpl::new)
                .inputSourceId(1).outputSourceId(2);

        try (CloseableHelloWorld helloWorld = builder.inputWriter(CloseableHelloWorld.class);
             MethodReader replyReader = builder.outputReader(replier);
             ServiceWrapper helloWorldService = builder.get()) {

            helloWorld.hello("April");
            helloWorld.hello("June");

//            System.out.println(helloWorldService.inputQueues()[0].dump());
            for (int i = 0; i < 2; i++) {
                while (!replyReader.readOne()) {
                    Thread.yield();
                }
            }
//            System.out.println(helloWorldService.outputQueue().dump());
            verify(replier);
        } finally {
            builder.closeQueues();
            try {
                IOTools.deleteDirWithFiles(new File(input), 2);
                IOTools.deleteDirWithFiles(new File(output), 2);
            } catch (IORuntimeException e) {
                e.printStackTrace();
            }
        }
    }
 
示例26
@SuppressWarnings("deprecation")
public static void main(String[] args) {
    String base = path + "/delete-" + System.nanoTime() + ".me";
    long start = System.nanoTime();
    long count = 0;
    nbs = NativeBytesStore.nativeStoreWithFixedCapacity(size);

    long blockSize = OS.is64Bit() ? 4L << 30 : 256L << 20;
    try (ChronicleQueue q = SingleChronicleQueueBuilder.binary(base)
            .rollCycle(RollCycles.LARGE_HOURLY_XSPARSE)
            .blockSize(blockSize)
            .build()) {

        ExcerptAppender appender = q.acquireAppender();
        count += appender.batchAppend(time * 1000, ThroughputPerfMain2::writeMessages);
    }

    nbs.releaseLast();
    long mid = System.nanoTime();
    long time1 = mid - start;

    Bytes bytes = Bytes.allocateElasticDirect(64);
    try (ChronicleQueue q = SingleChronicleQueueBuilder.binary(base)
            .rollCycle(RollCycles.LARGE_HOURLY_XSPARSE)
            .blockSize(blockSize)
            .build()) {
        ExcerptTailer tailer = q.createTailer();
        for (long i = 0; i < count; i++) {
            try (DocumentContext dc = tailer.readingDocument()) {
                bytes.clear();
                bytes.write(dc.wire().bytes());
            }
        }
    }
    bytes.releaseLast();
    long end = System.nanoTime();
    long time2 = end - mid;

    System.out.printf("Writing %,d messages took %.3f seconds, at a rate of %,d per second%n",
            count, time1 / 1e9, (long) (1e9 * count / time1));
    System.out.printf("Reading %,d messages took %.3f seconds, at a rate of %,d per second%n",
            count, time2 / 1e9, (long) (1e9 * count / time2));

    System.gc(); // make sure its cleaned up for windows to delete.
    IOTools.deleteDirWithFiles(base, 2);
}
 
示例27
@Test
public void testCompleteHeader() throws FileNotFoundException {
    // too many hacks are required to make the (artificial) code below release resources correctly
    AbstractCloseable.disableCloseableTracing();

    @NotNull File dir = DirectoryUtils.tempDir("testCompleteHeader");
    dir.mkdirs();

    @NotNull MappedBytes bytes = MappedBytes.mappedBytes(new File(dir, "19700101" + SingleChronicleQueue.SUFFIX),
            ChronicleQueue.TEST_BLOCK_SIZE * 2);
    @NotNull Wire wire = new BinaryWire(bytes);
    try (DocumentContext dc = wire.writingDocument(true)) {
        dc.wire().writeEventName(() -> "header").typePrefix(SingleChronicleQueueStore.class).marshallable(w -> {
            w.write(() -> "wireType").object(WireType.BINARY);
            w.write(() -> "writePosition").int64forBinding(0);
            w.write(() -> "roll").typedMarshallable(new SCQRoll(RollCycles.TEST4_DAILY, 0, null, null));
            w.write(() -> "indexing").typedMarshallable(new SCQIndexing(WireType.BINARY, 32, 4));
            w.write(() -> "lastAcknowledgedIndexReplicated").int64forBinding(0);
        });
    }

    assertEquals("--- !!meta-data #binary\n" +
            "header: !SCQStore {\n" +
            "  wireType: !WireType BINARY,\n" +
            "  writePosition: 0,\n" +
            "  roll: !SCQSRoll {\n" +
            "    length: !int 86400000,\n" +
            "    format: yyyyMMdd'T4',\n" +
            "    epoch: 0\n" +
            "  },\n" +
            "  indexing: !SCQSIndexing {\n" +
            "    indexCount: 32,\n" +
            "    indexSpacing: 4,\n" +
            "    index2Index: 0,\n" +
            "    lastIndex: 0\n" +
            "  },\n" +
            "  lastAcknowledgedIndexReplicated: 0\n" +
            "}\n", Wires.fromSizePrefixedBlobs(bytes.readPosition(0)));
    bytes.releaseLast();

    try (@NotNull ChronicleQueue queue = binary(dir)
            .rollCycle(RollCycles.TEST4_DAILY)
            .testBlockSize()
            .build()) {
        testQueue(queue);
    }

    try {
        IOTools.shallowDeleteDirWithFiles(dir.getAbsolutePath());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
示例28
@Test
    public void testCompleteHeader2() throws FileNotFoundException {
        @NotNull File dir = new File(OS.TARGET, getClass().getSimpleName() + "-" + System.nanoTime());
        dir.mkdir();

        @NotNull MappedBytes bytes = MappedBytes.mappedBytes(new File(dir, "19700101-02" + SingleChronicleQueue.SUFFIX), ChronicleQueue.TEST_BLOCK_SIZE * 2);
        @NotNull Wire wire = new BinaryWire(bytes);
        try (final SingleChronicleQueueStore store = new SingleChronicleQueueStore(RollCycles.HOURLY, WireType.BINARY, bytes, 4 << 10, 4)) {
            try (DocumentContext dc = wire.writingDocument(true)) {

                dc.wire().write("header").typedMarshallable(store);
            }
            assertEquals("--- !!meta-data #binary\n" +
                    "header: !SCQStore {\n" +
                    "  writePosition: [\n" +
                    "    0,\n" +
                    "    0\n" +
                    "  ],\n" +
                    "  indexing: !SCQSIndexing {\n" +
                    "    indexCount: !short 4096,\n" +
                    "    indexSpacing: 4,\n" +
                    "    index2Index: 0,\n" +
                    "    lastIndex: 0\n" +
                    "  },\n" +
                    "  dataFormat: 1\n" +
                    "}\n", Wires.fromSizePrefixedBlobs(bytes.readPosition(0)));
        }

@NotNull RollingChronicleQueue queue = binary(dir)
        .testBlockSize()
        .rollCycle(RollCycles.HOURLY)
        .build();
        testQueue(queue);
        assertEquals(2, queue.firstCycle());
        queue.close();
        try {
            IOTools.shallowDeleteDirWithFiles(dir.getAbsolutePath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
示例29
@AfterClass
public static void afterClass() {
    for (File file : pathsToDelete) {
        IOTools.shallowDeleteDirWithFiles(file);
    }
}
 
示例30
@Test
public void toEndAfterWriteTest() {
    File file = DirectoryUtils.tempDir("toEndAfterWriteTest");
    IOTools.shallowDeleteDirWithFiles(file);

    final SetTimeProvider stp = new SetTimeProvider();
    stp.currentTimeMillis(1470757797000L);

    try (ChronicleQueue wqueue = SingleChronicleQueueBuilder
            .binary(file)
            .testBlockSize()
            .rollCycle(RollCycles.TEST_SECONDLY)
            .timeProvider(stp)
            .build()) {
        ExcerptAppender appender = wqueue.acquireAppender();

        for (int i = 0; i < 10; i++) {
            try (DocumentContext dc = appender.writingDocument()) {
                dc.wire().getValueOut().text("hi-" + i);
                lastCycle = wqueue.rollCycle().toCycle(dc.index());
            }

            stp.currentTimeMillis(stp.currentTimeMillis() + 1000);
        }
    }

    try (ChronicleQueue rqueue = SingleChronicleQueueBuilder
            .binary(file)
            .testBlockSize()
            .rollCycle(RollCycles.TEST_SECONDLY)
            .timeProvider(stp)
            .build()) {

        ExcerptTailer tailer = rqueue.createTailer();
        stp.currentTimeMillis(stp.currentTimeMillis() + 1000);

        //noinspection StatementWithEmptyBody
        while (tailer.readText() != null) ;

        assertNull(tailer.readText());
        stp.currentTimeMillis(stp.currentTimeMillis() + 1000);

        ExcerptTailer tailer1 = rqueue.createTailer();
        ExcerptTailer excerptTailer = tailer1.toEnd();
        assertNull(excerptTailer.readText());
    }
    System.gc();
    pathsToDelete.add(file);
}