public void TestCompositeTaskRunner() { int attempts = 0; CompositeTaskRunner runner = new CompositeTaskRunner(); CountingTask task1 = new CountingTask("task1", 100); CountingTask task2 = new CountingTask("task2", 200); runner.AddTask(task1); runner.AddTask(task2); runner.Wakeup(); while (attempts++ != 10) { Thread.Sleep(1000); if (task1.Count == 100 && task2.Count == 200) { break; } } Assert.IsTrue(task1.Count == 100); Assert.IsTrue(task2.Count == 200); runner.RemoveTask(task1); runner.RemoveTask(task2); }
public void CompositeTaskRunnerDoesntHoldLockWhileCallingIterate() { object lockObj = new object(); // Start a task running that takes a shared lock during it's iterate. CompositeTaskRunner runner = new CompositeTaskRunner(); runner.AddTask(new LockingTask(lockObj)); // Start a separate thread that holds that same lock whilst manipulating the CompositeTaskRunner (See InactivityMonitor for real example). AutoResetEvent resetEvent = new AutoResetEvent(false); ThreadPool.QueueUserWorkItem(_ => { for (int i = 0; i < 10000; i++) { lock (lockObj) { var countingTask = new CountingTask("task1", 100); runner.AddTask(countingTask); runner.RemoveTask(countingTask); } } resetEvent.Set(); }); // Wait for the second thread to finish 10000 attempts. Assert.That(resetEvent.WaitOne(TimeSpan.FromSeconds(10)), "The secondary lock user didn't finish 10000 iterations in less than 10 seconds. Probably dead locked!"); runner.Shutdown(); }
private void StartMonitorThreads() { lock ( _monitor ) { if (IsDisposed || _disposing) { return; } if (_monitorStarted.Value) { return; } if (_localWireFormatInfo == null) { return; } if (_remoteWireFormatInfo == null) { return; } if (_localWireFormatInfo.MaxInactivityDuration != 0 && _remoteWireFormatInfo.WriteCheckInterval != 0) { ReadCheckTime = Math.Max( _localWireFormatInfo.ReadCheckInterval, _remoteWireFormatInfo.WriteCheckInterval); _asyncErrorTask = new AsyncSignalReadErrorkTask(this, Next.RemoteAddress); } if (_localWireFormatInfo.MaxInactivityDuration != 0) { if (_remoteWireFormatInfo.Version > 1.0) { WriteCheckTime = Math.Max(_localWireFormatInfo.WriteCheckInterval, _remoteWireFormatInfo.ReadCheckInterval); } else { WriteCheckTime = _localWireFormatInfo.WriteCheckInterval; } _asyncWriteTask = new AsyncWriteTask(this); } InitialDelayTime = _localWireFormatInfo.MaxInactivityDurationInitialDelay > 0 ? _localWireFormatInfo.MaxInactivityDurationInitialDelay : WriteCheckTime; _asyncTasks = new CompositeTaskRunner(); if (_asyncErrorTask != null) { _asyncTasks.AddTask(_asyncErrorTask); } if (_asyncWriteTask != null) { Tracer.WarnFormat("InactivityMonitor[{0}]: Write Check time interval: {1}", _instanceId, WriteCheckTime); _asyncTasks.AddTask(_asyncWriteTask); } if (_asyncErrorTask == null && _asyncWriteTask == null) { return; } Tracer.WarnFormat("InactivityMonitor[{0}]: Starting the Monitor Timer.", _instanceId); _monitorStarted.Value = true; _connectionCheckTimer = new Timer( CheckConnection, null, InitialDelayTime, WriteCheckTime ); } }