//[Test]
        // Demonstration that this queue isn't MPSC

        // This test is disabled, because on rare occasions (or if Count is too small) it might fail.
        // Increasing Count will increase the likelyhood of a successful test.
        public void ConcurrencyTest2()
        {
            int count = 10000;
            var q     = new SPSCQueue <int>(count);


            Thread writer = new Thread(() =>
            {
                for (int i = 0; i < count / 2; i++)
                {
                    while (!q.TryEnqueue(i))
                    {
                    }
                }
            });
            Thread writer2 = new Thread(() =>
            {
                for (int i = 0; i < count / 2; i++)
                {
                    while (!q.TryEnqueue(i))
                    {
                    }
                }
            });

            writer.Start();
            writer2.Start();


            writer.Join();
            writer2.Join();

            Assert.AreNotEqual(count, q.Count);
        }
        public void TryActionTest()
        {
            var q = new SPSCQueue <int>(16);

            //Inserts 10 items.
            QueueTestSetup.SplitQueue(q);

            //Insert 6 more to fill the queue
            for (int i = 0; i < 6; i++)
            {
                q.TryEnqueue(999);
            }

            Assert.IsFalse(q.TryEnqueue(10));
            Assert.IsTrue(q.TryPeek(out int result));
            Assert.AreEqual(0, result);

            for (int i = 0; i < 10; i++)
            {
                Assert.IsTrue(q.TryDequeue(out int val));
                Assert.AreEqual(i, val);
            }

            //Empty 6 last items
            for (int i = 0; i < 6; i++)
            {
                Assert.IsTrue(q.TryDequeue(out int val));
                Assert.AreEqual(999, val);
            }

            //Empty queue
            Assert.IsFalse(q.TryPeek(out int res));
        }
Beispiel #3
0
        public void StartTestSPSCQueue()
        {
            SpinWait spinner = new SpinWait();

            _consumerThread = new Thread(() =>
            {
                Console.WriteLine("SPSCQueue consumer started");
                Stopwatch sw = Stopwatch.StartNew();
                for (ulong i = 0; i < Count;)
                {
                    ulong val;
                    while (!_spscQueue.TryDequeue(out val))
                    {
                        spinner.SpinOnce();
                    }

                    if (val != i)
                    {
                        Console.WriteLine("wrong value " + val + " ,correct: " + i);
                    }
                    i++;
                }
                Console.WriteLine("SPSCQueue consumer done {0}", sw.Elapsed);
            });

            _producerThread = new Thread(() =>
            {
                Console.WriteLine("SPSCQueue producer started");
                Stopwatch sw = Stopwatch.StartNew();
                for (ulong i = 0; i < Count;)
                {
                    while (!_spscQueue.TryEnqueue(i))
                    {
                        spinner.SpinOnce();
                    }

                    i++;
                }
                Console.WriteLine("SPSCQueue producer done {0}", sw.Elapsed);
            });

            _consumerThread.Start();
            _producerThread.Start();

#if !UNITY_EDITOR && !UNITY_STANDALONE
            _consumerThread.Join();
            _producerThread.Join();
#endif
        }
        public void ExpandTest()
        {
            var q = new SPSCQueue <int>(16);

            QueueTestSetup.SplitQueue(q);

            //Fill buffer to capacity.
            for (int i = 0; i < 6; i++)
            {
                Assert.IsTrue(q.TryEnqueue(999));
            }


            //Buffer is full, can no longer insert.
            Assert.IsFalse(q.TryEnqueue(10));
        }
        // Demonstration that this queue is SPSC
        public void ConcurrencyTest()
        {
            var q     = new SPSCQueue <int>(16);
            int count = 10000;

            Thread reader = new Thread(() =>
            {
                for (int i = 0; i < count; i++)
                {
                    int item;
                    while (!q.TryDequeue(out item))
                    {
                    }

                    Assert.AreEqual(i, item);
                }
            });

            reader.Start();

            for (int i = 0; i < count; i++)
            {
                while (!q.TryEnqueue(i))
                {
                }
            }

            reader.Join();
        }
        public void SPSCQueue()
        {
            // ADD values
            for (int i = 0; i < COUNT; i++)
            {
                _spscQueue.TryEnqueue(i);
            }

            for (int i = 0; i < COUNT; i++)
            {
                _spscQueue.TryDequeue(out long result);
            }

            _spscQueue.Clear();
        }
        public void DequeueTest()
        {
            var q = new SPSCQueue <int>(10);

            for (int i = 0; i < 10; i++)
            {
                q.TryEnqueue(i * i);
            }

            for (int i = 0; i < 10; i++)
            {
                q.TryDequeue(out int num);
                Assert.AreEqual(i * i, num);
            }
        }
        public void EnqueueTest()
        {
            var q = new SPSCQueue <int>(10);

            for (int i = 0; i < 10; i++)
            {
                q.TryEnqueue(i * i);
            }

            Assert.AreEqual(10, q.Count);
            Assert.AreEqual(10, q.Capacity);

            q.Clear();

            Assert.AreEqual(0, q.Count);
            Assert.AreEqual(10, q.Capacity);
        }
        public void PeekTest()
        {
            var q = new SPSCQueue <int>(10);

            for (int i = 0; i < 10; i++)
            {
                q.TryEnqueue((int)Math.Pow(i + 2, 2));
            }

            for (int i = 0; i < 10; i++)
            {
                q.TryPeek(out int num);
                Assert.AreEqual(4, num);
            }

            //Verify no items are dequeued
            Assert.AreEqual(10, q.Count);
        }