diff options
| author | Shulhan <ms@kilabit.info> | 2019-05-07 19:12:18 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2019-05-07 19:12:18 +0700 |
| commit | 7fc15e2a463083de7366b75696eae08bcc1a0e55 (patch) | |
| tree | 567c6faaa45b3467e47fa89d3e0370ec49ceff04 | |
| parent | c6fcbd9eba91a0d12e6df5601da0d53cd621dc2f (diff) | |
| download | pakakeh.go-7fc15e2a463083de7366b75696eae08bcc1a0e55.tar.xz | |
io: fix data race on testing DirWatcher
| -rw-r--r-- | lib/io/dirwatcher_test.go | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/lib/io/dirwatcher_test.go b/lib/io/dirwatcher_test.go index a891eba8..5753a555 100644 --- a/lib/io/dirwatcher_test.go +++ b/lib/io/dirwatcher_test.go @@ -11,6 +11,7 @@ import ( "os" "path/filepath" "sync" + "sync/atomic" "testing" "time" ) @@ -61,7 +62,8 @@ func TestDirWatcher(t *testing.T) { path: filepath.Join(dir, "/assets/new"), }} - x := 0 + var x int32 + dw := &DirWatcher{ Path: dir, Delay: 150 * time.Millisecond, @@ -73,13 +75,14 @@ func TestDirWatcher(t *testing.T) { `.*\.html$`, }, Callback: func(ns *NodeState) { - if exps[x].path != ns.Node.Path { + localx := atomic.LoadInt32(&x) + if exps[localx].path != ns.Node.Path { log.Fatalf("TestDirWatcher got node path %q, want %q\n", ns.Node.Path, exps[x].path) } - if exps[x].state != ns.State { + if exps[localx].state != ns.State { log.Fatalf("TestDirWatcher got state %d, want %d\n", ns.State, exps[x].state) } - x++ + atomic.AddInt32(&x, 1) wg.Done() }, } |
