Skip to content

Commit

Permalink
changing utest framework to xunit
Browse files Browse the repository at this point in the history
  • Loading branch information
Guriy Samarin committed Apr 13, 2024
1 parent f7ceb17 commit 5b190d6
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 48 deletions.
36 changes: 21 additions & 15 deletions csharp/tests/Integration/GetAndSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Runtime.InteropServices;

using Glide;
using FluentAssertions;

using static Tests.Integration.IntegrationTestBase;

Expand All @@ -11,10 +12,12 @@ public class GetAndSet
{
private async Task GetAndSetValues(AsyncClient client, string key, string value)
{
string? setResult = await client.SetAsync(key, value);
Assert.That(setResult, Is.EqualTo("OK"));
string? result = await client.GetAsync(key);
Assert.That(result, Is.EqualTo(value));
_ = (await client.SetAsync(key, value))
.Should()
.Be("OK");
_ = (await client.GetAsync(key))
.Should()
.Be(value);
}

private async Task GetAndSetRandomValues(AsyncClient client)
Expand All @@ -24,14 +27,14 @@ private async Task GetAndSetRandomValues(AsyncClient client)
await GetAndSetValues(client, key, value);
}

[Test]
[Fact]
public async Task GetReturnsLastSet()
{
using AsyncClient client = new("localhost", TestConfiguration.STANDALONE_PORTS[0], false);
await GetAndSetRandomValues(client);
}

[Test]
[Fact]
public async Task GetAndSetCanHandleNonASCIIUnicode()
{
using AsyncClient client = new("localhost", TestConfiguration.STANDALONE_PORTS[0], false);
Expand All @@ -40,15 +43,16 @@ public async Task GetAndSetCanHandleNonASCIIUnicode()
await GetAndSetValues(client, key, value);
}

[Test]
[Fact]
public async Task GetReturnsNull()
{
using AsyncClient client = new("localhost", TestConfiguration.STANDALONE_PORTS[0], false);
string? result = await client.GetAsync(Guid.NewGuid().ToString());
Assert.That(result, Is.EqualTo(null));
_ = (await client.GetAsync(Guid.NewGuid().ToString()))
.Should()
.BeNull();
}

[Test]
[Fact]
public async Task GetReturnsEmptyString()
{
using AsyncClient client = new("localhost", TestConfiguration.STANDALONE_PORTS[0], false);
Expand All @@ -57,13 +61,14 @@ public async Task GetReturnsEmptyString()
await GetAndSetValues(client, key, value);
}

[Test]
[Fact]
public async Task HandleVeryLargeInput()
{
// TODO invesitage and fix
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
Assert.Ignore("Flaky on MacOS");
//"Flaky on MacOS"
return;
}
using AsyncClient client = new("localhost", TestConfiguration.STANDALONE_PORTS[0], false);

Expand All @@ -80,7 +85,7 @@ public async Task HandleVeryLargeInput()

// This test is slow and hardly a unit test, but it caught timing and releasing issues in the past,
// so it's being kept.
[Test]
[Fact]
public void ConcurrentOperationsWork()
{
using AsyncClient client = new("localhost", TestConfiguration.STANDALONE_PORTS[0], false);
Expand All @@ -99,8 +104,9 @@ public void ConcurrentOperationsWork()
}
else
{
string? result = await client.GetAsync(Guid.NewGuid().ToString());
Assert.That(result, Is.EqualTo(null));
_ = (await client.GetAsync(Guid.NewGuid().ToString()))
.Should()
.BeNull();
}
}
}));
Expand Down
53 changes: 26 additions & 27 deletions csharp/tests/Integration/IntegrationTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

using System.Diagnostics;

using Xunit.Abstractions;

// Note: All IT should be in the same namespace
namespace Tests.Integration;

[SetUpFixture]
public class IntegrationTestBase
public class IntegrationTestBase : IDisposable
{
internal class TestConfiguration
{
Expand All @@ -15,9 +16,25 @@ internal class TestConfiguration
public static Version REDIS_VERSION { get; internal set; } = new();
}

[OneTimeSetUp]
public void SetUp()
private readonly ITestOutputHelper _output;

public IntegrationTestBase(ITestOutputHelper output)
{
_output = output;

string? projectDir = Directory.GetCurrentDirectory();
while (!(Path.GetFileName(projectDir) == "csharp" || projectDir == null))
{
projectDir = Path.GetDirectoryName(projectDir);
}

if (projectDir == null)
{
throw new FileNotFoundException("Can't detect the project dir. Are you running tests from `csharp` directory?");
}

_scriptDir = Path.Combine(projectDir, "..", "utils");

// Stop all if weren't stopped on previous test run
StopRedis(false);

Expand All @@ -31,35 +48,17 @@ public void SetUp()
// Get redis version
TestConfiguration.REDIS_VERSION = GetRedisVersion();

TestContext.Progress.WriteLine($"Cluster ports = {string.Join(',', TestConfiguration.CLUSTER_PORTS)}");
TestContext.Progress.WriteLine($"Standalone ports = {string.Join(',', TestConfiguration.STANDALONE_PORTS)}");
TestContext.Progress.WriteLine($"Redis version = {TestConfiguration.REDIS_VERSION}");
_output.WriteLine($"Cluster ports = {string.Join(',', TestConfiguration.CLUSTER_PORTS)}");
_output.WriteLine($"Standalone ports = {string.Join(',', TestConfiguration.STANDALONE_PORTS)}");
_output.WriteLine($"Redis version = {TestConfiguration.REDIS_VERSION}");
}

[OneTimeTearDown]
public void TearDown() =>
public void Dispose() =>
// Stop all
StopRedis(true);

private readonly string _scriptDir;

// Nunit requires a public default constructor. These variables would be set in SetUp method.
public IntegrationTestBase()
{
string? projectDir = Directory.GetCurrentDirectory();
while (!(Path.GetFileName(projectDir) == "csharp" || projectDir == null))
{
projectDir = Path.GetDirectoryName(projectDir);
}

if (projectDir == null)
{
throw new FileNotFoundException("Can't detect the project dir. Are you running tests from `csharp` directory?");
}

_scriptDir = Path.Combine(projectDir, "..", "utils");
}

internal List<uint> StartRedis(bool cluster, bool tls = false, string? name = null)
{
string cmd = $"start {(cluster ? "--cluster-mode" : "-r 0")} {(tls ? " --tls" : "")} {(name != null ? " --prefix " + name : "")}";
Expand Down Expand Up @@ -92,7 +91,7 @@ private string RunClusterManager(string cmd, bool ignoreExitCode)
string? output = script?.StandardOutput.ReadToEnd();
int? exit_code = script?.ExitCode;

TestContext.Progress.WriteLine($"cluster_manager.py stdout\n====\n{output}\n====\ncluster_manager.py stderr\n====\n{error}\n====\n");
_output.WriteLine($"cluster_manager.py stdout\n====\n{output}\n====\ncluster_manager.py stderr\n====\n{error}\n====\n");

return !ignoreExitCode && exit_code != 0
? throw new ApplicationException($"cluster_manager.py script failed: exit code {exit_code}.")
Expand Down
2 changes: 1 addition & 1 deletion csharp/tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0

global using NUnit.Framework;
global using Xunit;
18 changes: 13 additions & 5 deletions csharp/tests/tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand All @@ -16,11 +18,17 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 5b190d6

Please sign in to comment.