예제 #1
0
        public AsyncLifetimeManager(Func <Task> initialization, bool executeOnCallingThread = true)
        {
            var options = GetOptions(executeOnCallingThread);

            _underlyingManager = new DisposableAsyncLazy <byte>(
                factory: AsFactory(initialization),
                options);
        }
예제 #2
0
        public async Task FactoryTest()
        {
            var lazy = new DisposableAsyncLazy <byte>(
                cancellation => { return(Task.FromResult <byte>(12)); },
                _ => { throw null; },
                DisposableAsyncLazyOptions.None);

            Assert.AreEqual(12, await lazy);
        }
예제 #3
0
        public void IsNotAutostartedTest()
        {
            var lazy = new DisposableAsyncLazy <byte>(
                _ => { throw null; },
                _ => { throw null; },
                DisposableAsyncLazyOptions.None);

            Assert.IsFalse(lazy.IsStarted);
        }
예제 #4
0
        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);
        }
예제 #5
0
        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);
        }
예제 #6
0
        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();
            });
        }
예제 #7
0
파일: Issue22.cs 프로젝트: AI4E/AI4E.Utils
        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);
        }
예제 #8
0
        //[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;
            }
        }
예제 #9
0
        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);
        }
예제 #10
0
파일: Issue22.cs 프로젝트: AI4E/AI4E.Utils
        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);
        }