Exemplo n.º 1
0
        public void TestBoundaryExtension()
        {
            BlockingQueue <int> col = new BlockingQueue <int>(100);

            for (int i = 0; i < 100; i++)
            {
                col.Add(i);
            }

            Assert.IsFalse(col.TryAdd(int.MaxValue));
            Assert.AreEqual(100, col.BoundedCapacity);

            col.IncreaseBoundedCapacity(10);
            Assert.AreEqual(110, col.BoundedCapacity);


            for (int i = 100; i < 110; i++)
            {
                col.Add(i);
            }

            Assert.AreEqual(110, col.Count);

            for (int i = 0; i < 110; i++)
            {
                Assert.AreEqual(i, col.Take());
            }

            Assert.AreEqual(0, col.Count);
            Assert.AreEqual(110, col.BoundedCapacity);
        }
Exemplo n.º 2
0
        //[TestMethod]
        //[Ignore("Old behaviour is not good. Changing it can break production code. This test should be enabled when BlockingQueue dispose logic will be updated")]
        public void TestDisposeInterruptWaitersOnAdd()
        {
            BlockingQueue <int> queue = new BlockingQueue <int>(2);

            queue.Add(1);
            queue.Add(2);
            Barrier bar = new Barrier(2);

            Task disposeTask = Task.Run(() =>
            {
                bar.SignalAndWait();
                Thread.Sleep(10);
                queue.Dispose();
            });

            try
            {
                bar.SignalAndWait();
                bool added = queue.TryAdd(3, 10000);
                Assert.Fail();
            }
            catch (OperationInterruptedException)
            {
            }
            catch (ObjectDisposedException)
            {
            }
            catch (Exception)
            {
                Assert.Fail("Unexpected exception");
            }


            disposeTask.Wait();
        }
        public virtual void TestDrainTo()
        {
            BlockingQueue queue = new BlockingQueue();

            queue.Add(new object());
            queue.Add(new object());
            Collection4 list = new Collection4();

            Assert.AreEqual(2, queue.DrainTo(list));
            Assert.AreEqual(2, list.Size());
            Assert.IsFalse(queue.HasNext());
        }
        public virtual void TestNext()
        {
            IQueue4 queue = new BlockingQueue();

            string[] data = new string[] { "a", "b", "c", "d" };
            queue.Add(data[0]);
            Assert.AreSame(data[0], queue.Next());
            queue.Add(data[1]);
            queue.Add(data[2]);
            Assert.AreSame(data[1], queue.Next());
            Assert.AreSame(data[2], queue.Next());
        }
Exemplo n.º 5
0
        public void PeekWakesUpTest()
        {
            var queue = new BlockingQueue <int>();

            Barrier            bar         = new Barrier(3);
            AtomicNullableBool peekResult  = new AtomicNullableBool();
            AtomicNullableBool peekResult2 = new AtomicNullableBool();
            Task task = Task.Run(() =>
            {
                bar.SignalAndWait();
                int item         = 0;
                peekResult.Value = queue.TryPeek(out item, 60000);
                Assert.AreEqual(100, item);
            });

            Task task2 = Task.Run(() =>
            {
                bar.SignalAndWait();
                int item          = 0;
                peekResult2.Value = queue.TryPeek(out item, 60000);
                Assert.AreEqual(100, item);
            });

            bar.SignalAndWait();
            Thread.Sleep(20);
            Assert.IsFalse(peekResult.HasValue);
            Assert.IsFalse(peekResult2.HasValue);

            queue.Add(100);
            TimingAssert.AreEqual(10000, true, () => peekResult.Value);
            TimingAssert.AreEqual(10000, true, () => peekResult2.Value);

            Task.WaitAll(task, task2);
        }
