Example #1
0
            /// <exception cref="System.Exception"/>
            internal virtual int RecycleAll(TestByteArrayManager.Recycler recycler)
            {
                int n = futures.Count;

                foreach (Future <byte[]> f in futures)
                {
                    recycler.Submit(f.Get());
                }
                futures.Clear();
                return(n);
            }
Example #2
0
        public virtual void TestAllocateRecycle()
        {
            int  countThreshold         = 4;
            int  countLimit             = 8;
            long countResetTimePeriodMs = 200L;

            ByteArrayManager.Impl bam = new ByteArrayManager.Impl(new ByteArrayManager.Conf(countThreshold
                                                                                            , countLimit, countResetTimePeriodMs));
            ByteArrayManager.CounterMap counters = bam.GetCounters();
            ByteArrayManager.ManagerMap managers = bam.GetManagers();
            int[] uncommonArrays = new int[] { 0, 1, 2, 4, 8, 16, 32, 64 };
            int   arrayLength    = 1024;

            TestByteArrayManager.Allocator allocator = new TestByteArrayManager.Allocator(bam
                                                                                          );
            TestByteArrayManager.Recycler recycler = new TestByteArrayManager.Recycler(bam);
            try
            {
                {
                    // allocate within threshold
                    for (int i = 0; i < countThreshold; i++)
                    {
                        allocator.Submit(arrayLength);
                    }
                    WaitForAll(allocator.futures);
                    NUnit.Framework.Assert.AreEqual(countThreshold, counters.Get(arrayLength, false).
                                                    GetCount());
                    NUnit.Framework.Assert.IsNull(managers.Get(arrayLength, false));
                    foreach (int n in uncommonArrays)
                    {
                        NUnit.Framework.Assert.IsNull(counters.Get(n, false));
                        NUnit.Framework.Assert.IsNull(managers.Get(n, false));
                    }
                }
                {
                    // recycle half of the arrays
                    for (int i = 0; i < countThreshold / 2; i++)
                    {
                        recycler.Submit(RemoveLast(allocator.futures).Get());
                    }
                    foreach (Future <int> f in recycler.furtures)
                    {
                        NUnit.Framework.Assert.AreEqual(-1, f.Get());
                    }
                    recycler.furtures.Clear();
                }
                {
                    // allocate one more
                    allocator.Submit(arrayLength).Get();
                    NUnit.Framework.Assert.AreEqual(countThreshold + 1, counters.Get(arrayLength, false
                                                                                     ).GetCount());
                    NUnit.Framework.Assert.IsNotNull(managers.Get(arrayLength, false));
                }
                {
                    // recycle the remaining arrays
                    int n = allocator.RecycleAll(recycler);
                    recycler.Verify(n);
                }
                {
                    // allocate until the maximum.
                    for (int i = 0; i < countLimit; i++)
                    {
                        allocator.Submit(arrayLength);
                    }
                    WaitForAll(allocator.futures);
                    // allocate one more should be blocked
                    TestByteArrayManager.AllocatorThread t = new TestByteArrayManager.AllocatorThread
                                                                 (arrayLength, bam);
                    t.Start();
                    // check if the thread is waiting, timed wait or runnable.
                    for (int i_1 = 0; i_1 < 5; i_1++)
                    {
                        Sharpen.Thread.Sleep(100);
                        Sharpen.Thread.State threadState = t.GetState();
                        if (threadState != Sharpen.Thread.State.Runnable && threadState != Sharpen.Thread.State
                            .Waiting && threadState != Sharpen.Thread.State.TimedWaiting)
                        {
                            NUnit.Framework.Assert.Fail("threadState = " + threadState);
                        }
                    }
                    // recycle an array
                    recycler.Submit(RemoveLast(allocator.futures).Get());
                    NUnit.Framework.Assert.AreEqual(1, RemoveLast(recycler.furtures).Get());
                    // check if the thread is unblocked
                    Sharpen.Thread.Sleep(100);
                    NUnit.Framework.Assert.AreEqual(Sharpen.Thread.State.Terminated, t.GetState());
                    // recycle the remaining, the recycle should be full.
                    NUnit.Framework.Assert.AreEqual(countLimit - 1, allocator.RecycleAll(recycler));
                    recycler.Submit(t.array);
                    recycler.Verify(countLimit);
                    // recycle one more; it should not increase the free queue size
                    NUnit.Framework.Assert.AreEqual(countLimit, bam.Release(new byte[arrayLength]));
                }
            }
            finally
            {
                allocator.pool.Shutdown();
                recycler.pool.Shutdown();
            }
        }