예제 #1
0
        public void TestUnlockWhenItemReceive()
        {
            int processed = 0;
            ManualResetEventSlim waiter = new ManualResetEventSlim(false);

            using (DelegateQueueAsyncProcessor <int> proc = new DelegateQueueAsyncProcessor <int>(Environment.ProcessorCount, 1000, "name", (elem, token) =>
            {
                Interlocked.Increment(ref processed);
                waiter.Wait(token);
            }))
            {
                proc.Start();

                for (int i = 0; i < proc.ThreadCount; i++)
                {
                    proc.Add(i);
                }

                //if (!SpinWait.SpinUntil(() => Volatile.Read(ref processed) == proc.ThreadCount, 5000))
                //{
                //    System.Diagnostics.Debugger.Launch();
                //    throw new Exception("Proc: " + Volatile.Read(ref processed).ToString() + ", threads: " + proc.ActiveThreadCount.ToString() +
                //                        ", tasks: " + proc.ElementCount.ToString());
                //}

                TimingAssert.IsTrue(15000, () => Volatile.Read(ref processed) == proc.ThreadCount, "FAILED: wait for all thread start");

                waiter.Set();
                proc.Stop(true, true, true);

                Assert.IsTrue(proc.State == QueueAsyncProcessorState.Stopped, "proc.State == QueueAsyncProcessorState.Stopped");
            }
        }
예제 #2
0
        public void TestWaitUntilStop()
        {
            int processed = 0;

            using (DelegateQueueAsyncProcessor <int> proc = new DelegateQueueAsyncProcessor <int>(Environment.ProcessorCount, 1000, "name", (elem, token) =>
            {
                Thread.Sleep(1000);
                Interlocked.Increment(ref processed);
            }).ThenStart())
            {
                for (int i = 0; i < 10; i++)
                {
                    proc.Add(i);
                }

                proc.Stop(false, true, false);

                Assert.IsTrue(proc.State == QueueAsyncProcessorState.StopRequested);

                proc.WaitUntilStop();

                Assert.IsTrue(proc.State == QueueAsyncProcessorState.Stopped);
                Assert.IsTrue(processed == 10);
            }
        }
예제 #3
0
        public void TestHardStopWork()
        {
            int processed               = 0;
            int startedTask             = 0;
            ManualResetEventSlim waiter = new ManualResetEventSlim(false);

            using (DelegateQueueAsyncProcessor <int> proc = new DelegateQueueAsyncProcessor <int>(Environment.ProcessorCount, 1000, "name", (elem, token) =>
            {
                try
                {
                    Interlocked.Increment(ref startedTask);
                    waiter.Wait(token);
                }
                finally
                {
                    Interlocked.Increment(ref processed);
                }
            }))
            {
                proc.Start();

                for (int i = 0; i < 5 * Environment.ProcessorCount; i++)
                {
                    proc.Add(i);
                }

                Assert.IsTrue(proc.ThreadCount > 0, "proc.ThreadCount > 0");
                Assert.IsTrue(proc.ThreadCount == Environment.ProcessorCount, "proc.ThreadCount == Environment.ProcessorCount");

                TimingAssert.IsTrue(10000, () => proc.ActiveThreadCount >= 0, "FAILED: wait while thread activated");
                TimingAssert.IsTrue(10000, () => proc.ActiveThreadCount == proc.ThreadCount, "FAILED: wait while all threads activated");

                TimingAssert.IsTrue(10000, () => Volatile.Read(ref startedTask) >= 0, "FAILED: wait while first thread blocked");
                TimingAssert.IsTrue(10000, () => Volatile.Read(ref startedTask) == proc.ThreadCount, () => "FAILED: wait while all thread blocked. Currently blocked = " + Volatile.Read(ref startedTask).ToString() + ", expected = " + proc.ThreadCount.ToString());
                proc.Stop(true, false, true);

                Assert.IsTrue(proc.State == QueueAsyncProcessorState.Stopped, "proc.State == QueueAsyncProcessorState.Stopped");
                Assert.IsTrue(processed > 0, "processed > 0");
            }
        }