forked from syncthing/syncthing-inotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncwatcher_test.go
214 lines (198 loc) · 6.32 KB
/
syncwatcher_test.go
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// syncwatcher_test.go
package main
import (
"testing"
"time"
"os"
"strconv"
)
var (
slash = string(os.PathSeparator)
)
func TestDebouncedFileWatch(t *testing.T) {
// Log file change
testOK := false
testRepo := "test1"
testDirectory := getHomeDir() + slash + "Sync"
testFile := "a"+slash+"file1"
testDebounceTimeout := 2 * time.Millisecond
testDirVsFiles := 10
fileChange := func(repo string, sub string) {
if repo != testRepo || sub != testFile {
t.Error("Invalid result for file change: "+repo+" "+sub)
}
testOK = true
}
informChangeDebounced := informChangeDebounce(testDebounceTimeout, testRepo, testDirectory, testDirVsFiles, fileChange)
informChangeDebounced(testDirectory+slash+testFile)
time.Sleep(testDebounceTimeout*2)
if !testOK {
t.Error("Callback not triggered")
}
}
func TestDebouncedDirectoryWatch(t *testing.T) {
// Log directory change
testOK := false
testRepo := "test1"
testDirectory := getHomeDir() + slash + "Sync"
testFile := "a"+slash
testDebounceTimeout := 2 * time.Millisecond
testDirVsFiles := 10
fileChange := func(repo string, sub string) {
if repo != testRepo || sub+slash != testFile {
t.Error("Invalid result for directory change: "+repo+" "+sub)
}
testOK = true
}
informChangeDebounced := informChangeDebounce(testDebounceTimeout, testRepo, testDirectory, testDirVsFiles, fileChange)
informChangeDebounced(testDirectory+slash+testFile)
time.Sleep(testDebounceTimeout*2)
if !testOK {
t.Error("Callback not triggered")
}
}
func TestDebouncedParentDirectoryWatch(t *testing.T) {
// Convert a/file1.txt a/file2 a/file3.ogg to a
testOK := false
testRepo := "test1"
testDirectory := getHomeDir() + slash + "Sync"
testChangeDir := "a"+slash
testFiles := [...]string{ testChangeDir+"file1.txt", testChangeDir+"file2", testChangeDir+"file3.ogg" }
testDebounceTimeout := 20 * time.Millisecond
testDirVsFiles := 3
fileChange := func(repo string, sub string) {
if repo != testRepo || sub != "a" {
t.Error("Invalid result for directory change: "+repo+" "+sub)
}
testOK = true
}
informChangeDebounced := informChangeDebounce(testDebounceTimeout, testRepo, testDirectory, testDirVsFiles, fileChange)
for i := range testFiles {
informChangeDebounced(testDirectory+slash+testFiles[i])
}
time.Sleep(testDebounceTimeout*2)
if !testOK {
t.Error("Callback not triggered")
}
}
func TestDebouncedParentDirectoryWatch2(t *testing.T) {
// Convert a a/file1.txt a/file2 b a/file3.ogg to a b
testOK := 0
testRepo := "test1"
testDirectory := getHomeDir() + slash + "Sync"
testChangeDir1 := "a"+slash
testChangeDir2 := "b"+slash
testFiles := [...]string{ testChangeDir1, testChangeDir1+"file1.txt", testChangeDir1+"file2", testChangeDir2, testChangeDir1+"file3.ogg" }
testDebounceTimeout := 20 * time.Millisecond
testDirVsFiles := 3
fileChange := func(repo string, sub string) {
if testOK == 0 {
if repo != testRepo || sub != "a" {
t.Error("Invalid result for directory change 1: "+repo+" "+sub)
}
} else if testOK == 1 {
if repo != testRepo || sub != "b" {
t.Error("Invalid result for directory change 2: "+repo+" "+sub)
}
}
testOK += 1
}
informChangeDebounced := informChangeDebounce(testDebounceTimeout, testRepo, testDirectory, testDirVsFiles, fileChange)
for i := range testFiles {
informChangeDebounced(testDirectory+slash+testFiles[i])
}
time.Sleep(testDebounceTimeout*2)
if testOK != 2 {
t.Error("Callback not correctly triggered")
}
}
func TestDebouncedParentDirectoryWatch3(t *testing.T) {
// Convert a/b/file1.txt a/c/file2 a/d/file3.ogg to a/b a/c a/d
testOK := 0
testRepo := "test1"
testDirectory := getHomeDir() + slash + "Sync"
testFiles := [...]string{
"a"+slash+"b"+slash+"file1.txt",
"a"+slash+"c"+slash+"file2",
"a"+slash+"d"+slash+"file3.ogg" }
testDebounceTimeout := 20 * time.Millisecond
testDirVsFiles := 3
fileChange := func(repo string, sub string) {
if repo != testRepo || sub != testFiles[testOK] {
t.Error("Invalid result for directory change "+strconv.Itoa(testOK)+": "+repo+" "+sub)
}
testOK += 1
}
informChangeDebounced := informChangeDebounce(testDebounceTimeout, testRepo, testDirectory, testDirVsFiles, fileChange)
for i := range testFiles {
informChangeDebounced(testDirectory+slash+testFiles[i])
}
time.Sleep(testDebounceTimeout*2)
if testOK != 3 {
t.Error("Callback not correctly triggered")
}
}
func TestDebouncedParentDirectoryWatch4(t *testing.T) {
// Convert a/e a/b/d a/b/file1.txt a/b/file2 a/b/file3.ogg a/b/c/file4 to a/b a/e
testOK := 0
testRepo := "test1"
testDirectory := getHomeDir() + slash + "Sync"
testFiles := [...]string{
"a"+slash+"e",
"a"+slash+"b"+slash+"d",
"a"+slash+"b"+slash+"file1.txt",
"a"+slash+"b"+slash+"file2",
"a"+slash+"b"+slash+"file3.ogg",
"a"+slash+"b"+slash+"c"+slash+"file4" }
testDebounceTimeout := 20 * time.Millisecond
testDirVsFiles := 3
fileChange := func(repo string, sub string) {
if testOK == 0 {
if repo != testRepo || sub != "a"+slash+"b" {
t.Error("Invalid result for directory change "+strconv.Itoa(testOK)+": "+repo+" "+sub)
}
}
if testOK == 1 {
if repo != testRepo || sub != "a"+slash+"e" {
t.Error("Invalid result for directory change "+strconv.Itoa(testOK)+": "+repo+" "+sub)
}
}
testOK += 1
}
informChangeDebounced := informChangeDebounce(testDebounceTimeout, testRepo, testDirectory, testDirVsFiles, fileChange)
for i := range testFiles {
informChangeDebounced(testDirectory+slash+testFiles[i])
}
time.Sleep(testDebounceTimeout*2)
if testOK != 2 {
t.Error("Callback not correctly triggered")
}
}
func TestDebouncedParentDirectoryWatch5(t *testing.T) {
// Convert a/b a/c file1 file2 file3 to _ (main folder)
testOK := false
testRepo := "test1"
testDirectory := getHomeDir() + slash + "Sync"
testFiles := [...]string{
"a"+slash+"b",
"a"+slash+"c",
"file1",
"file2",
"file3" }
testDebounceTimeout := 20 * time.Millisecond
testDirVsFiles := 3
fileChange := func(repo string, sub string) {
if repo != testRepo || sub != "" {
t.Error("Invalid result for directory change: "+repo)
}
testOK = true
}
informChangeDebounced := informChangeDebounce(testDebounceTimeout, testRepo, testDirectory, testDirVsFiles, fileChange)
for i := range testFiles {
informChangeDebounced(testDirectory+slash+testFiles[i])
}
time.Sleep(testDebounceTimeout*2)
if !testOK {
t.Error("Callback not correctly triggered")
}
}