Exemplo n.º 6
0
        public void TestForceEnqueue()
        {
            BlockingQueue <int> col = new BlockingQueue <int>(100);

            for (int i = 0; i < 100; i++)
            {
                col.Add(i);
            }

            Assert.AreEqual(100, col.Count);
            Assert.IsFalse(col.TryAdd(int.MaxValue));

            col.AddForced(100);
            Assert.AreEqual(101, col.Count);
            Assert.AreEqual(100, col.BoundedCapacity);
            Assert.IsFalse(col.TryAdd(int.MaxValue));

            Assert.AreEqual(0, col.Take());
            Assert.AreEqual(100, col.Count);
            Assert.AreEqual(100, col.BoundedCapacity);
            Assert.IsFalse(col.TryAdd(int.MaxValue));

            for (int i = 1; i < 101; i++)
            {
                Assert.AreEqual(i, col.Take());
            }
        }
 public virtual void MessageLoop()
 {
     while (IsMessageDispatcherAlive())
     {
         Msg message = null;
         try
         {
             message = Msg.ReadMessage(this, Transaction(), _socket);
         }
         catch (Db4oIOException exc)
         {
             if (DTrace.enabled)
             {
                 DTrace.ClientMessageLoopException.Log(exc.ToString());
             }
             return;
         }
         if (message == null)
         {
             continue;
         }
         if (IsClientSideMessage(message))
         {
             _asynchronousMessageQueue.Add(message);
         }
         else
         {
             _synchronousMessageQueue.Add(message);
         }
     }
 }
Exemplo n.º 8
0
 private void ReceiveInternal(IOState state)
 {
     if (!state.BandwidthController.CanTransmit(state.PendingBytes))
     {
         _receiveQueue.Add(state);
         return;
     }
     if (state.WaitingForBuffer)
     {
         state.Buffer = _bufferAllocator.Allocate(state.Bytes);
         if (state.WaitingForBuffer)
         {
             _receiveQueue.Add(state);
             return;
         }
     }
     state.BandwidthController.SetTransmittion(state.PendingBytes);
     state.Connection.Receive(state.GetBufferForPending(), (readCount, success) =>
     {
         try
         {
             if (success && readCount > 0)
             {
                 if (readCount < state.PendingBytes)
                 {
                     state.PendingBytes -= readCount;
                     _receiveQueue.Add(state);
                 }
                 else
                 {
                     var data = state.GetData();
                     state.SuccessCallback(data);
                 }
             }
             else
             {
                 state.FailureCallback();
             }
         }
         finally
         {
             state.Release();
             _bufferAllocator.Free(state.Buffer);
         }
     });
 }
        public virtual void TestTimeoutNext()
        {
            BlockingQueue queue = new BlockingQueue();

            Assert.IsNull(AssertTakeAtLeast(200, new _IClosure4_35(queue)));
            object obj = new object();

            queue.Add(obj);
            Assert.AreSame(obj, AssertTakeLessThan(50, new _IClosure4_46(queue)));
            Assert.IsNull(AssertTakeAtLeast(200, new _IClosure4_53(queue)));
        }
        public virtual void TestStop()
        {
            BlockingQueue queue = new BlockingQueue();

            string[] data = new string[] { "a", "b", "c", "d" };
            queue.Add(data[0]);
            Assert.AreSame(data[0], queue.Next());
            BlockingQueueTestCase.StopThread notifyThread = new BlockingQueueTestCase.StopThread
                                                                (queue);
            notifyThread.Start();
            Assert.Expect(typeof(BlockingQueueStoppedException), new _ICodeBlock_110(queue));
        }
        public virtual void TestIterator()
        {
            IQueue4 queue = new BlockingQueue();

            string[] data = new string[] { "a", "b", "c", "d" };
            for (int idx = 0; idx < data.Length; idx++)
            {
                AssertIterator(queue, data, idx);
                queue.Add(data[idx]);
                AssertIterator(queue, data, idx + 1);
            }
        }
 public override void JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException)
 {
     if (jobException == null)
     {
         // do whatever you want
     }
     else
     {
         _queue.Add(DateTime.UtcNow);
     }
     base.JobWasExecuted(context, jobException);
 }
