Пример #1
0
        public async Task Enqueue_Action_Should_Cancel_Task_When_On_Disposed_Queue()
        {
            MonoThreadedQueue queue = null;

            //arrange
            using (queue = new MonoThreadedQueue())
            {
                var task = queue.Enqueue(() => TaskFactory());
            }

            bool Done       = false;
            Task newesttask = queue.Enqueue(() => { Done = true; });

            TaskCanceledException error = null;

            try
            {
                await newesttask;
            }
            catch (TaskCanceledException e)
            {
                error = e;
            }

            newesttask.IsCanceled.Should().BeTrue();
            error.Should().NotBeNull();
            Done.Should().BeFalse();
        }
Пример #2
0
        public async Task Enqueue_Func_T_Should_Cancel_Task_When_On_Disposed_Queue()
        {
            MonoThreadedQueue queue = null;

            //arrange
            using (queue = new MonoThreadedQueue())
            {
                var task = queue.Enqueue(() => TaskFactory());
            }


            Func <int> func       = () => { return(25); };
            Task       newesttask = queue.Enqueue(func);

            TaskCanceledException error = null;

            try
            {
                await newesttask;
            }
            catch (TaskCanceledException e)
            {
                error = e;
            }

            newesttask.IsCanceled.Should().BeTrue();
            error.Should().NotBeNull();
        }
Пример #3
0
        internal async Task <Tuple <T, MonoThreadedQueue> > InternalBuildAsync <T>(Func <T> concrete) where T : class
        {
            var queue = new MonoThreadedQueue(_OnCreate);
            var actor = await queue.Enqueue(() => Build <T>(concrete(), queue)).ConfigureAwait(false);

            return(new Tuple <T, MonoThreadedQueue>(actor, queue));
        }
Пример #4
0
        public async Task Enqueue_Action_Should_Cancel_Not_Started_Task_When_OnDispose()
        {
            Task newTask = null, notstarted = null;
            bool Done = false;

            //arrange
            using (var target = new MonoThreadedQueue(t => t.Priority = ThreadPriority.Highest))
            {
                newTask = target.Enqueue(() => TaskFactory(3));

                while (_RunningThread == null)
                {
                    Thread.Sleep(100);
                }

                notstarted = target.Enqueue(() => { Done = true; });
            }

            await newTask;

            TaskCanceledException error = null;

            try
            {
                await notstarted;
            }
            catch (TaskCanceledException e)
            {
                error = e;
            }

            notstarted.IsCanceled.Should().BeTrue();
            error.Should().NotBeNull();
            Done.Should().BeFalse();
        }
Пример #5
0
        private T Build <T>(T concrete, MonoThreadedQueue queue) where T : class
        {
            var asyncDisposable = concrete as IAsyncDisposable;

            return(CreateIActorLifeCycle(concrete, queue, TypeHelper.IActorCompleteLifeCycleType,
                                         new ActorCompleteLifeCycleInterceptor(queue, asyncDisposable),
                                         new ActorLifeCycleInterceptor(queue, asyncDisposable)));
        }
        public MonoThreadedQueueSynchronizationContext(MonoThreadedQueue dispatcher)
        {
            if (dispatcher == null)
                throw new ArgumentNullException("dispatcher");

            _dispatcher = dispatcher;

            SetWaitNotificationRequired();
        }
Пример #7
0
        public async Task Enqueue_Should_Run_OnSeparatedThread()
        {
            Thread current = Thread.CurrentThread;

            //arrange
            using (var target = new MonoThreadedQueue())
            {
                //act
                await target.Enqueue(() => TaskFactory());

                //assert
                _RunningThread.Should().NotBeNull();
                _RunningThread.Should().NotBe(current);
            }
        }
Пример #8
0
        public async Task Enqueue_Should_Work_With_Result()
        {
            Thread current = Thread.CurrentThread;

            //arrange
            using (var target = new MonoThreadedQueue())
            {
                //act
                var res = await target.Enqueue(() => TaskFactory <int>(25));

                //assert
                res.Should().Be(25);
                _RunningThread.Should().NotBeNull();
                _RunningThread.Should().NotBe(current);
            }
        }
Пример #9
0
        public async Task Enqueue_Should_Run_OnSameThread()
        {
            Thread current = Thread.CurrentThread;

            //arrange
            using (var target = new MonoThreadedQueue())
            {
                //act
                await target.Enqueue(() => TaskFactory());

                var first = _RunningThread;
                await target.Enqueue(() => TaskFactory());

                //assert
                _RunningThread.Should().Be(first);
            }
        }
Пример #10
0
        public async Task Enqueue_Should_Work_OnAction()
        {
            Thread current = Thread.CurrentThread;

            //arrange
            using (var target = new MonoThreadedQueue())
            {
                bool   done = false;
                Action act  = () => done = true;
                //act

                await target.Enqueue(act);

                //assert
                done.Should().BeTrue();
            }
        }
