private async Task Start() { if (this.HasId) { using (var cts = new CancellationTokenSource()) { var attachConsumerTask = TestcontainersClient.Instance.AttachAsync(this.Id, this.Configuration.OutputConsumer, cts.Token); var startTask = TestcontainersClient.Instance.StartAsync(this.Id, cts.Token); var waitTask = WaitStrategy.WaitUntil(() => this.Configuration.WaitStrategy.Until(this.Id), cancellationToken: cts.Token); var handleDockerExceptionTask = startTask.ContinueWith(task => { task.Exception?.Handle(exception => { cts.Cancel(); return(false); }); }); await Task.WhenAll(attachConsumerTask, startTask, waitTask, handleDockerExceptionTask); } } }
public async Task UntilAfter1ms() { await Assert.ThrowsAsync <TimeoutException>(async() => { await WaitStrategy.WaitUntil(() => this.Until(string.Empty), timeout: 1); }); }
public async Task UntilSomeRepeats(int maxCallCount, int expectedCallsCount) { // Given var callCounter = 0; // When var wait = Wait.ForUnixContainer().UntilOperationIsSucceeded(() => ++ callCounter >= expectedCallsCount, maxCallCount); await WaitStrategy.WaitUntil(() => wait.Build().Skip(1).First().Until(null, string.Empty)); // Then Assert.Equal(expectedCallsCount, callCounter); }
public async Task UntilSomeRepeats(int maxCallCount, int expectedCallsCount) { // Given var callCounter = 0; // When var wait = new WaitUntilOperationSucceeded(() => ++ callCounter >= expectedCallsCount, maxCallCount); await WaitStrategy.WaitUntil(() => wait.Until(string.Empty)); // Then Assert.Equal(expectedCallsCount, callCounter); }
public async Task UntilMaxRepeats(int maxCallCount, int expectedCallsCount) { // Given var callCounter = 0; // When await Assert.ThrowsAsync <TimeoutException>(async() => { var wait = new WaitUntilOperationSucceeded(() => { callCounter += 1; return(false); }, maxCallCount); await WaitStrategy.WaitUntil(() => wait.Until(string.Empty)); }); // Then Assert.Equal(expectedCallsCount, callCounter); }
public async Task UntilMessageIsLogged() { // Given const string expectedMessage = nameof(this.UntilMessageIsLogged); // When // Then using (var memoryStream = new MemoryStream()) { using (var streamWriter = new StreamWriter(memoryStream)) { streamWriter.Write(expectedMessage); streamWriter.Flush(); var wait = Wait.ForUnixContainer().UntilMessageIsLogged(memoryStream, expectedMessage); await WaitStrategy.WaitUntil(() => wait.Build().Skip(1).First().Until(null, string.Empty)); } } }
private async Task <ContainerListResponse> Start(string id, CancellationToken ct = default) { using (var cts = new CancellationTokenSource()) { await this.client.AttachAsync(id, this.configuration.OutputConsumer, cts.Token) .ConfigureAwait(false); var startTask = this.client.StartAsync(id, cts.Token); var waitTask = Task.Run(async() => { foreach (var waitStrategy in this.configuration.WaitStrategies) { await WaitStrategy.WaitUntil(() => waitStrategy.Until(this.configuration.Endpoint, id), 100, ct: cts.Token) .ConfigureAwait(false); } }, cts.Token); var tasks = Task.WhenAll(startTask, waitTask); try { await tasks.ConfigureAwait(false); } catch (Exception) { // Get all thrown exceptions in tasks. if (tasks.Exception != null) { TestcontainersHostService.GetLogger <TestcontainersContainer>().LogError(tasks.Exception, "Can not start container {id}", id); throw tasks.Exception; } } finally { cts.Cancel(); } } return(await this.client.GetContainer(id, ct) .ConfigureAwait(false)); }
public async Task UntilMaxRepeats(int maxCallCount, int expectedCallsCount) { // Given var callCounter = 0; // When await Assert.ThrowsAsync <TimeoutException>(async() => { var wait = Wait.ForUnixContainer().UntilOperationIsSucceeded(() => { callCounter += 1; return(false); }, maxCallCount); await WaitStrategy.WaitUntil(() => wait.Build().Skip(1).First().Until(null, string.Empty)); }); // Then Assert.Equal(expectedCallsCount, callCounter); }
private async Task <ContainerInspectResponse> Start(string id, CancellationToken ct = default) { await this.client.AttachAsync(id, this.configuration.OutputConsumer, ct) .ConfigureAwait(false); await this.client.StartAsync(id, ct) .ConfigureAwait(false); this.container = await this.client.InspectContainer(id, ct) .ConfigureAwait(false); await this.configuration.StartupCallback(this, ct) .ConfigureAwait(false); // Do not use a too small frequency. Especially with a lot of containers, // we send many operations to the Docker endpoint. The endpoint may cancel operations. var frequency = (int)TimeSpan.FromSeconds(1).TotalMilliseconds; const int timeout = -1; foreach (var waitStrategy in this.configuration.WaitStrategies) { await WaitStrategy.WaitUntil( async() => { this.container = await this.client.InspectContainer(id, ct) .ConfigureAwait(false); return(await waitStrategy.Until(this, this.Logger) .ConfigureAwait(false)); }, frequency, timeout, ct) .ConfigureAwait(false); } return(this.container); }
private async Task StartServices(CancellationToken ct) { if (ct.IsCancellationRequested) { return; } try { await WaitStrategy.WaitUntil(this); _logger.LogInformation("Container {} started!", DockerImageName); } catch (Exception e) { _logger.LogError(e, "Unable to start container services: {}", DockerImageName); await PrintContainerLogs(ct); throw; } }
private async Task <ContainerListResponse> Start(string id, CancellationToken ct = default) { using (var cts = new CancellationTokenSource()) { var attachOutputConsumerTask = this.client.AttachAsync(id, this.configuration.OutputConsumer, cts.Token); var startTask = this.client.StartAsync(id, cts.Token); var waitTask = Task.Run(async() => { foreach (var waitStrategy in this.configuration.WaitStrategies) { await WaitStrategy.WaitUntil(() => waitStrategy.Until(this.configuration.Endpoint, id), ct: cts.Token); } }, cts.Token); var tasks = Task.WhenAll(attachOutputConsumerTask, startTask, waitTask); try { await tasks; } catch (Exception) { if (tasks.Exception != null) { throw tasks.Exception; } } finally { cts.Cancel(); } } return(await this.client.GetContainer(id, ct)); }
private Task Start() { return(this.id.IfSomeAsync(id => { var cts = new CancellationTokenSource(); var attachConsumerTask = TestcontainersClient.Instance.AttachAsync(id, this.Configuration.OutputConsumer, cts.Token); var startTask = TestcontainersClient.Instance.StartAsync(id, cts.Token); var waitTask = WaitStrategy.WaitUntil(() => this.Configuration.WaitStrategy.Until(id), cancellationToken: cts.Token); var handleDockerExceptionTask = startTask.ContinueWith((task) => { task.Exception?.Handle(exception => { cts.Cancel(); return false; }); }); return Task.WhenAll(attachConsumerTask, startTask, waitTask, handleDockerExceptionTask); })); }
public async Task UntilMessageIsLogged() { // Given const string expectedMessage = "Message has been logged."; await using var memoryStream = new MemoryStream(); await using var streamWriter = new StreamWriter(memoryStream); // When await streamWriter.WriteAsync(expectedMessage); await streamWriter.FlushAsync(); var wait = Wait.ForUnixContainer() .UntilMessageIsLogged(memoryStream, expectedMessage) .Build() .Skip(1) .First(); // Then var exception = await Record.ExceptionAsync(() => WaitStrategy.WaitUntil(() => wait.Until(null, null))); Assert.Null(exception); }
public async Task RethrowUntil() { await Assert.ThrowsAsync <NotImplementedException>(() => WaitStrategy.WaitUntil(() => this.Until(string.Empty))); }
public async Task UntilImmediately() { await WaitStrategy.WaitUntil(() => this.Until(string.Empty)); }
public async Task UntilAfter1Ms() { await Assert.ThrowsAsync <TimeoutException>(() => WaitStrategy.WaitUntil(() => this.Until(null, string.Empty), timeout: 1)); }
public async Task UntilAfter1Ms() { await Assert.ThrowsAsync <TimeoutException>(() => WaitStrategy.WaitUntil(() => this.Until(null, null), 1000, 1)); }
public async Task UntilImmediately() { var exception = await Record.ExceptionAsync(() => WaitStrategy.WaitUntil(() => this.Until(null, null))); Assert.Null(exception); }