/*
 * Copyright 2014 Higher Frequency Trading
 *
 * http://www.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;

import net.openhft.lang.io.IOTools;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class VanillaFilePerMinuteTest extends VanillaChronicleTestBase {

    static final String FMT = "yyyyMMddHHmm";
    static final int    MIN = 60 * 1000;

    @Test
    public void filePerMinuteTest() throws Exception {
        final String basePath = getTestPath();
        IOTools.deleteDir(basePath);

        LOGGER.info("Basepath is {}", basePath);

        final ExecutorService svc = Executors.newFixedThreadPool(2);
        svc.execute(createWriter(createChronicle(basePath)));
        svc.execute(createReader(createChronicle(basePath)));
        svc.awaitTermination(10, TimeUnit.MINUTES);
    }

    // *************************************************************************
    //
    // *************************************************************************

    static Runnable createReader(final Chronicle chron) {
        return new Runnable() {
            @Override
            public void run() {
                try(ExcerptTailer tailer = chron.createTailer()) {
                    for(int i=0; i<1000;) {
                        if(tailer.nextIndex()) {
                            LOGGER.info("> R : {}", tailer.readInt());
                            i++;
                        } else {
                            Thread.sleep(1000);
                        }
                    }
                } catch (Exception e) {
                    LOGGER.warn("", e);
                }
            }
        };
    }

    static Runnable createWriter(final Chronicle chron) {
        return new Runnable() {
            @Override
            public void run() {
                try(ExcerptAppender appender = chron.createAppender()) {
                    for (int i = 0; i < 1000; i++) {
                        LOGGER.info("> W : {}", i);

                        appender.startExcerpt(4);
                        appender.writeInt(i);
                        appender.finish();

                        Thread.sleep(1000);
                    }
                } catch (Exception e) {
                    LOGGER.warn("", e);
                }
            }
        };
    }

    static Chronicle createChronicle(String basePath) throws IOException {
        return ChronicleQueueBuilder.vanilla(basePath)
            .cycleLength(MIN, false)
            .cycleFormat(FMT)
            .build();
    }
}

@@ -709,16 +709,41 @@
 OTools;%0A
+import org.junit.Ignore;%0A
 import o
@@ -757,16 +757,16 @@
 t.Test;%0A
-
 %0Aimport 
@@ -909,16 +909,24 @@
 eUnit;%0A%0A
+@Ignore%0A
 public c
@@ -1030,16 +1030,21 @@
 yyyyMMdd
+%22; //
 HHmm%22;%0A 
@@ -1071,16 +1071,26 @@
    MIN =
+ 24 * 60 *
  60 * 10
@@ -1269,17 +1269,18 @@
 fo(%22Base
-p
+ P
 ath is %7B
@@ -3240,16 +3240,98 @@
 at(FMT)%0A
+            //.entriesPerCycle(1L %3C%3C 20)%0A            //.indexBlockSize(16L %3C%3C 10)%0A
         

test case for cycle length/format