-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Azure.Messaging.EventGrid based tests (#24)
* Migrate the Azure.Messaging.EventGrid tests into here from a previously uncontrolled separate solution. * Add test Traits
- Loading branch information
Showing
23 changed files
with
216 additions
and
33 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
src/AzureEventGridSimulator.Tests/AzureMessagingTests/ActualSimulatorFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace AzureEventGridSimulator.Tests.AzureMessagingTests | ||
{ | ||
public class ActualSimulatorFixture : IDisposable, IAsyncLifetime | ||
{ | ||
private const string SimulatorFileName = "AzureEventGridSimulator"; | ||
private bool _disposed; | ||
private string _simulatorExePath; | ||
|
||
private Process _simulatorProcess; | ||
|
||
public async Task InitializeAsync() | ||
{ | ||
var simulatorDirectory = Directory.GetCurrentDirectory(); | ||
_simulatorExePath = Path.Combine(simulatorDirectory, $"{SimulatorFileName}.exe"); | ||
|
||
KillExistingSimulators(); | ||
|
||
_simulatorProcess = Process.Start(new ProcessStartInfo(_simulatorExePath) | ||
{ | ||
WorkingDirectory = simulatorDirectory, | ||
UseShellExecute = false, | ||
RedirectStandardOutput = true, | ||
CreateNoWindow = true, | ||
Environment = { new KeyValuePair<string, string>("ASPNETCORE_ENVIRONMENT", "Development") } | ||
}); | ||
|
||
var isAlive = false; | ||
|
||
while (!isAlive && !_simulatorProcess.StandardOutput.EndOfStream) | ||
{ | ||
var line = await _simulatorProcess.StandardOutput.ReadLineAsync(); | ||
|
||
isAlive = line.Contains("It's Alive"); | ||
} | ||
|
||
if (!isAlive) | ||
{ | ||
} | ||
} | ||
|
||
public Task DisposeAsync() | ||
{ | ||
Dispose(); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (!_disposed) | ||
{ | ||
if (_simulatorProcess?.HasExited == false) | ||
{ | ||
_simulatorProcess?.Kill(true); | ||
_simulatorProcess?.WaitForExit(); | ||
} | ||
|
||
_disposed = true; | ||
GC.SuppressFinalize(this); | ||
} | ||
} | ||
|
||
private void KillExistingSimulators() | ||
{ | ||
try | ||
{ | ||
// Kill any existing instances of the test simulator that may still be hanging around. | ||
// Note: there shouldn't be any unless something went wrong and the test runner didn't exit cleanly. | ||
var simulatorProcesses = Process.GetProcesses() | ||
.Where(o => o.ProcessName == SimulatorFileName) | ||
.Where(o => string.Equals(o.MainModule?.FileName, _simulatorExePath, StringComparison.OrdinalIgnoreCase)) | ||
.ToArray(); | ||
|
||
foreach (var process in simulatorProcesses) | ||
{ | ||
process.Kill(); | ||
} | ||
} | ||
catch | ||
{ | ||
// | ||
} | ||
} | ||
|
||
~ActualSimulatorFixture() | ||
{ | ||
Dispose(); | ||
} | ||
} | ||
|
||
[CollectionDefinition(nameof(ActualSimulatorFixtureCollection))] | ||
public class ActualSimulatorFixtureCollection : ICollectionFixture<ActualSimulatorFixture> | ||
{ | ||
// This class has no code, and is never created. Its purpose is simply | ||
// to be the place to apply [CollectionDefinition] | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
src/AzureEventGridSimulator.Tests/AzureMessagingTests/AzureMessagingEventGridTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Azure; | ||
using Azure.Core; | ||
using Azure.Messaging.EventGrid; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace AzureEventGridSimulator.Tests.AzureMessagingTests | ||
{ | ||
/// <summary> | ||
/// Simple tests to check that we can send an event via Azure.Messaging.EventGrid library. | ||
/// NOTE: These tests require (and automatically start) an actual instance of AzureEventGridSimulator.exe as there is no way to inject an HttpClient (from a WebApplicationFactory) | ||
/// into Azure.Messaging.EventGrid. | ||
/// </summary> | ||
[Collection(nameof(ActualSimulatorFixtureCollection))] | ||
[Trait("Category","integration-actual")] | ||
public class AzureMessagingEventGridTest | ||
{ | ||
private readonly ActualSimulatorFixture _actualSimulatorFixture; | ||
|
||
public AzureMessagingEventGridTest(ActualSimulatorFixture actualSimulatorFixture) | ||
{ | ||
_actualSimulatorFixture = actualSimulatorFixture; | ||
} | ||
|
||
[Fact] | ||
public async Task GivenValidEvent_WhenUriContainsNonStandardPort_ThenItShouldBeAccepted() | ||
{ | ||
var client = new EventGridPublisherClient( | ||
new Uri("https://localhost:60101/api/events"), | ||
new AzureKeyCredential("TheLocal+DevelopmentKey=")); | ||
|
||
var response = await client.SendEventAsync(new EventGridEvent("/the/subject", "The.Event.Type", "v1", new { Id = 1, Foo = "Bar" })); | ||
|
||
response.Status.ShouldBe((int)HttpStatusCode.OK); | ||
} | ||
|
||
[Fact] | ||
public async Task GivenValidEvents_WhenUriContainsNonStandardPort_TheyShouldBeAccepted() | ||
{ | ||
var client = new EventGridPublisherClient( | ||
new Uri("https://localhost:60101/api/events"), | ||
new AzureKeyCredential("TheLocal+DevelopmentKey=")); | ||
|
||
var events = new[] | ||
{ | ||
new EventGridEvent("/the/subject1", "The.Event.Type1", "v1", new { Id = 1, Foo = "Bar" }), | ||
new EventGridEvent("/the/subject2", "The.Event.Type2", "v1", new { Id = 2, Foo = "Baz" }) | ||
}; | ||
|
||
var response = await client.SendEventsAsync(events); | ||
|
||
response.Status.ShouldBe((int)HttpStatusCode.OK); | ||
} | ||
|
||
[Fact] | ||
public async Task GivenValidEvent_WhenUriContainsNonExistentPort_ThenItShouldNotBeAccepted() | ||
{ | ||
var client = new EventGridPublisherClient( | ||
new Uri("https://localhost:19999/api/events"), | ||
new AzureKeyCredential("TheLocal+DevelopmentKey="), | ||
new EventGridPublisherClientOptions | ||
{ Retry = { Mode = RetryMode.Fixed, MaxRetries = 0, NetworkTimeout = TimeSpan.FromSeconds(5) } }); | ||
|
||
var exception = await Should.ThrowAsync<RequestFailedException>(async () => | ||
{ | ||
await client.SendEventAsync(new EventGridEvent("/the/subject", "The.Event.Type", "v1", | ||
new { Id = 1, Foo = "Bar" })); | ||
}); | ||
|
||
exception.Message.ShouldContain("actively refused"); | ||
exception.Status.ShouldBe(0); | ||
} | ||
|
||
[Fact] | ||
public async Task GivenValidEvent_WhenKeyIsWrong_ThenItShouldNotBeAccepted() | ||
{ | ||
var client = new EventGridPublisherClient( | ||
new Uri("https://localhost:60101/api/events"), | ||
new AzureKeyCredential("TheWrongLocal+DevelopmentKey=")); | ||
|
||
var exception = await Should.ThrowAsync<RequestFailedException>(async () => | ||
{ | ||
await client.SendEventAsync(new EventGridEvent("/the/subject", "The.Event.Type", "v1", | ||
new { Id = 1, Foo = "Bar" })); | ||
}); | ||
|
||
exception.Status.ShouldBe((int)HttpStatusCode.Unauthorized); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.