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();
     }
 }
示例#4
0
        public void ShouldBeAbleToSpecifyThatSyncWorkIsLongRunning()
        {
            var taskCompletionSource = new TaskCompletionSource <object>();

            _scheduler.Schedule(
                () =>
            {
                try
                {
                    ActorThreadAssertions.CurrentThreadShouldNotBeThreadPoolThread();
                    taskCompletionSource.TrySetResult(null);
                }
                catch (Exception exception)
                {
                    taskCompletionSource.TrySetException(exception);
                }
            }, TimeSpan.FromMilliseconds(1000), ActorScheduleOptions.NoInitialDelay | ActorScheduleOptions.WorkIsLongRunning);

            taskCompletionSource.Awaiting(x => x.Task).Should().NotThrow();
        }
        public void ForNonLongRunningTasksShouldUseActiveThreadIfNotFromThreadPool()
        {
            UnpauseScheduler();

            var task1ThreadId = 0;
            var task2ThreadId = 0;
            var task3ThreadId = 0;
            var barrier       = new TaskCompletionSource <bool>();
            var task1         = _actorTaskFactory.StartNew(
                () =>
            {
                task1ThreadId = Thread.CurrentThread.ManagedThreadId;
                ActorThreadAssertions.CurrentThreadShouldNotBeThreadPoolThread();
                ThrowIfWaitTimesOut(barrier.Task);
            }, CancellationToken.None, TaskCreationOptions.LongRunning);
            var task2 = _actorTaskFactory.StartNew(
                () =>
            {
                task2ThreadId = Thread.CurrentThread.ManagedThreadId;
                ActorThreadAssertions.CurrentThreadShouldNotBeThreadPoolThread();
            }, CancellationToken.None, TaskCreationOptions.None);
            var task3 = _actorTaskFactory.StartNew(
                () =>
            {
                task3ThreadId = Thread.CurrentThread.ManagedThreadId;
                ActorThreadAssertions.CurrentThreadShouldNotBeThreadPoolThread();
            }, CancellationToken.None, TaskCreationOptions.None);

            barrier.SetResult(true);

            ThrowIfWaitTimesOut(task1);
            ThrowIfWaitTimesOut(task2);
            ThrowIfWaitTimesOut(task3);

            task1ThreadId.Should().NotBe(0);
            task2ThreadId.Should().Be(task1ThreadId);
            task3ThreadId.Should().Be(task1ThreadId);
        }