/*
 * Copyright 2016 higherfrequencytrading.com
 *
 * 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.queue.impl.single;

import net.openhft.chronicle.bytes.ref.BinaryLongReference;
import net.openhft.chronicle.queue.ExcerptAppender;
import net.openhft.chronicle.queue.ExcerptTailer;
import net.openhft.chronicle.queue.impl.RollingChronicleQueue;
import net.openhft.chronicle.wire.DocumentContext;
import net.openhft.chronicle.wire.WireType;
import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

import static net.openhft.chronicle.queue.ChronicleQueueTestBase.getTmpDir;


public class NotCompleteTest {

    private static final int TIMES = 100;


    /**
     * tests that when flags are set to not complete we are able to recover
     */
    @Test
    public void testUsingANotCompleteQueue() throws TimeoutException, ExecutionException,
            InterruptedException {

        BinaryLongReference.startCollecting();

        File tmpDir = getTmpDir();
        try (final RollingChronicleQueue queue = new SingleChronicleQueueBuilder(tmpDir)
                .wireType(WireType.BINARY)
                .build()) {

            ExcerptAppender appender = queue.createAppender();

            try (DocumentContext documentContext = appender.writingDocument()) {
                documentContext.wire().write("some").text("data");
            }

            Thread.sleep(100);

            System.out.println(queue.dump());
        }

        // this is what will corrupt the queue
        BinaryLongReference.forceAllToNotCompleteState();

        try (final RollingChronicleQueue queue = new SingleChronicleQueueBuilder(tmpDir)
                .wireType(WireType.BINARY)
                .timeoutMS(500)
                .build()) {
            System.out.println(queue.dump());

            ExcerptTailer tailer = queue.createTailer();

            try (DocumentContext documentContext = tailer.readingDocument()) {
                Assert.assertEquals("data", documentContext.wire().read(() -> "some").text());
            }
        }
    }
}
@@ -654,16 +654,81 @@
 ingle;%0A%0A
+import net.openhft.chronicle.bytes.ref.BinaryLongArrayReference;%0A
 import n
@@ -771,32 +771,32 @@
 yLongReference;%0A
-
 import net.openh
@@ -877,24 +877,71 @@
 erptTailer;%0A
+import net.openhft.chronicle.queue.RollCycles;%0A
 import net.o
@@ -1840,32 +1840,82 @@
 ireType.BINARY)%0A
+                .rollCycle(RollCycles.TEST_DAILY)%0A
                 
@@ -2290,16 +2290,16 @@
 e queue%0A
-
         
@@ -2642,24 +2642,1393 @@
 eTailer();%0A%0A
+            try (DocumentContext documentContext = tailer.readingDocument()) %7B%0A                Assert.assertEquals(%22data%22, documentContext.wire().read(() -%3E %22some%22).text());%0A            %7D%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void testUsingANotCompleteArrayQueue() throws TimeoutException, ExecutionException,%0A            InterruptedException %7B%0A%0A        BinaryLongArrayReference.startCollecting();%0A%0A        File tmpDir = getTmpDir();%0A        try (final RollingChronicleQueue queue = new SingleChronicleQueueBuilder(tmpDir)%0A                .wireType(WireType.BINARY)%0A                .rollCycle(RollCycles.TEST_DAILY)%0A                .build()) %7B%0A%0A            ExcerptAppender appender = queue.createAppender();%0A%0A            try (DocumentContext documentContext = appender.writingDocument()) %7B%0A                documentContext.wire().write(%22some%22).text(%22data%22);%0A            %7D%0A%0A            Thread.sleep(100);%0A%0A            System.out.println(queue.dump());%0A        %7D%0A%0A        // this is what will corrupt the queue%0A        BinaryLongArrayReference.forceAllToNotCompleteState();%0A%0A        try (final RollingChronicleQueue queue = new SingleChronicleQueueBuilder(tmpDir)%0A                .wireType(WireType.BINARY)%0A                .timeoutMS(500)%0A                .build()) %7B%0A            System.out.println(queue.dump());%0A%0A            ExcerptTailer tailer = queue.createTailer();%0A%0A
             

Fix a bug where it doesn't recover on failure while creating the index2index