Java Code Examples for net.openhft.chronicle.core.io.IOTools#shallowDeleteDirWithFiles()

The following examples show how to use net.openhft.chronicle.core.io.IOTools#shallowDeleteDirWithFiles() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SingleCQFormatTest.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
@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());
    }
}
 
Example 2
Source File: ReadWriteTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@After
public void teardown() {
    try {
        IOTools.shallowDeleteDirWithFiles(chroniclePath);
    } catch (Exception e) {
        if (e instanceof AccessDeniedException && OS.isWindows())
            System.err.println(e);
        else
            throw e;
    }
}
 
Example 3
Source File: SingleCQFormatTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 4
Source File: SingleCQFormatTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@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();
        }
    }
}
 
Example 5
Source File: ToEndTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@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());
    }
}
 
Example 6
Source File: ToEndTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 7
Source File: SingleCQFormatTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@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();
    }
}
 
Example 8
Source File: SingleCQFormatTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@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();
        }
    }
 
Example 9
Source File: ToEndTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@AfterClass
public static void afterClass() {
    for (File file : pathsToDelete) {
        IOTools.shallowDeleteDirWithFiles(file);
    }
}
 
Example 10
Source File: ToEndTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@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);
}