From 7fc15e2a463083de7366b75696eae08bcc1a0e55 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Tue, 7 May 2019 19:12:18 +0700 Subject: io: fix data race on testing DirWatcher --- lib/io/dirwatcher_test.go | 11 +++++++---- 1 file 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() }, } -- cgit v1.3