Пример #11
0
        public async Task Enqueue_Func_T_Should_Work_AsExpected_With_Result()
        {
            Thread current = Thread.CurrentThread;

            //arrange
            using (var target = new MonoThreadedQueue())
            {
                Func <int> func = () => { _RunningThread = Thread.CurrentThread; return(25); };

                //act
                var res = await target.Enqueue(func);

                //assert
                res.Should().Be(25);
                _RunningThread.Should().NotBeNull();
                _RunningThread.Should().NotBe(current);
            }
        }
Пример #12
0
        public async Task Dispose_Running_Task_Should_Continue_After_Stoping_Queue()
        {
            //arrange
            MonoThreadedQueue queue = new MonoThreadedQueue();
            var task = queue.Enqueue(() => TaskFactory(3));

            while (_RunningThread == null)
            {
                Thread.Sleep(100);
            }

            //act
            queue.Dispose();
            await task;

            //assert
            task.IsCompleted.Should().BeTrue();
        }
Пример #13
0
        public async Task Enqueue_Exception_Should_Not_Kill_MainThead()
        {
            Thread current = Thread.CurrentThread;

            //arrange
            using (var target = new MonoThreadedQueue())
            {
                //act
                try
                {
                    await target.Enqueue(() => Throw());
                }
                catch
                {
                }

                await target.Enqueue(() => TaskFactory());
            }
        }
Пример #14
0
        public async Task Enqueue_Should_DispatchException_With_Result()
        {
            //arrange
            using (var target = new MonoThreadedQueue())
            {
                Exception error = null;
                //act
                try
                {
                    await target.Enqueue(() => Throw <string>());
                }
                catch (Exception e)
                {
                    error = e;
                }

                //assert
                error.Should().NotBeNull();
            }
        }
Пример #15
0
        public async Task Enqueue_Should_Not_Cancel_Already_Runing_Task_OnDispose()
        {
            Task newTask = null;

            //arrange
            using (var target = new MonoThreadedQueue(t => t.Priority = ThreadPriority.Highest))
            {
                newTask = target.Enqueue(() => TaskFactory(3));

                while (_RunningThread == null)
                {
                    Thread.Sleep(100);
                }
            }

            await newTask;

            _RunningThread.Should().NotBeNull();

            newTask.IsCanceled.Should().BeFalse();
        }
Пример #16
0
        public async Task Dispose_Enqueued_Task_Should_Continue_After_Stoping_Queue()
        {
            //arrange
            MonoThreadedQueue queue = new MonoThreadedQueue();
            Task task = queue.Enqueue(() => TaskFactory(3)), enqueuedtask = null;

            while (_RunningThread == null)
            {
                Thread.Sleep(100);
            }
            enqueuedtask = queue.Enqueue(() => TaskFactory(3));

            //act
            await queue.Stop(() => TaskBuilder.Completed);

            await enqueuedtask;

            //assert
            task.IsCompleted.Should().BeTrue();
            enqueuedtask.IsCompleted.Should().BeTrue();
        }
Пример #17
0
        public async Task Enqueue_Exception_Should_Not_Kill_MainThead_With_Result()
        {
            Thread current = Thread.CurrentThread;

            //arrange
            using (var target = new MonoThreadedQueue())
            {
                //act
                try
                {
                    await target.Enqueue(() => Throw <int>());
                }
                catch
                {
                }

                var res = await target.Enqueue(() => TaskFactory <int>(25));

                //assert
                res.Should().Be(25);
            }
        }
Пример #18
0
        public async Task Enqueue_Should_DispatchException()
        {
            Thread current = Thread.CurrentThread;

            //arrange
            using (var target = new MonoThreadedQueue())
            {
                Exception error = null;
                //act
                try
                {
                    await target.Enqueue(() => Throw());
                }
                catch (Exception e)
                {
                    error = e;
                }

                //assert
                error.Should().NotBeNull();
            }
        }
Пример #19
0
        public async Task Dispose_Enqueue_Items_Should_Return_Canceled_Task_After_Stoping_Queue()
        {
            //arrange
            MonoThreadedQueue queue = new MonoThreadedQueue();

            queue.Dispose();

            bool Done = false;

            TaskCanceledException error = null;

            try
            {
                await queue.Enqueue(() => { Done = true; });
            }
            catch (TaskCanceledException e)
            {
                error = e;
            }

            error.Should().NotBeNull();
            Done.Should().BeFalse();
        }
 public DispatcherSynchronizationContextTest()
 {
     _Queue      = new MonoThreadedQueue(t => t.Priority = ThreadPriority.Highest);
     _Dispatcher = new  MonoThreadedQueueSynchronizationContext(_Queue);
 }
Пример #21
0
        public Task <T> BuildAsync <T>(Func <T> concrete) where T : class
        {
            var queue = new MonoThreadedQueue(_OnCreate);

            return(queue.Enqueue(() => Build <T>(concrete(), queue)));
        }
 public DispatcherSynchronizationContextTest()
 {
     _Queue = new MonoThreadedQueue(t => t.Priority = ThreadPriority.Highest);
     _Dispatcher = new  MonoThreadedQueueSynchronizationContext(_Queue);
 }