Skip to content

Commit

Permalink
Merge pull request #67 from mauroservienti/move-docker-compose-to-eac…
Browse files Browse the repository at this point in the history
…h-test-execution

Make so that each test is isolated with its own dedicated docker-compose
  • Loading branch information
mauroservienti authored Jun 4, 2020
2 parents 2cb4267 + 836b78a commit 3d52686
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/MyService/ASaga.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ASaga : Saga<ASagaData>,
{
public Task Handle(StartASaga message, IMessageHandlerContext context)
{
return RequestTimeout<MyTimeout>(context, DateTime.Now.AddDays(10));
return RequestTimeout<MyTimeout>(context, DateTime.UtcNow.AddDays(10));
}

public Task Handle(CompleteASaga message, IMessageHandlerContext context)
Expand Down
39 changes: 39 additions & 0 deletions src/MySystem.AcceptanceTests/DockerCompose.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using static SimpleExec.Command;

namespace MySystem.AcceptanceTests
{
public static class DockerCompose
{
public static async Task Up()
{
Run("docker-compose", "up -d", workingDirectory: AppDomain.CurrentDomain.BaseDirectory);
Run("docker", "ps -a");

static async Task<bool> statusChecker()
{
try
{
using var client = new HttpClient();
var response = await client.GetAsync("http://localhost:15672/");
return response.IsSuccessStatusCode;
}
catch
{
return false;
}
}
while (!await statusChecker())
{
await Task.Delay(500);
}
}

public static void Down()
{
Run("docker-compose", "down", workingDirectory: AppDomain.CurrentDomain.BaseDirectory);
}
}
}
7 changes: 7 additions & 0 deletions src/MySystem.AcceptanceTests/MySystem.AcceptanceTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="NServiceBus.AcceptanceTesting" Version="7.3.0" />
<PackageReference Include="SimpleExec" Version="6.2.0" />
</ItemGroup>

<ItemGroup>
Expand All @@ -20,4 +21,10 @@
<ProjectReference Include="..\NServiceBus.IntegrationTesting\NServiceBus.IntegrationTesting.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="docker-compose.yml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
15 changes: 13 additions & 2 deletions src/MySystem.AcceptanceTests/When_requesting_a_timeout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,32 @@
using NServiceBus.IntegrationTesting;
using NUnit.Framework;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace MySystem.AcceptanceTests
{
public class When_requesting_a_timeout
{
[OneTimeSetUp]
public async Task Setup()
{
await DockerCompose.Up();
}

[OneTimeTearDown]
public void Teardown()
{
DockerCompose.Down();
}

[Test]
public async Task It_should_be_rescheduled_and_handled()
{
var context = await Scenario.Define<IntegrationScenarioContext>(ctx =>
{
ctx.RegisterTimeoutRescheduleRule<ASaga.MyTimeout>(currentDelay =>
{
return new DoNotDeliverBefore(DateTime.Now.AddSeconds(5));
return new DoNotDeliverBefore(DateTime.UtcNow.AddSeconds(5));
});
})
.WithEndpoint<MyServiceEndpoint>(g => g.When(session => session.Send("MyService", new StartASaga() { SomeId = Guid.NewGuid() })))
Expand Down
12 changes: 12 additions & 0 deletions src/MySystem.AcceptanceTests/When_sending_AMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ namespace MySystem.AcceptanceTests
{
public class When_sending_AMessage
{
[OneTimeSetUp]
public async Task Setup()
{
await DockerCompose.Up();
}

[OneTimeTearDown]
public void Teardown()
{
DockerCompose.Down();
}

[Test]
public async Task AReplyMessage_is_received_and_ASaga_is_started()
{
Expand Down
12 changes: 12 additions & 0 deletions src/MySystem.AcceptanceTests/When_sending_CompleteASaga.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ namespace MySystem.AcceptanceTests
{
public class When_sending_CompleteASaga
{
[OneTimeSetUp]
public async Task Setup()
{
await DockerCompose.Up();
}

[OneTimeTearDown]
public void Teardown()
{
DockerCompose.Down();
}

[Test]
public async Task ASaga_is_completed()
{
Expand Down
22 changes: 1 addition & 21 deletions targets/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,7 @@ public static void Main(string[] args)

Target("test", DependsOn("build"),
Directory.EnumerateFiles(sourceDir, "*Tests.csproj", SearchOption.AllDirectories),
proj =>
{
static bool statusChecker()
{
try
{
using var client = new HttpClient();
var response = client.GetAsync("http://localhost:15672/").GetAwaiter().GetResult();
return response.IsSuccessStatusCode;
}
catch
{
return false;
}
}
using (new DockerCompose(proj, statusChecker))
{
Run(sdk.GetDotnetCliPath(), $"test \"{proj}\" --configuration Release --no-build");
}
});
proj => Run(sdk.GetDotnetCliPath(), $"test \"{proj}\" --configuration Release --no-build"));

RunTargetsAndExit(args);
}
Expand Down

0 comments on commit 3d52686

Please sign in to comment.