-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make sure that that SNS & SQS Handler are using scoped. (#15)
So after usage scoped objects are disposed. Co-authored-by: Wouter Crooy <[email protected]>
- Loading branch information
Showing
6 changed files
with
198 additions
and
8 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
90 changes: 90 additions & 0 deletions
90
tests/Tests.Lambda.Template/Sns/SnsEventHandlerDisposalTests.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,90 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Amazon.Lambda.Core; | ||
using Amazon.Lambda.SNSEvents; | ||
using Amazon.Lambda.SQSEvents; | ||
using Amazon.Lambda.TestUtilities; | ||
using Kralizek.Lambda; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using NUnit.Framework; | ||
|
||
namespace Tests.Lambda.Sns | ||
{ | ||
public class SnsEventHandlerDisposalTests | ||
{ | ||
[Test] | ||
public async Task EventHandler_Should_Use_Scoped_Object_In_ForEach_Loop() | ||
{ | ||
var snsEvent = new SNSEvent | ||
{ | ||
Records = new List<SNSEvent.SNSRecord> | ||
{ | ||
new SNSEvent.SNSRecord | ||
{ | ||
Sns = new SNSEvent.SNSMessage | ||
{ | ||
Message = "{}" | ||
} | ||
}, | ||
new SNSEvent.SNSRecord | ||
{ | ||
Sns = new SNSEvent.SNSMessage | ||
{ | ||
Message = "{}" | ||
} | ||
} | ||
} | ||
}; | ||
|
||
var dependency = new DisposableDependency(); | ||
|
||
var services = new ServiceCollection(); | ||
|
||
services.AddScoped(_ => dependency); | ||
|
||
var tcs = new TaskCompletionSource<TestNotification>(); | ||
services.AddTransient<IEventHandler<SQSEvent>, SqsEventHandler<TestNotification>>(); | ||
|
||
services.AddTransient<INotificationHandler<TestNotification>, | ||
TestNotificationScopedHandler>(provider => | ||
new TestNotificationScopedHandler(provider.GetRequiredService<DisposableDependency>(), tcs)); | ||
|
||
var sp = services.BuildServiceProvider(); | ||
var snsEventHandler = new SnsEventHandler<TestNotification>(sp, new NullLoggerFactory()); | ||
|
||
var task = snsEventHandler.HandleAsync(snsEvent, new TestLambdaContext()); | ||
|
||
Assert.That(dependency.Disposed, Is.False, "Dependency should not be disposed"); | ||
Assert.That(task.IsCompleted, Is.False, "The task should not be completed"); | ||
|
||
tcs.SetResult(new TestNotification()); | ||
|
||
await task; | ||
|
||
Assert.That(dependency.Disposed, Is.True, "Dependency should be disposed"); | ||
Assert.That(task.IsCompleted, Is.True, "The task should be completed"); | ||
} | ||
|
||
private class DisposableDependency : IDisposable | ||
{ | ||
public bool Disposed { get; private set; } | ||
public void Dispose() => Disposed = true; | ||
} | ||
|
||
private class TestNotificationScopedHandler: INotificationHandler<TestNotification> | ||
{ | ||
private readonly DisposableDependency _dependency; | ||
private readonly TaskCompletionSource<TestNotification> _tcs; | ||
|
||
public TestNotificationScopedHandler(DisposableDependency dependency, TaskCompletionSource<TestNotification> tcs) | ||
{ | ||
_dependency = dependency; | ||
_tcs = tcs; | ||
} | ||
|
||
public Task HandleAsync(TestNotification message, ILambdaContext context) => _tcs.Task; | ||
} | ||
} | ||
} |
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
83 changes: 83 additions & 0 deletions
83
tests/Tests.Lambda.Template/Sqs/SqsEventHandlerDisposalTests.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.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Amazon.Lambda.Core; | ||
using Amazon.Lambda.SQSEvents; | ||
using Amazon.Lambda.TestUtilities; | ||
using Kralizek.Lambda; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using NUnit.Framework; | ||
|
||
namespace Tests.Lambda.Sqs | ||
{ | ||
public class SqsEventHandlerDisposalTests | ||
{ | ||
[Test] | ||
public async Task EventHandler_Should_Use_Scoped_Object_In_ForEach_Loop() | ||
{ | ||
var sqsEvent = new SQSEvent | ||
{ | ||
Records = new List<SQSEvent.SQSMessage> | ||
{ | ||
new SQSEvent.SQSMessage | ||
{ | ||
Body = "{}" | ||
}, | ||
new SQSEvent.SQSMessage | ||
{ | ||
Body = "{}" | ||
}, | ||
} | ||
}; | ||
|
||
var dependency = new DisposableDependency(); | ||
|
||
var services = new ServiceCollection(); | ||
|
||
services.AddScoped(_ => dependency); | ||
|
||
var tcs = new TaskCompletionSource<TestMessage>(); | ||
services.AddTransient<IEventHandler<SQSEvent>, SqsEventHandler<TestMessage>>(); | ||
|
||
services.AddTransient<IMessageHandler<TestMessage>, | ||
TestMessageScopedHandler>(provider => | ||
new TestMessageScopedHandler(provider.GetRequiredService<DisposableDependency>(), tcs)); | ||
|
||
var sp = services.BuildServiceProvider(); | ||
var sqsEventHandler = new SqsEventHandler<TestMessage>(sp, new NullLoggerFactory()); | ||
|
||
var task = sqsEventHandler.HandleAsync(sqsEvent, new TestLambdaContext()); | ||
|
||
Assert.That(dependency.Disposed, Is.False, "Dependency should not be disposed"); | ||
Assert.That(task.IsCompleted, Is.False, "The task should not be completed"); | ||
|
||
tcs.SetResult(new TestMessage()); | ||
await task; | ||
Assert.That(dependency.Disposed, Is.True, "Dependency should be disposed"); | ||
Assert.That(task.IsCompleted, Is.True, "The task should be completed"); | ||
|
||
|
||
} | ||
|
||
private class DisposableDependency : IDisposable | ||
{ | ||
public bool Disposed { get; private set; } | ||
public void Dispose() => Disposed = true; | ||
} | ||
|
||
private class TestMessageScopedHandler : IMessageHandler<TestMessage> | ||
{ | ||
private readonly DisposableDependency _dependency; | ||
private readonly TaskCompletionSource<TestMessage> _tcs; | ||
|
||
public TestMessageScopedHandler(DisposableDependency dependency, TaskCompletionSource<TestMessage> tcs) | ||
{ | ||
_dependency = dependency; | ||
_tcs = tcs; | ||
} | ||
|
||
public Task HandleAsync(TestMessage message, ILambdaContext context) => _tcs.Task; | ||
} | ||
} | ||
} |
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