protected void Run(Action <IActorRuntime> test, Configuration configuration = null) { configuration ??= this.GetConfiguration(); ILogger logger = this.GetLogger(configuration); try { configuration.IsMonitoringEnabledInInProduction = true; var runtime = ActorRuntimeFactory.Create(configuration); runtime.Logger = logger; for (int i = 0; i < configuration.TestingIterations; i++) { test(runtime); } } catch (Exception ex) { Assert.False(true, ex.Message + "\n" + ex.StackTrace); } finally { logger.Dispose(); } }
protected async Task RunWithExceptionAsync <TException>(Func <Task> test, Configuration configuration = null) { configuration ??= this.GetConfiguration(); Exception actualException = null; Type exceptionType = typeof(TException); Assert.True(exceptionType.IsSubclassOf(typeof(Exception)), "Please configure the test correctly. " + $"Type '{exceptionType}' is not an exception type."); ILogger logger = this.GetLogger(configuration); try { configuration.IsMonitoringEnabledInInProduction = true; var runtime = ActorRuntimeFactory.Create(configuration); var errorCompletion = new TaskCompletionSource <Exception>(); runtime.OnFailure += (e) => { errorCompletion.TrySetResult(e); }; runtime.Logger = logger; for (int i = 0; i < configuration.TestingIterations; i++) { await test(); if (configuration.TestingIterations is 1) { Assert.True(errorCompletion.Task.Wait(GetExceptionTimeout()), "Timeout waiting for error"); actualException = errorCompletion.Task.Result; } } } catch (Exception ex) { actualException = ex; } finally { logger.Dispose(); } if (actualException is null) { Assert.True(false, string.Format("Error not found after all {0} test iterations", configuration.TestingIterations)); } Assert.True(actualException.GetType() == exceptionType, actualException.Message + "\n" + actualException.StackTrace); }
private async Task RunWithErrorsAsync(Func <IActorRuntime, Task> test, Configuration configuration, TestErrorChecker errorChecker) { configuration ??= this.GetConfiguration(); string errorMessage = string.Empty; ILogger logger = this.GetLogger(configuration); try { configuration.IsMonitoringEnabledInInProduction = true; var runtime = ActorRuntimeFactory.Create(configuration); var errorCompletion = new TaskCompletionSource <Exception>(); runtime.OnFailure += (e) => { errorCompletion.TrySetResult(e); }; runtime.Logger = logger; for (int i = 0; i < configuration.TestingIterations; i++) { await test(runtime); if (configuration.TestingIterations is 1) { Assert.True(errorCompletion.Task.Wait(GetExceptionTimeout()), "Timeout waiting for error"); errorMessage = ExtractErrorMessage(errorCompletion.Task.Result); } } } catch (Exception ex) { errorMessage = ExtractErrorMessage(ex); } finally { logger.Dispose(); } if (string.IsNullOrEmpty(errorMessage)) { Assert.True(false, string.Format("Error not found after all {0} test iterations", configuration.TestingIterations)); } errorChecker(errorMessage); }
protected async Task RunAsync(Func <IActorRuntime, Task> test, Configuration configuration = null, bool handleFailures = true) { configuration ??= this.GetConfiguration(); uint iterations = Math.Max(1, configuration.TestingIterations); for (int i = 0; i < iterations; i++) { ILogger logger = this.GetLogger(configuration); try { configuration.IsMonitoringEnabledInInProduction = true; var runtime = ActorRuntimeFactory.Create(configuration); runtime.Logger = logger; var errorTask = new TaskCompletionSource <Exception>(); if (handleFailures) { runtime.OnFailure += (e) => { errorTask.TrySetResult(Unwrap(e)); }; } // BUGBUG: but is this actually letting the test complete in the case // of actors which run completely asynchronously? await Task.WhenAny(test(runtime), errorTask.Task); if (handleFailures && errorTask.Task.IsCompleted) { Assert.False(true, errorTask.Task.Result.Message); } } catch (Exception ex) { Exception e = Unwrap(ex); Assert.False(true, e.Message + "\n" + e.StackTrace); } finally { logger.Dispose(); } } }