Exemple #1
0
        public void TestMultipleBackgroundAction()
        {
            // Test that background actions don't run at the same time.
            var worker    = new AsynchronousSerialWorkQueue(AsynchronousOperationListenerProvider.NullListener);
            var doneEvent = new AutoResetEvent(false);

            var action1Ran = false;
            var action2Ran = false;

            worker.EnqueueBackgroundWork(() =>
            {
                Assert.NotSame(_foregroundSyncContext, SynchronizationContext.Current);
                action1Ran = true;

                // Simulate work to ensure that if tasks overlap that we will
                // see it.
                Thread.Sleep(1000);
                Assert.False(action2Ran);
            }, "Test", CancellationToken.None);

            worker.EnqueueBackgroundWork(() =>
            {
                Assert.NotSame(_foregroundSyncContext, SynchronizationContext.Current);
                action2Ran = true;
                doneEvent.Set();
            }, "Test", CancellationToken.None);

            doneEvent.WaitOne();
            Assert.True(action1Ran);
            Assert.True(action2Ran);
        }
Exemple #2
0
        public void TestMultipleBackgroundAction()
        {
            var exportProvider   = EditorTestCompositions.EditorFeatures.ExportProviderFactory.CreateExportProvider();
            var threadingContext = exportProvider.GetExportedValue <IThreadingContext>();
            var listenerProvider = exportProvider.GetExportedValue <IAsynchronousOperationListenerProvider>();

            // Test that background actions don't run at the same time.
            var worker    = new AsynchronousSerialWorkQueue(threadingContext, listenerProvider.GetListener("Test"));
            var doneEvent = new AutoResetEvent(false);

            var action1Ran = false;
            var action2Ran = false;

            worker.EnqueueBackgroundWork(() =>
            {
                Assert.NotSame(_foregroundSyncContext, SynchronizationContext.Current);
                action1Ran = true;

                // Simulate work to ensure that if tasks overlap that we will
                // see it.
                Thread.Sleep(1000);
                Assert.False(action2Ran);
            }, "Test", CancellationToken.None);

            worker.EnqueueBackgroundWork(() =>
            {
                Assert.NotSame(_foregroundSyncContext, SynchronizationContext.Current);
                action2Ran = true;
                doneEvent.Set();
            }, "Test", CancellationToken.None);

            doneEvent.WaitOne();
            Assert.True(action1Ran);
            Assert.True(action2Ran);
        }
Exemple #3
0
        public void TestBackgroundCancelMultipleActions()
        {
            var exportProvider   = EditorTestCompositions.EditorFeatures.ExportProviderFactory.CreateExportProvider();
            var threadingContext = exportProvider.GetExportedValue <IThreadingContext>();
            var listenerProvider = exportProvider.GetExportedValue <IAsynchronousOperationListenerProvider>();

            // Ensure that multiple background actions are cancelled if they
            // use the same cancellation token.
            var worker = new AsynchronousSerialWorkQueue(threadingContext, listenerProvider.GetListener("Test"));

            var taskRunningEvent = new AutoResetEvent(false);
            var cancelEvent      = new AutoResetEvent(false);
            var doneEvent        = new AutoResetEvent(false);

            var source            = new CancellationTokenSource();
            var cancellationToken = source.Token;

            var action1Ran = false;
            var action2Ran = false;

            worker.EnqueueBackgroundWork(() =>
            {
                action1Ran = true;

                Assert.NotSame(_foregroundSyncContext, SynchronizationContext.Current);
                Assert.False(cancellationToken.IsCancellationRequested);

                taskRunningEvent.Set();
                cancelEvent.WaitOne();

                cancellationToken.ThrowIfCancellationRequested();
                Assert.True(false);
            }, "Test", source.Token);

            // We should not run this action.
            worker.EnqueueBackgroundWork(() =>
            {
                action2Ran = true;
                Assert.False(true);
            }, "Test", source.Token);

            taskRunningEvent.WaitOne();

            source.Cancel();
            cancelEvent.Set();

            try
            {
                worker.GetTestAccessor().WaitUntilCompletion();
                Assert.True(false);
            }
            catch (AggregateException ae)
            {
                Assert.IsAssignableFrom <OperationCanceledException>(ae.InnerException);
            }

            Assert.True(action1Ran);
            Assert.False(action2Ran);
        }
