public void DisposeInAsync() { int i = 0; // This tests that ContextDisposable.Dispose() is not done in async mode. var d = new ContextDisposable(new SynchronizationContext(), Disposable.Create(() => { Thread.Sleep(200); i++; })); d.Dispose(); Assert.IsTrue(d.IsDisposed, "#1"); Assert.AreEqual(0, i, "#2"); // not (very likely) yet Assert.IsTrue(SpinWait.SpinUntil(() => i != 0, 1000), "#3"); }
public void ContextDisposable() { var disp = false; var m = new MySync(); var c = new ContextDisposable(m, Disposable.Create(() => { disp = true; })); Assert.IsFalse(m._disposed); Assert.IsFalse(disp); c.Dispose(); Assert.IsTrue(m._disposed); Assert.IsTrue(disp); }
public static void ContextDisposable_WillExecuteItsDisposableImplementation_OnTheSpecifiedSynchronizationContext(Action <string> logging = null) { //Needs to be run from a UI application logging = logging ?? (message => Debug.WriteLine(message)); var contextDisposable = new ContextDisposable( SynchronizationContext.Current, // Will throw if run from the console, but will capture the UI sync context if run inside an event handler on a Form Disposable.Create(() => logging("Disposing Thread ID: " + Thread.CurrentThread.ManagedThreadId))); Scheduler.Default.Schedule(() => { logging("Calling Thread ID: " + Thread.CurrentThread.ManagedThreadId); contextDisposable.Dispose(); }); }
public void ContextDisposable1_Test() { // arrange bool isDisposedOnContext = false; var context = new CustomSynchronizationContext(); var innerDisposable = Disposable.Create(() => isDisposedOnContext = (SynchronizationContext.Current == context)); var disp = new ContextDisposable(context, innerDisposable); // act disp.Dispose(); // just blocking until context execution completion context.Wait(); // verify Assert.IsNull(SynchronizationContext.Current); Assert.IsTrue(disp.IsDisposed); Assert.IsTrue(isDisposedOnContext); }