-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Nybus.Extensions.Hosting.Lambda package (#84)
- Loading branch information
Showing
7 changed files
with
321 additions
and
4 deletions.
There are no files selected for viewing
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
20 changes: 20 additions & 0 deletions
20
...extensions/hosting/Nybus.Extensions.Hosting.Lambda/Nybus.Extensions.Hosting.Lambda.csproj
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,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<RootNamespace>Nybus</RootNamespace> | ||
<AssemblyName>Nybus.Extensions.Hosting.Lambda</AssemblyName> | ||
|
||
<Description>Nybus hosting in AWS Lambda</Description> | ||
<PackageTags>dotnet-standard;queue;rx;nybus;aws-lambda;lambda</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\Nybus.Abstractions\Nybus.Abstractions.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Kralizek.Lambda.Template" Version="3.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
50 changes: 50 additions & 0 deletions
50
src/extensions/hosting/Nybus.Extensions.Hosting.Lambda/NybusFunction.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,50 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Amazon.Lambda.Core; | ||
using Kralizek.Lambda; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Nybus | ||
{ | ||
public abstract class NybusFunction<TInput> : Function | ||
{ | ||
protected NybusFunction() | ||
{ | ||
_busHost = ServiceProvider.GetRequiredService<IBusHost>(); | ||
} | ||
|
||
private readonly IBusHost _busHost; | ||
|
||
public async Task FunctionHandlerAsync(TInput input, ILambdaContext context) | ||
{ | ||
try | ||
{ | ||
await _busHost.StartAsync().ConfigureAwait(false); | ||
|
||
using (var scope = ServiceProvider.CreateScope()) | ||
{ | ||
var handler = scope.ServiceProvider.GetService<Kralizek.Lambda.IEventHandler<TInput>>(); | ||
|
||
if (handler == null) | ||
{ | ||
Logger.LogCritical($"No IEventHandler<{typeof(TInput).Name}> could be found."); | ||
throw new InvalidOperationException($"No IEventHandler<{typeof(TInput).Name}> could be found."); | ||
} | ||
|
||
Logger.LogInformation("Invoking handler"); | ||
await handler.HandleAsync(input, context).ConfigureAwait(false); | ||
} | ||
} | ||
finally | ||
{ | ||
await _busHost.StopAsync().ConfigureAwait(false); | ||
} | ||
} | ||
|
||
protected void RegisterHandler<THandler>(IServiceCollection services) where THandler : class, Kralizek.Lambda.IEventHandler<TInput> | ||
{ | ||
services.AddTransient<Kralizek.Lambda.IEventHandler<TInput>, THandler>(); | ||
} | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
tests/Tests.Nybus.Extensions.Hosting.Lambda/NybusFunctionTests.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,102 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Amazon.Lambda.Core; | ||
using Moq; | ||
using NUnit.Framework; | ||
|
||
namespace Tests | ||
{ | ||
[TestFixture] | ||
public class NybusFunctionTests | ||
{ | ||
[Test, AutoMoqData] | ||
public void TestFunction_is_set_up(TestFunction sut) | ||
{ | ||
Assert.That(sut.BusHost, Is.Not.Null); | ||
|
||
Assert.That(sut.Handler, Is.Not.Null); | ||
} | ||
|
||
[Test, AutoMoqData] | ||
public void TestFunctionWithHandler_is_set_up(TestFunctionWithHandler sut) | ||
{ | ||
Assert.That(sut.BusHost, Is.Not.Null); | ||
} | ||
|
||
[Test, AutoMoqData] | ||
public void TestFunctionWithNoHandler_is_set_up(TestFunctionWithNoHandler sut) | ||
{ | ||
Assert.That(sut.BusHost, Is.Not.Null); | ||
} | ||
|
||
[Test, AutoMoqData] | ||
public async Task FunctionHandlerAsync_starts_bus(TestFunction sut, string payload, ILambdaContext context) | ||
{ | ||
await sut.FunctionHandlerAsync(payload, context); | ||
|
||
Mock.Get(sut.BusHost).Verify(p => p.StartAsync()); | ||
} | ||
|
||
[Test, AutoMoqData] | ||
public async Task FunctionHandlerAsync_stops_bus(TestFunction sut, string payload, ILambdaContext context) | ||
{ | ||
await sut.FunctionHandlerAsync(payload, context); | ||
|
||
Mock.Get(sut.BusHost).Verify(p => p.StopAsync()); | ||
} | ||
|
||
[Test, AutoMoqData] | ||
public async Task FunctionHandlerAsync_starts_bus(TestFunctionWithHandler sut, string payload, ILambdaContext context) | ||
{ | ||
await sut.FunctionHandlerAsync(payload, context); | ||
|
||
Mock.Get(sut.BusHost).Verify(p => p.StartAsync()); | ||
} | ||
|
||
[Test, AutoMoqData] | ||
public async Task FunctionHandlerAsync_stops_bus(TestFunctionWithHandler sut, string payload, ILambdaContext context) | ||
{ | ||
await sut.FunctionHandlerAsync(payload, context); | ||
|
||
Mock.Get(sut.BusHost).Verify(p => p.StopAsync()); | ||
} | ||
|
||
[Test, AutoMoqData] | ||
public void FunctionHandlerAsync_starts_bus(TestFunctionWithNoHandler sut, string payload, ILambdaContext context) | ||
{ | ||
Assert.ThrowsAsync<InvalidOperationException>(() => sut.FunctionHandlerAsync(payload, context)); | ||
|
||
Mock.Get(sut.BusHost).Verify(p => p.StartAsync()); | ||
} | ||
|
||
[Test, AutoMoqData] | ||
public void FunctionHandlerAsync_stops_bus(TestFunctionWithNoHandler sut, string payload, ILambdaContext context) | ||
{ | ||
Assert.ThrowsAsync<InvalidOperationException>(() => sut.FunctionHandlerAsync(payload, context)); | ||
|
||
Mock.Get(sut.BusHost).Verify(p => p.StopAsync()); | ||
} | ||
|
||
[Test, AutoMoqData] | ||
public async Task FunctionHandlerAsync_invokes_handler(TestFunction sut, string payload, ILambdaContext context) | ||
{ | ||
await sut.FunctionHandlerAsync(payload, context); | ||
|
||
Mock.Get(sut.Handler).Verify(p => p.HandleAsync(payload, context)); | ||
} | ||
|
||
[Test, AutoMoqData] | ||
public async Task FunctionHandlerAsync_invokes_registered_handler(TestFunctionWithHandler sut, string payload, ILambdaContext context) | ||
{ | ||
await sut.FunctionHandlerAsync(payload, context); | ||
|
||
Mock.Get(TestHandler.InnerHandler).Verify(p => p.HandleAsync(payload, context)); | ||
} | ||
|
||
[Test, AutoMoqData] | ||
public void FunctionHandlerAsync_throws_if_no_handler_is_registered(TestFunctionWithNoHandler sut, string payload, ILambdaContext context) | ||
{ | ||
Assert.ThrowsAsync<InvalidOperationException>(() => sut.FunctionHandlerAsync(payload, context)); | ||
} | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
tests/Tests.Nybus.Extensions.Hosting.Lambda/TestFunction.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,83 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Amazon.Lambda.Core; | ||
using AutoFixture; | ||
using AutoFixture.AutoMoq; | ||
using Kralizek.Lambda; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Moq; | ||
using Nybus; | ||
|
||
namespace Tests | ||
{ | ||
public class TestFunction : NybusFunction<string> | ||
{ | ||
protected override void ConfigureServices(IServiceCollection services, IExecutionEnvironment executionEnvironment) | ||
{ | ||
IFixture fixture = new Fixture(); | ||
fixture.Customize(new AutoMoqCustomization | ||
{ | ||
GenerateDelegates = true, | ||
ConfigureMembers = true | ||
}); | ||
|
||
BusHost = fixture.Create<IBusHost>(); | ||
services.AddSingleton(BusHost); | ||
|
||
Handler = fixture.Create<Kralizek.Lambda.IEventHandler<string>>(); | ||
services.AddSingleton(Handler); | ||
} | ||
|
||
public IBusHost BusHost { get; private set; } | ||
|
||
public Kralizek.Lambda.IEventHandler<string> Handler { get; private set; } | ||
} | ||
|
||
public class TestFunctionWithHandler : NybusFunction<string> | ||
{ | ||
protected override void ConfigureServices(IServiceCollection services, IExecutionEnvironment executionEnvironment) | ||
{ | ||
IFixture fixture = new Fixture(); | ||
fixture.Customize(new AutoMoqCustomization | ||
{ | ||
GenerateDelegates = true, | ||
ConfigureMembers = true | ||
}); | ||
|
||
BusHost = fixture.Create<IBusHost>(); | ||
services.AddSingleton(BusHost); | ||
|
||
RegisterHandler<TestHandler>(services); | ||
} | ||
|
||
public IBusHost BusHost { get; private set; } | ||
} | ||
|
||
public class TestFunctionWithNoHandler : NybusFunction<string> | ||
{ | ||
protected override void ConfigureServices(IServiceCollection services, IExecutionEnvironment executionEnvironment) | ||
{ | ||
IFixture fixture = new Fixture(); | ||
fixture.Customize(new AutoMoqCustomization | ||
{ | ||
GenerateDelegates = true, | ||
ConfigureMembers = true | ||
}); | ||
|
||
BusHost = fixture.Create<IBusHost>(); | ||
services.AddSingleton(BusHost); | ||
} | ||
|
||
public IBusHost BusHost { get; private set; } | ||
} | ||
|
||
public class TestHandler : Kralizek.Lambda.IEventHandler<string> | ||
{ | ||
public Task HandleAsync(string input, ILambdaContext context) | ||
{ | ||
return InnerHandler.HandleAsync(input, context); | ||
} | ||
|
||
public static Kralizek.Lambda.IEventHandler<string> InnerHandler { get; } = Mock.Of<Kralizek.Lambda.IEventHandler<string>>(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
tests/Tests.Nybus.Extensions.Hosting.Lambda/Tests.Nybus.Extensions.Hosting.Lambda.csproj
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,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.1</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="nunit" Version="3.11.0" /> | ||
<PackageReference Include="NUnit3TestAdapter" Version="3.11.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\extensions\hosting\Nybus.Extensions.Hosting.Lambda\Nybus.Extensions.Hosting.Lambda.csproj" /> | ||
<ProjectReference Include="..\TestUtils\TestUtils.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |