Esempio n. 1
0
        public async Task TestConcurrentCancelFirst()
        {
            var entryContext = AsyncTestContext.Current;
            var sut          = new FastAsyncLock();
            var ct           = new CancellationTokenSource();

            using (var otherThread = new AsyncTestRunner(CommonTwoStepsLock(sut)))
            {
                // Acquire the lock on another async context
                await otherThread.Advance();

                Assert.IsTrue(otherThread.HasLock());

                // Try to acquire the lock from this async context
                entryContext.Validate();
                var locking = sut.LockAsync(CancellationToken.None);
                Assert.AreEqual(TaskStatus.WaitingForActivation, locking.Status);

                // Cancel the CT of the other thread ... this should not impact the awaiter !
                ct.Cancel();
                Assert.AreEqual(TaskStatus.WaitingForActivation, locking.Status);

                // Finally validate that if we release the lock from the other thread, we are still able to acquire the lock
                await otherThread.Advance();

                Assert.AreEqual(TaskStatus.RanToCompletion, locking.Status);
            }
        }
Esempio n. 2
0
        public async Task TestExitFromAnotherExecutionContext()
        {
            var sut          = new FastAsyncLock();
            var locking      = default(IDisposable);
            var entryContext = default(AsyncTestContext);

            using (var externalThread = new AsyncTestRunner(ExternalThread))
            {
                entryContext = AsyncTestContext.Current;
                locking      = await sut.LockAsync(CancellationToken.None);

                await externalThread.AdvanceTo(1);

                Assert.IsTrue(externalThread.HasLock());

                await externalThread.AdvanceToEnd();
            }

            async Task ExternalThread(CancellationToken ct, AsyncTestRunner r)
            {
                Assert.AreNotEqual(entryContext, AsyncTestContext.Current);
                locking.Dispose();

                // Try to relock from the external execution context to validate that the lock was effectively released
                using (await sut.LockAsync(ct))
                {
                    r.HasLock(true);
                    r.Sync(position: 1);
                    await Task.Yield();
                }

                r.HasLock(false);
            }
        }
Esempio n. 3
0
        public static void TestRunnerControlInstantiation()
        {
            TestRunnerControl control = null;
            AsyncTestRunner   runner  = null;
            ObjectProxy       proxy   = null;

            try
            {
                proxy  = new StubObjectProxy();
                runner = new PmlTestRunner(proxy, new StubMethodInvoker());
                proxy  = null;
                var provider = new FileIndexTestCaseProvider();
                control = new TestRunnerControl(provider, runner, new RegistrySettingsProvider());
                runner  = null;
            }
            finally
            {
                if (proxy != null)
                {
                    proxy.Dispose();
                }
                if (runner != null)
                {
                    runner.Dispose();
                }
                if (control != null)
                {
                    control.Dispose();
                }
            }
        }
        public void InvokingAsyncVoidWhichThrowsIsObservedAndRethrown()
        {
            var methodInfo = typeof(WhenStepScannerFactoryAsyncMethods).GetMethod("AsyncTaskMethod", BindingFlags.Instance | BindingFlags.NonPublic);
            var stepAction = StepActionFactory.GetStepAction(methodInfo, new object[] { new SomeScenario() });

            Should.Throw <ArgumentException>(() => AsyncTestRunner.Run(() => stepAction(this)));
        }
        private void RunTests()
        {
            this.Title = "Unit Tests for " + new AssemblyName(this.GetType().Assembly.FullName).Name;

            if (TestRunner == null)
            {
                try {
                    OnTestRunStarted();
                }
                catch (Exception e) {
                    MethodInfo method = GetType().GetMethod("OnTestRunStarted", BindingFlags.NonPublic | BindingFlags.Instance);
                    this.m_headerOutcome = new MethodOutcome(method);
                    this.m_headerOutcome.SetOutcome(e);

                    this.m_headerView.SetHtml(TestClassResultActivity.GetHTMLDescriptionFor(this.m_headerOutcome));
                    this.m_headerView.Visibility = ViewStates.Visible;

                    this.ResultBar.SetColorByState(TestState.Failed);
                    Toast.MakeText(this, "OnTestRunStarted() notification failed.", ToastLength.Long).Show();
                    return;
                }

                this.m_headerView.Visibility = ViewStates.Gone;

                this.ResultBar.SetColorByState(TestState.Running);
                AsyncTestRunner.Run(this, this.CreateTestRunner, this.OnTestRunFinished);
            }
        }