Exemplo n.º 13
0
        internal void FillServicesQueue(IEnumerable <IOrganizationService> services)
        {
            foreach (var service in services)
            {
                servicesQueue.Add(service);
            }

            if (servicesQueue.Any())
            {
                IsValid = true;
            }
        }
Exemplo n.º 14
0
        public void TestSimpleEnqueueDequeue()
        {
            BlockingQueue <int> col = new BlockingQueue <int>();

            for (int i = 0; i < 100; i++)
            {
                Assert.AreEqual(i, col.Count);
                col.Add(i);
            }

            for (int i = 0; i < 100; i++)
            {
                Assert.AreEqual(100 - i, col.Count);
                Assert.AreEqual(i, col.Take());
            }
        }
Exemplo n.º 15
0
        public void AddTakeMultithreadTest()
        {
            const int ItemsCount = 10000;

            using (var queue = new BlockingQueue <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 virtual void TestBlocking()
        {
            IQueue4 queue = new BlockingQueue();

            string[] data = new string[] { "a", "b", "c", "d" };
            queue.Add(data[0]);
            Assert.AreSame(data[0], queue.Next());
            BlockingQueueTestCase.NotifyThread notifyThread = new BlockingQueueTestCase.NotifyThread
                                                                  (queue, data[1]);
            notifyThread.Start();
            long start = Runtime.CurrentTimeMillis();

            Assert.AreSame(data[1], queue.Next());
            long end = Runtime.CurrentTimeMillis();

            Assert.IsGreater(500, end - start);
        }
Exemplo n.º 17
0
        public void AddTakeSequentialTest()
        {
            const int ItemsCount = 10000;

            using (var queue = new BlockingQueue <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]");
                    }
                }
        }
Exemplo n.º 18
0
        private void DoInBackground()
        {
            while (!_cts.IsCancellationRequested)
            {
                T data;

                try {
                    data = _dataRetriever.TryRetrieveData();
                } catch (Exception e) {
                    if (_allException == null)
                    {
                        _allException = e;
                    }
                    else
                    {
                        _allException =
                            new Exception(_allException.Message + "\n======================================\n" +
                                          e.Message);
                    }
                    _continuousFailCnt++;
                    if (_continuousFailCnt == 10)
                    {
                        ProductionFailed?.Invoke(_allException);
                        break;
                    }
                    Thread.Sleep(1000);
                    continue;
                }
                NewProduct?.Invoke(data);
                _continuousFailCnt = 0;
                if (_cts.IsCancellationRequested)
                {
                    break;
                }
                BlockingQueue.Add(data); // blocking method
                ProductCnt++;
                if (ProductCnt >= TargetCnt.GetValueOrDefault(int.MaxValue))
                {
                    HitTarget?.Invoke();
                    break;
                }
            }
        }
Exemplo n.º 19
0
        public void TestEnqueueDequeueToTheLimit()
        {
            BlockingQueue <int> col = new BlockingQueue <int>(100);

            for (int i = 0; i < 100; i++)
            {
                col.Add(i);
            }

            Assert.IsFalse(col.TryAdd(int.MaxValue));

            for (int i = 0; i < 100; i++)
            {
                Assert.AreEqual(i, col.Take());
            }

            int resItem = 0;

            Assert.IsFalse(col.TryTake(out resItem));
        }
Exemplo n.º 20
0
        public void TestPeekWait()
        {
            BlockingQueue <int> col = new BlockingQueue <int>(100);
            int startedFlag         = 0;

            int peekVal = 0;

            Task.Run(() =>
            {
                Interlocked.Exchange(ref startedFlag, 1);
                peekVal = col.Peek();
            });

            TimingAssert.IsTrue(5000, () => Volatile.Read(ref startedFlag) == 1);
            Thread.Sleep(100);
            Assert.AreEqual(0, peekVal);

            col.Add(100);

            TimingAssert.AreEqual(5000, 100, () => Volatile.Read(ref peekVal));
            TimingAssert.AreEqual(5000, 1, () => col.Count);
        }
