コード例 #1
0
        public void ReentrancyPostAndWaitTest()
        {
            int actionCallCount      = 0;
            int innerActionCallCount = 0;

            using (var context = UnitTestSynchronizationContext.Create())
            {
                Func <Task> asyncInnerAction = async() =>
                {
                    await Task.Delay(1);

                    Interlocked.Increment(ref innerActionCallCount);
                };

                Func <Task> asyncAction = async() =>
                {
                    await Task.Delay(1);

                    asyncInnerAction().Wait(context);
                    await Task.Delay(1);

                    asyncInnerAction().Wait(context);
                    Interlocked.Increment(ref actionCallCount);
                };

                var task = asyncAction();
                Assert.AreEqual(0, actionCallCount);
                Assert.AreEqual(0, innerActionCallCount);

                task.Wait(context);
                Assert.AreEqual(1, actionCallCount);
                Assert.AreEqual(2, innerActionCallCount);
            }
        }
コード例 #2
0
        public void WaitForTest()
        {
            using (var context = UnitTestSynchronizationContext.Create())
            {
                bool   flag        = false;
                Action asyncAction = async() =>
                {
                    await Task.Delay(10);

                    flag = true;
                };

                asyncAction();
                Assert.IsFalse(flag);

                context.WaitFor(() => flag, TimeSpan.FromSeconds(1));
                Assert.IsTrue(flag);

                AssertHelper.ExpectedException <TimeoutException>(() => context.WaitFor(() => !flag, TimeSpan.FromMilliseconds(10)));

                AssertHelper.ExpectedException <ArgumentNullException>(() => UnitTestSynchronizationContextExtensions.WaitFor(context, null, TimeSpan.FromMilliseconds(10)));
            }

            AssertHelper.ExpectedException <ArgumentNullException>(() => UnitTestSynchronizationContextExtensions.WaitFor(null, () => true, TimeSpan.FromMilliseconds(10)));
        }
コード例 #3
0
        public void PostAndWaitTest()
        {
            int actionCallCount = 0;
            var context         = UnitTestSynchronizationContext.Create();

            Func <Task> asyncAction = async() =>
            {
                await Task.Delay(1);

                Interlocked.Increment(ref actionCallCount);
            };

            var task = asyncAction();

            Assert.AreEqual(0, actionCallCount);

            task.Wait(context);
            Assert.AreEqual(1, actionCallCount);

            task = asyncAction();
            context.Wait(TimeSpan.FromMilliseconds(50));
            Assert.AreEqual(2, actionCallCount);

            context.Dispose();
            Assert.AreEqual(2, actionCallCount);
        }
コード例 #4
0
 public void PostAndWaitExceptionTest()
 {
     using (var context = UnitTestSynchronizationContext.Create())
     {
         AssertHelper.ExpectedException <ArgumentNullException>(() => context.Post(null, null));
         AssertHelper.ExpectedException <ArgumentNullException>(() => context.Wait(null));
     }
 }
コード例 #5
0
ファイル: ApplicationsTest.cs プロジェクト: zwq00000/waf
        protected override void OnInitialize()
        {
            base.OnInitialize();

            Context = UnitTestSynchronizationContext.Create();

            var configuration = new ContainerConfiguration()
                                .WithAssembly(typeof(ShellViewModel).GetTypeInfo().Assembly)
                                .WithAssembly(typeof(ApplicationsTest).GetTypeInfo().Assembly);

            Container = configuration.CreateContainer();
        }
コード例 #6
0
        public void SendTest()
        {
            int actionCallCount = 0;
            var context         = UnitTestSynchronizationContext.Create();

            context.Send(state => actionCallCount++, null);
            Assert.AreEqual(1, actionCallCount);

            AssertHelper.ExpectedException <ArgumentNullException>(() => context.Send(null, null));

            context.Dispose();
            context.Send(state => actionCallCount++, null);
            Assert.AreEqual(1, actionCallCount);
        }
