Exemple #1
0
func buildFileMonitor(fileChangeDelay time.Duration) (filemonitor.FileMonitor, error) {
	if portStr := os.Getenv(listenerPortVar); portStr != "" {
		port, err := strconv.Atoi(portStr)
		if err != nil {
			return nil, fmt.Errorf("%s must be an integer or empty string: %v", listenerPortVar, err)
		}

		ln, err := net.ListenTCP("tcp", &net.TCPAddr{
			IP:   net.ParseIP("127.0.0.1"),
			Port: port,
		})
		if err != nil {
			return nil, err
		}

		return filemonitor.NewFileListener(fileChangeDelay, ln), nil
	}

	monitor, err := filemonitor.NewFileMonitor(fileChangeDelay)
	if err != nil {
		return nil, err
	}

	return monitor, nil
}
Exemple #2
0
func TestFileMonitor(t *testing.T) {
	dir, err := ioutil.TempDir("", "zeus_test")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(dir)

	files, err := writeTestFiles(dir)
	if err != nil {
		t.Fatal(err)
	}

	fm, err := filemonitor.NewFileMonitor(filemonitor.DefaultFileChangeDelay)
	if err != nil {
		t.Fatal(err)
	}
	defer fm.Close()

	changeCh := fm.Listen()

	// Without a short sleep we get notified for the original
	// file creation using FSEvents
	time.Sleep(20 * time.Millisecond)

	watched := files[0:2]
	for i, file := range watched {
		if err := fm.Add(file); err != nil {
			t.Fatalf("%d: %v", i, err)
		}
	}

	// Changing a file emits only that filename
	if err := ioutil.WriteFile(files[0], []byte("bar"), 0); err != nil {
		t.Fatal(err)
	}

	if err := expectChanges(changeCh, files[0:1]); err != nil {
		t.Fatal(err)
	}

	// Changing all files emits watched filenames together
	for i, file := range files {
		if err := ioutil.WriteFile(file, []byte("baz"), 0); err != nil {
			t.Fatalf("%d: %v", i, err)
		}
	}

	if err := expectChanges(changeCh, watched); err != nil {
		t.Fatal(err)
	}
}