public async Task WhenTailing_ThenCancelStopsTask() { var stream = new Mock <IAsyncReader <string> >(); stream.Setup(s => s.ReadAsync( It.IsAny <CancellationToken>())) .ReturnsAsync(""); var adapter = new Mock <IComputeEngineAdapter>(); adapter.Setup(a => a.GetSerialPortOutput( It.IsAny <InstanceLocator>(), 1)).Returns(stream.Object); // Let it load successfully... var model = await SerialOutputModel.LoadAsync( "display-name", adapter.Object, new InstanceLocator("project-1", "zone-1", "instance-1"), ConsolePort, CancellationToken.None); using (var cts = new CancellationTokenSource()) { var tailTask = model.TailAsync( _ => { }, cts.Token); cts.Cancel(); // Now the task should finish quickly. await tailTask; } }
public async Task WhenLoadAsyncCompletes_ThenOutputContainsExistingData( [WindowsInstance] InstanceRequest testInstance) { await testInstance.AwaitReady(); var model = await SerialOutputModel.LoadAsync( new ComputeEngineAdapter(Defaults.GetCredential()), await testInstance.GetInstanceAsync(), SerialPortStream.ConsolePort, CancellationToken.None); Assert.IsFalse(string.IsNullOrWhiteSpace(model.Output)); StringAssert.Contains("Instance setup finished", model.Output); }
public async Task WhenLoadAsyncCompletes_ThenOutputContainsExistingData( [WindowsInstance] ResourceTask <InstanceLocator> testInstance, [Credential(Role = PredefinedRole.ComputeViewer)] ResourceTask <ICredential> credential) { await testInstance; var model = await SerialOutputModel.LoadAsync( "display-name", new ComputeEngineAdapter(await credential), await testInstance, ConsolePort, CancellationToken.None); Assert.IsFalse(string.IsNullOrWhiteSpace(model.Output)); Assert.AreEqual("display-name", model.DisplayName); StringAssert.Contains("Finished running startup scripts", model.Output); }
public async Task WhenLoadAsyncCompletes_ThenOutputContainsExistingData( [WindowsInstance] InstanceRequest testInstance, [Credential(Role = PredefinedRole.ComputeViewer)] CredentialRequest credential) { await testInstance.AwaitReady(); var model = await SerialOutputModel.LoadAsync( "display-name", new ComputeEngineAdapter(await credential.GetCredentialAsync()), await testInstance.GetInstanceAsync(), SerialPortStream.ConsolePort, CancellationToken.None); Assert.IsFalse(string.IsNullOrWhiteSpace(model.Output)); Assert.AreEqual("display-name", model.DisplayName); StringAssert.Contains("Instance setup finished", model.Output); }
public async Task WhenApiThrowsException_ThenMessageIsTailedToOutput() { var stream = new Mock <IAsyncReader <string> >(); stream.Setup(s => s.ReadAsync( It.IsAny <CancellationToken>())) .ReturnsAsync(""); var adapter = new Mock <IComputeEngineAdapter>(); adapter.Setup(a => a.GetSerialPortOutput( It.IsAny <InstanceLocator>(), 1)).Returns(stream.Object); // Let it load successfully... var model = await SerialOutputModel.LoadAsync( "display-name", adapter.Object, new InstanceLocator("project-1", "zone-1", "instance-1"), ConsolePort, CancellationToken.None); // ...but fail the tailing. stream.Setup(s => s.ReadAsync( It.IsAny <CancellationToken>())) .Returns(Task.FromException <string>( new TokenResponseException(new TokenErrorResponse()))); using (var cts = new CancellationTokenSource()) { var newOutput = new StringBuilder(); var tailTask = model.TailAsync( s => newOutput.Append(s), cts.Token); // The exception should cause the task should finish. await tailTask; StringAssert.Contains("session timed out", newOutput.ToString()); StringAssert.DoesNotContain("session timed out", model.Output); } }