public void TestLocker_WaitForCompletion_SeparateObjects_LockParentAsWell() { List <string> resultsList = new List <string>(); int numberOfDone = 0; var sw = Stopwatch.StartNew(); var tester = new LockerTester(LockerMode.SeparateObjects); Task task; lock (tester) { task = Task.Factory.StartNew(tester.ShortLock).ContinueWith((x) => NotifyDone($"Short done", ref numberOfDone, resultsList)); task.Wait(3000); } Assert.AreEqual(TaskStatus.RanToCompletion, task.Status); Assert.IsTrue(sw.ElapsedMilliseconds < 1100); Assert.IsTrue(resultsList.First() == "Short done" && numberOfDone == 1); }
public void TestLocker_SameObject() { List <string> resultsList = new List <string>(); int numberOfDone = 0; var sw = Stopwatch.StartNew(); var tester = new LockerTester(LockerMode.SameObject); Task.Factory.StartNew(tester.LongLock).ContinueWith((x) => NotifyDone("Long done", ref numberOfDone, resultsList)); Thread.Sleep(1); //ensure that first task manages to acquire the lock Task.Factory.StartNew(tester.ShortLock).ContinueWith((x) => NotifyDone("Short done", ref numberOfDone, resultsList)); while (numberOfDone < 2) { Thread.Sleep(1); } Console.WriteLine($"All done in {sw.ElapsedMilliseconds}"); Assert.IsTrue(sw.ElapsedMilliseconds >= 6000 && sw.ElapsedMilliseconds < 8000); Assert.AreEqual("Long done", resultsList.First()); }
public void TestLocker_WaitForCompletion_LockThis_LockParentAsWell() { List <string> resultsList = new List <string>(); int numberOfDone = 0; var sw = Stopwatch.StartNew(); var tester = new LockerTester(LockerMode.This); Task task; lock (tester) { task = Task.Factory.StartNew(tester.ShortLock).ContinueWith((x) => NotifyDone($"Short done", ref numberOfDone, resultsList)); task.Wait(5000); } Assert.AreEqual(TaskStatus.WaitingForActivation, task.Status); //not sure why the status is 'waiting for activation', as the task has started working... Assert.IsTrue(sw.ElapsedMilliseconds >= 5000); Assert.IsTrue(resultsList.Count == 0 && numberOfDone == 0); }