Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xunit #12

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
39 changes: 23 additions & 16 deletions csharp/tests/Integration/GetAndSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

using System.Runtime.InteropServices;

using FluentAssertions;

using Glide;

using static Tests.Integration.IntegrationTestBase;

namespace Tests.Integration;
public class GetAndSet
public class GetAndSet : IClassFixture<IntegrationTestBase>
{
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 +28,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 +44,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 +62,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 +86,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 +105,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
54 changes: 28 additions & 26 deletions csharp/tests/Integration/IntegrationTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

using System.Diagnostics;

using Xunit.Abstractions;
using Xunit.Sdk;

// 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 +17,24 @@ internal class TestConfiguration
public static Version REDIS_VERSION { get; internal set; } = new();
}

[OneTimeSetUp]
public void SetUp()
private readonly IMessageSink _diagnosticMessageSink;

public IntegrationTestBase(IMessageSink diagnosticMessageSink)
{
_diagnosticMessageSink = diagnosticMessageSink;
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,34 +48,19 @@ 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}");
TestConsoleWriteLine($"Cluster ports = {string.Join(',', TestConfiguration.CLUSTER_PORTS)}");
TestConsoleWriteLine($"Standalone ports = {string.Join(',', TestConfiguration.STANDALONE_PORTS)}");
TestConsoleWriteLine($"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");
}
private void TestConsoleWriteLine(string message) =>
_ = _diagnosticMessageSink.OnMessage(new DiagnosticMessage(message));

internal List<uint> StartRedis(bool cluster, bool tls = false, string? name = null)
{
Expand Down Expand Up @@ -92,7 +94,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");
TestConsoleWriteLine($"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
Loading