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);
        }
        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);
        }
        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);
        }
        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);
        }
Exemple #5
0
    public virtual async Task Send(T value)
    {
        if (!m_isOpen) // in go, a send to a closed channel panics
        {
            throw new InvalidOperationException("channel closed");
        }

        var tkn = Interlocked.Increment(ref SToken);

        while (true)
        {
            var receiver = await m_receivers.Dequeue().ConfigureAwait(false);

            if (receiver.TryReceive(value, true))
            {
                return;
            }
            // else we lost the race with another sender, wait for the next one
        }
    }