Exemplo n.º 21
0
        public void TestPeek()
        {
            BlockingQueue <int> col = new BlockingQueue <int>(100);

            for (int i = 0; i < 100; i++)
            {
                Assert.AreEqual(i, col.Count);
                col.Add(i);
                Assert.AreEqual(0, col.Peek());
            }

            int item = 0;

            Assert.IsTrue(col.TryPeek(out item));

            for (int i = 0; i < 100; i++)
            {
                Assert.AreEqual(i, col.Peek());
                Assert.AreEqual(i, col.Take());
            }

            Assert.IsFalse(col.TryPeek(out item));
        }
Exemplo n.º 22
0
        private static TimeSpan RunConcurrentBQ(string name, int elemCount, int addThCount, int takeThCount, int addSpin, int takeSpin)
        {
            BlockingQueue <int> col = new BlockingQueue <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);
                    SpinWaitHelper.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);
                        SpinWaitHelper.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 + ". BlocQ. Time = " + sw.ElapsedMilliseconds.ToString() + "ms");
            }
            return(sw.Elapsed);
        }
 public virtual void AddCommittedInfoMsg(MCommittedInfo message)
 {
     _committedInfosQueue.Add(message);
 }
Exemplo n.º 24
0
        private void RunComplexTest(BlockingQueue <int> q, int elemCount, int thCount)
        {
            int atomicRandom = 0;

            int trackElemCount = elemCount;
            int addFinished    = 0;

            Thread[] threadsTake = new Thread[thCount];
            Thread[] threadsAdd  = new Thread[thCount];

            CancellationTokenSource tokSrc = new CancellationTokenSource();

            List <int> global = new List <int>(elemCount);

            Action addAction = () =>
            {
                Random rnd = new Random(Environment.TickCount + Interlocked.Increment(ref atomicRandom) * thCount * 2);

                while (true)
                {
                    int item = Interlocked.Decrement(ref trackElemCount);
                    if (item < 0)
                    {
                        break;
                    }

                    if (rnd.Next(100) == 0)
                    {
                        q.AddForced(item);
                    }
                    else
                    {
                        q.Add(item);
                    }


                    int sleepTime = rnd.Next(100);

                    int tmpItem = 0;
                    if (q.TryPeek(out tmpItem) && tmpItem == item)
                    {
                        sleepTime += 100;
                    }

                    if (sleepTime > 0)
                    {
                        Thread.SpinWait(sleepTime);
                    }

                    if (rnd.Next(100) == 0)
                    {
                        q.IncreaseBoundedCapacity(1);
                    }
                    if (rnd.Next(100) == 0 && q.BoundedCapacity > 20)
                    {
                        q.DecreaseBoundedCapacity(1);
                    }
                }

                Interlocked.Increment(ref addFinished);
            };


            Action takeAction = () =>
            {
                Random rnd = new Random(Environment.TickCount + Interlocked.Increment(ref atomicRandom) * thCount * 2);

                List <int> data = new List <int>();

                try
                {
                    while (Volatile.Read(ref addFinished) < thCount)
                    {
                        int tmp = 0;
                        if (q.TryTake(out tmp, -1, tokSrc.Token))
                        {
                            data.Add((int)tmp);
                        }

                        int sleepTime = rnd.Next(100);
                        if (sleepTime > 0)
                        {
                            Thread.SpinWait(sleepTime);
                        }
                    }
                }
                catch (OperationCanceledException) { }

                int tmp2;
                while (q.TryTake(out tmp2))
                {
                    data.Add((int)tmp2);
                }

                lock (global)
                    global.AddRange(data);
            };

            Task.Delay(1000).ContinueWith(t => q.IncreaseBoundedCapacity(50));


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


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


            for (int i = 0; i < threadsAdd.Length; i++)
            {
                threadsAdd[i].Join();
            }
            tokSrc.Cancel();
            for (int i = 0; i < threadsTake.Length; i++)
            {
                threadsTake[i].Join();
            }


            Assert.AreEqual(elemCount, global.Count);
            global.Sort();

            for (int i = 0; i < elemCount; i++)
            {
                Assert.AreEqual(i, global[i]);
            }
        }