public void WorkItemWaitCanceling() { Assert.ThrowsException <WorkItemTimeoutException>(() => { STP smartThreadPool = new STP(); ManualResetEvent cancelWaitHandle = new ManualResetEvent(false); // Queue a work item that will occupy the thread in the pool IWorkItemResult wir1 = smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); // Queue another work item that will wait for the first to complete IWorkItemResult wir2 = smartThreadPool.QueueWorkItem(new WorkItemCallback(this.SignalCancel), cancelWaitHandle); try { wir1.GetResult(System.Threading.Timeout.Infinite, true, cancelWaitHandle); } finally { smartThreadPool.Shutdown(); } }); }
public void QueueWorkItem_WhenQueueMaxLengthZero_RejectsInsteadOfQueueing() { new Thread(() => Assert.ThrowsException <QueueRejectedException>(() => { var info = new STPStartInfo { MaxQueueLength = 0, MinWorkerThreads = 2, MaxWorkerThreads = 2, }; var pool = new STP(info); pool.Start(); try { pool.QueueWorkItem(SleepForOneSecond); // Taken by waiter immediately. Not queued. pool.QueueWorkItem(SleepForOneSecond); // Taken by waiter immediately. Not queued. } catch (QueueRejectedException e) { throw new Exception("Caught QueueRejectedException too early: ", e); } pool.QueueWorkItem(SleepForOneSecond); })).Start(); }
public void ExceptionThrowing() { STP smartThreadPool = new STP(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); DivArgs divArgs = new DivArgs { x = 10, y = 0 }; IWorkItemResult wir = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoDiv), divArgs); try { wir.GetResult(); } catch (WorkItemResultException wire) { Assert.IsTrue(wire.InnerException is DivideByZeroException); return; } catch (Exception e) { e.GetHashCode(); Assert.Fail(); } Assert.Fail(); }
private void PauseSTP(STP stp) { _pauseSTP.Reset(); stp.QueueWorkItem( new WorkItemCallback(this.DoPauseSTP), null); }
public void QueueWorkItem_WhenMaxIsSet_ThrowsExceptionWhenHit() { Assert.ThrowsException <QueueRejectedException>(() => { var info = new STPStartInfo { MaxQueueLength = 1, MinWorkerThreads = 1, MaxWorkerThreads = 1, }; var pool = new STP(info); pool.Start(); try { pool.QueueWorkItem(SleepForOneSecond); // Taken by waiter immediately. Not queued. pool.QueueWorkItem(SleepForOneSecond); // No waiters available, pool at max threads. Queued. } catch (QueueRejectedException e) { throw new Exception("Caught QueueRejectedException too early: ", e); } // No waiters available, queue is at max (1). Throws. pool.QueueWorkItem(SleepForOneSecond); }); }
public void WaitAny() { STP smartThreadPool = new STP(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); bool success = false; IWorkItemResult[] wirs = new IWorkItemResult[5]; for (int i = 0; i < wirs.Length; ++i) { wirs[i] = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); } int index = STP.WaitAny(wirs); if (wirs[index].IsCompleted) { int result = (int)wirs[index].GetResult(); if (1 == result) { success = true; } } smartThreadPool.Shutdown(); Assert.IsTrue(success); }
/// <summary> /// Example of how to use the post execute callback /// </summary> private bool DoTestPostExecute(CallToPostExecute callToPostExecute, bool answer) { STP smartThreadPool = new STP(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); bool success = false; PostExecuteResult postExecuteResult = new PostExecuteResult(); IWorkItemResult wir = workItemsGroup.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), postExecuteResult, new PostExecuteWorkItemCallback(this.DoSomePostExecuteWork), callToPostExecute); if (!wir.IsCompleted) { int result = (int)wir.GetResult(); success = (1 == result); success = success && (postExecuteResult.wh.WaitOne(1000, true) == answer); } smartThreadPool.Shutdown(); return(success); }
public void WorkItemWaitCanceling() { STP smartThreadPool = new STP(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); ManualResetEvent cancelWaitHandle = new ManualResetEvent(false); bool success = false; // Queue a work item that will occupy the thread in the pool IWorkItemResult wir1 = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); // Queue another work item that will wait for the first to complete IWorkItemResult wir2 = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.SignalCancel), cancelWaitHandle); try { wir1.GetResult(System.Threading.Timeout.Infinite, true, cancelWaitHandle); } catch (WorkItemTimeoutException) { success = true; } smartThreadPool.Shutdown(); Assert.IsTrue(success); }
public void TwoWIGsStartSuspended() { STP stp = new STP(); WIGStartInfo wigStartInfo = new WIGStartInfo { StartSuspended = true }; IWorkItemsGroup wig1 = stp.CreateWorkItemsGroup(10, wigStartInfo); IWorkItemsGroup wig2 = stp.CreateWorkItemsGroup(10, wigStartInfo); wig1.QueueWorkItem(new WorkItemCallback(this.DoWork)); wig2.QueueWorkItem(new WorkItemCallback(this.DoWork)); Assert.IsFalse(wig1.WaitForIdle(200)); Assert.IsFalse(wig2.WaitForIdle(200)); wig1.Start(); Assert.IsTrue(wig1.WaitForIdle(200)); Assert.IsFalse(wig2.WaitForIdle(200)); wig2.Start(); Assert.IsTrue(wig1.WaitForIdle(0)); Assert.IsTrue(wig2.WaitForIdle(200)); }
/// <summary> /// Example of how to use the post execute callback /// </summary> private bool DoTestDefaultPostExecute(CallToPostExecute callToPostExecute, bool answer) { STPStartInfo stpStartInfo = new STPStartInfo { CallToPostExecute = callToPostExecute, PostExecuteWorkItemCallback = new PostExecuteWorkItemCallback(this.DoSomePostExecuteWork) }; STP smartThreadPool = new STP(stpStartInfo); bool success = false; PostExecuteResult postExecuteResult = new PostExecuteResult(); IWorkItemResult wir = smartThreadPool.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), postExecuteResult); if (!wir.IsCompleted) { int result = (int)wir.GetResult(); success = (1 == result); success = success && (postExecuteResult.wh.WaitOne(1000, true) == answer); } smartThreadPool.Shutdown(); return(success); }
public void STPAndWIGStartSuspended() { STPStartInfo stpStartInfo = new STPStartInfo { StartSuspended = true }; STP stp = new STP(stpStartInfo); WIGStartInfo wigStartInfo = new WIGStartInfo { StartSuspended = true }; IWorkItemsGroup wig = stp.CreateWorkItemsGroup(10, wigStartInfo); wig.QueueWorkItem(new WorkItemCallback(this.DoWork)); Assert.IsFalse(wig.WaitForIdle(200)); wig.Start(); Assert.IsFalse(wig.WaitForIdle(200)); stp.Start(); Assert.IsTrue(wig.WaitForIdle(5000), "WIG is not idle"); Assert.IsTrue(stp.WaitForIdle(5000), "STP is not idle"); }
public void TestWIGConcurrencyChange() { STP smartThreadPool = new STP(10 * 1000, 25, 0); IWorkItemsGroup wig = smartThreadPool.CreateWorkItemsGroup(smartThreadPool.MaxThreads); bool success = false; for (int i = 0; i < 100; ++i) { wig.QueueWorkItem(new WorkItemCallback(this.DoSomeLongWork), null); } wig.Concurrency = 1; success = WaitForWIGThreadsInUse(wig, 1, 1 * 1000); Assert.IsTrue(success); wig.Concurrency = 5; success = WaitForWIGThreadsInUse(wig, 5, 2 * 1000); Assert.IsTrue(success); wig.Concurrency = 25; success = WaitForWIGThreadsInUse(wig, 25, 4 * 1000); Assert.IsTrue(success); wig.Concurrency = 10; success = WaitForWIGThreadsInUse(wig, 10, 10 * 1000); Assert.IsTrue(success); smartThreadPool.Shutdown(); }
public void DontDisposeCallerState() { STP smartThreadPool = new STP(); WIGStartInfo wigStartInfo = new WIGStartInfo(); wigStartInfo.DisposeOfStateObjects = false; IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue, wigStartInfo); CallerState nonDisposableCallerState = new NonDisposableCallerState(); CallerState disposableCallerState = new DisposableCallerState(); IWorkItemResult wir1 = workItemsGroup.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), nonDisposableCallerState); IWorkItemResult wir2 = workItemsGroup.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), disposableCallerState); wir1.GetResult(); bool success = (1 == nonDisposableCallerState.Value); wir2.GetResult(); success = success && (1 == disposableCallerState.Value); smartThreadPool.Shutdown(); Assert.IsTrue(success); }
public void WaitForIdleOnSTPThreadForAnotherWorkItemsGroup() { STP smartThreadPool = new STP(10 * 1000, 25, 0); IWorkItemsGroup workItemsGroup1 = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); IWorkItemsGroup workItemsGroup2 = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); workItemsGroup1.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), 1000); workItemsGroup1.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), 1000); IWorkItemResult wir = workItemsGroup2.QueueWorkItem( new WorkItemCallback(this.DoWaitForIdle), workItemsGroup1); Exception e; wir.GetResult(out e); smartThreadPool.Shutdown(); Assert.IsNull(e); }
public void ExceptionReturning() { bool success = true; STP smartThreadPool = new STP(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); DivArgs divArgs = new DivArgs { x = 10, y = 0 }; IWorkItemResult wir = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoDiv), divArgs); Exception e = null; try { wir.GetResult(out e); } catch (Exception ex) { ex.GetHashCode(); success = false; } Assert.IsTrue(success); Assert.IsTrue(e is DivideByZeroException); }
public void Init() { STPStartInfo stpStartInfo = new STPStartInfo { FillStateWithArgs = true//FIXME:FillStateWithParams }; _stp = new STP(stpStartInfo); }
public void TestWIGConcurrencyChange2WIGs() { STP smartThreadPool = new STP(10 * 1000, 2, 0); Assert.IsTrue(0 == smartThreadPool.WaitingCallbacks); PauseSTP(smartThreadPool); PauseSTP(smartThreadPool); Thread.Sleep(100); Assert.IsTrue(0 == smartThreadPool.WaitingCallbacks); IWorkItemsGroup wig1 = smartThreadPool.CreateWorkItemsGroup(1); IWorkItemsGroup wig2 = smartThreadPool.CreateWorkItemsGroup(1); wig1.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); Assert.IsTrue(1 == smartThreadPool.WaitingCallbacks); wig2.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); Assert.IsTrue(2 == smartThreadPool.WaitingCallbacks); wig1.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); Assert.IsTrue(2 == smartThreadPool.WaitingCallbacks); wig2.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); Assert.IsTrue(2 == smartThreadPool.WaitingCallbacks); wig1.Concurrency = 2; Thread.Sleep(100); Assert.IsTrue(3 == smartThreadPool.WaitingCallbacks); wig2.Concurrency = 2; Thread.Sleep(100); Assert.IsTrue(4 == smartThreadPool.WaitingCallbacks); ResumeSTP(smartThreadPool); Thread.Sleep(100); Assert.IsTrue(0 == smartThreadPool.WaitingCallbacks); PauseSTP(smartThreadPool); PauseSTP(smartThreadPool); Thread.Sleep(100); wig1.Concurrency = 1; wig1.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); Assert.IsTrue(1 == smartThreadPool.WaitingCallbacks); wig2.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); Assert.IsTrue(2 == smartThreadPool.WaitingCallbacks); ResumeSTP(smartThreadPool); Thread.Sleep(100); Assert.IsTrue(0 == smartThreadPool.WaitingCallbacks); smartThreadPool.Shutdown(); }
public void Init() { _stp = new STP(); WIGStartInfo wigStartInfo = new WIGStartInfo { FillStateWithArgs = true }; _wig = _stp.CreateWorkItemsGroup(10, wigStartInfo); }
public void GoodCallback() { STP smartThreadPool = new STP(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); workItemsGroup.QueueWorkItem(new WorkItemCallback(DoWork)); workItemsGroup.WaitForIdle(); smartThreadPool.Shutdown(); }
public void StpStartInfo_WithZeroMaxQueueLength_IsAllowed() { var info = new STPStartInfo { MaxQueueLength = 0, }; var pool = new STP(info); pool.Start(); Assert.IsTrue(0 == pool.STPStartInfo.MaxQueueLength); }
public void QueueWorkItem_WhenMaxIsNull_Queues() { var info = new STPStartInfo { MaxQueueLength = null, }; var pool = new STP(info); pool.Start(); var workItem = pool.QueueWorkItem <object>(ReturnNull); // If rejected, an exception would have been thrown instead. Assert.IsTrue(workItem.GetResult() == null); }
public void SetMaxQueueLength_FromNonZeroValueToZero_DisablesQueueing() { Assert.ThrowsException <QueueRejectedException>(() => { var info = new STPStartInfo { MinWorkerThreads = 1, MaxWorkerThreads = 1, MaxQueueLength = 1, }; var pool = new STP(info); pool.Start(); try { pool.QueueWorkItem(SleepForOneSecond); // Picked up by waiter. pool.QueueWorkItem(SleepForOneSecond); // Queued. } catch (QueueRejectedException e) { throw new Exception("Caught QueueRejectedException too early: ", e); } try { pool.QueueWorkItem(SleepForOneSecond); } catch (QueueRejectedException) { // Expected Assert.IsTrue(true); } pool.MaxQueueLength = 0; Thread.Sleep(2100); // Let the work items complete. try { pool.QueueWorkItem(SleepForOneSecond); // Picked up by waiter. } catch (QueueRejectedException e) { throw new Exception("Caught QueueRejectedException too early: ", e); } pool.QueueWorkItem(SleepForOneSecond); // Rejected (max queue length is zero). }); }
public void WaitForIdleEvent() { STP smartThreadPool = new STP(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(1); ManualResetEvent wigIsIdle = new ManualResetEvent(false); workItemsGroup.OnIdle += wig => wigIsIdle.Set(); workItemsGroup.QueueWorkItem(() => { }); bool eventFired = wigIsIdle.WaitOne(100, true); smartThreadPool.Shutdown(); Assert.IsTrue(eventFired); }
public void StartSuspended() { STPStartInfo stpStartInfo = new STPStartInfo { StartSuspended = true }; STP stp = new STP(stpStartInfo); stp.QueueWorkItem(new WorkItemCallback(this.DoWork)); Assert.IsFalse(stp.WaitForIdle(200)); stp.Start(); Assert.IsTrue(stp.WaitForIdle(200)); }
private static void CheckIsBackground(bool isBackground) { STPStartInfo stpStartInfo = new STPStartInfo { AreThreadsBackground = isBackground }; STP stp = new STP(stpStartInfo); IWorkItemResult <bool> wir = stp.QueueWorkItem(() => GetCurrentThreadIsBackground()); bool resultIsBackground = wir.GetResult(); stp.WaitForIdle(); Assert.AreEqual(isBackground, resultIsBackground); }
public void ChainedDelegatesCallback() { Assert.ThrowsException <NotSupportedException>(() => { STP smartThreadPool = new STP(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); WorkItemCallback workItemCallback = new WorkItemCallback(DoWork); workItemCallback += new WorkItemCallback(DoWork); workItemsGroup.QueueWorkItem(workItemCallback); workItemsGroup.WaitForIdle(); smartThreadPool.Shutdown(); }); }
private void Concurrency( int concurrencyPerWig, int wigsCount, int workItemsCount) { Console.WriteLine( "Testing : concurrencyPerWig = {0}, wigsCount = {1}, workItemsCount = {2}", concurrencyPerWig, wigsCount, workItemsCount); _success = true; _concurrencyPerWig = concurrencyPerWig; _randGen = new Random(0); STPStartInfo stpStartInfo = new STPStartInfo { StartSuspended = true }; STP stp = new STP(stpStartInfo); _concurrentOps = new int[wigsCount]; IWorkItemsGroup[] wigs = new IWorkItemsGroup[wigsCount]; for (int i = 0; i < wigs.Length; ++i) { wigs[i] = stp.CreateWorkItemsGroup(_concurrencyPerWig); for (int j = 0; j < workItemsCount; ++j) { wigs[i].QueueWorkItem(new WorkItemCallback(this.DoWork), i); } wigs[i].Start(); } stp.Start(); stp.WaitForIdle(); Assert.IsTrue(_success); stp.Shutdown(); }
public void QueueWorkItem_WhenBiggerMaxIsSet_ThrowsExceptionWhenHit() { new Thread(() => Assert.ThrowsException <QueueRejectedException>(() => { var info = new STPStartInfo { MaxQueueLength = 5, MinWorkerThreads = 5, MaxWorkerThreads = 10, }; var pool = new STP(info); pool.Start(); try { // Pool starts with 5 available waiters. pool.QueueWorkItem(SleepForOneSecond); // Taken by waiter immediately. Not queued. pool.QueueWorkItem(SleepForOneSecond); // Taken by waiter immediately. Not queued. pool.QueueWorkItem(SleepForOneSecond); // Taken by waiter immediately. Not queued. pool.QueueWorkItem(SleepForOneSecond); // Taken by waiter immediately. Not queued. pool.QueueWorkItem(SleepForOneSecond); // Taken by waiter immediately. Not queued. pool.QueueWorkItem(SleepForOneSecond); // New thread created, takes work item. Not queued. pool.QueueWorkItem(SleepForOneSecond); // New thread created, takes work item. Not queued. pool.QueueWorkItem(SleepForOneSecond); // New thread created, takes work item. Not queued. pool.QueueWorkItem(SleepForOneSecond); // New thread created, takes work item. Not queued. pool.QueueWorkItem(SleepForOneSecond); // New thread created, takes work item. Not queued. pool.QueueWorkItem(SleepForOneSecond); // No waiters available. Queued. pool.QueueWorkItem(SleepForOneSecond); // No waiters available. Queued. pool.QueueWorkItem(SleepForOneSecond); // No waiters available. Queued. pool.QueueWorkItem(SleepForOneSecond); // No waiters available. Queued. pool.QueueWorkItem(SleepForOneSecond); // No waiters available. Queued. } catch (QueueRejectedException e) { throw new Exception("Caught QueueRejectedException too early: ", e); } // All threads are busy, and queue is at its max. Throws. pool.QueueWorkItem(SleepForOneSecond); })).Start(); }
public void WaitForIdleWithCancel() { STP smartThreadPool = new STP(10 * 1000, 1, 1); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(2); _x = 0; IWorkItemResult wir1 = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), 1000); IWorkItemResult wir2 = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), 1000); IWorkItemResult wir3 = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), 1000); while (0 == _x) { Thread.Sleep(10); } Console.WriteLine("{0}: Cancelling WIG", DateTime.Now.ToLongTimeString()); workItemsGroup.Cancel(); // At this point: // The first work item is running // The second work item is cancelled, but waits in the STP queue // The third work item is cancelled. Assert.AreEqual(1, _x); Assert.IsTrue(wir2.IsCanceled); Assert.IsTrue(wir3.IsCanceled); // Make sure the workItemsGroup is still idle Assert.IsFalse(workItemsGroup.IsIdle); Console.WriteLine("{0}: Waiting for 1st result", DateTime.Now.ToLongTimeString()); wir1.GetResult(); Assert.AreEqual(2, _x); bool isIdle = workItemsGroup.WaitForIdle(100); Assert.IsTrue(isIdle); smartThreadPool.Shutdown(); }
public void Timeout() { Assert.ThrowsException <WorkItemTimeoutException>(() => { STP smartThreadPool = new STP(); IWorkItemResult wir = smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); try { wir.GetResult(500, true); } finally { smartThreadPool.Shutdown(); } }); }