public void TestEnableRaisingEvents() { using (Watcher watcher = new Watcher(TestFolder)) { CreateTestFile(1); WaitWatcher(); Assert.AreEqual(0, watcher.GetChangeList().Count); watcher.EnableRaisingEvents = true; CreateTestFile(2); string name = GetPathname(); WaitWatcher(40000,watcher,1); Assert.AreEqual(1, watcher.GetChangeList().Count); Assert.AreEqual(name, watcher.GetChangeList()[0]); CreateTestFile(3); name = GetPathname(); WaitWatcher(40000,watcher,2); Assert.AreEqual(2, watcher.GetChangeList().Count); Assert.AreEqual(name, watcher.GetChangeList()[1]); watcher.EnableRaisingEvents = false; CreateTestFile(4); WaitWatcher(); Assert.AreEqual(2, watcher.GetChangeList().Count); watcher.EnableRaisingEvents = true; CreateTestFile(5); name = GetPathname(); WaitWatcher(40000,watcher,3); Assert.AreEqual(3, watcher.GetChangeList().Count); Assert.AreEqual(name, watcher.GetChangeList()[2]); } }
public static void WaitWatcher(int milliseconds, Watcher watcher, int expect) { #if __MonoCS__ milliseconds = milliseconds * 10; #endif while (milliseconds >= 0) { if (watcher.GetChangeList().Count >= expect) { return; } Thread.Sleep(10); milliseconds = milliseconds - 10; } Console.WriteLine("Timeout"); }
public void TestChangeTypeForMoveInNotSyncedFolder() { using (Watcher watcher = new Watcher(TestFolder)) { watcher.EnableRaisingEvents = true; CreateTestFile(oldnameOut, 1); WaitWatcher(); File.Move(oldnameOut, newnameOut); WaitWatcher(); Assert.AreEqual(0, watcher.GetChangeList().Count); } }
public void TestChangeTypeForMoveOutOfSyncedFolder() { using (Watcher watcher = new Watcher(TestFolder)) { watcher.EnableRaisingEvents = true; CreateTestFile(oldname, 1); WaitWatcher(40000,watcher,(w) => { #if __MonoCS__ List<Watcher.ChangeTypes> types = new List<Watcher.ChangeTypes>(); types.Add(Watcher.ChangeTypes.Created); types.Add(Watcher.ChangeTypes.Changed); return types.Contains(w.GetChangeType(oldname)); #else return w.GetChangeType(oldname) == Watcher.ChangeTypes.Changed; #endif }); File.Move(oldname, newnameOut); Console.WriteLine("moved:"+oldname); WaitWatcher(40000,watcher,(w) => { return w.GetChangeType(oldname) == Watcher.ChangeTypes.Deleted; }); Assert.AreEqual(1, watcher.GetChangeList().Count); Assert.AreEqual(oldname, watcher.GetChangeList()[0]); Assert.AreEqual(Watcher.ChangeTypes.Deleted, watcher.GetChangeType(oldname)); } }
public void TestChangeTypeForMoveIntoSyncedFolder() { using (Watcher watcher = new Watcher(TestFolder)) { watcher.EnableRaisingEvents = true; CreateTestFile(oldnameOut, 1); WaitWatcher(); File.Move(oldnameOut, newname); WaitWatcher(40000,watcher,1); Assert.AreEqual(1, watcher.GetChangeList().Count); Assert.AreEqual(newname, watcher.GetChangeList()[0]); Assert.AreEqual(Watcher.ChangeTypes.Created, watcher.GetChangeType(newname)); } }
public void TestChangeTypeForMoveInsideSyncedFolder() { using (Watcher watcher = new Watcher(TestFolder)) { watcher.EnableRaisingEvents = true; CreateTestFile(oldname, 1); WaitWatcher(40000,watcher,1); File.Move(oldname, newname); WaitWatcher(40000,watcher,2); WaitWatcher(40000,watcher,(w)=> { return w.GetChangeType(oldname) == Watcher.ChangeTypes.Deleted; }); List<string> changeList = watcher.GetChangeList(); Assert.AreEqual(2, changeList.Count); Assert.Contains(oldname, changeList); Assert.Contains(newname, changeList); Assert.AreEqual(Watcher.ChangeTypes.Deleted, watcher.GetChangeType(oldname)); Assert.AreEqual(Watcher.ChangeTypes.Created, watcher.GetChangeType(newname)); } }
public void TestChangeTypeDeleted() { using (Watcher watcher = new Watcher(TestFolder)) { watcher.EnableRaisingEvents = true; List<string> names = new List<string>(); for (int i = 0; i < NormalNumber; ++i) { CreateTestFile(); names.Add(GetPathname()); } WaitWatcher(40000,watcher,NormalNumber); for (int i = 0; i < NormalNumber; ++i) { File.Delete(names[i]); } WaitWatcher(40000,watcher,(w) => { return w.GetChangeType(names[NormalNumber-1]) == Watcher.ChangeTypes.Deleted; }); List<string> changeList = watcher.GetChangeList(); Assert.AreEqual(NormalNumber, changeList.Count); for (int i = 0; i < NormalNumber; ++i) { Assert.Contains(names[i], changeList); Assert.AreEqual( watcher.GetChangeType(names[i]), Watcher.ChangeTypes.Deleted); } } }
public void TestChangeTypeChanged() { using (Watcher watcher = new Watcher(TestFolder)) { watcher.EnableRaisingEvents = true; List<string> names = new List<string>(); for (int i = 0; i < NormalNumber; ++i) { CreateTestFile(); names.Add(GetPathname()); } for (int i = 0; i < NormalNumber; ++i) { CreateTestFile(names[i], i + 1); } WaitWatcher(40000,watcher,NormalNumber); WaitWatcher(40000,watcher,(w)=> { #if __MonoCS__ List<Watcher.ChangeTypes> types = new List<Watcher.ChangeTypes>(); types.Add(Watcher.ChangeTypes.Created); types.Add(Watcher.ChangeTypes.Changed); #endif for (int i = 0; i < NormalNumber; ++i) { #if __MonoCS__ if (!types.Contains(w.GetChangeType(names[i]))) #else if (w.GetChangeType(names[i]) != Watcher.ChangeTypes.Changed) #endif { return false; } } return true; }); List<string> changeList = watcher.GetChangeList(); Assert.AreEqual(NormalNumber, changeList.Count); for (int i = 0; i < NormalNumber; ++i) { Assert.Contains(names[i], changeList); #if __MonoCS__ List<Watcher.ChangeTypes> types = new List<Watcher.ChangeTypes>(); types.Add(Watcher.ChangeTypes.Created); types.Add(Watcher.ChangeTypes.Changed); Assert.Contains( watcher.GetChangeType(names[i]), types); #else Assert.AreEqual( watcher.GetChangeType(names[i]), Watcher.ChangeTypes.Changed); #endif } } }
public void TestChangeTypeCreatedHeavy() { using (Watcher watcher = new Watcher(TestFolder)) { watcher.EnableRaisingEvents = true; List<string> names = new List<string>(); for (int i = 0; i < HeavyNumber; ++i) { CreateTestFile(0, i / FileInFolderNumber); names.Add(GetPathname(i / FileInFolderNumber)); } Assert.IsTrue(watcher.EnableRaisingEvents); int totalNumber = HeavyNumber + (HeavyNumber - 1) / FileInFolderNumber; WaitWatcher(60000,watcher,totalNumber); Assert.AreEqual(totalNumber, watcher.GetChangeList().Count); for (int i = 0; i < HeavyNumber; ++i) { #if __MonoCS__ List<Watcher.ChangeTypes> types = new List<Watcher.ChangeTypes>(); types.Add(Watcher.ChangeTypes.Created); types.Add(Watcher.ChangeTypes.Changed); Assert.Contains( watcher.GetChangeType(names[i]), types); #else Assert.AreEqual( watcher.GetChangeType(names[i]), Watcher.ChangeTypes.Created, names[i]); #endif } } }
public void TestRemoveInsert() { using (Watcher watcher = new Watcher(TestFolder)) { for (int i = 0; i < NormalNumber; ++i) { watcher.InsertChange(i.ToString(), Watcher.ChangeTypes.None); } Assert.AreEqual(0, watcher.GetChangeList().Count); for (int i = 0; i < NormalNumber; ++i) { watcher.InsertChange(i.ToString(), Watcher.ChangeTypes.Created); } Assert.AreEqual(NormalNumber, watcher.GetChangeList().Count); for (int i = 0; i < NormalNumber; ++i) { Assert.AreEqual(i.ToString(), watcher.GetChangeList()[i]); Assert.AreEqual(Watcher.ChangeTypes.Created, watcher.GetChangeType(i.ToString())); } for (int i = 0; i < NormalNumber; ++i) { watcher.InsertChange(i.ToString(), Watcher.ChangeTypes.Deleted); } Assert.AreEqual(NormalNumber, watcher.GetChangeList().Count); for (int i = 0; i < NormalNumber; ++i) { Assert.AreEqual(i.ToString(), watcher.GetChangeList()[i]); Assert.AreEqual(Watcher.ChangeTypes.Created, watcher.GetChangeType(i.ToString())); } for (int i = 0; i < NormalNumber; ++i) { watcher.RemoveChange(i.ToString(), Watcher.ChangeTypes.Deleted); } Assert.AreEqual(NormalNumber, watcher.GetChangeList().Count); for (int i = 0; i < NormalNumber; ++i) { Assert.AreEqual(i.ToString(), watcher.GetChangeList()[i]); Assert.AreEqual(Watcher.ChangeTypes.Created, watcher.GetChangeType(i.ToString())); } for (int i = 0; i < NormalNumber; ++i) { watcher.RemoveChange(i.ToString(), Watcher.ChangeTypes.Created); } Assert.AreEqual(0, watcher.GetChangeList().Count); } }
public void TestRemove() { using (Watcher watcher = new Watcher(TestFolder)) { watcher.EnableRaisingEvents = true; List<string> names = new List<string>(); for (int i = 0; i < NormalNumber; ++i) { CreateTestFile(); names.Add(GetPathname()); } WaitWatcher(40000,watcher,NormalNumber); Assert.AreEqual(NormalNumber, watcher.GetChangeList().Count); for (int i = 0; i < NormalNumber; ++i) { Assert.AreEqual(NormalNumber - i, watcher.GetChangeList().Count); if (i % 2 == 0) { watcher.RemoveChange(names[i]); } else { watcher.RemoveChange(names[i],Watcher.ChangeTypes.Created); } } names.Clear(); for (int i = 0; i < NormalNumber; ++i) { CreateTestFile(); } WaitWatcher(40000,watcher,NormalNumber); Assert.AreEqual(NormalNumber, watcher.GetChangeList().Count); watcher.RemoveAll(); Assert.AreEqual(0, watcher.GetChangeList().Count); } }
public void TestChangeTypeForMove() { string oldnameOut = Path.Combine(TestFolderParent, "test.old"); string newnameOut = Path.Combine(TestFolderParent, "test.new"); string oldname = Path.Combine(TestFolder, "test.old"); string newname = Path.Combine(TestFolder, "test.new"); using (Watcher watcher = new Watcher(TestFolder)) { File.Delete(newname); watcher.EnableRaisingEvents = true; CreateTestFile(oldname, 1); WaitWatcher(40000,watcher,1); File.Move(oldname, newname); WaitWatcher(40000,watcher,2); WaitWatcher(40000,watcher,(w)=> { return w.GetChangeType(oldname) == Watcher.ChangeTypes.Deleted; }); List<string> changeList = watcher.GetChangeList(); Assert.AreEqual(2, changeList.Count); Assert.Contains(oldname, changeList); Assert.Contains(newname, changeList); Assert.AreEqual(Watcher.ChangeTypes.Deleted, watcher.GetChangeType(oldname)); Assert.AreEqual(Watcher.ChangeTypes.Created, watcher.GetChangeType(newname)); File.Delete(newname); } using (Watcher watcher = new Watcher(TestFolder)) { File.Delete(newname); watcher.EnableRaisingEvents = true; CreateTestFile(oldnameOut, 1); WaitWatcher(); File.Move(oldnameOut, newname); WaitWatcher(40000,watcher,1); Assert.AreEqual(1, watcher.GetChangeList().Count); Assert.AreEqual(newname, watcher.GetChangeList()[0]); Assert.AreEqual(Watcher.ChangeTypes.Created, watcher.GetChangeType(newname)); File.Delete(newname); } using (Watcher watcher = new Watcher(TestFolder)) { File.Delete(newnameOut); watcher.EnableRaisingEvents = true; CreateTestFile(oldname, 1); WaitWatcher(40000,watcher,1); File.Move(oldname, newnameOut); WaitWatcher(40000,watcher,(w) => { return w.GetChangeType(oldname) == Watcher.ChangeTypes.Deleted; }); Assert.AreEqual(1, watcher.GetChangeList().Count); Assert.AreEqual(oldname, watcher.GetChangeList()[0]); Assert.AreEqual(Watcher.ChangeTypes.Deleted, watcher.GetChangeType(oldname)); File.Delete(newnameOut); } using (Watcher watcher = new Watcher(TestFolder)) { File.Delete(newnameOut); watcher.EnableRaisingEvents = true; CreateTestFile(oldnameOut, 1); WaitWatcher(); File.Move(oldnameOut, newnameOut); WaitWatcher(); Assert.AreEqual(0, watcher.GetChangeList().Count); File.Delete(newnameOut); } }
public void TestChangeTypeCreated() { using (Watcher watcher = new Watcher(TestFolder)) { watcher.EnableRaisingEvents = true; List<string> names = new List<string>(); for (int i = 0; i < NormalNumber; ++i) { CreateTestFile(); names.Add(GetPathname()); } WaitWatcher(40000,watcher,NormalNumber); Assert.AreEqual(NormalNumber, watcher.GetChangeList().Count); for (int i = 0; i < NormalNumber; ++i) { Assert.AreEqual(names[i], watcher.GetChangeList()[i]); Assert.AreEqual( Watcher.ChangeTypes.Created, watcher.GetChangeType((string)watcher.GetChangeList()[i])); } } }