When_Awaiting_Running_Task_And_Using_SynchronizationContext_Then_Continuation_Run_In_Synchronization_Context()
        {
            var mockSynchronizationContext = new MockSynchronizationContext();

            using (var contextScope = new ScopedSynchronizationContext(mockSynchronizationContext))
            {
                When_Awaiting_Running_Task_And_Using_SynchronizationContext_Then_Continuation_Run_In_Synchronization_Context_impl(
                    mockSynchronizationContext).Wait();
            }
        }
        When_Awaiting_Already_Completed_Task_And_Using_SynchronizationContext_Then_Continuation_Does_Not_Run_In_SynchronizationContext()
        {
            var mockSynchronizationContext = new MockSynchronizationContext();

            using (var contextScope = new ScopedSynchronizationContext(mockSynchronizationContext))
            {
                When_Awaiting_Already_Completed_Task_And_Using_SynchronizationContext_Then_Synchronization_Context_Is_Not_Used_Impl(
                    mockSynchronizationContext).Wait();
            }
        }
        When_Awaiting_Many_Running_Tasks_And_Using_Using_CurrentThread_SynchronizationContext_Then_Only_First_Continuation_Run_In_Synchronization_Context(int numberOfAwaitedTasks)
        {
            var mockSynchronizationContext = new MockSynchronizationContext();

            using (var contextScope = new ScopedSynchronizationContext(mockSynchronizationContext))
            {
                When_Awaiting_Many_Running_Tasks_And_Using_CurrentThread_SynchronizationContext_Then_Only_First_Continuation_Run_In_Synchronization_Context(
                    numberOfAwaitedTasks,
                    mockSynchronizationContext).Wait();
            }
        }
        Task When_Awaiting_Many_Running_Tasks_And_Using_CurrentThread_SynchronizationContext_Then_Only_First_Continuation_Run_In_Synchronization_Context(
            int numberOfTasks,
            MockSynchronizationContext mockSynchronizationContext)
        {
            const int EXPECTED_SYNCHRONIZATION_CONTEXT_CALLS = 1;

            for (int i = 0; i < numberOfTasks; i++)
            {
                await runningTaskCommon();
            }

            var totalCallsInSynchronizationContext = mockSynchronizationContext.TotalCalls;

            Assert.That(totalCallsInSynchronizationContext, Is.EqualTo(EXPECTED_SYNCHRONIZATION_CONTEXT_CALLS));
        }
        private async Task When_Awaiting_Already_Completed_Task_And_Using_SynchronizationContext_Then_Synchronization_Context_Is_Not_Used_Impl(MockSynchronizationContext mockSynchronizationContext)
        {
            const int SYNCH_CONTEXT_NOT_USED = 0;
            var       task = getcompletedTask();
            await     task;

            Assert.That(mockSynchronizationContext.TotalCalls, Is.EqualTo(SYNCH_CONTEXT_NOT_USED));
        }
        private async Task When_Awaiting_Running_Task_And_Using_SynchronizationContext_Then_Continuation_Run_In_Synchronization_Context_impl(MockSynchronizationContext mockSynchronizationContext)
        {
            const int EXPECTED_SYNCHRONIZATION_CONTEXT_CALLS = 1;

            await runningTaskCommon();

            var totalCallsInSynchronizationContext = mockSynchronizationContext.TotalCalls;

            Assert.That(totalCallsInSynchronizationContext, Is.EqualTo(EXPECTED_SYNCHRONIZATION_CONTEXT_CALLS));
        }