Esempio n. 6
0
        public async Task TestReleaseThenReAcquireWithConcurrentAccess()
        {
            var sut = new FastAsyncLock();

            using (var otherThread = new AsyncTestRunner(OtherThread))
            {
                await otherThread.AdvanceTo(1);

                Assert.IsTrue(otherThread.HasLock());

                await otherThread.AdvanceTo(2);

                Assert.IsFalse(otherThread.HasLock());

                using (await sut.LockAsync(CancellationToken.None))
                {
                    await Task.Yield();

                    await otherThread.AdvanceAndFreezeBefore(3);

                    Assert.IsFalse(otherThread.HasLock());
                }

                await otherThread.AdvanceTo(4);

                Assert.IsTrue(otherThread.HasLock());
            }

            async Task OtherThread(CancellationToken ct, AsyncTestRunner r)
            {
                using (await sut.LockAsync(CancellationToken.None))
                {
                    await Task.Yield();

                    r.HasLock(true);
                    r.Sync(position: 1);
                }

                await Task.Yield();

                r.HasLock(false);
                r.Sync(position: 2);

                using (await sut.LockAsync(CancellationToken.None))
                {
                    await Task.Yield();

                    r.HasLock(true);
                    r.Sync(position: 3);
                    r.Sync(position: 4);
                }

                await Task.Yield();

                r.HasLock(false);
            }
        }
Esempio n. 7
0
        public virtual void SetUp()
        {
            contextManagerStub      = new Mock <IContextManager>();
            testExecutionEngineStub = new Mock <ITestExecutionEngine>();
            testExecutionEngineStub.Setup(tr => tr.ScenarioContext).Returns(() => contextManagerStub.Object.ScenarioContext);
            testExecutionEngineStub.Setup(tr => tr.FeatureContext).Returns(() => contextManagerStub.Object.FeatureContext);
            testExecutionEngineStub.Setup(m => m.OnScenarioStart(It.IsAny <ScenarioInfo>())).Callback(
                (ScenarioInfo si) => contextManagerStub.Setup(cm => cm.ScenarioContext).Returns(new ScenarioContext(si, asyncTestRunner, null))
                );
            fakeAsyncTestExecutor = new FakeAsyncTestExecutor();

            asyncTestRunner = new AsyncTestRunner(testExecutionEngineStub.Object);
        }
Esempio n. 8
0
        public TestForm()
        {
            ObjectProxy       proxy   = null;
            AsyncTestRunner   runner  = null;
            TestRunnerControl control = null;

            try
            {
                Index         = new MutablePathIndex(Path.GetFullPath("..\\..\\..\\..\\pmllib-tests"));
                proxy         = new StubObjectProxy();
                runner        = new PmlTestRunner(proxy, new ControlMethodInvoker(this), new StubClock(), Index);
                proxy         = null;
                control       = new TestRunnerControl(Index, runner, new RegistrySettingsProvider());
                RunnerControl = control;
                runner        = null;
                control.Dock  = DockStyle.Fill;

                InitializeComponent();

                PathComboBox.Text          = Index.Path;
                FolderBrowser.SelectedPath = Index.Path;

                ControlPanel.Controls.Add(control);

                control = null;
            }
            finally
            {
                if (control != null)
                {
                    control.Dispose();
                }
                if (runner != null)
                {
                    runner.Dispose();
                }
                if (proxy != null)
                {
                    proxy.Dispose();
                }
            }
        }
Esempio n. 9
0
        public async Task TestConcurrentAccess()
        {
            var entryContext = AsyncTestContext.Current;
            var sut          = new FastAsyncLock();

            using (var otherThread = new AsyncTestRunner(CommonTwoStepsLock(sut)))
            {
                // Acquire the lock on another async context
                await otherThread.Advance();

                Assert.IsTrue(otherThread.HasLock());

                // Try to acquire the lock from this async context
                entryContext.Validate();
                var locking = sut.LockAsync(CancellationToken.None);
                Assert.AreEqual(TaskStatus.WaitingForActivation, locking.Status);

                await otherThread.Advance();                                 // Will release the lock

                Assert.AreEqual(TaskStatus.RanToCompletion, locking.Status); // so lock is now acquired (sync)
            }
        }
Esempio n. 10
0
        public async Task TestConcurrentCancelSecond()
        {
            var entryContext = AsyncTestContext.Current;
            var sut          = new FastAsyncLock();
            var ct           = new CancellationTokenSource();

            using (var otherThread = new AsyncTestRunner(CommonTwoStepsLock(sut)))
            {
                // Acquire the lock on another async context
                await otherThread.Advance();

                Assert.IsTrue(otherThread.HasLock());

                // Try to acquire the lock from this async context
                entryContext.Validate();
                var locking = sut.LockAsync(ct.Token);
                Assert.AreEqual(TaskStatus.WaitingForActivation, locking.Status);

                // But cancel before the other async context completes
                ct.Cancel();
                Assert.AreEqual(TaskStatus.Canceled, locking.Status);
            }
        }
