Skip to content

Commit

Permalink
Retry redis docker (#3265)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennanConroy committed Nov 6, 2018
1 parent 7ae4cb0 commit 56f78b4
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions test/Microsoft.AspNetCore.SignalR.Redis.Tests/Docker.cs
Expand Up @@ -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;

Expand Down Expand Up @@ -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");
Expand All @@ -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
Expand Down

0 comments on commit 56f78b4

Please sign in to comment.