Skip to content

Commit 05dee28

Browse files
authored
Merge pull request #3 from ploxiln/watcher_test_deflake
tests: watcher test deflake
2 parents 9417698 + b42b27e commit 05dee28

6 files changed

+27
-35
lines changed

validator.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ func (um *UserMap) LoadAuthenticatedEmailsFile() {
5959
atomic.StorePointer(&um.m, unsafe.Pointer(&updated))
6060
}
6161

62-
func newValidatorImpl(domains []string, usersFile string,
63-
done <-chan bool, onUpdate func()) func(string) bool {
62+
func newValidatorImpl(domains []string, usersFile string, done <-chan bool, onUpdate func()) func(string) bool {
6463
validUsers := NewUserMap(usersFile, done, onUpdate)
6564

6665
var allowAll bool

validator_test.go

+9-11
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ func NewValidatorTest(t *testing.T) *ValidatorTest {
2020
if err != nil {
2121
t.Fatal("failed to create temp file: " + err.Error())
2222
}
23-
vt.done = make(chan bool, 1)
23+
t.Logf("%s using temp file %s", t.Name(), vt.auth_email_file.Name())
24+
vt.done = make(chan bool)
2425
return vt
2526
}
2627

@@ -29,20 +30,17 @@ func (vt *ValidatorTest) TearDown() {
2930
os.Remove(vt.auth_email_file.Name())
3031
}
3132

32-
func (vt *ValidatorTest) NewValidator(domains []string,
33-
updated chan<- bool) func(string) bool {
34-
return newValidatorImpl(domains, vt.auth_email_file.Name(),
35-
vt.done, func() {
36-
if vt.update_seen == false {
37-
updated <- true
38-
vt.update_seen = true
39-
}
40-
})
33+
func (vt *ValidatorTest) NewValidator(domains []string, updated chan<- bool) func(string) bool {
34+
return newValidatorImpl(domains, vt.auth_email_file.Name(), vt.done, func() {
35+
if vt.update_seen == false {
36+
updated <- true
37+
vt.update_seen = true
38+
}
39+
})
4140
}
4241

4342
// This will close vt.auth_email_file.
4443
func (vt *ValidatorTest) WriteEmails(t *testing.T, emails []string) {
45-
defer vt.auth_email_file.Close()
4644
vt.auth_email_file.WriteString(strings.Join(emails, "\n"))
4745
if err := vt.auth_email_file.Close(); err != nil {
4846
t.Fatal("failed to close temp file " +

validator_watcher_copy_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build go1.3,!plan9,!solaris,!windows
1+
// +build !plan9,!solaris,!windows
22

33
// Turns out you can't copy over an existing file on Windows.
44

@@ -32,7 +32,7 @@ func TestValidatorOverwriteEmailListViaCopyingOver(t *testing.T) {
3232

3333
vt.WriteEmails(t, []string{"[email protected]"})
3434
domains := []string(nil)
35-
updated := make(chan bool)
35+
updated := make(chan bool, 1)
3636
validator := vt.NewValidator(domains, updated)
3737

3838
if !validator("[email protected]") {

validator_watcher_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build go1.3,!plan9,!solaris
1+
// +build !plan9,!solaris
22

33
package main
44

@@ -49,7 +49,7 @@ func TestValidatorOverwriteEmailListDirectly(t *testing.T) {
4949
5050
})
5151
domains := []string(nil)
52-
updated := make(chan bool)
52+
updated := make(chan bool, 1)
5353
validator := vt.NewValidator(domains, updated)
5454

5555
if !validator("[email protected]") {

watcher.go

+12-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build go1.3,!plan9,!solaris
1+
// +build !plan9,!solaris
22

33
package main
44

@@ -11,23 +11,18 @@ import (
1111
"github.com/fsnotify/fsnotify"
1212
)
1313

14-
func WaitForReplacement(filename string, op fsnotify.Op,
15-
watcher *fsnotify.Watcher) {
16-
const sleep_interval = 50 * time.Millisecond
14+
func WaitForReplacement(filename string, watcher *fsnotify.Watcher) {
15+
for i := 0; i < 20; i++ {
16+
time.Sleep(100 * time.Millisecond)
1717

18-
// Avoid a race when fsnofity.Remove is preceded by fsnotify.Chmod.
19-
if op&fsnotify.Chmod != 0 {
20-
time.Sleep(sleep_interval)
21-
}
22-
for {
2318
if _, err := os.Stat(filename); err == nil {
2419
if err := watcher.Add(filename); err == nil {
2520
log.Printf("watching resumed for %s", filename)
2621
return
2722
}
2823
}
29-
time.Sleep(sleep_interval)
3024
}
25+
log.Printf("failed to resume watching for %s", filename)
3126
}
3227

3328
func WatchForUpdates(filename string, done <-chan bool, action func()) {
@@ -37,22 +32,22 @@ func WatchForUpdates(filename string, done <-chan bool, action func()) {
3732
log.Fatal("failed to create watcher for ", filename, ": ", err)
3833
}
3934
go func() {
40-
defer watcher.Close()
4135
for {
4236
select {
4337
case _ = <-done:
4438
log.Printf("Shutting down watcher for: %s", filename)
45-
break
39+
watcher.Close()
40+
return
4641
case event := <-watcher.Events:
4742
// On Arch Linux, it appears Chmod events precede Remove events,
4843
// which causes a race between action() and the coming Remove event.
49-
// If the Remove wins, the action() (which calls
50-
// UserMap.LoadAuthenticatedEmailsFile()) crashes when the file
51-
// can't be opened.
52-
if event.Op&(fsnotify.Remove|fsnotify.Rename|fsnotify.Chmod) != 0 {
44+
if event.Op == fsnotify.Chmod {
45+
continue
46+
}
47+
if event.Op&(fsnotify.Remove|fsnotify.Rename) != 0 {
5348
log.Printf("watching interrupted on event: %s", event)
5449
watcher.Remove(filename)
55-
WaitForReplacement(filename, event.Op, watcher)
50+
WaitForReplacement(filename, watcher)
5651
}
5752
log.Printf("reloading after event: %s", event)
5853
action()

watcher_unsupported.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build !go1.3 plan9 solaris
1+
// +build plan9 solaris
22

33
package main
44

0 commit comments

Comments
 (0)