Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make integration test fully async #426

Merged
merged 17 commits into from
Nov 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions JustSaying.IntegrationTests/AwsTools/SqsQueueIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ namespace JustSaying.IntegrationTests.AwsTools
{
public abstract class WhenCreatingQueuesByName : XAsyncBehaviourTest<SqsQueueByName>
{
protected override void Given()
{
}
protected override Task Given() => Task.CompletedTask;

protected override SqsQueueByName CreateSystemUnderTest()
protected override async Task<SqsQueueByName> CreateSystemUnderTestAsync()
{
var fixture = new JustSayingFixture();

Expand All @@ -23,15 +21,14 @@ protected override SqsQueueByName CreateSystemUnderTest()
fixture.LoggerFactory);

// Force queue creation
queue.ExistsAsync().ResultSync();
await queue.ExistsAsync();

return queue;
}

protected override void PostAssertTeardown()
protected override async Task PostAssertTeardownAsync()
{
SystemUnderTest.DeleteAsync().ResultSync();
base.PostAssertTeardown();
await SystemUnderTest.DeleteAsync();
}
}
}
17 changes: 7 additions & 10 deletions JustSaying.IntegrationTests/AwsTools/WhenCreatingErrorQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ namespace JustSaying.IntegrationTests.AwsTools
[Collection(GlobalSetup.CollectionName)]
public class WhenCreatingErrorQueue : XAsyncBehaviourTest<ErrorQueue>
{
protected override void Given()
{
}
protected override Task Given() => Task.CompletedTask;

protected override async Task When()
{
Expand All @@ -26,26 +24,25 @@ protected override async Task When()

await SystemUnderTest.CreateAsync(queueConfig);

queueConfig.ErrorQueueRetentionPeriod = TimeSpan.FromSeconds(100);
queueConfig.ErrorQueueRetentionPeriod = TimeSpan.FromSeconds(100);

await SystemUnderTest.UpdateQueueAttributeAsync(queueConfig);
}

protected override ErrorQueue CreateSystemUnderTest()
protected override Task<ErrorQueue> CreateSystemUnderTestAsync()
{
var fixture = new JustSayingFixture();

return new ErrorQueue(
return Task.FromResult(new ErrorQueue(
fixture.Region,
fixture.UniqueName,
fixture.CreateSqsClient(),
fixture.LoggerFactory);
fixture.LoggerFactory));
}

protected override void PostAssertTeardown()
protected override async Task PostAssertTeardownAsync()
{
SystemUnderTest.DeleteAsync().ResultSync();
base.PostAssertTeardown();
await SystemUnderTest.DeleteAsync();
}

[AwsFact]
Expand Down
13 changes: 5 additions & 8 deletions JustSaying.IntegrationTests/AwsTools/WhenCreatingTopicByName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ public abstract class WhenCreatingTopicByName : XAsyncBehaviourTest<SnsTopicByNa

private JustSayingFixture TestFixture { get; } = new JustSayingFixture();

protected override void Given()
{
}
protected override Task Given() => Task.CompletedTask;

protected override SnsTopicByName CreateSystemUnderTest()
protected override async Task<SnsTopicByName> CreateSystemUnderTestAsync()
{
Client = TestFixture.CreateSnsClient();

Expand All @@ -40,15 +38,14 @@ protected override SnsTopicByName CreateSystemUnderTest()
LoggerFactory,
new NonGenericMessageSubjectProvider());

CreatedTopic.CreateAsync().ResultSync();
await CreatedTopic.CreateAsync();

return CreatedTopic;
}

