Skip to content

Commit

Permalink
Upgrade nugets and allow tests to work on linx (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
pm7y authored Feb 7, 2023
1 parent cbd75eb commit f9983fb
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 187 deletions.
Original file line number Diff line number Diff line change
@@ -1,85 +1,87 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

namespace AzureEventGridSimulator.Tests.ActualSimulatorTests;

public class ActualSimulatorFixture : IDisposable, IAsyncLifetime
{
private const string SimulatorFileName = "AzureEventGridSimulator";
private bool _disposed;
private string _simulatorExePath;

private Process _simulatorProcess;

public 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", "Test") }
});

return Task.CompletedTask;
}

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();
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Xunit;

namespace AzureEventGridSimulator.Tests.ActualSimulatorTests;

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();
var executable = Path.GetFileNameWithoutExtension(typeof(Program).Assembly.Location);
_simulatorExePath = Path.Combine(simulatorDirectory, executable);

KillExistingSimulators();

_simulatorProcess = Process.Start(new ProcessStartInfo(_simulatorExePath)
{
WorkingDirectory = simulatorDirectory,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true,
Environment = { new KeyValuePair<string, string>("ASPNETCORE_ENVIRONMENT", "Test") }
});

await Task.Delay(1000);
}

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();
}
}
Original file line number Diff line number Diff line change
@@ -1,99 +1,99 @@
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.ActualSimulatorTests;

/// <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
{
// ReSharper disable once NotAccessedField.Local
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="),
new EventGridPublisherClientOptions
{ Retry = { Mode = RetryMode.Fixed, MaxRetries = 0, NetworkTimeout = TimeSpan.FromSeconds(5) } });

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="),
new EventGridPublisherClientOptions
{ Retry = { Mode = RetryMode.Fixed, MaxRetries = 0, NetworkTimeout = TimeSpan.FromSeconds(5) } });

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="),
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.Status.ShouldBe((int)HttpStatusCode.Unauthorized);
}
}
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.ActualSimulatorTests;

/// <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
{
// ReSharper disable once NotAccessedField.Local
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="),
new EventGridPublisherClientOptions
{ Retry = { Mode = RetryMode.Fixed, MaxRetries = 0, NetworkTimeout = TimeSpan.FromSeconds(5) } });

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="),
new EventGridPublisherClientOptions
{ Retry = { Mode = RetryMode.Fixed, MaxRetries = 0, NetworkTimeout = TimeSpan.FromSeconds(5) } });

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("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="),
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.Status.ShouldBe((int)HttpStatusCode.Unauthorized);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Messaging.EventGrid" Version="4.12.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.0" />
<PackageReference Include="Azure.Messaging.EventGrid" Version="4.13.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="Shouldly" Version="4.1.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
Expand Down

0 comments on commit f9983fb

Please sign in to comment.