private void FailPendingWrites(Exception cause)
 {
     if (_pendingWrites != null)
     {
         _pendingWrites.RemoveAndFailAll(cause);
         _pendingWrites = null;
     }
 }
        public void TestRemoveAndFailAllReentrantFailAll()
        {
            EmbeddedChannel   channel = NewChannel();
            PendingWriteQueue queue   = new PendingWriteQueue(channel.Pipeline.FirstContext());

            IPromise promise = channel.NewPromise();

            promise.Task.ContinueWith(t => queue.RemoveAndFailAll(new InvalidOperationException()), TaskContinuationOptions.ExecuteSynchronously);
            queue.Add(1L, promise);

            IPromise promise2 = channel.NewPromise();

            queue.Add(2L, promise2);
            queue.RemoveAndFailAll(new Exception());
            Assert.True(promise.IsCompleted);
            Assert.False(promise.IsSuccess);
            Assert.True(promise2.IsCompleted);
            Assert.False(promise2.IsSuccess);
            Assert.False(channel.Finish());
        }
        public void TestRemoveAndFailAllReentrantWrite()
        {
            List <int>        failOrder = new List <int>();
            EmbeddedChannel   channel   = NewChannel();
            PendingWriteQueue queue     = new PendingWriteQueue(channel.Pipeline.FirstContext());

            IPromise promise  = channel.NewPromise();
            IPromise promise3 = channel.NewPromise();

            promise3.Task.ContinueWith(t =>
            {
                lock (s_lock)
                {
                    failOrder.Add(3);
                }
            }, TaskContinuationOptions.ExecuteSynchronously);
            promise.Task.ContinueWith(t =>
            {
                lock (s_lock)
                {
                    failOrder.Add(1);
                }
                queue.Add(3L, promise3);
            }, TaskContinuationOptions.ExecuteSynchronously);
            queue.Add(1L, promise);

            IPromise promise2 = channel.NewPromise();

            promise2.Task.ContinueWith(t =>
            {
                lock (s_lock)
                {
                    failOrder.Add(2);
                }
            }, TaskContinuationOptions.ExecuteSynchronously);
            queue.Add(2L, promise2);
            queue.RemoveAndFailAll(new Exception());
            Assert.True(promise.IsCompleted);
            Assert.False(promise.IsSuccess);
            Assert.True(promise2.IsCompleted);
            Assert.False(promise2.IsSuccess);
            Assert.True(promise3.IsCompleted);
            Assert.False(promise3.IsSuccess);
            Assert.False(channel.Finish());
            Assert.Equal(1, failOrder[0]);
            Assert.Equal(2, failOrder[1]);
            Assert.Equal(3, failOrder[2]);
        }
        public void TestCloseChannelOnCreation()
        {
            EmbeddedChannel        channel = NewChannel();
            IChannelHandlerContext context = channel.Pipeline.FirstContext();

            try
            {
                channel.CloseAsync().GetAwaiter().GetResult();
            }
            catch { }

            PendingWriteQueue queue = new PendingWriteQueue(context);

            var      ex      = new InvalidOperationException();
            IPromise promise = channel.NewPromise();

            queue.Add(1L, promise);
            queue.RemoveAndFailAll(ex);
            Assert.Same(ex, promise.Task.Exception.InnerException);
        }