Beispiel #1
0
        public void TestDisposeCancellWaiters()
        {
            using (BalancingStaticPoolManager <int> testInst = new BalancingStaticPoolManager <int>())
            {
                Task.Delay(400).ContinueWith(t => testInst.Dispose());

                using (var item = testInst.Rent())
                {
                }
            }
        }
Beispiel #2
0
        public void TestNotAddToDisposed()
        {
            using (BalancingStaticPoolManager <int> testInst = new BalancingStaticPoolManager <int>())
            {
                testInst.Dispose();

                for (int i = 0; i < 10; i++)
                {
                    testInst.AddElement(i);
                }
            }
        }
Beispiel #3
0
        public void TestDisposeCancellWaitersWithoutException()
        {
            using (BalancingStaticPoolManager <int> testInst = new BalancingStaticPoolManager <int>())
            {
                Task.Delay(400).ContinueWith(t => testInst.Dispose());

                using (var item = testInst.Rent(false))
                {
                    Assert.IsFalse(item.IsValid);
                }
            }
        }
Beispiel #4
0
        public void WaitForElementReleaseNotBlockAfterStop()
        {
            using (BalancingStaticPoolManager <int> testInst = new BalancingStaticPoolManager <int>())
            {
                testInst.AddElement(1);

                using (var item = testInst.Rent())
                {
                    Assert.IsTrue(item.IsValid);
                }

                testInst.Dispose(DisposeFlags.WaitForElementsRelease);
            }
        }
Beispiel #5
0
        public void WaitForElementReleaseWaits()
        {
            using (BalancingStaticPoolManager <int> testInst = new BalancingStaticPoolManager <int>())
            {
                int elementRented   = 0;
                int elementReleased = 0;
                testInst.AddElement(1);

                Task.Run(() =>
                {
                    using (var item = testInst.Rent())
                    {
                        Interlocked.Increment(ref elementRented);
                        Thread.Sleep(250);
                    }
                    Interlocked.Increment(ref elementReleased);
                });

                SpinWait.SpinUntil(() => Volatile.Read(ref elementRented) == 1);
                testInst.Dispose(DisposeFlags.WaitForElementsRelease);
                TimingAssert.AreEqual(10000, 1, () => Volatile.Read(ref elementReleased));
            }
        }
Beispiel #6
0
        private void RunConcurrentUseWithDispose(int maxPoolElemCount, int threadCount, int opCount)
        {
            using (BalancingStaticPoolManager <int> testInst = new BalancingStaticPoolManager <int>())
            {
                for (int i = 0; i < maxPoolElemCount; i++)
                {
                    testInst.AddElement(i);
                }

                Thread[] threads  = new Thread[threadCount];
                Barrier  startBar = new Barrier(threadCount + 1);

                int totalExecutedOpCount = 0;

                Action thAct = () =>
                {
                    Random localRand = new Random(Thread.CurrentThread.ManagedThreadId + Environment.TickCount);
                    startBar.SignalAndWait();

                    try
                    {
                        while (true)
                        {
                            int curSpinTime = localRand.Next(0, 10000);

                            Interlocked.Increment(ref totalExecutedOpCount);
                            using (var el = testInst.Rent(60 * 1000, throwOnUnavail: true))
                            {
                                if (curSpinTime > 5000)
                                {
                                    Thread.Sleep(0);
                                }
                                else
                                {
                                    Thread.SpinWait(curSpinTime);
                                }
                            }
                        }
                    }
                    catch (ObjectDisposedException)
                    {
                    }
                    catch (CantRetrieveElementException)
                    {
                    }
                };


                for (int i = 0; i < threads.Length; i++)
                {
                    threads[i] = new Thread(new ThreadStart(thAct));
                }

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

                startBar.SignalAndWait();


                while (Volatile.Read(ref totalExecutedOpCount) < opCount)
                {
                    Thread.Sleep(1);
                }

                testInst.Dispose();

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

                Assert.AreEqual(testInst.ElementCount, testInst.FreeElementCount, "testInst.ElementCount != testInst.FreeElementCount");
                // Assert.AreEqual(testInst.ElementsCreated, testInst.ElementsDestroyed, "ElementsCreated != ElementsDestroyed");
            }
        }