Exemple #4
0
        public void TestBackgroundCancelMultipleActions()
        {
            // Ensure that multiple background actions are cancelled if they
            // use the same cancellation token.
            var listener = new AggregateAsynchronousOperationListener(Enumerable.Empty <Lazy <IAsynchronousOperationListener, FeatureMetadata> >(), "Test");
            var worker   = new AsynchronousSerialWorkQueue(listener);

            var taskRunningEvent = new AutoResetEvent(false);
            var cancelEvent      = new AutoResetEvent(false);
            var doneEvent        = new AutoResetEvent(false);

            var source            = new CancellationTokenSource();
            var cancellationToken = source.Token;

            var action1Ran = false;
            var action2Ran = false;

            worker.EnqueueBackgroundWork(() =>
            {
                action1Ran = true;

                Assert.NotSame(_foregroundSyncContext, SynchronizationContext.Current);
                Assert.False(cancellationToken.IsCancellationRequested);

                taskRunningEvent.Set();
                cancelEvent.WaitOne();

                cancellationToken.ThrowIfCancellationRequested();
                Assert.True(false);
            }, "Test", source.Token);

            // We should not run this action.
            worker.EnqueueBackgroundWork(() =>
            {
                action2Ran = true;
                Assert.False(true);
            }, "Test", source.Token);

            taskRunningEvent.WaitOne();

            source.Cancel();
            cancelEvent.Set();

            try
            {
                worker.WaitUntilCompletion_ForTestingPurposesOnly();
                Assert.True(false);
            }
            catch (AggregateException ae)
            {
                Assert.IsAssignableFrom <OperationCanceledException>(ae.InnerException);
            }

            Assert.True(action1Ran);
            Assert.False(action2Ran);
        }
Exemple #5
0
        public void TestBackgroundAction()
        {
            var exportProvider =
                EditorTestCompositions.EditorFeatures.ExportProviderFactory.CreateExportProvider();
            var threadingContext = exportProvider.GetExportedValue <IThreadingContext>();
            var listenerProvider =
                exportProvider.GetExportedValue <IAsynchronousOperationListenerProvider>();

            var worker = new AsynchronousSerialWorkQueue(
                threadingContext,
                listenerProvider.GetListener("Test")
                );
            var doneEvent = new AutoResetEvent(initialState: false);

            var actionRan = false;

            worker.EnqueueBackgroundWork(
                () =>
            {
                // Assert.NotNull(SynchronizationContext.Current);
                Assert.NotSame(_foregroundSyncContext, SynchronizationContext.Current);
                actionRan = true;
                doneEvent.Set();
            },
                GetType().Name + ".TestBackgroundAction",
                CancellationToken.None
                );

            doneEvent.WaitOne();
            Assert.True(actionRan);
        }
Exemple #6
0
        public void TestBackgroundCancel1()
        {
            var exportProvider =
                EditorTestCompositions.EditorFeatures.ExportProviderFactory.CreateExportProvider();
            var threadingContext = exportProvider.GetExportedValue <IThreadingContext>();
            var listenerProvider =
                exportProvider.GetExportedValue <IAsynchronousOperationListenerProvider>();

            // Ensure that we can cancel a background action.
            var worker = new AsynchronousSerialWorkQueue(
                threadingContext,
                listenerProvider.GetListener("Test")
                );

            var taskRunningEvent = new AutoResetEvent(false);
            var cancelEvent      = new AutoResetEvent(false);
            var doneEvent        = new AutoResetEvent(false);

            var source            = new CancellationTokenSource();
            var cancellationToken = source.Token;

            var actionRan = false;

            worker.EnqueueBackgroundWork(
                () =>
            {
                actionRan = true;

                Assert.NotSame(_foregroundSyncContext, SynchronizationContext.Current);
                Assert.False(cancellationToken.IsCancellationRequested);

                taskRunningEvent.Set();
                cancelEvent.WaitOne();

                Assert.True(cancellationToken.IsCancellationRequested);

                doneEvent.Set();
            },
                "Test",
                source.Token
                );

            taskRunningEvent.WaitOne();

            source.Cancel();
            cancelEvent.Set();

            doneEvent.WaitOne();
            Assert.True(actionRan);
        }
