From 56f78b45931353f6eb733c5197384c36f4312372 Mon Sep 17 00:00:00 2001 From: BrennanConroy Date: Tue, 6 Nov 2018 15:23:03 -0800 Subject: [PATCH] Retry redis docker (#3265) --- .../Docker.cs | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/test/Microsoft.AspNetCore.SignalR.Redis.Tests/Docker.cs b/test/Microsoft.AspNetCore.SignalR.Redis.Tests/Docker.cs index 10f087ac6a2c..ff38d1b5de9d 100644 --- a/test/Microsoft.AspNetCore.SignalR.Redis.Tests/Docker.cs +++ b/test/Microsoft.AspNetCore.SignalR.Redis.Tests/Docker.cs @@ -6,6 +6,7 @@ using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; +using System.Threading; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; @@ -70,6 +71,29 @@ private static string GetDockerLocation() return null; } + private void StartRedis(ILogger logger) + { + try + { + Run(); + } + catch (Exception ex) + { + logger.LogError(ex, "Error starting redis docker container, retrying."); + Thread.Sleep(1000); + Run(); + } + + void Run() + { + // create and run docker container, remove automatically when stopped, map 6379 from the container to 6379 localhost + // use static name 'redisTestContainer' so if the container doesn't get removed we don't keep adding more + // use redis base docker image + // 30 second timeout to allow redis image to be downloaded, should be a rare occurrence, only happening when a new version is released + RunProcessAndThrowIfFailed(_path, $"run --rm -p 6380:6379 --name {_dockerContainerName} -d redis", "redis", logger, TimeSpan.FromSeconds(30)); + } + } + public void Start(ILogger logger) { logger.LogInformation("Starting docker container"); @@ -78,11 +102,7 @@ public void Start(ILogger logger) RunProcessAndWait(_path, $"stop {_dockerMonitorContainerName}", "docker stop", logger, TimeSpan.FromSeconds(15), out var _); RunProcessAndWait(_path, $"stop {_dockerContainerName}", "docker stop", logger, TimeSpan.FromSeconds(15), out var output); - // create and run docker container, remove automatically when stopped, map 6379 from the container to 6379 localhost - // use static name 'redisTestContainer' so if the container doesn't get removed we don't keep adding more - // use redis base docker image - // 20 second timeout to allow redis image to be downloaded, should be a rare occurrence, only happening when a new version is released - RunProcessAndThrowIfFailed(_path, $"run --rm -p 6380:6379 --name {_dockerContainerName} -d redis", "redis", logger, TimeSpan.FromSeconds(20)); + StartRedis(logger); // inspect the redis docker image and extract the IPAddress. Necessary when running tests from inside a docker container, spinning up a new docker container for redis // outside the current container requires linking the networks (difficult to automate) or using the IP:Port combo