Skip to content

Commit

Permalink
Add PulsarService and functionality to PulsarBuilder
Browse files Browse the repository at this point in the history
A new file, PulsarService.cs, has been added, with a struct named PulsarService that allows the creation of different Pulsar services. Changes have also been made to PulsarBuilder.cs, mainly to incorporate the use of PulsarService. Rather than using a constant value, `WithAuthentication()` and `WithFunctionsWorker(bool functionsWorkerEnabled = true)` now use elements from the PulsarService struct. Wait strategy has been updated to better account for various enabled services.
  • Loading branch information
David Jensen committed Apr 12, 2024
1 parent c4402be commit c52101c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 15 deletions.
47 changes: 32 additions & 15 deletions src/Testcontainers.Pulsar/PulsarBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Linq;

namespace Testcontainers.Pulsar;

/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" />
Expand All @@ -17,6 +19,8 @@ public sealed class PulsarBuilder : ContainerBuilder<PulsarBuilder, PulsarContai
public const string Username = "test-user";

private static readonly IReadOnlyDictionary<string, string> AuthenticationEnvVars;

private static readonly ISet<PulsarService> EnabledServices = new HashSet<PulsarService>();

static PulsarBuilder()
{
Expand Down Expand Up @@ -58,22 +62,27 @@ private PulsarBuilder(PulsarConfiguration resourceConfiguration)
protected override PulsarConfiguration DockerResourceConfiguration { get; }

/// <summary>
///
/// Enables authentication in the Pulsar container.
/// </summary>
/// <returns></returns>
/// <returns>A <see cref="PulsarBuilder"/> instance.</returns>
public PulsarBuilder WithAuthentication()
{
EnabledServices.Add(PulsarService.Authentication);

return Merge(DockerResourceConfiguration, new PulsarConfiguration(authenticationEnabled: true))
.WithEnvironment(AuthenticationEnvVars);
}

/// <summary>
///
/// Initializes a new instance of the <see cref="WithFunctionsWorkerWorker" /> class.
/// </summary>
/// <returns></returns>
/// <param name="functionsWorkerEnabled">Determines if the functions worker is enabled.</param>
/// <returns>A new instance of the <see cref="PulsarBuilder" /> class.</returns>
public PulsarBuilder WithFunctionsWorker(bool functionsWorkerEnabled = true)
{
// TODO: When enabled we need to adjust the wait strategy.
if (functionsWorkerEnabled)
EnabledServices.Add(PulsarService.FunctionWorker);

return Merge(DockerResourceConfiguration, new PulsarConfiguration(functionsWorkerEnabled: functionsWorkerEnabled));
}

Expand All @@ -84,18 +93,26 @@ public override PulsarContainer Build()

var waitStrategy = Wait.ForUnixContainer();

//TODO We need to switch between the default and custom WaitStrategy depending on if the user used WithAuthentication.
//Would you prefer we handled it in a similar to Couchbase?
waitStrategy = waitStrategy.UntilHttpRequestIsSucceeded(request
=> request
.ForPath("/admin/v2/clusters")
.ForPort(PulsarWebServicePort)
.ForResponseMessageMatching(VerifyPulsarStatusAsync));

waitStrategy.AddCustomWaitStrategy(new WaitUntil());
if (EnabledServices.Contains(PulsarService.Authentication))
{
waitStrategy.AddCustomWaitStrategy(new WaitUntil());
}
else
{
waitStrategy = waitStrategy.UntilHttpRequestIsSucceeded(request
=> request
.ForPath("/admin/v2/clusters")
.ForPort(PulsarWebServicePort)
.ForResponseMessageMatching(VerifyPulsarStatusAsync));
}

var pulsarBuilder = WithWaitStrategy(waitStrategy);
//TODO Can we have more then one wait strategy? So it works if a user run WithAuthentication and WithFunctionsWorker?
if (EnabledServices.Contains(PulsarService.FunctionWorker))
{
waitStrategy.UntilMessageIsLogged(".*Function worker service started.*");
}

var pulsarBuilder = WithWaitStrategy(waitStrategy);
return new PulsarContainer(pulsarBuilder.DockerResourceConfiguration);
}

Expand Down
30 changes: 30 additions & 0 deletions src/Testcontainers.Pulsar/PulsarService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace Testcontainers.Pulsar;

internal readonly struct PulsarService
{
/// <summary>
/// Initializes a new instance of the <see cref="PulsarService" /> struct.
/// </summary>
/// <param name="identifier">The identifier.</param>
private PulsarService(string identifier)
{
Identifier = identifier;
}

/// <summary>
/// Gets the Auth.
/// </summary>
public static readonly PulsarService Authentication = new PulsarService("auth");

/// <summary>
/// Gets the Function worker service.
/// </summary>
public static readonly PulsarService FunctionWorker = new PulsarService("funk");

/// <summary>
/// Gets the identifier.
/// </summary>
public string Identifier { get; }
}


0 comments on commit c52101c

Please sign in to comment.