public void WhenCollectionIsCompletedAndThereAreItems_ConsumerShouldGetAllItems()
        {
            using (var sut = new SimpleBlockingCollection <int>())
            {
                sut.Add(10);
                sut.Add(20);
                sut.Add(30);

                sut.CompleteAdding();

                Assert.AreEqual(sut.Take(), 10);
                Assert.AreEqual(sut.Take(), 20);
                Assert.AreEqual(sut.Take(), 30);
            }
        }
        public void WhenCollectionIsCompleted_WaitedConsumersShouldBeCancelled()
        {
            using (var sut = new SimpleBlockingCollection <int>())
            {
                var consumer1 = Task.Run(() => sut.Take());
                var consumer2 = Task.Run(() => sut.Take());

                ConcurrentAssert.EnsureThatTaskIsNeverCompleted(Task.WhenAny(consumer1, consumer2));

                sut.CompleteAdding();

                Assert.ThrowsExceptionAsync <OperationCanceledException>(() => consumer1);
                Assert.ThrowsExceptionAsync <OperationCanceledException>(() => consumer2);
            }
        }
        public void WhenCollectionIsCompletedAndThereAreNoItems_TakingAttemptShouldRaiseException()
        {
            using (var sut = new SimpleBlockingCollection <int>())
            {
                sut.CompleteAdding();

                Assert.ThrowsException <InvalidOperationException>(() => sut.Take());
            }
        }
 public void WhenThereIsNoItem_TakeShouldBeBlocked()
 {
     using (var sut = new SimpleBlockingCollection <int>())
     {
         ConcurrentAssert.EnsureThatActionIsNeverCompleted(() =>
         {
             var a = sut.Take();
         });
     }
 }
        public void WhenThereIsItem_TakeShouldNotBeBlocked()
        {
            using (var sut = new SimpleBlockingCollection <int>())
            {
                sut.Add(10);

                ConcurrentAssert.EnsureThatActionIsCompleted(() =>
                {
                    Assert.AreEqual(sut.Take(), 10);
                });
            }
        }
        public void WhenThereIsWaitedConsumer_ItShouldWakeupAfterAddingNewElement()
        {
            using (var sut = new SimpleBlockingCollection <int>())
            {
                var task = Task.Run(() =>
                {
                    return(sut.Take());
                });

                ConcurrentAssert.EnsureThatTaskIsNeverCompleted(task);

                sut.Add(10);

                Assert.AreEqual(task.Result, 10);
            }
        }