Example #1
0
        public async Task 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));

            await executor.ShutdownGracefullyAsync(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
        }
Example #2
0
        public void Rejected(IRunnable task, SingleThreadEventExecutor executor)
        {
            if (!executor.InEventLoop)
            {
                for (int i = 0; i < _retries; i++)
                {
                    // Try to wake up the executor so it will empty its task queue.
                    executor.WakeUp(false);

                    Thread.Sleep(_delay);
                    if (executor.OfferTask(task))
                    {
                        return;
                    }
                }
            }
            // Either we tried to add the task from within the EventLoop or we was not able to add it even with backoff.
            ThrowHelper.ThrowRejectedExecutionException();
        }
Example #3
0
 public void Rejected(IRunnable task, SingleThreadEventExecutor executor)
 {
     ThrowHelper.ThrowRejectedExecutionException();
 }