Esempio n. 11
0
 internal static void HasLock(this AsyncTestRunner runner, bool value) => runner.Set("hasLock", value);
Esempio n. 12
0
 internal static bool HasLock(this AsyncTestRunner runner) => runner.Get <bool>("hasLock");
Esempio n. 13
0
        [Ignore] // https://github.com/nventive/Uno.Core/issues/48
        public async Task TestUnlockReleaseNextSynchronously()
        {
            Console.WriteLine($"Running on thread {Thread.CurrentThread.ManagedThreadId}");

            var sut = new FastAsyncLock();
            var thread1ExitingThread = -1;
            var thread2LockingTask   = default(Task <IDisposable>);
            var thread1 = new AsyncTestRunner(Thread1);
            var thread2 = new AsyncTestRunner();

            thread2.Run(Thread2);

            using (thread1)
                using (thread2)
                {
                    // Acquire the lock on thread 1
                    await thread1.AdvanceTo(1);

                    // Wait for the thread 2 to be stuck to acquire the lock
                    var thread2Locking = thread2.AdvanceTo(1);
                    while (thread2LockingTask == null)
                    {
                        await Task.Yield();
                    }

                    // Make sure that the thread 2 is really awaiting the thread2LockingTask before requesting thread1 to continue
                    await Task.Delay(100);

                    Assert.AreEqual(TaskStatus.WaitingForActivation, thread2LockingTask.Status);

                    // Relase the thread 1 and make sure that the thread 2 is able to acquire the lock
                    var   thread1Release = thread1.AdvanceTo(2);
                    await thread2Locking;
                }

            async Task Thread1(CancellationToken ct, AsyncTestRunner r)
            {
                Console.WriteLine($"Thread 1: {Thread.CurrentThread.ManagedThreadId}");

                var ctx = AsyncTestContext.Current;

                using (await sut.LockAsync(ct))
                {
                    Console.WriteLine($"Acquired lock for Thread1 on thread: {Thread.CurrentThread.ManagedThreadId}");

                    ctx.Validate();
                    await Task.Yield();

                    r.Sync(position: 1);

                    thread1ExitingThread = Thread.CurrentThread.ManagedThreadId;
                    Console.WriteLine($"Releasing lock from Thread1 on thread: {Thread.CurrentThread.ManagedThreadId}");
                }

                Console.WriteLine($"Released lock from Thread1 on thread: {Thread.CurrentThread.ManagedThreadId}");

                // This must have run synchronously when lock got released (disposed).
                Assert.AreEqual(TaskStatus.RanToCompletion, thread2LockingTask.Status);

                r.Sync(position: 2);
            }

            async Task Thread2(CancellationToken ct, AsyncTestRunner r)
            {
                Console.WriteLine($"Thread 2: {Thread.CurrentThread.ManagedThreadId}");

                var ctx = AsyncTestContext.Current;

                thread2LockingTask = sut.LockAsync(ct);

                // Validate that we are running on thread 2
                Assert.AreEqual(thread2.ThreadId, Thread.CurrentThread.ManagedThreadId);

                Console.WriteLine("Thread 2 is waiting for lock");
                using (await thread2LockingTask)
                {
                    Console.WriteLine($"Acquired lock for Thread2 on thread: {Thread.CurrentThread.ManagedThreadId}");

                    // Here we should run on the thread 1 since the lock is released synchronously from thread 1
                    Assert.AreEqual(thread1ExitingThread, Thread.CurrentThread.ManagedThreadId);

                    // But we should have kept the ExecutionContext from the thread 2
                    ctx.Validate();

                    await Task.Yield();

                    r.Sync(position: 1);

                    Console.WriteLine($"Releasing lock from Thread2 on thread: {Thread.CurrentThread.ManagedThreadId}");
                }

                Console.WriteLine($"Released lock from Thread2 on thread: {Thread.CurrentThread.ManagedThreadId}");

                r.Sync(position: 2);
            }
        }
        public void CallingAsyncVoidWhichThrowsIsObservedAndRethrown()
        {
            var stepAction = StepActionFactory.GetStepAction <SomeScenario>(s => AsyncVoidMethod(s));

            Should.Throw <ArgumentException>(() => AsyncTestRunner.Run(() => stepAction(new SomeScenario())));
        }
