Esempio n. 1
0
        public async Task InitializeAsync_OnlyExecutesActionOnce_Success()
        {
            int invocationCount = 0;
            var lazy            = new AsyncLazyInitializer(async delegate
            {
                invocationCount++;
                await Task.Yield();
            });

            Assert.Equal(0, invocationCount);
            Assert.False(lazy.IsCompleted);
            Assert.False(lazy.IsCompletedSuccessfully);

            await lazy.InitializeAsync();

            Assert.Equal(1, invocationCount);
            Assert.True(lazy.IsCompleted);
            Assert.True(lazy.IsCompletedSuccessfully);

            await lazy.InitializeAsync();

            Assert.Equal(1, invocationCount);
            Assert.True(lazy.IsCompleted);
            Assert.True(lazy.IsCompletedSuccessfully);
        }
Esempio n. 2
0
        public void Initialize_RequiresMainThreadHeldByOther()
        {
            var context = this.InitializeJTCAndSC();
            var jtf     = context.Factory;

            var evt  = new AsyncManualResetEvent();
            var lazy = new AsyncLazyInitializer(
                async delegate
            {
                await evt;     // use an event here to ensure it won't resume till the Main thread is blocked.
            },
                jtf);

            var resultTask = lazy.InitializeAsync();

            Assert.False(resultTask.IsCompleted);

            evt.Set(); // setting this event allows the value factory to resume, once it can get the Main thread.

            // The interesting bit we're testing here is that
            // the value factory has already been invoked.  It cannot
            // complete until the Main thread is available and we're blocking
            // the Main thread waiting for it to complete.
            // This will deadlock unless the AsyncLazyExecution joins
            // the action's JoinableTaskFactory with the currently blocking one.
            lazy.Initialize();

            // Now that the action has completed, the earlier acquired
            // task should have no problem completing.
            Assert.True(resultTask.Wait(AsyncDelay));
        }
Esempio n. 3
0
        public async Task InitializeAsync_OnlyExecutesActionOnce_Failure()
        {
            int invocationCount = 0;
            var lazy            = new AsyncLazyInitializer(async delegate
            {
                invocationCount++;
                await Task.Yield();
                throw new InvalidOperationException();
            });

            Assert.Equal(0, invocationCount);
            Assert.False(lazy.IsCompleted);
            Assert.False(lazy.IsCompletedSuccessfully);

            await Assert.ThrowsAsync <InvalidOperationException>(() => lazy.InitializeAsync());

            Assert.Equal(1, invocationCount);
            Assert.True(lazy.IsCompleted);
            Assert.False(lazy.IsCompletedSuccessfully);

            await Assert.ThrowsAsync <InvalidOperationException>(() => lazy.InitializeAsync());

            Assert.Equal(1, invocationCount);
            Assert.True(lazy.IsCompleted);
            Assert.False(lazy.IsCompletedSuccessfully);
        }
Esempio n. 4
0
        public async Task InitializeAsync_Canceled()
        {
            var  cts      = new CancellationTokenSource();
            var  evt      = new AsyncManualResetEvent();
            var  lazy     = new AsyncLazyInitializer(evt.WaitAsync);
            Task lazyTask = lazy.InitializeAsync(cts.Token);

            cts.Cancel();
            await Assert.ThrowsAnyAsync <OperationCanceledException>(() => lazyTask);
        }
Esempio n. 5
0
        public async Task InitializeAsync_Precanceled()
        {
            bool invoked = false;
            var  evt     = new AsyncManualResetEvent();
            var  lazy    = new AsyncLazyInitializer(delegate
            {
                invoked = true;
                return(evt.WaitAsync());
            });
            await Assert.ThrowsAnyAsync <OperationCanceledException>(() => lazy.InitializeAsync(new CancellationToken(canceled: true)));

            Assert.False(invoked);
        }
 private NuGetBrokeredServiceFactory()
 {
     _lazyInitializer = new AsyncLazyInitializer(InitializeAsync, ThreadHelper.JoinableTaskFactory);
 }
Esempio n. 7
0
 public NuGetSolutionService()
 {
     _initializer = new AsyncLazyInitializer(InitializeAsync, NuGetUIThreadHelper.JoinableTaskFactory);
 }