Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/Aspire.Hosting.Valkey/ValkeyBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public static IResourceBuilder<ValkeyResource> AddValkey(this IDistributedApplic
string name,
int? port = null)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(name);

var valkey = new ValkeyResource(name);
return builder.AddResource(valkey)
.WithEndpoint(port: port, targetPort: 6379, name: ValkeyResource.PrimaryEndpointName)
Expand Down Expand Up @@ -80,6 +83,8 @@ public static IResourceBuilder<ValkeyResource> AddValkey(this IDistributedApplic
public static IResourceBuilder<ValkeyResource> WithDataVolume(this IResourceBuilder<ValkeyResource> builder,
string? name = null, bool isReadOnly = false)
{
ArgumentNullException.ThrowIfNull(builder);

builder.WithVolume(name ?? VolumeNameGenerator.CreateVolumeName(builder, "data"), ValkeyContainerDataDirectory,
isReadOnly);
if (!isReadOnly)
Expand Down Expand Up @@ -111,6 +116,9 @@ public static IResourceBuilder<ValkeyResource> WithDataVolume(this IResourceBuil
public static IResourceBuilder<ValkeyResource> WithDataBindMount(this IResourceBuilder<ValkeyResource> builder,
string source, bool isReadOnly = false)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(source);

builder.WithBindMount(source, ValkeyContainerDataDirectory, isReadOnly);
if (!isReadOnly)
{
Expand Down Expand Up @@ -138,11 +146,16 @@ public static IResourceBuilder<ValkeyResource> WithDataBindMount(this IResourceB
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<ValkeyResource> WithPersistence(this IResourceBuilder<ValkeyResource> builder,
TimeSpan? interval = null, long keysChangedThreshold = 1)
=> builder.WithAnnotation(new CommandLineArgsCallbackAnnotation(context =>
{
ArgumentNullException.ThrowIfNull(builder);

return builder.WithAnnotation(new CommandLineArgsCallbackAnnotation(context =>
{
context.Args.Add("--save");
context.Args.Add((interval ?? TimeSpan.FromSeconds(60)).TotalSeconds.ToString(CultureInfo.InvariantCulture));
context.Args.Add(
(interval ?? TimeSpan.FromSeconds(60)).TotalSeconds.ToString(CultureInfo.InvariantCulture));
context.Args.Add(keysChangedThreshold.ToString(CultureInfo.InvariantCulture));
return Task.CompletedTask;
}), ResourceAnnotationMutationBehavior.Replace);
}
}
123 changes: 123 additions & 0 deletions tests/Aspire.Hosting.Valkey.Tests/ValkeyPublicApiTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Aspire.Hosting.ApplicationModel;
using Xunit;

namespace Aspire.Hosting.Valkey.Tests;

public class ValkeyPublicApiTests
{
#region ValkeyBuilderExtensions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove Addrees pls.


[Fact]
public void AddValkeyContainerShouldThrowsWhenBuilderIsNull()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: ShouldThrows -> ShouldThrow

{
IDistributedApplicationBuilder builder = null!;
const string name = "Valkey";

var action = () => builder.AddValkey(name);

Assert.Multiple(() =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on this comment. Assert.Multyple is not necessary.

You should just leave it here:
var exception = Assert.Throws(action);
Assert.Equal(nameof(builder), exception.ParamName);

Same for the code below

{
var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(builder), exception.ParamName);
});
}

[Fact]
public void AddValkeyContainerShouldThrowsWhenNameIsNull()
{
IDistributedApplicationBuilder builder = new DistributedApplicationBuilder([]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use TestDistributedApplicationBuilder.Create();

string name = null!;

var action = () => builder.AddValkey(name);

Assert.Multiple(() =>
{
var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(name), exception.ParamName);
});
}

[Fact]
public void WithDataVolumeShouldThrowsWhenBuilderIsNull()
{
IResourceBuilder<ValkeyResource> builder = null!;

var action = () => builder.WithDataVolume();

Assert.Multiple(() =>
{
var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(builder), exception.ParamName);
});
}

[Fact]
public void WithDataBindMountShouldThrowsWhenBuilderIsNull()
{
IResourceBuilder<ValkeyResource> builder = null!;
const string source = "Valkey";

var action = () => builder.WithDataBindMount(source);

Assert.Multiple(() =>
{
var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(builder), exception.ParamName);
});
}

[Fact]
public void WithDataBindMountShouldThrowsWhenSourceIsNull()
{
var distributedApplicationBuilder = new DistributedApplicationBuilder([]);
const string name = "Valkey";
var resource = new ValkeyResource(name);
var builder = distributedApplicationBuilder.AddResource(resource);
string source = null!;

var action = () => builder.WithDataBindMount(source);

Assert.Multiple(() =>
{
var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(source), exception.ParamName);
});
}

[Fact]
public void WithPersistenceShouldThrowsWhenBuilderIsNull()
{
IResourceBuilder<ValkeyResource> builder = null!;

var action = () => builder.WithPersistence();

Assert.Multiple(() =>
{
var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(builder), exception.ParamName);
});
}

#endregion

#region ValkeyResource

[Fact]
public void CtorValkeyResourceShouldThrowsWhenNameIsNull()
{
string name = null!;

var action = () => new ValkeyResource(name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a static check in the class
private static string ThrowIfNull([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
=> argument ?? throw new ArgumentNullException(paramName);


Assert.Multiple(() =>
{
var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(name), exception.ParamName);
});
}

#endregion
}