LocalSqsSnsMessaging 0.5.3

dotnet add package LocalSqsSnsMessaging --version 0.5.3                
NuGet\Install-Package LocalSqsSnsMessaging -Version 0.5.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LocalSqsSnsMessaging" Version="0.5.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LocalSqsSnsMessaging --version 0.5.3                
#r "nuget: LocalSqsSnsMessaging, 0.5.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install LocalSqsSnsMessaging as a Cake Addin
#addin nuget:?package=LocalSqsSnsMessaging&version=0.5.3

// Install LocalSqsSnsMessaging as a Cake Tool
#tool nuget:?package=LocalSqsSnsMessaging&version=0.5.3                

LocalSqsSnsMessaging

NuGet build

Overview

This .NET library is intended to provide a simple in-memory drop-in replacement for the AWS SDK for SQS and SNS, primarily for testing (but can be used for local development too).

Why?

Why would you build this when LocalStack already exists, and is awesome?

One word: Speed ๐ŸŽ๏ธ๏ธโšกโšก

While LocalStack is relatively quick, nothing is a replacement for in-memory operations, it means you can run your tests faster, and you can run them in parallel without worrying about port conflicts.

Don't take my word for it, here are our tests for this project at the time of writing, ran against this library and LocalStack (to verify correctness): Test run example

[!TIP] The LocalStack tests above were ran with the default behaviour of xUnit, which is to not run tests in parallel when a test collection is used. You can speed up this sort of test suite by fighting against xUnits defaults (see Meziantou.Xunit.ParallelTestFramework for example) and getting tests to run in parallel. LocalStack has feature where if you pass an access key that looks like an account id, it will use this account for any resources created, this can help isolate tests from each other allowing them to run in parallel.

Additionally, some tests rely on the passage of time, but now with .NETs TimeProvider you can control time in your tests, and travel through time like it's 1985, Great Scott!

Examples

Basic Usage

Creating a topic, a queue, subscribing the queue to the topic, and sending a message to the topic, then receiving the message from the queue.

using Amazon.SimpleNotificationService.Model;
using LocalSqsSnsMessaging;

var bus = new InMemoryAwsBus();
using var sqs = bus.CreateSqsClient();
using var sns = bus.CreateSnsClient();

// create a queue and a topic
var queueUrl = (await sqs.CreateQueueAsync("test-queue")).QueueUrl;
var topicArn = (await sns.CreateTopicAsync("test-topic")).TopicArn;
var queueArn = (await sqs.GetQueueAttributesAsync(queueUrl, ["QueueArn"])).Attributes["QueueArn"];

// subscribe the queue to the topic
await sns.SubscribeAsync(new SubscribeRequest(topicArn, "sqs", queueArn)
{
    Attributes = new() { ["RawMessageDelivery"] = "true" }
});

// send a message to the topic
await sns.PublishAsync(topicArn, "Hello, World!");

// receive the message from the queue
var receiveMessageResponse = await sqs.ReceiveMessageAsync(queueUrl);
var message = receiveMessageResponse.Messages.Single();

Console.WriteLine(message.Body); // Hello, World!

Time Travel

Creating a queue, sending a message to the queue with a delay, advancing time, and receiving the message from the queue.

using Amazon.SQS.Model;
using LocalSqsSnsMessaging;
using Microsoft.Extensions.Time.Testing;

var timeProvider = new FakeTimeProvider(); // from `Microsoft.Extensions.TimeProvider.Testing` package

var bus = new InMemoryAwsBus { TimeProvider = timeProvider};
using var sqs = bus.CreateSqsClient();
using var sns = bus.CreateSnsClient();

// create a queue
var queueUrl = (await sqs.CreateQueueAsync("test-queue")).QueueUrl;

// send a message to the topic
await sqs.SendMessageAsync(new SendMessageRequest(queueUrl, "Hello, World!")
{
    DelaySeconds = 30
});

// receive the message from the queue
var firstReceiveMessageResponse = await sqs.ReceiveMessageAsync(queueUrl);
Console.WriteLine(firstReceiveMessageResponse.Messages.Count); // 0

// advance time by 31 seconds
timeProvider.Advance(TimeSpan.FromSeconds(31));

// receive the message from the queue
var secondReceiveMessageResponse = await sqs.ReceiveMessageAsync(queueUrl);
var message = secondReceiveMessageResponse.Messages.Single();

Console.WriteLine(message.Body); // Hello, World!

All actions in this library that depend on delays or timeouts use the TimeProvider to control time. So you can also take advantage of this feature with features like visibility timeouts.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.5.3 138 10/14/2024