public static async Task BaseModuleTest <T1, T2>( string name1, string name2, Func <ExceptionsBag, T1, T2, CancellationToken, Task> testFunc) where T1 : class, IModule where T2 : class, IModule { using var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(15)); var cancellationToken = cancellationTokenSource.Token; var exceptions = new ExceptionsBag(); await using var manager = new ModuleManager <IModule>( Path.Combine(Path.GetTempPath(), $"H.Containers.Tests_{name1}_{name2}")); manager.ExceptionOccurred += (_, exception) => { Console.WriteLine($"ExceptionOccurred: {exception}"); exceptions.OnOccurred(exception); // ReSharper disable once AccessToDisposedClosure cancellationTokenSource.Cancel(); }; using var instance1 = await manager.AddModuleAsync <T1>( CreateContainer (name1), name1, name1, ResourcesUtilities.ReadFileAsBytes($"{name1}.zip"), cancellationToken); using var instance2 = await manager.AddModuleAsync <T2>( CreateContainer (name2), name2, name2, ResourcesUtilities.ReadFileAsBytes($"{name2}.zip"), cancellationToken); Assert.IsNotNull(instance1); Assert.IsNotNull(instance2); foreach (var instance in new IModule[] { instance1, instance2 }) { instance.EnableLog(); } await testFunc(exceptions, instance1, instance2, cancellationToken); try { await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken); } catch (OperationCanceledException) { } exceptions.EnsureNoExceptions(); }
public static async Task BaseModuleTest <T>( string name, Func <T, CancellationToken, Task> testFunc) where T : class, IModule { using var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(15)); var cancellationToken = cancellationTokenSource.Token; var receivedException = (Exception?)null; await using var manager = new ModuleManager <IModule>( Path.Combine(Path.GetTempPath(), $"H.Containers.Tests_{name}")); manager.ExceptionOccurred += (_, exception) => { Console.WriteLine($"ExceptionOccurred: {exception}"); receivedException = exception; // ReSharper disable once AccessToDisposedClosure cancellationTokenSource.Cancel(); }; var bytes = ResourcesUtilities.ReadFileAsBytes($"{name}.zip"); using var instance = await manager.AddModuleAsync <T>( new ProcessContainer (name), name, name, bytes, cancellationToken); Assert.IsNotNull(instance); instance.EnableLog(); (await manager.GetTypesAsync(cancellationToken)).Log("Available types"); instance.ShortName.Log(nameof(instance.ShortName)); instance.GetAvailableSettings().Log("Available settings"); await testFunc(instance, cancellationToken); try { await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken); } catch (OperationCanceledException) { } if (receivedException != null) { Assert.Fail(receivedException.ToString()); } }