Skip to content

Commit 5f74a52

Browse files
committed
test: Refactor unit tests for ConfigurationOptions and ConnectionMultiplexer
- Updated exception message assertion in ConfigurationOptionsReadFromTests to be case insensitive. - Replaced reflection-based method invocation with direct calls to CreateClientConfigBuilder in ConnectionMultiplexerReadFromMappingTests for better readability and performance. - Added new tests to verify ReadFrom configuration flows correctly to ConnectionConfig for both standalone and cluster configurations. - Removed unnecessary reflection helper method to streamline the test code.
1 parent c69bd39 commit 5f74a52

File tree

6 files changed

+611
-998
lines changed

6 files changed

+611
-998
lines changed

sources/Valkey.Glide/Abstract/ConfigurationOptions.cs

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -538,18 +538,26 @@ private void ValidateAndSetReadFrom()
538538
switch (strategy)
539539
{
540540
case ReadFromStrategy.AzAffinity:
541-
if (string.IsNullOrWhiteSpace(tempAz))
541+
if (tempAz == null)
542542
{
543543
throw new ArgumentException("Availability zone should be set when using AzAffinity strategy");
544544
}
545+
if (string.IsNullOrWhiteSpace(tempAz))
546+
{
547+
throw new ArgumentException("Availability zone cannot be empty or whitespace when using AzAffinity strategy");
548+
}
545549
readFrom = new ReadFrom(strategy, tempAz);
546550
break;
547551

548552
case ReadFromStrategy.AzAffinityReplicasAndPrimary:
549-
if (string.IsNullOrWhiteSpace(tempAz))
553+
if (tempAz == null)
550554
{
551555
throw new ArgumentException("Availability zone should be set when using AzAffinityReplicasAndPrimary strategy");
552556
}
557+
if (string.IsNullOrWhiteSpace(tempAz))
558+
{
559+
throw new ArgumentException("Availability zone cannot be empty or whitespace when using AzAffinityReplicasAndPrimary strategy");
560+
}
553561
readFrom = new ReadFrom(strategy, tempAz);
554562
break;
555563

@@ -570,7 +578,7 @@ private void ValidateAndSetReadFrom()
570578
break;
571579

572580
default:
573-
throw new ArgumentOutOfRangeException(nameof(tempReadFromStrategy), $"ReadFrom strategy '{strategy}' is not supported");
581+
throw new ArgumentException($"ReadFrom strategy '{strategy}' is not supported. Valid strategies are: Primary, PreferReplica, AzAffinity, AzAffinityReplicasAndPrimary");
574582
}
575583
}
576584
}
@@ -582,14 +590,14 @@ private static ReadFromStrategy ParseReadFromStrategy(string key, string value)
582590
throw new ArgumentException($"Keyword '{key}' requires a ReadFrom strategy value; the value cannot be empty", key);
583591
}
584592

585-
return value.ToLowerInvariant() switch
593+
try
586594
{
587-
"primary" => ReadFromStrategy.Primary,
588-
"preferreplica" => ReadFromStrategy.PreferReplica,
589-
"azaffinity" => ReadFromStrategy.AzAffinity,
590-
"azaffinityreplicasandprimary" => ReadFromStrategy.AzAffinityReplicasAndPrimary,
591-
_ => throw new ArgumentException($"ReadFrom strategy '{value}' is not supported. Supported values are: Primary, PreferReplica, AzAffinity, AzAffinityReplicasAndPrimary", key)
592-
};
595+
return Enum.Parse<ReadFromStrategy>(value, ignoreCase: true);
596+
}
597+
catch (ArgumentException)
598+
{
599+
throw new ArgumentException($"ReadFrom strategy '{value}' is not supported. Valid strategies are: Primary, PreferReplica, AzAffinity, AzAffinityReplicasAndPrimary", key);
600+
}
593601
}
594602

595603
private static void ValidateReadFromConfiguration(ReadFrom readFromConfig)
@@ -644,30 +652,13 @@ private static void ValidateReadFromConfiguration(ReadFrom readFromConfig)
644652
/// <param name="readFromConfig">The ReadFrom configuration to format.</param>
645653
private static void FormatReadFrom(StringBuilder sb, ReadFrom readFromConfig)
646654
{
647-
Append(sb, OptionKeys.ReadFrom, FormatReadFromStrategy(readFromConfig.Strategy));
655+
Append(sb, OptionKeys.ReadFrom, readFromConfig.Strategy.ToString());
648656
if (!string.IsNullOrWhiteSpace(readFromConfig.Az))
649657
{
650658
Append(sb, OptionKeys.Az, readFromConfig.Az);
651659
}
652660
}
653661

654-
/// <summary>
655-
/// Converts a ReadFromStrategy enum value to its string representation.
656-
/// </summary>
657-
/// <param name="strategy">The ReadFromStrategy to format.</param>
658-
/// <returns>The string representation of the strategy.</returns>
659-
private static string FormatReadFromStrategy(ReadFromStrategy strategy)
660-
{
661-
return strategy switch
662-
{
663-
ReadFromStrategy.Primary => "Primary",
664-
ReadFromStrategy.PreferReplica => "PreferReplica",
665-
ReadFromStrategy.AzAffinity => "AzAffinity",
666-
ReadFromStrategy.AzAffinityReplicasAndPrimary => "AzAffinityReplicasAndPrimary",
667-
_ => strategy.ToString(),
668-
};
669-
}
670-
671662
/// <summary>
672663
/// Specify the connection protocol type.
673664
/// </summary>

sources/Valkey.Glide/Abstract/ConnectionMultiplexer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ private ConnectionMultiplexer(ConfigurationOptions configuration, Database db)
159159
_db = db;
160160
}
161161

162-
private static T CreateClientConfigBuilder<T>(ConfigurationOptions configuration)
162+
internal static T CreateClientConfigBuilder<T>(ConfigurationOptions configuration)
163163
where T : ClientConfigurationBuilder<T>, new()
164164
{
165165
T config = new();

0 commit comments

Comments
 (0)