<commit_before>/*-
 * Copyright 2015 Square Inc.
 *
 * 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 main

import (
	"io/ioutil"
	"os"
	"testing"
	"time"
)

func TestWatchers(t *testing.T) {
	// Setup
	tmpDir, err := ioutil.TempDir("", "ghostunnel-test")
	panicOnError(err)

	tmpFile, err := ioutil.TempFile(tmpDir, "")
	tmpFile.WriteString("test")
	tmpFile.Sync()
	defer os.Remove(tmpFile.Name())

	// Start watching
	watcherAuto := make(chan bool, 1)
	watcherTimed := make(chan bool, 1)

	go watchAuto([]string{tmpFile.Name()}, watcherAuto)
	go watchTimed([]string{tmpFile.Name()}, time.Duration(10)*time.Millisecond, watcherTimed)

	time.Sleep(time.Duration(3) * time.Second)

	// Must detect new writes
	tmpFile.WriteString("new")
	tmpFile.Sync()
	tmpFile.Close()

	select {
	case _ = <-watcherTimed:
	case _ = <-time.Tick(time.Duration(1) * time.Second):
		t.Fatalf("timeout, no notification on changed file")
	}

	select {
	case _ = <-watcherAuto:
	case _ = <-time.Tick(time.Duration(1) * time.Second):
		t.Fatalf("timeout, no notification on changed file")
	}
}<commit_msg>Add tests file rm/create<commit_after>/*-
 * Copyright 2015 Square Inc.
 *
 * 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 main

import (
	"io/ioutil"
	"os"
	"testing"
	"time"
)

func TestWatchers(t *testing.T) {
	// Setup
	tmpDir, err := ioutil.TempDir("", "ghostunnel-test")
	panicOnError(err)

	tmpFile, err := ioutil.TempFile(tmpDir, "")
	tmpFile.WriteString("test")
	tmpFile.Sync()
	defer os.Remove(tmpFile.Name())

	// Start watching
	watcherAuto := make(chan bool, 1)
	watcherTimed := make(chan bool, 1)

	go watchAuto([]string{tmpFile.Name()}, watcherAuto)
	go watchTimed([]string{tmpFile.Name()}, time.Duration(10)*time.Millisecond, watcherTimed)

	time.Sleep(time.Duration(3) * time.Second)

	// Must detect new writes
	tmpFile.WriteString("new")
	tmpFile.Sync()
	tmpFile.Close()

	select {
	case _ = <-watcherTimed:
	case _ = <-time.Tick(time.Duration(1) * time.Second):
		t.Fatalf("timeout, no notification on changed file")
	}

	select {
	case _ = <-watcherAuto:
	case _ = <-time.Tick(time.Duration(1) * time.Second):
		t.Fatalf("timeout, no notification on changed file")
	}

	// Must detect file being replaced
	os.Remove(tmpFile.Name())
	tmpFile, err = os.Create(tmpFile.Name())
	panicOnError(err)

	tmpFile.WriteString("blubb")
	tmpFile.Sync()
	tmpFile.Close()

	select {
	case _ = <-watcherTimed:
	case _ = <-time.Tick(time.Duration(1) * time.Second):
		t.Fatalf("timeout, no notification on changed file")
	}

	select {
	case _ = <-watcherAuto:
	case _ = <-time.Tick(time.Duration(1) * time.Second):
		t.Fatalf("timeout, no notification on changed file")
	}
}
{'fraction_non_alphanumeric': 0.13416283473086285, 'fraction_numerical': 0.008655666756829862, 'mean_word_length': 4.121883656509695, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6462431', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1214, 'n_words': 352}