public void ForNonLongRunningTasksShouldUseThreadPoolThreads()
        {
            UnpauseScheduler();

            // It would be nice to check here that a new queue on thread pool occurs for
            // each task rather than using the same thread. However, that's not possible
            // as you will probably get the same thread anyway.
            var task1ThreadId = 0;
            var task2ThreadId = 0;
            var barrier1      = new TaskCompletionSource <bool>();
            var task1         = _actorTaskFactory.StartNew(
                () =>
            {
                task1ThreadId = Thread.CurrentThread.ManagedThreadId;
                ActorThreadAssertions.CurrentThreadShouldBeThreadPoolThread();
                ThrowIfWaitTimesOut(barrier1.Task);
            }, CancellationToken.None, TaskCreationOptions.None);
            var task2 = _actorTaskFactory.StartNew(
                () =>
            {
                task2ThreadId = Thread.CurrentThread.ManagedThreadId;
                ActorThreadAssertions.CurrentThreadShouldBeThreadPoolThread();
            }, CancellationToken.None, TaskCreationOptions.None);

            barrier1.SetResult(true);

            ThrowIfWaitTimesOut(task1);
            ThrowIfWaitTimesOut(task2);

            task1ThreadId.Should().NotBe(0);
            task2ThreadId.Should().NotBe(0);
        }
        public void ForLongRunningTasksShouldUseNonThreadPoolThreadIfActiveThreadFromThreadPool()
        {
            UnpauseScheduler();

            var shortTaskThreadId = 0;
            var longTaskThreadId  = 0;
            var barrier           = new TaskCompletionSource <bool>();
            var shortTask         = _actorTaskFactory.StartNew(
                () =>
            {
                ActorThreadAssertions.CurrentThreadShouldBeThreadPoolThread();
                ThrowIfWaitTimesOut(barrier.Task);
                shortTaskThreadId = Thread.CurrentThread.ManagedThreadId;
            }, CancellationToken.None, TaskCreationOptions.None, ActorTaskTraits.None);
            var longTask = _actorTaskFactory.StartNew(
                () =>
            {
                ActorThreadAssertions.CurrentThreadShouldNotBeThreadPoolThread();
                longTaskThreadId = Thread.CurrentThread.ManagedThreadId;
            }, CancellationToken.None, TaskCreationOptions.LongRunning, ActorTaskTraits.None);

            barrier.SetResult(true);

            ThrowIfWaitTimesOut(shortTask);
            ThrowIfWaitTimesOut(longTask);

            shortTaskThreadId.Should().NotBe(0);
            longTaskThreadId.Should().NotBe(0);
            longTaskThreadId.Should().NotBe(shortTaskThreadId);
        }
예제 #3
0
 private static void ValidateActorThread(ActorEnqueueOptions enqueueOptions)
 {
     if (enqueueOptions.HasFlag(ActorEnqueueOptions.WorkIsLongRunning))
     {
         ActorThreadAssertions.CurrentThreadShouldNotBeThreadPoolThread();
     }
     else
     {
         ActorThreadAssertions.CurrentThreadShouldBeThreadPoolThread();
     }
 }