Exemple #7
0
        public void TestBackgroundAction()
        {
            var worker    = new AsynchronousSerialWorkQueue(AsynchronousOperationListenerProvider.NullListener);
            var doneEvent = new AutoResetEvent(initialState: false);

            var actionRan = false;

            worker.EnqueueBackgroundWork(() =>
            {
                // Assert.NotNull(SynchronizationContext.Current);
                Assert.NotSame(_foregroundSyncContext, SynchronizationContext.Current);
                actionRan = true;
                doneEvent.Set();
            }, GetType().Name + ".TestBackgroundAction", CancellationToken.None);

            doneEvent.WaitOne();
            Assert.True(actionRan);
        }
Exemple #8
0
        public void TestBackgroundAction()
        {
            var listener  = new AggregateAsynchronousOperationListener(Enumerable.Empty <Lazy <IAsynchronousOperationListener, FeatureMetadata> >(), "Test");
            var worker    = new AsynchronousSerialWorkQueue(listener);
            var doneEvent = new AutoResetEvent(initialState: false);

            var actionRan = false;

            worker.EnqueueBackgroundWork(() =>
            {
                // Assert.NotNull(SynchronizationContext.Current);
                Assert.NotSame(_foregroundSyncContext, SynchronizationContext.Current);
                actionRan = true;
                doneEvent.Set();
            }, GetType().Name + ".TestBackgroundAction", CancellationToken.None);

            doneEvent.WaitOne();
            Assert.True(actionRan);
        }
Exemple #9
0
        public void TestBackgroundCancel1()
        {
            // Ensure that we can cancel a background action.
            var listener = new AggregateAsynchronousOperationListener(Enumerable.Empty <Lazy <IAsynchronousOperationListener, FeatureMetadata> >(), "Test");
            var worker   = new AsynchronousSerialWorkQueue(listener);

            var taskRunningEvent = new AutoResetEvent(false);
            var cancelEvent      = new AutoResetEvent(false);
            var doneEvent        = new AutoResetEvent(false);

            var source            = new CancellationTokenSource();
            var cancellationToken = source.Token;

            var actionRan = false;

            worker.EnqueueBackgroundWork(() =>
            {
                actionRan = true;

                Assert.NotSame(_foregroundSyncContext, SynchronizationContext.Current);
                Assert.False(cancellationToken.IsCancellationRequested);

                taskRunningEvent.Set();
                cancelEvent.WaitOne();

                Assert.True(cancellationToken.IsCancellationRequested);

                doneEvent.Set();
            }, "Test", source.Token);

            taskRunningEvent.WaitOne();

            source.Cancel();
            cancelEvent.Set();

            doneEvent.WaitOne();
            Assert.True(actionRan);
        }
Exemple #10
0
        public void TestBackgroundCancelOneAction()
        {
            // Ensure that when a background action is cancelled the next
            // one starts (if it has a different cancellation token).
            var worker = new AsynchronousSerialWorkQueue(AsynchronousOperationListenerProvider.NullListener);

            var taskRunningEvent = new AutoResetEvent(false);
            var cancelEvent      = new AutoResetEvent(false);
            var doneEvent        = new AutoResetEvent(false);

            var source1 = new CancellationTokenSource();
            var source2 = new CancellationTokenSource();
            var token1  = source1.Token;
            var token2  = source2.Token;

            var action1Ran = false;
            var action2Ran = false;

            worker.EnqueueBackgroundWork(() =>
            {
                action1Ran = true;

                Assert.NotSame(_foregroundSyncContext, SynchronizationContext.Current);
                Assert.False(token1.IsCancellationRequested);

                taskRunningEvent.Set();
                cancelEvent.WaitOne();

                token1.ThrowIfCancellationRequested();
                Assert.True(false);
            }, "Test", source1.Token);

            worker.EnqueueBackgroundWork(() =>
            {
                action2Ran = true;

                Assert.NotSame(_foregroundSyncContext, SynchronizationContext.Current);
                Assert.False(token2.IsCancellationRequested);

                taskRunningEvent.Set();
                cancelEvent.WaitOne();

                doneEvent.Set();
            }, "Test", source2.Token);

            // Wait for the first task to start.
            taskRunningEvent.WaitOne();

            // Cancel it
            source1.Cancel();
            cancelEvent.Set();

            // Wait for the second task to start.
            taskRunningEvent.WaitOne();
            cancelEvent.Set();

            // Wait for the second task to complete.
            doneEvent.WaitOne();
            Assert.True(action1Ran);
            Assert.True(action2Ran);
        }