public void Stress_Tasks() { int maxThreadPool = 0; int completionPortThreads = 0; System.Threading.ThreadPool.GetMaxThreads(out maxThreadPool, out completionPortThreads); System.Threading.ThreadPool.SetMaxThreads(10, 10); var mre = new ManualResetEvent(false); int taskCountCompleted = 0; List <int> threadIdList = new List <int>(); for (int i = 0; i < 50; i++) { TasksHost.Enqueue <StressTask>(completed: (dic) => { var threadId = (int)dic["ThreadId"]; if (!threadIdList.Any(t => t == threadId)) { threadIdList.Add(threadId); } taskCountCompleted++; if (taskCountCompleted == 50) { mre.Set(); } }); } mre.WaitOne(); System.Threading.ThreadPool.SetMaxThreads(maxThreadPool, completionPortThreads); Check.That(threadIdList.Count).IsGreaterThan(0); }
public void Enqueue_And_Cancel() { var mre = new ManualResetEvent(false); var key = Guid.NewGuid(); var parameter = new Dictionary <string, object>(); parameter.Add("count", 0); TasksHost.Enqueue <LongTask>(key, parameter, completed: (dic) => { mre.Set(); }); System.Threading.Thread.Sleep(2 * 1000); TasksHost.Cancel(key); mre.WaitOne(); var history = TasksHost.GetHistory(key); Check.That(history.CanceledDate).IsNotNull(); var count = history.Parameters["count"]; Check.That((int)count).IsLessThan(10); }
public void Enqueue_Multi_Task() { var chrono = new System.Diagnostics.Stopwatch(); chrono.Start(); TasksHost.Enqueue <LongTask>(); TasksHost.Enqueue <LongTask>(); TasksHost.Enqueue <LongTask>(); TasksHost.Enqueue <LongTask>(); TasksHost.Enqueue <LongTask>(); TasksHost.Enqueue <LongTask>(); TasksHost.Enqueue <LongTask>(); TasksHost.Enqueue <LongTask>(); TasksHost.Enqueue <LongTask>(); TasksHost.Enqueue <LongTask>(); while (true) { if (!TasksHost.IsRunning()) { break; } System.Threading.Thread.Sleep(1000); } chrono.Stop(); Check.That(chrono.ElapsedMilliseconds).IsLessThan(10 * 10 * 1000); }
public void Stop_TasksHost() { TasksHost.Enqueue <MyTask>(); TasksHost.Enqueue <MyTask>(); TasksHost.Enqueue <MyTask>(); TasksHost.Enqueue <MyTask>(); TasksHost.Enqueue <StressTask>(); TasksHost.Stop(); }
public void Task_Is_Running() { var id = Guid.NewGuid(); var mre = new ManualResetEvent(false); TasksHost.Enqueue <LongTask>(id, completed: (dic) => mre.Set()); System.Threading.Thread.Sleep(1 * 1000); var result = TasksHost.IsRunning(id); mre.WaitOne(); Check.That(result).IsTrue(); }
public void Cancel_Terminated_Task() { var id = Guid.NewGuid(); var mre = new ManualResetEvent(false); TasksHost.Enqueue <MyTask>(id, completed: (dic) => { mre.Set(); }); mre.WaitOne(); TasksHost.Cancel(id); }
public void Task_Exists() { var id = Guid.NewGuid(); var mre = new ManualResetEvent(false); TasksHost.Enqueue <MyTask>(id, completed: (dic) => { mre.Set(); }); mre.WaitOne(); var result = TasksHost.Exists(id); Check.That(result).IsTrue(); }
public void Tasks_Cleanup() { var id = Guid.NewGuid(); var mre = new ManualResetEvent(false); TasksHost.Enqueue <MyTask>(id, completed: (dic) => { mre.Set(); }); mre.WaitOne(); TasksHost.Cleanup(); var result = TasksHost.GetHistory(id); Check.That(result).IsNull(); }
public void Enqueue_Fail_Task() { var id = Guid.NewGuid(); var mre = new ManualResetEvent(false); TasksHost.Enqueue <FailedTask>(id , completed: (dic) => { mre.Set(); }); mre.WaitOne(); var histo = TasksHost.GetHistory(id); Check.That(histo.Exception != null).IsTrue(); }
public void Enqueue_With_Parameter() { var id = Guid.NewGuid(); var mre = new ManualResetEvent(false); TasksHost.Enqueue <ParameterizedTask>(id, new Dictionary <string, object>() { { "input", "test" } }, completed: (dic) => { var output = dic["output"]; Check.That(output).Equals("test"); mre.Set(); }); mre.WaitOne(); }
public void Enqueue_Task_With_Progress() { var tp = new MockTaskProgress(); TasksOnTime.Notification.NotificationService.SetTaskProgress(tp); var mre = new ManualResetEvent(false); var key = Guid.NewGuid(); TasksHost.Enqueue <ProgressTask>(key, completed: (dic) => { mre.Set(); }); mre.WaitOne(); Check.That(tp.NotificationList).IsNotNull(); Check.That(tp.NotificationList.Count).IsGreaterThan(0); }
public void Non_Generic_Enqueue() { var mre = new ManualResetEvent(false); var key = Guid.NewGuid(); TasksHost.Enqueue(key, typeof(MyTask), completed: (dic) => { mre.Set(); }); mre.WaitOne(); var history = TasksHost.GetHistory(key); Check.That(history).IsNotNull(); Check.That(history.TerminatedDate).IsNotNull(); Check.That(history.Context).IsNull(); }
public void Enqueue_With_Delay() { var task = new MyTask(); var mre = new ManualResetEvent(false); var chrono = new System.Diagnostics.Stopwatch(); chrono.Start(); TasksHost.Enqueue <MyTask>( completed: (dic) => { mre.Set(); }, delayInMillisecond: 5 * 1000); mre.WaitOne(); chrono.Stop(); Check.That(chrono.ElapsedMilliseconds).IsGreaterThan(4 * 1000); }
internal virtual void ProcessTask(ScheduledTask scheduledTask) { if (scheduledTask.IsQueued && !scheduledTask.AllowMultipleInstance) { GlobalConfiguration.Logger.Warn("scheduled task {0} is in queue and not allow multiple instance", scheduledTask.Name); return; } if (!scheduledTask.AllowMultipleInstance) { if (TasksHost.IsRunning(scheduledTask.Name)) { GlobalConfiguration.Logger.Warn("scheduled task {0} is already running and not allow multiple instance", scheduledTask.Name); return; } } var id = Guid.NewGuid(); scheduledTask.IsQueued = true; TasksHost.Enqueue( id , scheduledTask.Name , scheduledTask.TaskType , scheduledTask.Parameters , (dic) => { GlobalConfiguration.Logger.Info("scheduled task {0} completed", scheduledTask.Name); scheduledTask.IsQueued = false; try { if (scheduledTask.Completed != null) { scheduledTask.Completed(dic); } } catch (Exception ex) { GlobalConfiguration.Logger.Error(ex); } finally { scheduledTask.IsForced = false; if (scheduledTask.NextRunningDateFactory != null) { try { scheduledTask.NextRunningDate = scheduledTask.NextRunningDateFactory.Invoke(); } catch (Exception ex) { GlobalConfiguration.Logger.Error(ex); } } } } , (ex) => { scheduledTask.Exception = ex; GlobalConfiguration.Logger.Error(ex); if (TaskFailed != null) { try { TaskFailed.Invoke(scheduledTask.Name, ex); } catch { } } }, null, () => { GlobalConfiguration.Logger.Info("scheduled task {0} started", scheduledTask.Name); scheduledTask.StartedCount += 1; if (TaskStarted != null) { try { TaskStarted.Invoke(scheduledTask.Name); } catch { } } }, true, scheduledTask.IsForced); }
public void Enqueue_With_Task_Not_Implements_ITask() { TasksHost.Enqueue(typeof(BadTask)); }