Пример #1
0
        public virtual void TestByteArrayManager()
        {
            int  countThreshold         = 32;
            int  countLimit             = 64;
            long countResetTimePeriodMs = 1000L;

            ByteArrayManager.Impl bam = new ByteArrayManager.Impl(new ByteArrayManager.Conf(countThreshold
                                                                                            , countLimit, countResetTimePeriodMs));
            ByteArrayManager.CounterMap counters = bam.GetCounters();
            ByteArrayManager.ManagerMap managers = bam.GetManagers();
            ExecutorService             pool     = Executors.NewFixedThreadPool(128);

            TestByteArrayManager.Runner[] runners = new TestByteArrayManager.Runner[TestByteArrayManager.Runner
                                                                                    .NumRunners];
            Sharpen.Thread[] threads = new Sharpen.Thread[runners.Length];
            int num = 1 << 10;

            for (int i = 0; i < runners.Length; i++)
            {
                runners[i] = new TestByteArrayManager.Runner(i, countThreshold, countLimit, pool,
                                                             i, bam);
                threads[i] = runners[i].Start(num);
            }
            IList <Exception> exceptions = new AList <Exception>();

            Sharpen.Thread randomRecycler = new _Thread_332(runners, exceptions, threads);
            randomRecycler.Start();
            randomRecycler.Join();
            NUnit.Framework.Assert.IsTrue(exceptions.IsEmpty());
            NUnit.Framework.Assert.IsNull(counters.Get(0, false));
            for (int i_1 = 1; i_1 < runners.Length; i_1++)
            {
                if (!runners[i_1].assertionErrors.IsEmpty())
                {
                    foreach (Exception e in runners[i_1].assertionErrors)
                    {
                        Log.Error("AssertionError " + i_1, e);
                    }
                    NUnit.Framework.Assert.Fail(runners[i_1].assertionErrors.Count + " AssertionError(s)"
                                                );
                }
                int  arrayLength                      = TestByteArrayManager.Runner.Index2arrayLength(i_1);
                bool exceedCountThreshold             = counters.Get(arrayLength, false).GetCount() > countThreshold;
                ByteArrayManager.FixedLengthManager m = managers.Get(arrayLength, false);
                if (exceedCountThreshold)
                {
                    NUnit.Framework.Assert.IsNotNull(m);
                }
                else
                {
                    NUnit.Framework.Assert.IsNull(m);
                }
            }
        }
Пример #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();
            }
        }