package net.openhft.chronicle.queue.impl.single;

import net.openhft.chronicle.core.OS;
import net.openhft.chronicle.core.io.IOTools;
import net.openhft.chronicle.queue.ChronicleQueue;
import net.openhft.chronicle.queue.ExcerptAppender;
import net.openhft.chronicle.queue.ExcerptTailer;
import net.openhft.chronicle.queue.impl.RollingChronicleQueue;
import net.openhft.chronicle.wire.WireType;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * Created by daniel on 07/03/2016.
 */
public class ToEndTest {
    @Test
    public void toEndTest() {
        String baseDir = OS.TARGET + "/toEndTest";
        System.out.println(baseDir);
        IOTools.shallowDeleteDirWithFiles(baseDir);
        List<Integer> results = new ArrayList<>();
        RollingChronicleQueue queue = new SingleChronicleQueueBuilder(baseDir)
                .wireType(WireType.BINARY)
                .blockSize(4 << 20)
                .indexCount(8)
                .indexSpacing(1)
                .build();

        checkOneFile(baseDir);
        ExcerptAppender appender = queue.createAppender();
        checkOneFile(baseDir);

        for (int i = 0; i < 10; i++) {
            final int j = i;
            appender.writeDocument(wire -> wire.write(() -> "msg").int32(j));
        }

        checkOneFile(baseDir);

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

        tailer.toEnd();
        assertEquals(10, queue.rollCycle().toSequenceNumber(tailer.index()));
        checkOneFile(baseDir);
        fillResults(tailer, results);
        checkOneFile(baseDir);
        assertEquals(0, results.size());

        tailer.toStart();
        checkOneFile(baseDir);
        fillResults(tailer, results);
        assertEquals(10, results.size());
        checkOneFile(baseDir);
    }

    @Test
    public void toEndBeforeWriteTest() {
        String baseDir = OS.TARGET + "/toEndBeforeWriteTest";
        IOTools.shallowDeleteDirWithFiles(baseDir);

        ChronicleQueue queue = new SingleChronicleQueueBuilder(baseDir).build();
        checkOneFile(baseDir);

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

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

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

        tailer.toEnd();
        //checkOneFile(baseDir);

        tailer2.toEnd();
        checkOneFile(baseDir);

        /*for (int i = 0; i < 10; i++) {
            final int j = i;
            appender.writeDocument(wire -> wire.write(() -> "msg").int32(j));
        }*/
    }

    private void checkOneFile(String baseDir) {
        String[] files = new File(baseDir).list();

        if (files == null || files.length == 0)
            return;

        if (files.length == 1)
            assertTrue(files[0], files[0].startsWith("2"));
        else
            fail("Too many files " + Arrays.toString(files));
    }

    @NotNull
    private List<Integer> fillResults(ExcerptTailer tailer, List<Integer> results) {
        for (int i = 0; i < 10; i++) {
            if (!tailer.readDocument(wire -> results.add(wire.read(() -> "msg").int32())))
                break;
        }
        return results;
    }
}

@@ -347,52 +347,8 @@
 ue;%0A
-import net.openhft.chronicle.wire.WireType;%0A
 impo
@@ -385,16 +385,16 @@
 otNull;%0A
+
 import o
@@ -792,25 +792,46 @@
  %22/toEndTest
-%22
+-%22 + System.nanoTime()
 ;%0A        Sy
@@ -1043,87 +1043,8 @@
 ir)%0A
-                .wireType(WireType.BINARY)%0A                .blockSize(4 %3C%3C 20)%0A
     
@@ -1933,24 +1933,77 @@
 le(baseDir);
+%0A%0A        IOTools.shallowDeleteDirWithFiles(baseDir);
 %0A    %7D%0A%0A    
@@ -2876,32 +2876,32 @@
 sg%22).int32(j));%0A
-
         %7D*/%0A    
@@ -2896,16 +2896,68 @@
     %7D*/%0A
+        IOTools.shallowDeleteDirWithFiles(baseDir);%0A
     %7D%0A%0A 

Test whether there is too many files