public void ShouldReturnDifferentItemsToEachAwaitedClients()
        {
            var blockingPipe = new InfinityPipe <string>(_itemsContainer);

            var t1 = blockingPipe.ReceiveAsync();
            var t2 = blockingPipe.ReceiveAsync();

            blockingPipe.SendAsync("1").Wait();
            blockingPipe.SendAsync("2").Wait();

            Assert.AreEqual("1", t1.Result);
            Assert.AreEqual("2", t2.Result);
        }
        public void GetShouldReturnItemToAwaiterEvenIfFirstTryWasException()
        {
            int callCount = 0;

            string item;

            Mock.Arrange(() => _itemsContainer.TryGet(out item))
            .DoInstead <OutAction <string> >((out string item2) =>
            {
                callCount++;
                if (callCount == 3)
                {
                    throw new Exception();
                }

                if (_queue.Count == 0)
                {
                    item2 = null;
                    return(false);
                }
                item2 = _queue.Dequeue();
                return(true);
            }).Returns <string>(o => o != null);

            var blockingPipe = new InfinityPipe <string>(_itemsContainer);
            var t1           = blockingPipe.ReceiveAsync();

            Assert.Catch(() => blockingPipe.SendAsync("1").Wait());
            Assert.AreEqual(TaskStatus.WaitingForActivation, t1.Status);

            blockingPipe.SendAsync("1").Wait();

            Assert.AreEqual("1", t1.Result);
        }
        public void GetShouldBeBlockedWhenItemsContainerHasNoItems()
        {
            var blockingPipe = new InfinityPipe <string>(_itemsContainer);

            var t1 = blockingPipe.ReceiveAsync();

            Assert.AreEqual(TaskStatus.WaitingForActivation, t1.Status);
        }
        public void GetShouldReturnItemImmediatlyWhenItemIsValid()
        {
            var blockingPipe = new InfinityPipe <string>(_itemsContainer);

            blockingPipe.SendAsync("1").Wait();

            var t1 = blockingPipe.ReceiveAsync();

            Assert.AreEqual("1", t1.Result);
        }
        public void WhenSendBatchPipeShouldReturnItemToAwaiterOnlyWhenAllItemsInBatchWillBeAdded()
        {
            string receivedItem        = null;
            int    itemsContainerCount = -1;

            var blockingPipe = new InfinityPipe <string>(_itemsContainer);
            var t1           = blockingPipe.ReceiveAsync();

            t1.ContinueWith(o =>
            {
                receivedItem        = o.Result;
                itemsContainerCount = _queue.Count;
            }, TaskContinuationOptions.ExecuteSynchronously);

            blockingPipe.SendAsync(new[] { "1", "2" }).Wait();

            Assert.AreEqual("1", receivedItem);
            Assert.AreEqual(1, itemsContainerCount);
        }