public void Main_ExecutesAction() { var executed = false; AnankeRunner.Main(new StubbedSettings(), _ => { executed = true; }); Assert.That(executed, Is.EqualTo(true)); }
public void CustomFormatter_GetsStructuredState() { var actualState = new List <KeyValuePair <string, object> >(); string TestFormatter(string loggerName, LogLevel logLevel, EventId eventId, string message, Exception exception, IEnumerable <KeyValuePair <string, object> > state, IEnumerable <IEnumerable <KeyValuePair <string, object> > > scope, IEnumerable <string> scopeMessages) { actualState.AddRange(state); return(message); } var settings = new StubbedSettings { Formatter = TestFormatter, }; var timestamp = new DateTime(2018, 06, 01, 0, 0, 0, DateTimeKind.Utc); AnankeRunner.Main(settings, context => context.LoggerFactory.CreateLogger("testlogger") .LogInformation("Hello from {source} at {timestamp:o}!", "sourceValue", timestamp)); Assert.That(settings.StubStringLog.Messages, Has.Some.Contains("Hello from sourceValue at 2018-06-01T00:00:00.0000000Z!")); Assert.That(actualState, Has.Some.EqualTo(new KeyValuePair <string, object>("source", "sourceValue"))); Assert.That(actualState, Has.Some.EqualTo(new KeyValuePair <string, object>("timestamp", timestamp))); }
public void Action_ThrowsException_IsLogged() { var settings = new StubbedSettings(); AnankeRunner.Main(settings, (Action <AnankeContext>)(_ => throw new InvalidOperationException("Test message"))); Assert.That(settings.StubStringLog.Messages, Has.Some.Contains("Test message")); }
public void ExceptionLogging_EscapesEolCharacters() { var settings = new StubbedSettings(); AnankeRunner.Main(settings, (Action <AnankeContext>)(_ => throw new InvalidOperationException("test message"))); Assert.That(settings.StubStringLog.Messages, Has.Some.Contains("test message")); Assert.That(settings.StubStringLog.Messages, Has.None.Contains("\n")); }
public void Action_ThrowsException_IsTranslatedIntoExitCode64() { var settings = new StubbedSettings(); AnankeRunner.Main(settings, (Action <AnankeContext>)(_ => throw new InvalidOperationException())); Assert.That(settings.StubExitProcessService.ExitCode, Is.Not.Null); Assert.That(settings.StubExitProcessService.ExitCode, Is.EqualTo(64)); }
public void ExitCode_WhenActionHasNoReturnValue_IsZero() { var settings = new StubbedSettings(); AnankeRunner.Main(settings, _ => { }); Assert.That(settings.StubExitProcessService.ExitCode, Is.Not.Null); Assert.That(settings.StubExitProcessService.ExitCode, Is.EqualTo(0)); }
public void ExitCode_WhenActionSpecifiesReturnValue_IsActionReturnValue() { var settings = new StubbedSettings(); AnankeRunner.Main(settings, _ => 13); Assert.That(settings.StubExitProcessService.ExitCode, Is.Not.Null); Assert.That(settings.StubExitProcessService.ExitCode, Is.EqualTo(13)); }
static void Main() => AnankeRunner.Main(Settings, async context => { // Normally this is injected by your DI container; this example just creates it directly. var logger = context.LoggerFactory.CreateLogger <Program>(); while (true) { await Task.Delay(TimeSpan.FromSeconds(1), context.ExitRequested); logger.LogInformation("Hello World!"); } });
public void CustomLogger_IsUsedForLoggingConsoleOutput() { var settings = new StubbedSettings { StubLoggerProvider = new StubLoggerProvider(), }; AnankeRunner.Main(settings, context => context.LoggingConsoleStdout.WriteLine("Hello there")); Assert.That(settings.StubStringLog.Messages, Is.Empty); Assert.That(settings.StubLoggerProvider.Messages, Has.Some.Matches(Has.Property("Message").EqualTo("Hello there"))); }
public void CustomLogger_IsUsedForAnankeLogging() { var settings = new StubbedSettings { StubLoggerProvider = new StubLoggerProvider(), }; var exception = new InvalidOperationException("test message"); AnankeRunner.Main(settings, (Action <AnankeContext>)(context => throw exception)); Assert.That(settings.StubStringLog.Messages, Is.Empty); Assert.That(settings.StubLoggerProvider.Messages, Has.Some.Matches(Has.Property("CategoryName").EqualTo("Ananke") & Has.Property("Exception").EqualTo(exception))); }
public async Task ExitRequestedCancellationException_WhenExitRequested_IsTreatedAsNormalException() { var settings = new StubbedSettings(); var ready = new ManualResetEventSlim(); var task = Task.Run(() => AnankeRunner.Main(settings, async context => { ready.Set(); await Task.Delay(Timeout.InfiniteTimeSpan, context.ExitRequestedToken); })); ready.Wait(); var _ = Task.Run(() => settings.StubSignalService.Invoke("testSignal")); await task; Assert.That(settings.StubExitProcessService.ExitCode, Is.Zero); }
public void CustomLogger_IsUsedForApplicationLogging() { var settings = new StubbedSettings { StubLoggerProvider = new StubLoggerProvider(), }; var timestamp = new DateTime(2018, 06, 01, 0, 0, 0, DateTimeKind.Utc); AnankeRunner.Main(settings, context => context.LoggerFactory.CreateLogger("testlogger") .LogInformation("Hello from {source} at {timestamp:o}!", "sourceValue", timestamp)); Assert.That(settings.StubStringLog.Messages, Is.Empty); Assert.That(settings.StubLoggerProvider.Messages, Has.Some.Matches( Has.Property("CategoryName").EqualTo("testlogger") & Has.Property("Message").EqualTo("Hello from sourceValue at 2018-06-01T00:00:00.0000000Z!") & Has.Property("State").Some.EqualTo(new KeyValuePair <string, object>("source", "sourceValue")) & Has.Property("State").Some.EqualTo(new KeyValuePair <string, object>("timestamp", timestamp)))); }
public void ApplicationCode_ExceedsMaximumRuntime_ShutdownIsRequested() { var settings = new StubbedSettings { StubExitTimeout = Timeout.InfiniteTimeSpan, StubMaximumRuntime = TimeSpan.FromSeconds(1), }; CancellationToken exitRequested; AnankeRunner.Main(settings, async context => { exitRequested = context.ExitRequested; await Task.Delay(settings.StubMaximumRuntime * 2, context.ExitRequested); }); Assert.That(exitRequested.IsCancellationRequested, Is.True); Assert.That(settings.StubExitProcessService.ExitCode, Is.Zero); }
public void Signal_WhenApplicationCodeTakesTooLong_ExitsProcessWithCode66() { var settings = new StubbedSettings(); var ready = new ManualResetEventSlim(); var finish = new ManualResetEventSlim(); Task.Run(() => AnankeRunner.Main(settings, context => { ready.Set(); finish.Wait(); })); ready.Wait(); Task.Run(() => settings.StubSignalService.Invoke("testSignal")); Thread.Sleep(settings.StubExitTimeout * 2); Assert.That(settings.StubExitProcessService.ExitCode, Is.EqualTo(66)); finish.Set(); }
public async Task Signal_BlocksHandlerUntilAppExits() { var settings = new StubbedSettings(); var ready = new ManualResetEvent(false); var finish = new ManualResetEvent(false); var mainTask = Task.Run(() => AnankeRunner.Main(settings, context => { ready.Set(); finish.WaitOne(); })); ready.WaitOne(); // The signal handler should block until mainTask completes. var signalTask = Task.Run(() => settings.StubSignalService.Invoke("testSignal")); var signalTaskCompleted = signalTask.Wait(TimeSpan.FromMilliseconds(200)); Assert.That(signalTaskCompleted, Is.False); finish.Set(); await mainTask; await signalTask; }
public void Signal_CancelsExitRequested() { var settings = new StubbedSettings(); var ready = new ManualResetEvent(false); var finish = new ManualResetEvent(false); var cancellationTokenSourceSet = new ManualResetEvent(false); CancellationToken exitRequested; Task.Run(() => AnankeRunner.Main(settings, context => { exitRequested = context.ExitRequestedToken; ready.Set(); finish.WaitOne(); })); ready.WaitOne(); exitRequested.Register(() => cancellationTokenSourceSet.Set()); Task.Run(() => settings.StubSignalService.Invoke("testSignal")); cancellationTokenSourceSet.WaitOne(); Assert.That(exitRequested.IsCancellationRequested, Is.True); finish.Set(); }