-
-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Apache Pulsar module (#1103)
Co-authored-by: David Jensen <djn@danskecommodities.com> Co-authored-by: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com>
- Loading branch information
1 parent
734fb52
commit 4a512ef
Showing
19 changed files
with
667 additions
and
7 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# Apache Pulsar | ||
|
||
Testcontainers can be used to automatically create [Apache Pulsar](https://pulsar.apache.org) containers without the need for external services. Based on the official Apache Pulsar Docker image, it is recommended to read the official [getting started](https://pulsar.apache.org/docs/next/getting-started-docker/) guide. | ||
|
||
The following example uses the following NuGet packages: | ||
|
||
```console title="Install the NuGet dependencies" | ||
dotnet add package Testcontainers.Pulsar | ||
dotnet add package DotPulsar | ||
dotnet add package xunit | ||
``` | ||
|
||
IDEs and editors may also require the following packages to run tests: `xunit.runner.visualstudio` and `Microsoft.NET.Test.Sdk`. | ||
|
||
Copy and paste the following code into a new `.cs` test file within an existing test project. | ||
|
||
```csharp | ||
using System; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using DotPulsar; | ||
using DotPulsar.Extensions; | ||
using Xunit; | ||
|
||
namespace Testcontainers.Pulsar; | ||
|
||
public sealed class PulsarContainerTest : IAsyncLifetime | ||
{ | ||
private readonly PulsarContainer _pulsarContainer = | ||
new PulsarBuilder().Build(); | ||
|
||
[Fact] | ||
public async Task ConsumerReceivesSendMessage() | ||
{ | ||
const string helloPulsar = "Hello, Pulsar!"; | ||
|
||
var topic = $"persistent://public/default/{Guid.NewGuid():D}"; | ||
|
||
var name = Guid.NewGuid().ToString("D"); | ||
|
||
await using var client = PulsarClient.Builder() | ||
.ServiceUrl(new Uri(_pulsarContainer.GetBrokerAddress())) | ||
.Build(); | ||
|
||
await using var producer = client.NewProducer(Schema.String) | ||
.Topic(topic) | ||
.Create(); | ||
|
||
await using var consumer = client.NewConsumer(Schema.String) | ||
.Topic(topic) | ||
.SubscriptionName(name) | ||
.InitialPosition(SubscriptionInitialPosition.Earliest) | ||
.Create(); | ||
|
||
_ = await producer.Send(helloPulsar) | ||
.ConfigureAwait(true); | ||
|
||
var message = await consumer.Receive() | ||
.ConfigureAwait(true); | ||
|
||
Assert.Equal(helloPulsar, Encoding.Default.GetString(message.Data)); | ||
} | ||
|
||
public Task InitializeAsync() | ||
=> _pulsarContainer.StartAsync(); | ||
|
||
public Task DisposeAsync() | ||
=> _pulsarContainer.DisposeAsync().AsTask(); | ||
} | ||
``` | ||
|
||
To execute the tests, use the command `dotnet test` from a terminal. | ||
|
||
## Access Pulsar | ||
|
||
To get the Pulsar broker URL use: | ||
|
||
```csharp | ||
string pulsarBrokerUrl = _pulsarContainer.GetPulsarBrokerUrl(); | ||
``` | ||
|
||
To get the Pulsar service URL use: | ||
```csharp | ||
string pulsarServiceUrl = _pulsarContainer.GetHttpServiceUrl(); | ||
``` | ||
|
||
## Enable token authentication | ||
|
||
If you need to use token authentication, use the following builder configuration to enable authentication: | ||
|
||
```csharp | ||
PulsarContainer _pulsarContainer = PulsarBuilder().WithTokenAuthentication().Build(); | ||
``` | ||
|
||
Start the container and get the token from the running instance by using: | ||
|
||
```csharp | ||
var authToken = await container.CreateAuthenticationTokenAsync(TimeSpan.FromHours(1)) | ||
.ConfigureAwait(false); | ||
``` | ||
|
||
## Enable Pulsar Functions | ||
|
||
If you need to use Pulsar Functions, use the following builder configuration to enable it: | ||
|
||
```csharp | ||
PulsarContainer _pulsarContainer = PulsarBuilder().WithFunctions().Build(); | ||
``` |
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 |
---|---|---|
|
@@ -45,4 +45,5 @@ nav: | |
- modules/mssql.md | ||
- modules/neo4j.md | ||
- modules/postgres.md | ||
- modules/pulsar.md | ||
- modules/rabbitmq.md |
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 @@ | ||
root = true |
Oops, something went wrong.