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"); } }
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); } }
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"); } }