public void TaskQueueDrainTest()
        {
            var depth     = 10;
            var taskQueue = new TaskQueue();

            taskQueue.SetMaxQueueSize(depth);
            var wait = true;

            for (var i = 0; i < depth; i++)
            {
                taskQueue.Enqueue(t => Task.Run(() => {
                    while (wait)
                    {
                        Thread.Sleep(10);
                    }
                }), wait);
            }

            wait = false;
            taskQueue.Drain();
            taskQueue.Size.Should().Be(0);

            var newItem = taskQueue.Enqueue(t => Task.FromResult(0), wait);

            newItem.Should().NotBeNull();
        }
Ejemplo n.º 2
0
        public async Task DrainingTaskQueueShutsQueueOff()
        {
            var queue = new TaskQueue();
            await queue.Enqueue(() => Task.CompletedTask);

            await queue.Drain();

            // This would throw if the task was queued successfully
            await queue.Enqueue(() => Task.FromException(new Exception()));
        }
Ejemplo n.º 3
0
        public void DrainingTaskQueueShutsQueueOff()
        {
            var queue = new TaskQueue();

            queue.Enqueue(() => TaskAsyncHelper.Empty);
            queue.Drain();
            Task task = queue.Enqueue(() => TaskAsyncHelper.FromError(new Exception()));

            Assert.True(task.IsCompleted);
            Assert.False(task.IsFaulted);
        }
Ejemplo n.º 4
0
        private static Task Drain(TaskQueue queue, ILogger logger)
        {
            if (queue == null)
            {
                return(TaskAsyncHelper.Empty);
            }

            var tcs = new TaskCompletionSource <object>();

            queue.Drain().Catch(logger).ContinueWith(task =>
            {
                tcs.SetResult(null);
            });

            return(tcs.Task);
        }
Ejemplo n.º 5
0
        public void Stop(TimeSpan timeout)
        {
            lock (_startLock)
            {
                // Wait for the connection to connect
                if (_connectTask != null)
                {
                    try
                    {
                        _connectTask.Wait(timeout);
                    }
                    catch (Exception ex)
                    {
                        Trace(TraceLevels.Events, "Error: {0}", ex.GetBaseException());
                    }
                }

                if (_receiveQueue != null)
                {
                    // Close the receive queue so currently running receive callback finishes and no more are run.
                    // We can't wait on the result of the drain because this method may be on the stack of the task returned (aka deadlock).
                    _receiveQueue.Drain().Catch();
                }

                // This is racy since it's outside the _stateLock, but we are trying to avoid 30s deadlocks when calling _transport.Abort()
                if (State == ConnectionState.Disconnected)
                {
                    return;
                }

                Trace(TraceLevels.Events, "Stop");

                // Dispose the heart beat monitor so we don't fire notifications when waiting to abort
                _monitor.Dispose();

                _transport.Abort(this, timeout, _connectionData);

                Disconnect();
            }
        }
Ejemplo n.º 6
0
        public void TaskQueueDrainTest()
        {
            var depth = 10;
            var taskQueue = new TaskQueue();
            taskQueue.SetMaxQueueSize(depth);
            var wait = true;
            for (var i = 0; i < depth; i++)
            {
                taskQueue.Enqueue(t => Task.Run(() =>
                {
                    while (wait)
                        Thread.Sleep(10);
                }), wait);
            }

            wait = false;
            taskQueue.Drain();
            taskQueue.Size.Should().Be(0);

            var newItem = taskQueue.Enqueue(t => Task.FromResult(0), wait);
            newItem.Should().NotBeNull();
        }
Ejemplo n.º 7
0
 public void Dispose()
 {
     _queue.Drain().GetAwaiter().GetResult();
 }