Skip to content

Commit 9999550

Browse files
authored
docs(EventHubs, ServiceBus): Add example (#1396)
1 parent 76ce3e0 commit 9999550

File tree

7 files changed

+104
-0
lines changed

7 files changed

+104
-0
lines changed

docs/modules/eventhubs.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Azure Event Hubs
2+
3+
Azure [Event Hubs](https://learn.microsoft.com/en-us/azure/event-hubs/overview-emulator) emulator⁠ is designed to offer a local development experience for Azure Event Hubs⁠, enabling you to develop and test code against the service in isolation, free from cloud interference.
4+
5+
Add the following dependency to your project file:
6+
7+
```shell title="NuGet"
8+
dotnet add package Testcontainers.EventHubs
9+
```
10+
11+
You can start an Azure Event Hubs container instance from any .NET application. Here, we create different container instances and pass them to the base test class. This allows us to test different configurations.
12+
13+
=== "Create Container Instance"
14+
```csharp
15+
--8<-- "tests/Testcontainers.EventHubs.Tests/EventHubsContainerTest.cs:CreateEventHubsContainer"
16+
```
17+
18+
This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method.
19+
20+
=== "Usage Example"
21+
```csharp
22+
--8<-- "tests/Testcontainers.EventHubs.Tests/EventHubsContainerTest.cs:UseEventHubsContainer"
23+
```
24+
25+
The test example uses the following NuGet dependencies:
26+
27+
=== "Package References"
28+
```xml
29+
--8<-- "tests/Testcontainers.EventHubs.Tests/Testcontainers.EventHubs.Tests.csproj:PackageReferences"
30+
```
31+
32+
To execute the tests, use the command `dotnet test` from a terminal.
33+
34+
--8<-- "docs/modules/_call_out_test_projects.txt"
35+
36+
## Use a custom Azurite instance
37+
38+
The Event Hubs module depends on an Azurite container instance. The module automatically creates and configures the necessary resources and connects them. If you prefer to use your own instance, you can use the following method to configure the builder accordingly:
39+
40+
=== "Reuse Existing Resources"
41+
```csharp
42+
--8<-- "tests/Testcontainers.EventHubs.Tests/EventHubsContainerTest.cs:ReuseExistingAzuriteContainer"
43+
```

docs/modules/servicebus.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Azure Service Bus
2+
3+
Azure [Service Bus](https://learn.microsoft.com/en-us/azure/service-bus-messaging/overview-emulator) emulator⁠ is designed to offer a local development experience for Azure Service Bus⁠, enabling you to develop and test code against the service in isolation, free from cloud interference.
4+
5+
Add the following dependency to your project file:
6+
7+
```shell title="NuGet"
8+
dotnet add package Testcontainers.ServiceBus
9+
```
10+
11+
You can start an Azure Service Bus container instance from any .NET application. Here, we create different container instances and pass them to the base test class. This allows us to test different configurations.
12+
13+
=== "Create Container Instance"
14+
```csharp
15+
--8<-- "tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs:CreateServiceBusContainer"
16+
```
17+
18+
This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method.
19+
20+
=== "Usage Example"
21+
```csharp
22+
--8<-- "tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs:UseServiceBusContainer"
23+
```
24+
25+
The test example uses the following NuGet dependencies:
26+
27+
=== "Package References"
28+
```xml
29+
--8<-- "tests/Testcontainers.ServiceBus.Tests/Testcontainers.ServiceBus.Tests.csproj:PackageReferences"
30+
```
31+
32+
To execute the tests, use the command `dotnet test` from a terminal.
33+
34+
--8<-- "docs/modules/_call_out_test_projects.txt"
35+
36+
## Use a custom MSSQL instance
37+
38+
The Service Bus module depends on an MSSQL container instance. The module automatically creates and configures the necessary resources and connects them. If you prefer to use your own instance, you can use the following method to configure the builder accordingly:
39+
40+
=== "Reuse Existing Resources"
41+
```csharp
42+
--8<-- "tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs:ReuseExistingMsSqlContainer"
43+
```

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ nav:
4949
- modules/index.md
5050
- modules/cassandra.md
5151
- modules/pulsar.md
52+
- modules/eventhubs.md
53+
- modules/servicebus.md
5254
- modules/db2.md
5355
- modules/elasticsearch.md
5456
- modules/mongodb.md

tests/Testcontainers.EventHubs.Tests/EventHubsContainerTest.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ private EventHubsContainerTest(EventHubsContainer eventHubsContainer)
1313
_eventHubsContainer = eventHubsContainer;
1414
}
1515

16+
// # --8<-- [start:UseEventHubsContainer]
1617
public Task InitializeAsync()
1718
{
1819
return _eventHubsContainer.StartAsync();
@@ -52,7 +53,9 @@ await client.SendAsync(eventDataBatch)
5253
// Then
5354
Assert.NotNull(properties);
5455
}
56+
// # --8<-- [end:UseEventHubsContainer]
5557

58+
// # --8<-- [start:CreateEventHubsContainer]
5659
[UsedImplicitly]
5760
public sealed class EventHubsDefaultAzuriteConfiguration : EventHubsContainerTest
5861
{
@@ -64,6 +67,7 @@ public EventHubsDefaultAzuriteConfiguration()
6467
{
6568
}
6669
}
70+
// # --8<-- [end:CreateEventHubsContainer]
6771

6872
[UsedImplicitly]
6973
public sealed class EventHubsCustomAzuriteConfiguration : EventHubsContainerTest, IClassFixture<DatabaseFixture>
@@ -72,7 +76,9 @@ public EventHubsCustomAzuriteConfiguration(DatabaseFixture fixture)
7276
: base(new EventHubsBuilder()
7377
.WithAcceptLicenseAgreement(true)
7478
.WithConfigurationBuilder(GetServiceConfiguration())
79+
// # --8<-- [start:ReuseExistingAzuriteContainer]
7580
.WithAzuriteContainer(fixture.Network, fixture.Container, DatabaseFixture.AzuriteNetworkAlias)
81+
// # --8<-- [end:ReuseExistingAzuriteContainer]
7682
.Build())
7783
{
7884
}

tests/Testcontainers.EventHubs.Tests/Testcontainers.EventHubs.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
<IsPublishable>false</IsPublishable>
66
</PropertyGroup>
77
<ItemGroup>
8+
<!-- -8<- [start:PackageReferences] -->
89
<PackageReference Include="Microsoft.NET.Test.Sdk"/>
910
<PackageReference Include="coverlet.collector"/>
1011
<PackageReference Include="xunit.runner.visualstudio"/>
1112
<PackageReference Include="xunit"/>
1213
<PackageReference Include="Azure.Messaging.EventHubs"/>
14+
<!-- -8<- [end:PackageReferences] -->
1315
</ItemGroup>
1416
<ItemGroup>
1517
<ProjectReference Include="../../src/Testcontainers.EventHubs/Testcontainers.EventHubs.csproj"/>

tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ private ServiceBusContainerTest(ServiceBusContainer serviceBusContainer)
99
_serviceBusContainer = serviceBusContainer;
1010
}
1111

12+
// # --8<-- [start:UseServiceBusContainer]
1213
public Task InitializeAsync()
1314
{
1415
return _serviceBusContainer.StartAsync();
@@ -52,7 +53,9 @@ await sender.SendMessageAsync(message)
5253
// Then
5354
Assert.Equal(helloServiceBus, receivedMessage.Body.ToString());
5455
}
56+
// # --8<-- [end:UseServiceBusContainer]
5557

58+
// # --8<-- [start:CreateServiceBusContainer]
5659
[UsedImplicitly]
5760
public sealed class ServiceBusDefaultMsSqlConfiguration : ServiceBusContainerTest
5861
{
@@ -63,14 +66,17 @@ public ServiceBusDefaultMsSqlConfiguration()
6366
{
6467
}
6568
}
69+
// # --8<-- [end:CreateServiceBusContainer]
6670

6771
[UsedImplicitly]
6872
public sealed class ServiceBusCustomMsSqlConfiguration : ServiceBusContainerTest, IClassFixture<DatabaseFixture>
6973
{
7074
public ServiceBusCustomMsSqlConfiguration(DatabaseFixture fixture)
7175
: base(new ServiceBusBuilder()
7276
.WithAcceptLicenseAgreement(true)
77+
// # --8<-- [start:ReuseExistingMsSqlContainer]
7378
.WithMsSqlContainer(fixture.Network, fixture.Container, DatabaseFixture.DatabaseNetworkAlias)
79+
// # --8<-- [end:ReuseExistingMsSqlContainer]
7480
.Build())
7581
{
7682
}

tests/Testcontainers.ServiceBus.Tests/Testcontainers.ServiceBus.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
<IsPublishable>false</IsPublishable>
66
</PropertyGroup>
77
<ItemGroup>
8+
<!-- -8<- [start:PackageReferences] -->
89
<PackageReference Include="Microsoft.NET.Test.Sdk"/>
910
<PackageReference Include="coverlet.collector"/>
1011
<PackageReference Include="xunit.runner.visualstudio"/>
1112
<PackageReference Include="xunit"/>
1213
<PackageReference Include="Azure.Messaging.ServiceBus"/>
14+
<!-- -8<- [end:PackageReferences] -->
1315
</ItemGroup>
1416
<ItemGroup>
1517
<ProjectReference Include="../../src/Testcontainers.ServiceBus/Testcontainers.ServiceBus.csproj"/>

0 commit comments

Comments
 (0)