File: watcher_test.go

package info (click to toggle)
golang-github-syncthing-notify 0.0~git20190709.69c7a95-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 436 kB
  • sloc: makefile: 3
file content (86 lines) | stat: -rw-r--r-- 2,249 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (c) 2014-2015 The Notify Authors. All rights reserved.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

// +build darwin linux freebsd dragonfly netbsd openbsd windows solaris

package notify

import (
	"os"
	"testing"
	"time"
)

// NOTE Set NOTIFY_DEBUG env var or debug build tag for extra debugging info.

func TestWatcher(t *testing.T) {
	w := NewWatcherTest(t, "testdata/vfs.txt")
	defer w.Close()

	cases := [...]WCase{
		create(w, "src/github.com/ppknap/link/include/coost/.link.hpp.swp"),
		create(w, "src/github.com/rjeczalik/fs/fs_test.go"),
		create(w, "src/github.com/rjeczalik/fs/binfs/"),
		create(w, "src/github.com/rjeczalik/fs/binfs.go"),
		create(w, "src/github.com/rjeczalik/fs/binfs_test.go"),
		remove(w, "src/github.com/rjeczalik/fs/binfs/"),
		create(w, "src/github.com/rjeczalik/fs/binfs/"),
		create(w, "src/github.com/rjeczalik/fs/virfs"),
		remove(w, "src/github.com/rjeczalik/fs/virfs"),
		create(w, "file"),
		create(w, "dir/"),
	}

	w.ExpectAny(cases[:])
}

// Simulates the scenario, where outside of the programs control the base dir
// is removed. This is detected and the watch removed. Then the directory is
// restored and a new watch set up.
func TestStopPathNotExists(t *testing.T) {
	w := NewWatcherTest(t, "testdata/vfs.txt")
	defer w.Close()

	if err := os.RemoveAll(w.root); err != nil {
		panic(err)
	}
	Sync()

	// Don't check the returned error, as the public function (notify.Stop)
	// does not return a potential error. As long as everything later on
	// works as inteded, that's fine
	time.Sleep(time.Duration(100) * time.Millisecond)
	w.Watcher.Unwatch(w.root)
	time.Sleep(time.Duration(100) * time.Millisecond)

	if err := os.Mkdir(w.root, 0777); err != nil {
		panic(err)
	}
	Sync()
	w.Watch("", All)

	drainall(w.C)
	cases := [...]WCase{
		create(w, "file"),
		create(w, "dir/"),
	}
	w.ExpectAny(cases[:])
}

func TestWatcherUnwatch(t *testing.T) {
	w := NewWatcherTest(t, "testdata/vfs.txt")
	defer w.Close()

	remove(w, "src/github.com/ppknap/link/test/test_circular_calls.cpp").Action()
	w.Unwatch("")

	w.Watch("", All)

	drainall(w.C)
	cases := [...]WCase{
		create(w, "file"),
		create(w, "dir/"),
	}
	w.ExpectAny(cases[:])
}