<commit_before>package no.cantara.file.watcher;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import no.cantara.file.util.CommonUtil;
import no.cantara.file.watcher.event.FileWatchEvent;
import no.cantara.file.watcher.support.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileLock;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

import static org.testng.Assert.*;

/**
 * Created by oranheim on 19/10/2016.
 */
public class PathWatchTest {

    private final static Logger log = LoggerFactory.getLogger(PathWatchTest.class);

    @Test(enabled = true)
    public void testFileSystem() {
        log.trace("IsLinux: {}, isLinuxFileSystem: {}, isMacOS: {}, isMacOSFileSystem: {}",
                FileSystemSupport.isLinux(),
                FileSystemSupport.isLinuxFileSystem(),
                FileSystemSupport.isMacOS(),
                FileSystemSupport.isMacOSFileSystem());;
        log.trace("hasJDKEventDrivenFileSystem: {}", FileSystemSupport.hasJDKEventDrivenFileSystem());
    }

    public static class CreatedHandler implements FileWatchHandler {
        @Override
        public void invoke(FileWatchEvent event) {
            Path file = event.getFile();
            log.trace("OnCreatedFileAction - Received FileWatchEvent from Consumer: {}", file);
            try {
                if (Files.exists(file))
                    Files.delete(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static class ModifiedHandler implements FileWatchHandler {
        @Override
        public void invoke(FileWatchEvent event) {
            Path file = event.getFile();
            log.trace("OnModifiedFileAction - Received FileWatchEvent from Consumer: {}", file);
        }
    }

    public static class RemovedHandler implements FileWatchHandler {
        @Override
        public void invoke(FileWatchEvent event) {
            Path file = event.getFile();
            log.trace("OnRemovedFileHandler - Received FileWatchEvent from Consumer: {}", file);
        }
    }

    @Test(enabled = true, dependsOnGroups = "pre-test")
    public void testNativeEventsPathWatch() throws Exception {
        if (FileSystemSupport.isMacOS()) return;

        Path currentDir = FileWatchUtils.getCurrentPath();
        Path storeDir = currentDir.resolve("target/watcher");
        Path inboxDir = storeDir.resolve("inbox");
        FileWatchUtils.createDirectories(inboxDir);

        PathWatcher pw = PathWatcher.getInstance();
        //pw.forceFileSystemScannerMode(PathWatchScanner.NATIVE_FILE_SYSTEM); // it always will fallback on appropriate scanner mode

        execPathWatchTest(pw, inboxDir);
    }

    @Test(enabled = true, dependsOnGroups = "pre-test")
    public void testPollEventsPathWatch() throws Exception {
        Path currentDir = FileWatchUtils.getCurrentPath();
        Path storeDir = currentDir.resolve("target/watcher");
        Path inboxDir = storeDir.resolve("inbox");
        FileWatchUtils.createDirectories(inboxDir);

        PathWatcher pw = PathWatcher.getInstance();
        pw.forceFileSystemScannerMode(PathWatchScanner.POLL_FILE_SYSTEM);
        pw.setPathScanInterval(1000);
        pw.setThreadPollInterval(150);
        pw.setWorkerMode(FileCompletionWorkerMode.TIMEOUT);
        pw.setTimeoutOrRetryInterval(500);

        execPathWatchTest(pw, inboxDir);
    }

    private void execPathWatchTest(PathWatcher pw, Path inboxDir) throws IOException, InterruptedException {
        pw.watch(inboxDir);

        pw.registerCreatedHandler(new CreatedHandler());
        pw.registerModifiedHandler(new ModifiedHandler());
        pw.registerRemovedHandler(new RemovedHandler());

        pw.start();
        assertTrue(pw.isRunning());

        String fn = inboxDir.toFile() + "/a1.txt";
        log.trace("FN: {}", fn);
        FileOutputStream fis = new FileOutputStream(fn);
        fis.write("Test 1".getBytes());
        fis.close();

        fn = inboxDir.toFile() + "/a2.txt";
        log.trace("FN: {}", fn);
        fis = new FileOutputStream(fn);
        fis.write("Test 2".getBytes());
        fis.close();

        Thread.sleep(2500);

        log.trace("STOPPING..");
        pw.stop(); // blocking call
        log.trace("STOPPED!");

    }

    @Test(enabled = true, groups = "pre-test")
    public void testCreateUniqueMapToHandleMultipleDiscoveriesOfFiles() throws Exception {
        Multimap<String,String> map =  ArrayListMultimap.create();

        map.put("a", "a");<commit_msg>Fix test to avoid race condition<commit_after>package no.cantara.file.watcher;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import no.cantara.file.util.CommonUtil;
import no.cantara.file.watcher.event.FileWatchEvent;
import no.cantara.file.watcher.support.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileLock;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

import static org.testng.Assert.*;

/**
 * Created by oranheim on 19/10/2016.
 */
public class PathWatchTest {

    private final static Logger log = LoggerFactory.getLogger(PathWatchTest.class);

    @Test(enabled = true)
    public void testFileSystem() {
        log.trace("IsLinux: {}, isLinuxFileSystem: {}, isMacOS: {}, isMacOSFileSystem: {}",
                FileSystemSupport.isLinux(),
                FileSystemSupport.isLinuxFileSystem(),
                FileSystemSupport.isMacOS(),
                FileSystemSupport.isMacOSFileSystem());;
        log.trace("hasJDKEventDrivenFileSystem: {}", FileSystemSupport.hasJDKEventDrivenFileSystem());
    }

    public static class CreatedHandler implements FileWatchHandler {
        @Override
        public void invoke(FileWatchEvent event) {
            Path file = event.getFile();
            log.trace("OnCreatedFileAction - Received FileWatchEvent from Consumer: {}", file);
        }
    }

    public static class ModifiedHandler implements FileWatchHandler {
        @Override
        public void invoke(FileWatchEvent event) {
            Path file = event.getFile();
            log.trace("OnModifiedFileAction - Received FileWatchEvent from Consumer: {}", file);
        }
    }

    public static class RemovedHandler implements FileWatchHandler {
        @Override
        public void invoke(FileWatchEvent event) {
            Path file = event.getFile();
            log.trace("OnRemovedFileHandler - Received FileWatchEvent from Consumer: {}", file);
        }
    }

    @Test(enabled = true, dependsOnGroups = "pre-test")
    public void testNativeEventsPathWatch() throws Exception {
        if (FileSystemSupport.isMacOS()) return;

        Path currentDir = FileWatchUtils.getCurrentPath();
        Path storeDir = currentDir.resolve("target/watcher");
        Path inboxDir = storeDir.resolve("inbox");
        FileWatchUtils.createDirectories(inboxDir);

        PathWatcher pw = PathWatcher.getInstance();
        //pw.forceFileSystemScannerMode(PathWatchScanner.NATIVE_FILE_SYSTEM); // it always will fallback on appropriate scanner mode

        execPathWatchTest(pw, inboxDir);
    }

    @Test(enabled = true, dependsOnGroups = "pre-test")
    public void testPollEventsPathWatch() throws Exception {
        Path currentDir = FileWatchUtils.getCurrentPath();
        Path storeDir = currentDir.resolve("target/watcher");
        Path inboxDir = storeDir.resolve("inbox");
        FileWatchUtils.createDirectories(inboxDir);

        PathWatcher pw = PathWatcher.getInstance();
        pw.forceFileSystemScannerMode(PathWatchScanner.POLL_FILE_SYSTEM);
        pw.setPathScanInterval(1000);
        pw.setThreadPollInterval(150);
        pw.setWorkerMode(FileCompletionWorkerMode.TIMEOUT);
        pw.setTimeoutOrRetryInterval(500);

        execPathWatchTest(pw, inboxDir);
    }

    private void execPathWatchTest(PathWatcher pw, Path inboxDir) throws IOException, InterruptedException {
        pw.watch(inboxDir);

        pw.registerCreatedHandler(new CreatedHandler());
        pw.registerModifiedHandler(new ModifiedHandler());
        pw.registerRemovedHandler(new RemovedHandler());

        pw.start();
        assertTrue(pw.isRunning());

        String fn = inboxDir.toFile() + "/a1.txt";
        log.trace("FN: {}", fn);
        FileOutputStream fis = new FileOutputStream(fn);
        fis.write("Test 1".getBytes());
        fis.close();

        fn = inboxDir.toFile() + "/a2.txt";
        log.trace("FN: {}", fn);
        fis = new FileOutputStream(fn);
        fis.write("Test 2".getBytes());
        fis.close();

        Thread.sleep(2500);

        Files.list(inboxDir).forEach(filePath -> {
            try {
                Files.deleteIfExists(filePath);
            } catch (IOException ioe) {
                //
            }
        });

        Thread.sleep(1000);

        log.trace("STOPPING..");
        pw.stop(); // blocking call
        log.trace("STOPPED!");

    }

    @Test(enabled = true, groups = "pre-test")
    public void testCreateUniqueMapToHandleMultipleDiscoveriesOfFiles() throws Exception {
        Multimap<String,String> map =  ArrayListMultimap.create();

        map.put("a", "a");
{'fraction_non_alphanumeric': 0.11187537932429699, 'fraction_numerical': 0.00606918875177018, 'mean_word_length': 2.981876761981474, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '3629527', 'n_tokens_mistral': 2828, 'n_tokens_neox': 2699, 'n_words': 533}