public void TestRunAndStopWithLongWork() { int threadExit = 0; using (DelegateThreadSetManager testInst = new DelegateThreadSetManager(Environment.ProcessorCount, "name", (id, state, token) => { Thread.Sleep(2500); Interlocked.Increment(ref threadExit); })) { Assert.IsTrue(testInst.State == ThreadSetManagerState.Created); Assert.AreEqual(Environment.ProcessorCount, testInst.ThreadCount); Assert.IsFalse(testInst.IsWork); testInst.Start(); TimingAssert.IsTrue(15000, () => testInst.ActiveThreadCount == Environment.ProcessorCount); testInst.Stop(); Assert.IsTrue(testInst.State == ThreadSetManagerState.Stopped); Assert.AreEqual(0, testInst.ActiveThreadCount); Assert.AreEqual(Environment.ProcessorCount, threadExit); } }
public void TestSimpleRunAndStop() { int threadStart = 0; using (DelegateThreadSetManager testInst = new DelegateThreadSetManager(Environment.ProcessorCount, "name", (id, state, token) => { Interlocked.Increment(ref threadStart); })) { Assert.IsTrue(testInst.State == ThreadSetManagerState.Created, "State != Created"); Assert.AreEqual(Environment.ProcessorCount, testInst.ThreadCount); Assert.IsFalse(testInst.IsWork); testInst.Start(); SpinWait.SpinUntil(() => Volatile.Read(ref threadStart) >= testInst.ThreadCount, 10000); TestContext.WriteLine("All thread started"); bool byCondition = SpinWait.SpinUntil(() => testInst.ActiveThreadCount == 0, 5000); TestContext.WriteLine(byCondition ? "ActiveThreadCount == 0" : "ActiveThreadCount != 0 (timeout)"); TimingAssert.AreEqual(15000, Environment.ProcessorCount, () => Volatile.Read(ref threadStart)); TimingAssert.IsTrue(5000, () => testInst.State == ThreadSetManagerState.AllThreadsExited, "State != AllThreadsExited"); testInst.Stop(); Assert.IsTrue(testInst.State == ThreadSetManagerState.Stopped, "State != Stopped"); } }
public void TestProperties() { int threadExit = 0; using (DelegateThreadSetManager testInst = new DelegateThreadSetManager(Environment.ProcessorCount, "name", (id, state, token) => { Interlocked.Increment(ref threadExit); })) { Assert.IsNotNull(testInst.CurrentCulture); Assert.IsNotNull(testInst.CurrentUICulture); Assert.IsTrue(testInst.IsBackground == false); Assert.IsTrue(testInst.Priority == ThreadPriority.Normal); testInst.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; testInst.CurrentUICulture = System.Globalization.CultureInfo.InvariantCulture; testInst.IsBackground = true; testInst.Priority = ThreadPriority.AboveNormal; Assert.AreEqual(System.Globalization.CultureInfo.InvariantCulture, testInst.CurrentCulture); Assert.AreEqual(System.Globalization.CultureInfo.InvariantCulture, testInst.CurrentUICulture); Assert.IsTrue(testInst.IsBackground == true); Assert.IsTrue(testInst.Priority == ThreadPriority.AboveNormal); testInst.Stop(); } }
public void TestCancellationOnStop() { int threadEnter = 0; int threadExit = 0; ManualResetEventSlim waiter = new ManualResetEventSlim(false); using (DelegateThreadSetManager testInst = new DelegateThreadSetManager(Environment.ProcessorCount, "name", (id, state, token) => { try { Interlocked.Increment(ref threadEnter); waiter.Wait(token); } finally { Interlocked.Increment(ref threadExit); } })) { testInst.Start(); TimingAssert.IsTrue(15000, () => testInst.State == ThreadSetManagerState.Running); TimingAssert.IsTrue(15000, () => testInst.ActiveThreadCount == Environment.ProcessorCount); TimingAssert.IsTrue(15000, () => Volatile.Read(ref threadEnter) == Environment.ProcessorCount); testInst.Stop(); Assert.IsTrue(testInst.State == ThreadSetManagerState.Stopped); Assert.AreEqual(0, testInst.ActiveThreadCount); Assert.AreEqual(Environment.ProcessorCount, threadExit, "threadExit != configurated thread count"); } }
public void CanStopNotStartedManager() { int threadStart = 0; using (DelegateThreadSetManager testInst = new DelegateThreadSetManager(Environment.ProcessorCount, "name", (id, state, token) => { Interlocked.Increment(ref threadStart); })) { testInst.Stop(); } }
public void TestProperties() { int threadExit = 0; using (DelegateThreadSetManager testInst = new DelegateThreadSetManager(Environment.ProcessorCount, "name", (id, state, token) => { Interlocked.Increment(ref threadExit); })) { Assert.IsTrue(testInst.IsBackground == false); Assert.IsTrue(testInst.Priority == ThreadPriority.Normal); testInst.IsBackground = true; testInst.Priority = ThreadPriority.AboveNormal; Assert.IsTrue(testInst.IsBackground == true); Assert.IsTrue(testInst.Priority == ThreadPriority.AboveNormal); testInst.Stop(); } }
public void TestCultureProperties() { int threadExit = 0; using (DelegateThreadSetManager testInst = new DelegateThreadSetManager(Environment.ProcessorCount, "name", (id, state, token) => { Interlocked.Increment(ref threadExit); })) { Assert.IsNotNull(testInst.CurrentCulture); Assert.IsNotNull(testInst.CurrentUICulture); #if NET45 testInst.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; testInst.CurrentUICulture = System.Globalization.CultureInfo.InvariantCulture; Assert.AreEqual(System.Globalization.CultureInfo.InvariantCulture, testInst.CurrentCulture); Assert.AreEqual(System.Globalization.CultureInfo.InvariantCulture, testInst.CurrentUICulture); #endif testInst.Stop(); } }
/// <summary> /// Clean-up all resources /// </summary> /// <param name="isUserCall">Was called by user</param> protected override void Dispose(bool isUserCall) { if (!_isDisposed) { _isDisposed = true; if (_backgroundCompactionThread != null) { _backgroundCompactionThread.Stop(true); } _addMonitor.Dispose(); _takeMonitor.Dispose(); _peekMonitor.Dispose(); lock (_segmentOperationsLock) { foreach (var segment in _segments) { segment.Dispose(); } } } }
/// <summary> /// Clean-up all resources /// </summary> /// <param name="isUserCall">Was called by user</param> protected override void Dispose(bool isUserCall) { if (!_isDisposed) { _isDisposed = true; if (_backgroundTransferer != null) { _backgroundTransferer.Stop(waitForStop: true); } _addMonitor.Dispose(); _takeMonitor.Dispose(); _peekMonitor.Dispose(); _lowLevelQueue.Dispose(); _highLevelQueue.Dispose(); if (_backgoundTransfererExclusive != null) { _backgoundTransfererExclusive.Dispose(); } } }