Example #1
0
        public async void Test1()
        {
            var q = new MemoryQueue <String>();

            Assert.True(q.IsEmpty);
            Assert.Equal(0, q.Count);

            q.Add("test");
            q.Add("newlife", "stone");

            Assert.False(q.IsEmpty);
            Assert.Equal(3, q.Count);

            var s1 = q.TakeOne();

            Assert.Equal("test", s1);

            var ss = q.Take(3).ToArray();

            Assert.Equal(2, ss.Length);

            ThreadPoolX.QueueUserWorkItem(() =>
            {
                Thread.Sleep(1100);
                q.Add("delay");
            });

            var s2 = await q.TakeOneAsync(1500);

            Assert.Equal("delay", s2);
        }
Example #2
0
        // ==========================

        private void StableIsEmptyAndCountTest(bool ord, bool bcg)
        {
            const int ItemCount = 100000;

            MemoryQueue <int> high = new MemoryQueue <int>();
            MemoryQueue <int> low  = new MemoryQueue <int>();

            for (int i = 0; i < ItemCount + 1; i++)
            {
                low.Add(i);
            }

            using (var q = new LevelingQueue <int>(high, low, ord ? LevelingQueueAddingMode.PreserveOrder : LevelingQueueAddingMode.PreferLiveData, bcg))
            {
                Assert.AreEqual(ItemCount + 1, q.Count);
                if (bcg && !ord)
                {
                    q.Take();
                }
                else
                {
                    Assert.AreEqual(0, q.Take());
                }

                int startTime = Environment.TickCount;
                while (!low.IsEmpty && (Environment.TickCount - startTime) < 500)
                {
                    Assert.AreEqual(ItemCount, q.Count);
                    Assert.IsFalse(q.IsEmpty);
                }
            }
        }
Example #3
0
        public void BacgroundTransferingWorksNonOrdBcg()
        {
            MemoryQueue <int> high = new MemoryQueue <int>(10);
            MemoryQueue <int> low  = new MemoryQueue <int>(10);

            high.Add(1);
            while (low.TryAdd(low.Count + 2))
            {
                ;
            }

            using (var inst = new LevelingQueue <int>(high, low, LevelingQueueAddingMode.PreferLiveData, true))
            {
                Assert.AreEqual(1, inst.Take());

                TimingAssert.AreEqual(10000, 10, () => high.Count);
                Assert.AreEqual(0, low.Count);

                int item     = 0;
                int expected = 2;
                while (inst.TryTake(out item))
                {
                    Assert.AreEqual(expected++, item);
                }
            }
        }
        public void AddTakeMultithreadTest()
        {
            const int ItemsCount = 10000;

            using (var queue = new MemoryQueue <int>())
                using (var barrier = new Barrier(2))
                {
                    ConcurrentBag <int> bag = new ConcurrentBag <int>();

                    var task1 = Task.Run(() =>
                    {
                        barrier.SignalAndWait();
                        Parallel.For(0, ItemsCount, val =>
                        {
                            queue.Add(val);
                            Thread.SpinWait(val % 100);
                        });
                    });

                    var task2 = Task.Run(() =>
                    {
                        barrier.SignalAndWait();
                        Parallel.For(0, 10000, val =>
                        {
                            int res = 0;
                            if (!queue.TryTake(out res, 10000))
                            {
                                Assert.Fail("Value was expected in MemoryQueue");
                            }
                            bag.Add(res);
                            Thread.SpinWait((val + 37) % 100);
                        });
                    });

                    Task.WaitAll(task1, task2);

                    Assert.AreEqual(0, queue.Count);
                    Assert.AreEqual(ItemsCount, bag.Count);

                    var array = bag.ToArray();
                    Array.Sort(array);
                    for (int i = 0; i < array.Length; i++)
                    {
                        Assert.AreEqual(i, array[i], "i != array[i]");
                    }
                }
        }
        public void AddTakeSequentialTest()
        {
            const int ItemsCount = 10000;

            using (var queue = new MemoryQueue <int>())
                using (var barrier = new Barrier(2))
                {
                    List <int> bag = new List <int>();

                    var task1 = Task.Run(() =>
                    {
                        barrier.SignalAndWait();
                        for (int val = 0; val < ItemsCount; val++)
                        {
                            queue.Add(val);
                            Thread.SpinWait(val % 100);
                        }
                    });

                    var task2 = Task.Run(() =>
                    {
                        barrier.SignalAndWait();
                        for (int val = 0; val < ItemsCount; val++)
                        {
                            int res = 0;
                            if (!queue.TryTake(out res, 10000))
                            {
                                Assert.Fail("Value was expected in MemoryQueue");
                            }
                            bag.Add(res);
                            Thread.SpinWait((val + 37) % 100);
                        }
                    });

                    Task.WaitAll(task1, task2);

                    Assert.AreEqual(0, queue.Count);
                    Assert.AreEqual(ItemsCount, bag.Count);

                    for (int i = 0; i < bag.Count; i++)
                    {
                        Assert.AreEqual(i, bag[i], "i != bag[i]");
                    }
                }
        }
