예제 #1
0
        public void TestNegativeQueueing()
        {
            var q = new AwaitableQueue<int>();
            var hits = new List<int>();
            q.Dequeue().ContinueWith(t => {
                hits.Add(t.Result);
            }, TaskContinuationOptions.ExecuteSynchronously);
            q.Dequeue().ContinueWith(t => {
                hits.Add(t.Result);
            }, TaskContinuationOptions.ExecuteSynchronously);
            q.Dequeue().ContinueWith(t => {
                hits.Add(t.Result);
            }, TaskContinuationOptions.ExecuteSynchronously);

            Assert.AreEqual(0, q.Count);
            Assert.AreEqual(3, q.PromisedCount);

            q.Enqueue(1);
            CollectionAssert.AreEqual(new[] { 1 }, hits);

            q.Enqueue(2);
            CollectionAssert.AreEqual(new[] { 1, 2 }, hits);

            q.Enqueue(3);
            CollectionAssert.AreEqual(new[] { 1, 2, 3 }, hits);

            Assert.AreEqual(0, q.Count);
            Assert.AreEqual(0, q.PromisedCount);
        }
예제 #2
0
        public void TestNegativeQueueing()
        {
            var q    = new AwaitableQueue <int>();
            var hits = new List <int>();

            q.Dequeue().ContinueWith(t => {
                hits.Add(t.Result);
            }, TaskContinuationOptions.ExecuteSynchronously);
            q.Dequeue().ContinueWith(t => {
                hits.Add(t.Result);
            }, TaskContinuationOptions.ExecuteSynchronously);
            q.Dequeue().ContinueWith(t => {
                hits.Add(t.Result);
            }, TaskContinuationOptions.ExecuteSynchronously);

            Assert.AreEqual(0, q.Count);
            Assert.AreEqual(3, q.PromisedCount);

            q.Enqueue(1);
            CollectionAssert.AreEqual(new[] { 1 }, hits);

            q.Enqueue(2);
            CollectionAssert.AreEqual(new[] { 1, 2 }, hits);

            q.Enqueue(3);
            CollectionAssert.AreEqual(new[] { 1, 2, 3 }, hits);

            Assert.AreEqual(0, q.Count);
            Assert.AreEqual(0, q.PromisedCount);
        }
예제 #3
0
        public void Should_return_item_if_its_available()
        {
            //arrange
            _queue.Enqueue(_expected);

            //act
            ValueTask actual = _queue.AwaitEnqueued(CancellationToken.None);

            //assert
            actual.IsCompletedSuccessfully.Should().BeTrue(); //Should be return synchronously

            AssertDequeuedMessage();
        }
예제 #4
0
        public async Task TestPositiveQueueing()
        {
            var q = new AwaitableQueue<int>();
            q.Enqueue(1);
            q.Enqueue(2);
            q.Enqueue(3);

            Assert.AreEqual(3, q.Count);
            Assert.AreEqual(0, q.PromisedCount);

            Assert.AreEqual(1, await q.Dequeue());
            Assert.AreEqual(2, await q.Dequeue());
            Assert.AreEqual(3, await q.Dequeue());

            Assert.AreEqual(0, q.Count);
            Assert.AreEqual(0, q.PromisedCount);
        }
예제 #5
0
        public async Task TestPositiveQueueing()
        {
            var q = new AwaitableQueue <int>();

            q.Enqueue(1);
            q.Enqueue(2);
            q.Enqueue(3);

            Assert.AreEqual(3, q.Count);
            Assert.AreEqual(0, q.PromisedCount);

            Assert.AreEqual(1, await q.Dequeue());
            Assert.AreEqual(2, await q.Dequeue());
            Assert.AreEqual(3, await q.Dequeue());

            Assert.AreEqual(0, q.Count);
            Assert.AreEqual(0, q.PromisedCount);
        }
예제 #6
0
    public virtual Task <ReceivedValue <T> > ReceiveInto(IReceiver <T> receiver)
    {
        // in go, a receive from a closed channel returns the zero value immediately
        // unless there are "unfinished" senders
        if (!m_isOpen && m_receivers.PromisedCount == 0)
        {
            receiver.TryReceive(default(T), false);
            return(Task.FromResult(default(ReceivedValue <T>)));
        }

        var tkn = Interlocked.Increment(ref RToken);

        m_receivers.Enqueue(receiver);
        // now we have to wait for someone to call TryReceive on the receiver and for it to succeed
        // we can always block forever, it's not like Select where we have to retry
        return(receiver.ReceivedValue);
    }
예제 #7
0
        void startRespThread(int mtype, string muid, string session, byte[] content, AwaitableQueue <byte[]> q)
        {
            new Thread(() =>
            {
                byte[] returncontent;
                var result = HandleMessage((ServerMessageType)mtype, _keys[session], content, out returncontent);

                using (var wms = new MemoryStream())
                    using (var writer = new BinaryWriter(wms))
                    {
                        writer.Write(muid);
                        writer.Write((int)result);
                        writer.Write(session);
                        writer.Write(returncontent.Length);
                        writer.Write(returncontent);
                        q.Enqueue(wms.ToArray());
                        Logger.Log($"Enqueued response to {(ServerMessageType)mtype} {muid}");
                    }
            }).Start();
        }