Esempio n. 15
0
        public async Task TestConcurrentCancelSecondWithThird()
        {
            var sut = new FastAsyncLock();
            var ct  = new CancellationTokenSource();

            var thread2LockingTask = default(Task <IDisposable>);

            using (var thread1 = new AsyncTestRunner(Thread1))
                using (var thread2 = new AsyncTestRunner(Thread2))
                    using (var thread3 = new AsyncTestRunner(Thread3))
                    {
                        // Acquire the lock on thread 1 THEN
                        await thread1.AdvanceTo(1);

                        Assert.IsTrue(thread1.HasLock());

                        // Try to acquire the lock from this async context
                        await thread2.AdvanceAndFreezeBefore(1);

                        //var locking = sut.LockAsync(ct.Token);
                        Assert.AreEqual(TaskStatus.WaitingForActivation, thread2LockingTask.Status);
                        Assert.IsFalse(thread2.HasLock());

                        // Try to acquire it on thread 3
                        var t3Locked = thread3.AdvanceTo(1);
                        await thread3.IsFrozen();

                        Assert.IsFalse(thread3.HasLock());

                        // But cancel before the other async context completes
                        ct.Cancel();
                        Assert.AreEqual(TaskStatus.Canceled, thread2LockingTask.Status);

                        // Release the lock from thread1, and wait for thread 3 to acquire the lock
                        await thread1.AdvanceAndFreezeBefore(2);         // will freeze in continuation of thread 3

                        await t3Locked;

                        //Assert.IsFalse(thread1.HasLock()); // flag not set yet: the thread 1 is dead locked by the continuation of thread 3
                        Assert.IsTrue(thread3.HasLock());

                        await thread3.AdvanceToEnd();

                        //await thread1.AdvanceToEnd();
                        await Task.Delay(500);

                        Assert.IsFalse(thread1.HasLock());
                        Assert.IsFalse(thread3.HasLock());
                    }


            async Task Thread1(CancellationToken ct2, AsyncTestRunner r)
            {
                using (await sut.LockAsync(ct2))
                {
                    r.HasLock(true);
                    r.Sync(position: 1);
                }

                r.HasLock(false);
                r.Sync(position: 2);
            };

            async Task Thread2(CancellationToken ct2, AsyncTestRunner r)
            {
                thread2LockingTask = sut.LockAsync(ct.Token);
                using (await thread2LockingTask)
                {
                    r.HasLock(true);
                    r.Sync(position: 1);
                }

                r.HasLock(false);
                r.Sync(position: 2);
            }

            async Task Thread3(CancellationToken ct2, AsyncTestRunner r)
            {
                using (await sut.LockAsync(ct2))
                {
                    r.HasLock(true);
                    r.Sync(position: 1);
                }

                r.HasLock(false);
                r.Sync(position: 2);
            };
        }
            public static void Run(Context ctx, Func <TestRunner> testRunnerCreatorFunc, Action <TestRunner> finishedHandler)
            {
                AsyncTestRunner runner = new AsyncTestRunner(ctx, testRunnerCreatorFunc, finishedHandler);

                runner.Execute();
            }
Esempio n. 17
0
        public async Task TestReEntrencyWithConcurrentAccess()
        {
            var sut = new FastAsyncLock();

            using (var thread2 = new AsyncTestRunner(Thread2))
                using (var thread1 = new AsyncTestRunner(Thread1))
                {
                    // Enter from main thread
                    await thread1.AdvanceTo(1);

                    // Try enter from second thread
                    var thread2Locking = thread2.AdvanceTo(2);
                    await Task.Delay(100);

                    Assert.AreEqual(TaskStatus.WaitingForActivation, thread2Locking.Status);

                    // ReEnter
                    await thread1.AdvanceTo(2);

                    // Exit once
                    await thread1.AdvanceTo(3);

                    Assert.AreEqual(TaskStatus.WaitingForActivation, thread2Locking.Status);

                    // Final exit
                    thread1.AdvanceTo(4);
                    await thread2Locking;
                }

            async Task Thread1(CancellationToken ct, AsyncTestRunner r)
            {
                using (await sut.LockAsync(CancellationToken.None))
                {
                    await Task.Yield();

                    r.HasLock(true);
                    r.Sync(position: 1);

                    using (await sut.LockAsync(CancellationToken.None))
                    {
                        await Task.Yield();

                        r.Sync(position: 2);
                    }

                    r.Sync(position: 3);
                }

                r.HasLock(false);
                r.Sync(position: 4);
            }

            async Task Thread2(CancellationToken ct, AsyncTestRunner r)
            {
                using (await sut.LockAsync(ct))
                {
                    r.HasLock(true);
                    r.Sync(position: 1);
                }

                r.HasLock(false);
                r.Sync(position: 2);
            }
        }
Esempio n. 18
0
        public void CallingAsyncTaskWhichThrowsIsObservedAndRethrown()
        {
            var stepAction = StepActionFactory.GetStepAction <SomeScenario>(o => AsyncTaskMethod(o));

            Assert.Throws <ArgumentException>(() => AsyncTestRunner.Run(() => stepAction(new SomeScenario())));
        }