public void StopResponsiveService() { var test = new TestDef { MaxTime = TimeSpan.FromMinutes(1) }; var terminated = TimeSpan.Zero; test.AddScript("com:test", async env => { try { while (!env.Token.IsCancellationRequested) { await env.SimulateWork(10.Sec(), env.Token); } } finally { terminated = env.Time; } }); test.Run(async plan => { plan.StartServices(); await plan.Delay(TimeSpan.FromSeconds(1)); await plan.StopServices(); }); Assert.AreEqual(TimeSpan.FromSeconds(1), terminated); }
public void RebootTest() { var bootCount = 0; var test = new TestDef() { MaxTime = 2.Minutes(), MaxInactive = 2.Minutes() }; test.AddScript("com:test", async env => { bootCount++; while (!env.Token.IsCancellationRequested) { await env.SimulateWork(100.Ms()); } }); test.Run(async plan => { plan.StartServices(); await plan.Delay(TimeSpan.FromMinutes(1)); await plan.StopServices(); plan.StartServices(); }); Assert.AreEqual(2, bootCount); }
public void CancellationAbortsPromise() { var test = new TestDef(); var cancel = TimeSpan.MinValue; var result = false; test.AddScript("m:m", async env => { var promise = new SimFuture <bool>(5000, env.Token); try { result = await promise.Task; } catch (TaskCanceledException) { cancel = env.Time; } }); test.Run(async plan => { plan.StartServices(); await plan.Delay(1000.Sec()); await plan.StopServices(grace: 1.Sec()); }); Assert.IsFalse(result); Assert.AreEqual(TimeSpan.FromMilliseconds(1000), cancel, nameof(cancel)); }
public void NonResponsiveServiceIsKilledWithoutMercy() { var test = new TestDef { MaxTime = TimeSpan.FromMinutes(1) }; var launched = true; var terminated = false; test.AddScript("com:test", async env => { launched = true; try { while (!env.Token.IsCancellationRequested) { await env.SimulateWork(10000.Ms()); } } finally { // this should never hit terminated = true; } }); test.Run(async plan => { plan.StartServices(); await plan.Delay(1.Sec()); await plan.StopServices(grace: 1.Sec()); }); Assert.IsTrue(launched, nameof(launched)); Assert.IsFalse(terminated, nameof(terminated)); }
public void SlowDisposeIsKilled() { var test = new TestDef(); bool disposeStart = false; bool disposeComplete = false; test.AddService("run", e => new TestEngine( async() => { try { await e.SimulateWork(Timeout.InfiniteTimeSpan, e.Token); } catch (TaskCanceledException) { } e.Debug("Shutting down"); }, async() => { e.Debug("Disposing"); disposeStart = true; await e.SimulateWork(10.Sec()); disposeComplete = true; } )); test.Run(async e => { e.StartServices(); await e.Delay(5.Sec()); e.Debug(LogType.Info, "Start shutting down"); await e.StopServices(grace: 2.Sec()); }); Assert.IsTrue(disposeStart, nameof(disposeStart)); Assert.IsFalse(disposeComplete, nameof(disposeComplete)); }
static TestDef NewTestRuntime() { var run = new TestDef() { MaxTime = 2.Minutes(), //DebugNetwork = true }; return(run); }
static void AddHelloWorldClient(TestDef run, string endpoint, List <object> responses) { run.AddScript("localhost:console", async env => { try { using (var conn = await env.Connect(endpoint, 80)) { await conn.Write("Hello"); var response = await conn.Read(5.Sec()); responses.Add(response); } } catch (IOException ex) { env.Debug(ex.Message); responses.Add(ex); } }); }
public void DisposeIsFiredOnNormalTermination() { var test = new TestDef(); bool run = false; bool disposed = false; test.AddService("run", e => new TestEngine( async() => { run = true; }, async() => { disposed = true; } )); test.Run(); Assert.IsTrue(disposed, nameof(disposed)); Assert.IsTrue(run, nameof(run)); }
public void DisposeIsFiredOnAbnormalTermination() { var runtime = new TestDef(); bool run = false; bool disposed = false; runtime.AddService("run", e => new TestEngine( async() => { run = true; throw new InvalidOperationException(); }, async() => { disposed = true; } )); runtime.Run(); Assert.IsTrue(disposed, nameof(disposed)); Assert.IsTrue(run, nameof(run)); }
static void AddHelloWorldServer(TestDef run, string endpoint, List <object> requests) { run.AddScript(endpoint + ":engine", async env => { async void Handler(IConn conn) { using (conn) { var msg = await conn.Read(5.Sec()); requests.Add(msg); await conn.Write("World"); } } using (var socket = await env.Bind(80)) { while (!env.Token.IsCancellationRequested) { Handler(await socket.Accept()); } } }); }
public void SettingResultSyncCompletesPromise() { var test = new TestDef() { MaxSteps = 100, }; var completed = TimeSpan.MinValue; bool result = false; test.RunScript(async env => { var promise = new SimFuture <bool>(5000); promise.SetResult(true); result = await promise.Task; completed = env.Time; }); Assert.IsTrue(result); Assert.AreEqual(TimeSpan.Zero, completed); }
public void SettingErrorSyncCompletesPromise() { var test = new TestDef(); var failed = TimeSpan.MinValue; var result = false; test.RunScript(async env => { var promise = new SimFuture <bool>(5000); promise.SetError(new IOException()); try { result = await promise.Task; } catch (Exception) { failed = env.Time; } }); Assert.IsFalse(result); Assert.AreEqual(TimeSpan.Zero, failed); }
public void CompletionSourceTimesOut() { var test = new TestDef() { MaxSteps = 100, }; TimeSpan timedOut; test.RunScript(async env => { var promise = new SimFuture <bool>(5000); try { await promise.Task; } catch (TimeoutException) { timedOut = env.Time; } }); Assert.AreEqual(TimeSpan.FromSeconds(5), timedOut); }
public void DisposeIsNotFiredOnKill() { var runtime = new TestDef(); bool run = false; bool disposed = false; runtime.AddService("run", e => new TestEngine( async() => { run = true; await e.Delay(10.Minutes()); }, async() => { disposed = true; } )); runtime.Run(async e => { e.StartServices(); await e.Delay(5.Sec()); await e.StopServices(grace: 2.Sec()); }); Assert.IsFalse(disposed, nameof(disposed)); Assert.IsTrue(run, nameof(run)); }
static void AddHelloWorldProxy(TestDef run, string endpoint, string target) { run.AddScript(endpoint + ":engine", async env => { async void Handler(IConn conn) { using (conn) { var msg = await conn.Read(5.Sec()); using (var outgoing = await env.Connect(target, 80)) { await outgoing.Write(msg); var response = await outgoing.Read(5.Sec()); await conn.Write(response); } } } using (var socket = await env.Bind(80)) { while (!env.Token.IsCancellationRequested) { Handler(await socket.Accept()); } } }); }