public AsyncLifetimeManager(Func <Task> initialization, bool executeOnCallingThread = true) { var options = GetOptions(executeOnCallingThread); _underlyingManager = new DisposableAsyncLazy <byte>( factory: AsFactory(initialization), options); }
public async Task FactoryTest() { var lazy = new DisposableAsyncLazy <byte>( cancellation => { return(Task.FromResult <byte>(12)); }, _ => { throw null; }, DisposableAsyncLazyOptions.None); Assert.AreEqual(12, await lazy); }
public void IsNotAutostartedTest() { var lazy = new DisposableAsyncLazy <byte>( _ => { throw null; }, _ => { throw null; }, DisposableAsyncLazyOptions.None); Assert.IsFalse(lazy.IsStarted); }
public async Task AutostartTest() { var lazy = new DisposableAsyncLazy <byte>( cancellation => { return(Task.FromResult <byte>(12)); }, _ => { throw null; }, DisposableAsyncLazyOptions.Autostart); Assert.IsTrue(lazy.IsStarted); Assert.AreEqual(12, await lazy); }
public async Task DisposeIfNotYetStartedTest() { var disposeCalled = false; var lazy = new DisposableAsyncLazy <byte>( _ => { return(Task.FromResult <byte>(12)); }, p => { Assert.AreEqual(12, p); disposeCalled = true; return(Task.CompletedTask); }, DisposableAsyncLazyOptions.None); await lazy.DisposeAsync(); Assert.IsFalse(disposeCalled); }
public async Task DisposalThrowsTest() { var lazy = new DisposableAsyncLazy <byte>( _ => { return(Task.FromResult <byte>(12)); }, p => { throw new Exception(); }, DisposableAsyncLazyOptions.None); await lazy; await Assert.ThrowsExceptionAsync <Exception>(async() => { await lazy.DisposeAsync(); }); }
public async Task DisposalThrowsTest() { var lazy = new DisposableAsyncLazy <byte>( _ => { return(Task.FromResult <byte>(12)); }, p => { throw new CustomException(); }, DisposableAsyncLazyOptions.None); await lazy; await Assert.ThrowsExceptionAsync <CustomException>(async() => { await lazy.DisposeAsync(); }); Assert.AreEqual(TaskStatus.RanToCompletion, lazy.GetDisposeTask().Status); }
//[TestMethod] public async Task ExecuteOnCallingThreadTest() { using (var context = new AsyncContext()) { SynchronizationContext.SetSynchronizationContext(context.SynchronizationContext); var threadId = Thread.CurrentThread.ManagedThreadId; var lazy = new DisposableAsyncLazy <byte>( cancellation => { Assert.AreEqual(threadId, Thread.CurrentThread.ManagedThreadId); return(Task.FromResult <byte>(12)); }, _ => { throw null; }, DisposableAsyncLazyOptions.None); await lazy; } }
public async Task RetryOnFailureTest() { var @try = 1; var lazy = new DisposableAsyncLazy <byte>( cancellation => { if (@try == 1) { @try++; throw new Exception(); } return(Task.FromResult <byte>(12)); }, _ => { throw null; }, DisposableAsyncLazyOptions.RetryOnFailure); Assert.IsFalse(lazy.IsStarted); await Assert.ThrowsExceptionAsync <Exception>(async() => await lazy); Assert.AreEqual(12, await lazy); }
public static Task GetDisposeTask <T>(this DisposableAsyncLazy <T> instance) { var field = typeof(DisposableAsyncLazy <T>).GetField("_disposeTask", BindingFlags.NonPublic | BindingFlags.Instance); return(field.GetValue(instance) as Task); }