Esempio n. 1
0
        public void FuzzyScheduling(int producerCount, bool perCpu, int taskPerProducer)
        {
            int producerCountFinal = perCpu ? producerCount * Environment.ProcessorCount : producerCount;
            var valueContainer     = new Container <int>
            {
                Value = taskPerProducer * producerCountFinal
            };
            var    mre  = new ManualResetEvent(false);
            Action noop = () =>
            {
                if (--valueContainer.Value <= 0)
                {
                    Assert.Equal(0, valueContainer.Value);
                    mre.Set();
                }
            };
            var scheduler = new SingleThreadEventExecutor("test", TimeSpan.FromSeconds(1));
            IEnumerable <Task <Task> > producers = Enumerable.Range(1, producerCountFinal).Select(x => Task.Factory.StartNew(
                                                                                                      async() =>
            {
                var r = new Random((int)Stopwatch.GetTimestamp() ^ x);
                for (int i = 0; i < taskPerProducer; i++)
                {
                    scheduler.Execute(noop);
                    await Task.Delay(r.Next(10, 100));
                }
            },
                                                                                                      TaskCreationOptions.LongRunning));

            Task.WhenAll(producers).Wait();
            Assert.True(mre.WaitOne(TimeSpan.FromSeconds(5)));
        }
Esempio n. 2
0
        public void TaskSchedulerIsPreserved()
        {
            var executor = new SingleThreadEventExecutor("test", TimeSpan.FromSeconds(5));
            IEnumerable <Task <int> > tasks = Enumerable.Range(1, 1).Select(i =>
            {
                var completion = new TaskCompletionSource <int>();
                executor.Execute(async() =>
                {
                    try
                    {
                        Assert.True(executor.InEventLoop);
                        await Task.Delay(1);
                        Assert.True(executor.InEventLoop);
                        completion.TrySetResult(0); // all is well
                    }
                    catch (Exception ex)
                    {
                        completion.TrySetException(ex);
                    }
                });
                return(completion.Task);
            });

            Task.WhenAll(tasks).Wait(TimeSpan.FromSeconds(500));
            executor.ShutdownGracefullyAsync();
        }
        public void TaskSchedulerIsPreserved()
        {
            var executor = new SingleThreadEventExecutor("test", TimeSpan.FromSeconds(5));
            IEnumerable <Task <int> > tasks = Enumerable.Range(1, 1).Select(async i =>
            {
                //Clear SynchronizationContext set by xunit
                SynchronizationContext.SetSynchronizationContext(null);

                var completion = new TaskCompletionSource();
                executor.Execute(async() =>
                {
                    try
                    {
                        Assert.True(executor.InEventLoop);
                        await Task.Delay(1);
                        Assert.True(executor.InEventLoop);
                        completion.TryComplete(); // all is well
                    }
                    catch (Exception ex)
                    {
                        completion.TrySetException(ex);
                    }
                });
                await completion.Task;
                Assert.False(executor.InEventLoop);
                return(i);
            });

            Task.WhenAll(tasks).Wait(TimeSpan.FromSeconds(500));
            executor.ShutdownGracefullyAsync();
        }
Esempio n. 4
0
        public async Task ScheduledTaskFiresOnTimeWhileBusy()
        {
            var    scheduler       = new SingleThreadEventExecutor(null, TimeSpan.FromMilliseconds(10));
            var    promise         = new TaskCompletionSource();
            Action selfQueueAction = null;

            selfQueueAction = () =>
            {
                if (!promise.Task.IsCompleted)
                {
                    scheduler.Execute(selfQueueAction);
                }
            };

            scheduler.Execute(selfQueueAction);
            Task task = scheduler.ScheduleAsync(() => promise.Complete(), TimeSpan.FromMilliseconds(100));
            await Task.WhenAny(task, Task.Delay(TimeSpan.FromMilliseconds(300)));

            Assert.True(task.IsCompleted);
        }
Esempio n. 5
0
        public void STE_should_execute_scheduled_tasks_on_time()
        {
            var counter  = new AtomicCounter(0);
            var executor = new SingleThreadEventExecutor("Foo" + ThreadNameCounter.GetAndIncrement(),
                                                         TimeSpan.FromMilliseconds(100));
            Action <object> increment = o => ((AtomicCounter)o).GetAndIncrement();

            // schedule a delayed operation
            var checkCounter = executor.ScheduleAsync(o => Assert.True(((AtomicCounter)o).Current == 4), counter,
                                                      TimeSpan.FromMilliseconds(40));

            // schedule 4 immediate operations
            executor.Execute(increment, counter);
            executor.Execute(increment, counter);
            executor.Execute(increment, counter);
            executor.Execute(increment, counter);

            // delay should run after the first 4 previous
            checkCounter.Wait(TimeSpan.FromMilliseconds(100));
        }