github.com/Cloud-Foundations/Dominator@v0.3.4/lib/fsutil/waitFile_test.go (about) 1 package fsutil 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path" 7 "testing" 8 "time" 9 ) 10 11 func TestWaitFile(t *testing.T) { 12 dirname, err := ioutil.TempDir("", "WaitFileTests") 13 if err != nil { 14 t.Error(err) 15 } 16 defer os.RemoveAll(dirname) 17 pathNotExist := path.Join(dirname, "never-exists") 18 rc, err := WaitFile(pathNotExist, time.Microsecond) 19 if err == nil { 20 t.Errorf("Expected timeout error for non-existent file") 21 rc.Close() 22 } 23 pathExists := path.Join(dirname, "exists") 24 file, err := os.Create(pathExists) 25 if err != nil { 26 t.Error(err) 27 } 28 file.Close() 29 rc, err = WaitFile(pathExists, time.Microsecond) 30 if err != nil { 31 t.Error(err) 32 } else { 33 rc.Close() 34 } 35 pathExistsLater := path.Join(dirname, "exists-later") 36 go func() { 37 time.Sleep(time.Millisecond * 50) 38 file, err := os.Create(pathExistsLater) 39 if err != nil { 40 t.Error(err) 41 return 42 } 43 file.Close() 44 }() 45 rc, err = WaitFile(pathExistsLater, time.Millisecond*10) 46 if err == nil { 47 rc.Close() 48 t.Errorf("Expected timeout error for non-existent file") 49 } 50 rc, err = WaitFile(pathExistsLater, time.Millisecond*90) 51 if err != nil { 52 t.Error(err) 53 } else { 54 rc.Close() 55 } 56 }