public void TestRegisterActionExceedingTimeout() { using (ManualResetEvent rendezvousCompleted = new ManualResetEvent(false)) { bool actionCalled = false; RendezvousPoint rendezvousPoint = null; string rpId = "RpId" + Guid.NewGuid( ); try { rendezvousPoint = new RendezvousPoint(rpId, 500, EventLog.Application); // Register an action that never completes RendezvousPoint.RegisterAction(rpId, () => { // ReSharper disable once AccessToDisposedClosure rendezvousCompleted.WaitOne(5000); actionCalled = true; }); } finally { Assert.IsFalse(actionCalled, "Action should not be called."); rendezvousPoint?.Dispose( ); // If we are here the rendezvous has completed but the action has not. // That's a good thing ! Assert.IsFalse(actionCalled, "Action should not be called."); rendezvousCompleted.Set( ); } } }
public void TestRegisterAction() { bool actionCalled = false; string rpId = "RpId" + Guid.NewGuid(); using (new RendezvousPoint(rpId, 1000, EventLog.Application)) { RendezvousPoint.RegisterAction(rpId, () => { actionCalled = true; }); } Assert.IsTrue(actionCalled, "The action should be called"); }
public void TestRegisterActionWithNonExistantRendezvousPoint() { bool actionCalled = false; string rpId1 = "RpId1" + Guid.NewGuid(); string rpId2 = "RpId2" + Guid.NewGuid(); using (new RendezvousPoint(rpId1, 1000, EventLog.Application)) { RendezvousPoint.RegisterAction(rpId2, () => { actionCalled = true; }); } Assert.IsFalse(actionCalled, "The action should not be called"); }
/// <summary> /// Initializes a new instance of the <see cref="SyslogQueueingMessageWriter" /> class. /// </summary> /// <param name="messageWriter">The message writer.</param> /// <param name="maxRetries">The maximum retries.</param> /// <exception cref="System.ArgumentNullException">messageSender</exception> public SyslogQueueingMessageWriter(ISyslogMessageWriter messageWriter, int maxRetries = MaxRetries) { if (messageWriter == null) { throw new ArgumentNullException(nameof(messageWriter)); } _messageWriter = messageWriter; _maxRetries = maxRetries; if (_maxRetries > MaxRetries) { _maxRetries = MaxRetries; } CancellationToken cancellationToken = _cancellationTokenSource.Token; // Start listening for messages _msgWriterTask = Task.Run(() => { try { foreach (SyslogMessage message in _messageQueue.GetConsumingEnumerable(cancellationToken)) { WriteWithRetry(message); if (cancellationToken.IsCancellationRequested) { break; } } } catch (Exception ex) { _canWriteMessages = false; Trace.TraceError("An unexpected error occurred in the syslog message writer worker thread. No more messages will be written to the syslog server. Error:{0}.", ex); } }, _cancellationTokenSource.Token); // Register an action with the application exist rendezvous point so that any messages will be flushed. RendezvousPoint.RegisterAction(WellKnownRendezvousPoints.ApplicationExit, () => Dispose(true)); }
public void TestRegisterMultipleActions() { bool actionCalled1 = false; bool actionCalled2 = false; bool actionCalled3 = false; string rpId = "RpId" + Guid.NewGuid(); using (new RendezvousPoint(rpId, 1000, EventLog.Application)) { RendezvousPoint.RegisterAction(rpId, () => { actionCalled1 = true; }); RendezvousPoint.RegisterAction(rpId, () => { actionCalled2 = true; }); RendezvousPoint.RegisterAction(rpId, () => { actionCalled3 = true; }); } Assert.IsTrue(actionCalled1, "The action1 should be called"); Assert.IsTrue(actionCalled2, "The action2 should be called"); Assert.IsTrue(actionCalled3, "The action3 should be called"); }
public void TestRegisterNullId() { Assert.Throws <ArgumentNullException>(() => RendezvousPoint.RegisterAction(null, () => { })); }
public void TestRegisterNullAction() { Assert.Throws <ArgumentNullException>(() => RendezvousPoint.RegisterAction("id", null)); }