protected override void PostAssertTeardown()
protected override Task PostAssertTeardownAsync()
{
Client.DeleteTopicAsync(CreatedTopic.Arn).ResultSync();
base.PostAssertTeardown();
return Client.DeleteTopicAsync(CreatedTopic.Arn);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ public class WhenSettingUpMultipleHandlers : XAsyncBehaviourTest<IHaveFulfilledS
private string _queueName;
private CancellationTokenSource _subscriberCts;

protected override void Given()
{
}
protected override Task Given() => Task.CompletedTask;

protected override Task When()
{
return Task.CompletedTask;
}

protected override IHaveFulfilledSubscriptionRequirements CreateSystemUnderTest()
protected override Task<IHaveFulfilledSubscriptionRequirements> CreateSystemUnderTestAsync()
{
// Given 2 handlers
var uniqueTopicAndQueueNames = new UniqueNamingStrategy();
Expand All @@ -47,13 +45,13 @@ protected override IHaveFulfilledSubscriptionRequirements CreateSystemUnderTest(

_subscriberCts = new CancellationTokenSource();
subscription.StartListening(_subscriberCts.Token);
return subscription;
return Task.FromResult(subscription);
}

protected override void PostAssertTeardown()
protected override Task PostAssertTeardownAsync()
{
_subscriberCts.Cancel();
base.PostAssertTeardown();
return Task.CompletedTask;
}

[AwsFact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ public class WhenSettingUpMultipleHandlersFails : XAsyncBehaviourTest<IHaveFulfi
private int _handlersAttached = 0;
private NotSupportedException _capturedException;

protected override void Given()
{
}
protected override Task Given() => Task.CompletedTask;

protected override Task When() => Task.CompletedTask;

protected override IHaveFulfilledSubscriptionRequirements CreateSystemUnderTest()
protected override Task<IHaveFulfilledSubscriptionRequirements> CreateSystemUnderTestAsync()
{
var awsClientFactory = new ProxyAwsClientFactory();
var fixture = new JustSayingFixture();
Expand All @@ -38,7 +36,7 @@ protected override IHaveFulfilledSubscriptionRequirements CreateSystemUnderTest(
_capturedException = ex;
}

return null;
return Task.FromResult<IHaveFulfilledSubscriptionRequirements>(null);
}

private void AttachAHandler(IMayWantOptionalSettings busConfig)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public class WhenUpdatingDeliveryDelay : WhenCreatingQueuesByName
private TimeSpan _oldDeliveryDelay;
private TimeSpan _newDeliveryDelay;

protected override void Given()
protected override Task Given()
{
_oldDeliveryDelay = TimeSpan.FromMinutes(2);
_newDeliveryDelay = TimeSpan.FromMinutes(5);

base.Given();
return base.Given();
}

protected override async Task When()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public class WhenUpdatingRedrivePolicy : WhenCreatingQueuesByName
{
private int _newMaximumReceived;

protected override void Given()
protected override Task Given()
{
_newMaximumReceived = 2;

base.Given();
return base.Given();
}

protected override async Task When()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public class WhenUpdatingRetentionPeriod : WhenCreatingQueuesByName
private TimeSpan _oldRetentionPeriod;
private TimeSpan _newRetentionPeriod;

protected override void Given()
protected override Task Given()
{
_oldRetentionPeriod = TimeSpan.FromSeconds(600);
_newRetentionPeriod = TimeSpan.FromSeconds(700);

base.Given();
return base.Given();
}

protected override async Task When()
Expand Down
16 changes: 7 additions & 9 deletions JustSaying.IntegrationTests/FluentNotificationStackTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ public abstract class FluentNotificationStackTestBase : XAsyncBehaviourTest<Just

private bool _enableMockedBus;

protected override void Given()
{
}
protected override Task Given() => Task.CompletedTask;

protected override JustSaying.JustSayingFluently CreateSystemUnderTest()
protected override Task<JustSaying.JustSayingFluently> CreateSystemUnderTestAsync()
{
var fluent = TestFixture.Builder()
.ConfigurePublisherWith(x =>
Expand All @@ -42,7 +40,7 @@ protected override JustSaying.JustSayingFluently CreateSystemUnderTest()
InjectMockJustSayingBus(fluent);
}

return fluent;
return Task.FromResult(fluent);
}

private void InjectMockJustSayingBus(JustSaying.JustSayingFluently fluent)
Expand All @@ -65,8 +63,8 @@ protected void EnableMockedBus()
protected async Task DeleteTopicIfItAlreadyExists(string topicName)
{
var topics = await GetAllTopics(topicName).ConfigureAwait(false);
await Task.WhenAll(topics.Select(t => DeleteTopic(t))).ConfigureAwait(false);

await Task.WhenAll(topics.Select(DeleteTopicAsync)).ConfigureAwait(false);

var (topicExists, _) = await TryGetTopic(topicName).ConfigureAwait(false);

Expand Down Expand Up @@ -104,7 +102,7 @@ protected async Task DeleteQueueIfItAlreadyExists(string queueName)
throw new Exception($"Deleted queue still exists {(DateTime.Now - start).TotalSeconds} seconds after deletion!");
}

protected async Task DeleteTopic(Topic topic)
protected async Task DeleteTopicAsync(Topic topic)
{
var client = TestFixture.CreateSnsClient();
await client.DeleteTopicAsync(topic.TopicArn).ConfigureAwait(false);
Expand Down Expand Up @@ -171,7 +169,7 @@ private async Task<List<string>> GetAllQueues(string queueName)

await Task.Delay(TimeSpan.FromSeconds(sleepStep)).ConfigureAwait(false);
}

return (false, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="StructureMap" Version="4.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.4.1" />
martincostello marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Amazon;
using JustBehave;
using JustSaying.AwsTools;
Expand Down Expand Up @@ -34,7 +35,7 @@ public abstract class GivenANotificationStack : XAsyncBehaviourTest<IAmJustSayin

protected ILoggerFactory LoggerFactory => TestFixture.LoggerFactory;

protected RegionEndpoint Region =>TestFixture.Region;
protected RegionEndpoint Region => TestFixture.Region;

private JustSayingFixture TestFixture { get; } = new JustSayingFixture();

Expand All @@ -53,30 +54,30 @@ protected void RegisterConfig(IPublishConfiguration config)
_config = config;
}

protected override void Given()
protected override Task Given()
{
_stopwatch.Start();
return Task.CompletedTask;
}

protected override IAmJustSayingFluently CreateSystemUnderTest()
protected override Task<IAmJustSayingFluently> CreateSystemUnderTestAsync()
{
const int TimeoutMillis = 1000;
var timeout = TimeSpan.FromSeconds(1);

var snsHandler = Substitute.For<IHandlerAsync<SimpleMessage>>();
snsHandler.When(x => x.Handle(Arg.Any<SimpleMessage>()))
.Do(x =>
{
var msg = (SimpleMessage) x.Args()[0];
_snsHandler?.Complete(msg).Wait(TimeoutMillis);
});
snsHandler.Handle(Arg.Any<SimpleMessage>()).Returns(async x =>
{
var msg = (SimpleMessage) x.Args()[0];
await Tasks.WaitWithTimeoutAsync(_snsHandler?.Complete(msg), timeout);
});

var sqsHandler = Substitute.For<IHandlerAsync<AnotherSimpleMessage>>();
sqsHandler.When(x => x.Handle(Arg.Any<AnotherSimpleMessage>()))
.Do(x =>
{
var msg = (AnotherSimpleMessage)x.Args()[0];
_sqsHandler?.Complete(msg).Wait(TimeoutMillis);
});
sqsHandler.Handle(Arg.Any<AnotherSimpleMessage>())
.Returns(async x =>
{
var msg = (AnotherSimpleMessage)x.Args()[0];
await Tasks.WaitWithTimeoutAsync(_sqsHandler?.Complete(msg), timeout);
});

Monitoring = Substitute.For<IMessageMonitor>();

Expand Down Expand Up @@ -105,20 +106,19 @@ protected override IAmJustSayingFluently CreateSystemUnderTest()
_subscriberCts = new CancellationTokenSource();
ServiceBus.StartListening(_subscriberCts.Token);

return ServiceBus;
return Task.FromResult(ServiceBus);
}

protected override void PostAssertTeardown()
protected override Task PostAssertTeardownAsync()
{
base.PostAssertTeardown();

_stopwatch.Stop();
Teardown();

// TODO ITestOutputHelper
Console.WriteLine($"The test took {_stopwatch.ElapsedMilliseconds / 1000} seconds.");

_subscriberCts.Cancel();
return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ private async Task AndAMessageIsPublished()
_message = new SimpleMessage { Id = Guid.NewGuid() };
await _publisher.PublishAsync(_message);

await Task.Yield();
await Task.Delay(TimeSpan.FromSeconds(1));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ private async Task WhenMessagesArePublishedToBothRegions()
await _primaryPublisher.PublishAsync(_message1);
await _secondaryPublisher.PublishAsync(_message2);

await Task.Yield();
await Task.Delay(TimeSpan.FromSeconds(1));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ private async Task AndAMessageIsPublished()
_message = new SimpleMessage { Id = Guid.NewGuid() };
await _publisher.PublishAsync(_message);

await Task.Yield();
await Task.Delay(TimeSpan.FromSeconds(1));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ private async Task WhenMessagesArePublishedToBothRegions()
await _primaryPublisher.PublishAsync(_message1);
await _secondaryPublisher.PublishAsync(_message2);

await Task.Yield();
await Task.Delay(TimeSpan.FromSeconds(1));
}

Expand Down
Loading