コード例 #7
0
        public void CreateAndDisposingTest()
        {
            Assert.IsNull(SynchronizationContext.Current);
            using (var context = UnitTestSynchronizationContext.Create())
            {
                Assert.AreEqual(context, SynchronizationContext.Current);
                Assert.AreEqual(context, UnitTestSynchronizationContext.Current);

                using (var nestedContext = (UnitTestSynchronizationContext)context.CreateCopy())
                {
                    SynchronizationContext.SetSynchronizationContext(nestedContext);
                    Assert.AreEqual(nestedContext, SynchronizationContext.Current);
                    Assert.AreEqual(nestedContext, UnitTestSynchronizationContext.Current);
                }

                Assert.AreEqual(context, SynchronizationContext.Current);
            }
            Assert.IsNull(SynchronizationContext.Current);
        }
コード例 #8
0
        protected override void OnInitialize()
        {
            base.OnInitialize();

            context = UnitTestSynchronizationContext.Create();

            catalog = new AggregateCatalog();
            OnCatalogInitialize(catalog);

            container = new CompositionContainer(catalog, CompositionOptions.DisableSilentRejection);
            CompositionBatch batch = new CompositionBatch();

            batch.AddExportedValue(container);
            container.Compose(batch);

            ShellService shellService = Container.GetExportedValue <ShellService>();

            shellService.Settings = new AppSettings();

            ServiceLocator.RegisterInstance <IChangeTrackerService>(new ChangeTrackerService());
        }
コード例 #9
0
        public void WaitTest()
        {
            using (var context = UnitTestSynchronizationContext.Create())
            {
                int actionCallCount            = 0;
                Func <Task <int> > asyncAction = async() =>
                {
                    await Task.Delay(1);

                    Interlocked.Increment(ref actionCallCount);
                    return(actionCallCount);
                };

                var task = asyncAction();
                Assert.AreEqual(0, actionCallCount);

                task.Wait(context);
                Assert.AreEqual(1, actionCallCount);
            }

            AssertHelper.ExpectedException <ArgumentNullException>(() => UnitTestSynchronizationContextExtensions.Wait(Task.FromResult((object)null), null));
        }
コード例 #10
0
        public void InvokeMaxEveryDelayTimeTestWithSynchronizationContext()
        {
            using var context = UnitTestSynchronizationContext.Create();
            int actionCallCount = 0;
            var throttledAction = new ThrottledAction(() => Interlocked.Add(ref actionCallCount, 1), ThrottledActionMode.InvokeMaxEveryDelayTime, TimeSpan.FromMilliseconds(100));

            Assert.IsFalse(throttledAction.IsRunning);

            throttledAction.InvokeAccumulated();
            Assert.IsTrue(throttledAction.IsRunning);
            throttledAction.InvokeAccumulated();
            throttledAction.InvokeAccumulated();

            // As long the unit test synchronization context is not executed the actionCallCount must not be increased.
            Task.Delay(200).Wait();
            Assert.AreEqual(0, actionCallCount);

            // Execute the unit test synchronization context.
            context.WaitFor(() => actionCallCount > 0, TimeSpan.FromMilliseconds(200));
            Assert.AreEqual(1, actionCallCount);
            Assert.IsFalse(throttledAction.IsRunning);
        }
コード例 #11
0
        public void GetResultTest()
        {
            using (var context = UnitTestSynchronizationContext.Create())
            {
                int actionCallCount = 0;
                async Task <int> AsyncAction()
                {
                    await Task.Delay(1);

                    Interlocked.Increment(ref actionCallCount);
                    return(actionCallCount);
                }

                var task = AsyncAction();
                Assert.AreEqual(0, actionCallCount);

                var result = task.GetResult(context);
                Assert.AreEqual(1, actionCallCount);
                Assert.AreEqual(1, result);
            }

            AssertHelper.ExpectedException <ArgumentNullException>(() => UnitTestSynchronizationContextExtensions.GetResult(Task.FromResult((object)null !), null !));
        }