<commit_before>package com.tesco.mewbase.log.impl.file;

import com.tesco.mewbase.MewbaseTestBase;
import com.tesco.mewbase.bson.BsonObject;
import com.tesco.mewbase.client.MewException;
import com.tesco.mewbase.log.Log;
import com.tesco.mewbase.log.impl.file.faf.AFFileAccess;
import com.tesco.mewbase.util.AsyncResCF;
import io.vertx.core.buffer.Buffer;
import io.vertx.ext.unit.TestContext;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.BooleanSupplier;
import java.util.function.Function;

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

/**
 * Created by tim on 14/10/16.
 */
public class LogTestBase extends MewbaseTestBase {

    protected FileAccess faf;
    protected FileLogManagerOptions options;
    protected FileLogManager flm;
    protected Log log;
    protected File logDir;

    @Override
    protected void setup(TestContext context) throws Exception {
        super.setup(context);
        logDir = testFolder.newFolder();
        faf = new AFFileAccess(vertx);
    }

    @Override
    protected void tearDown(TestContext context) throws Exception {
        if (log != null) {
            log.close().thenCompose(v -> {
                AsyncResCF<Void> cf = new AsyncResCF<>();
                vertx.close(cf);
                return cf;
            }).get();
        }
    }

    protected FileLogManagerOptions getOptions() {
        return new FileLogManagerOptions().setLogDir(logDir.getPath());
    }

    protected void startLog() throws Exception {
        if (options == null) {
            options = getOptions();
        }
        startLog(options, TEST_CHANNEL_1);
    }

    protected void startLog(FileLogManagerOptions options, String channel) throws Exception {
        flm = new FileLogManager(vertx, options, faf);
        flm.createLog(TEST_CHANNEL_1).thenCompose(l -> flm.createLog(TEST_CHANNEL_2)).get();
        log = flm.getLog(channel);
        log.start().get();
    }

    protected void saveInfo(int fileNumber, int headPos, int fileHeadPos, boolean shutdown) {
        BsonObject info = new BsonObject();
        info.put("fileNumber", fileNumber);
        info.put("headPos", headPos);
        info.put("fileHeadPos", fileHeadPos);
        info.put("shutdown", shutdown);<commit_msg>Prevent file log tests from starting the log twice<commit_after>package com.tesco.mewbase.log.impl.file;

import com.tesco.mewbase.MewbaseTestBase;
import com.tesco.mewbase.bson.BsonObject;
import com.tesco.mewbase.client.MewException;
import com.tesco.mewbase.log.Log;
import com.tesco.mewbase.log.impl.file.faf.AFFileAccess;
import com.tesco.mewbase.util.AsyncResCF;
import io.vertx.core.buffer.Buffer;
import io.vertx.ext.unit.TestContext;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.BooleanSupplier;
import java.util.function.Function;

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

/**
 * Created by tim on 14/10/16.
 */
public class LogTestBase extends MewbaseTestBase {

    protected FileAccess faf;
    protected FileLogManagerOptions options;
    protected FileLogManager flm;
    protected Log log;
    protected File logDir;

    @Override
    protected void setup(TestContext context) throws Exception {
        super.setup(context);
        logDir = testFolder.newFolder();
        faf = new AFFileAccess(vertx);
    }

    @Override
    protected void tearDown(TestContext context) throws Exception {
        if (log != null) {
            log.close().thenCompose(v -> {
                AsyncResCF<Void> cf = new AsyncResCF<>();
                vertx.close(cf);
                return cf;
            }).get();
        }
    }

    protected FileLogManagerOptions getOptions() {
        return new FileLogManagerOptions().setLogDir(logDir.getPath());
    }

    protected void startLog() throws Exception {
        if (options == null) {
            options = getOptions();
        }
        startLog(options, TEST_CHANNEL_1);
    }

    protected void startLog(FileLogManagerOptions options, String channel) throws Exception {
        flm = new FileLogManager(vertx, options, faf);
        flm.createLog(TEST_CHANNEL_1).thenCompose(l -> flm.createLog(TEST_CHANNEL_2)).get();
        log = flm.getLog(channel);
    }

    protected void saveInfo(int fileNumber, int headPos, int fileHeadPos, boolean shutdown) {
        BsonObject info = new BsonObject();
        info.put("fileNumber", fileNumber);
        info.put("headPos", headPos);
        info.put("fileHeadPos", fileHeadPos);
        info.put("shutdown", shutdown);
{'fraction_non_alphanumeric': 0.10727272727272727, 'fraction_numerical': 0.0036363636363636364, 'mean_word_length': 3.1745362563237776, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5395015', 'n_tokens_mistral': 1500, 'n_tokens_neox': 1471, 'n_words': 320}