// does the callback get fired when the directory // the file is in gets renamed? // TODO: replace with tempfiles. func TestWatchFileRenameDirectory(t *testing.T) { assert := assert.New(t) folder := "/tmp/CANARYTEST" file_name := folder + "/test1.gems" os.Mkdir(folder, 0777) ioutil.WriteFile(file_name, []byte("tst"), 0644) cbInvoked := make(chan bool, 10) mutex := &sync.Mutex{} counter := 0 testcb := func(wfile *WatchedFile) { mutex.Lock() counter++ mutex.Unlock() cbInvoked <- true } wfile := NewWatchedFile(file_name, testcb) // file gets read on hook add wfile.StartListener() defer wfile.StopListening() <-cbInvoked // aight. let's rename the folder it's in. // let's create a tmp path we can rename to. folder2 := "/tmp/CANARYTEST2" os.Rename(folder, folder2) // file should now be missing. time.Sleep(TEST_POLL_SLEEP) assert.False(wfile.GetBeingWatched()) // let's then recreate a new file w/same path // recreate the old folderm os.Mkdir(folder, 0777) // write new file ioutil.WriteFile(file_name, []byte("tst2"), 0644) time.Sleep(TEST_POLL_SLEEP) // this file should be different, thus triggering // another callback mutex.Lock() assert.Equal(2, counter) mutex.Unlock() os.RemoveAll(folder) os.RemoveAll(folder2) }
func TestWatchFileFailure(t *testing.T) { assert := assert.New(t) file_content := "tst1" tf, _ := ioutil.TempFile("", "gems.lock") tf.Write([]byte(file_content)) tf.Close() cbInvoked := make(chan bool) testcb := func(nop *WatchedFile) { cbInvoked <- true } wfile := NewWatchedFile(tf.Name(), testcb) wfile.StartListener() // File is being wartched time.Sleep(TEST_POLL_SLEEP) assert.True(wfile.GetBeingWatched()) os.Remove(tf.Name()) time.Sleep(TEST_POLL_SLEEP) //Since the file is gone, we stopped watching it assert.False(wfile.GetBeingWatched()) wfile.StopListening() }