Example #6
0
        private static TimeSpan RunConcurrentMemQ(string name, int elemCount, int addThCount, int takeThCount, int addSpin, int takeSpin)
        {
            MemoryQueue <int> col = new MemoryQueue <int>(10000);

            CancellationTokenSource srcCancel = new CancellationTokenSource();

            Thread[] addThreads  = new Thread[addThCount];
            Thread[] takeThreads = new Thread[takeThCount];

            int        addedElemCount = 0;
            List <int> globalList     = new List <int>();

            Barrier barierStart  = new Barrier(1 + addThreads.Length + takeThreads.Length);
            Barrier barierAdders = new Barrier(1 + addThreads.Length);
            Barrier barierTakers = new Barrier(1 + takeThreads.Length);

            Action addAction = () =>
            {
                barierStart.SignalAndWait();

                int index = 0;
                while ((index = Interlocked.Increment(ref addedElemCount)) <= elemCount)
                {
                    col.Add(index - 1);
                    Thread.SpinWait(addSpin);
                }

                barierAdders.SignalAndWait();
            };


            Action takeAction = () =>
            {
                CancellationToken myToken = srcCancel.Token;
                List <int>        valList = new List <int>(elemCount / takeThCount + 100);

                barierStart.SignalAndWait();

                try
                {
                    while (!srcCancel.IsCancellationRequested)
                    {
                        int val = 0;
                        val = col.Take(myToken);

                        valList.Add(val);
                        Thread.SpinWait(takeSpin);
                    }
                }
                catch (OperationCanceledException)
                {
                }

                int val2 = 0;
                while (col.TryTake(out val2))
                {
                    valList.Add(val2);
                }

                barierTakers.SignalAndWait();

                lock (globalList)
                {
                    globalList.AddRange(valList);
                }
            };

            for (int i = 0; i < addThreads.Length; i++)
            {
                addThreads[i] = new Thread(new ThreadStart(addAction));
            }
            for (int i = 0; i < takeThreads.Length; i++)
            {
                takeThreads[i] = new Thread(new ThreadStart(takeAction));
            }


            for (int i = 0; i < takeThreads.Length; i++)
            {
                takeThreads[i].Start();
            }
            for (int i = 0; i < addThreads.Length; i++)
            {
                addThreads[i].Start();
            }

            barierStart.SignalAndWait();

            Stopwatch sw = Stopwatch.StartNew();

            barierAdders.SignalAndWait();
            srcCancel.Cancel();
            barierTakers.SignalAndWait();
            sw.Stop();

            for (int i = 0; i < addThreads.Length; i++)
            {
                addThreads[i].Join();
            }
            for (int i = 0; i < takeThreads.Length; i++)
            {
                takeThreads[i].Join();
            }

            globalList.Sort();
            if (globalList.Count != elemCount)
            {
                Console.WriteLine("Bad count");
            }

            for (int i = 0; i < globalList.Count; i++)
            {
                if (globalList[i] != i)
                {
                    Console.WriteLine("invalid elements");
                    break;
                }
            }

            if (name != null)
            {
                Console.WriteLine(name + ". MemQ. Time = " + sw.ElapsedMilliseconds.ToString() + "ms");
            }
            return(sw.Elapsed);
        }