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

Add simpler Redis DB fixture #18

Merged
merged 5 commits into from
Mar 29, 2024
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
--results-directory ${{ env.ArtifactDirectory }}

- name: Upload Test Results
if: ${{ github.event_name == 'pull_request' || github.ref == 'refs/heads/main' }}
if: ${{ always() && github.event_name == 'pull_request' || github.ref == 'refs/heads/main' }}
uses: actions/upload-artifact@v4
with:
name: test-results
Expand Down Expand Up @@ -125,7 +125,7 @@ jobs:
--results-directory ${{ env.ArtifactDirectory }}

- name: Upload Test Results
if: ${{ github.event_name == 'pull_request' || github.ref == 'refs/heads/main' }}
if: ${{ always() && github.event_name == 'pull_request' || github.ref == 'refs/heads/main' }}
uses: actions/upload-artifact@v4
with:
name: cosmos-test-results
Expand Down
43 changes: 43 additions & 0 deletions src/Ephemerally.Redis.Xunit/RedisDatabaseFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using StackExchange.Redis;
using Xunit;

namespace Ephemerally.Redis.Xunit;

public class RedisDatabaseFixture<TMultiplexerFixture>(TMultiplexerFixture multiplexerFixture)
: RedisDatabaseFixture(multiplexerFixture.Multiplexer)
where TMultiplexerFixture : IRedisMultiplexerFixture;

public class RedisDatabaseFixture : IAsyncLifetime
{
private bool _disposed;

private readonly IConnectionMultiplexer _multiplexer;

private readonly Lazy<Task<IEphemeralRedisDatabase>> _database;

public IEphemeralRedisDatabase Database => _database.Value.Result;

public RedisDatabaseFixture()
{
_database = new(CreateDatabaseAsync);
}

protected RedisDatabaseFixture(IConnectionMultiplexer multiplexer) : this()
{
_multiplexer = multiplexer;
}

protected virtual Task<IEphemeralRedisDatabase> CreateDatabaseAsync() =>
Task.FromResult(_multiplexer.GetEphemeralDatabase());

public async Task InitializeAsync() => await _database.Value;

public async Task DisposeAsync()
{
if (_disposed || !_database.IsValueCreated) return;

_disposed = true;

await Database.DisposeAsync();
}
}
7 changes: 6 additions & 1 deletion src/Ephemerally.Redis.Xunit/RedisMultiplexerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@

namespace Ephemerally.Redis.Xunit;

public interface IRedisMultiplexerFixture
{
IConnectionMultiplexer Multiplexer { get; }
}

public class RedisMultiplexerFixture<TEphemeralRedisInstance>()
: RedisMultiplexerFixture(new TEphemeralRedisInstance())
where TEphemeralRedisInstance : IRedisInstanceFixture, new();

public class RedisMultiplexerFixture : IAsyncLifetime, IAsyncDisposable
public class RedisMultiplexerFixture : IRedisMultiplexerFixture, IAsyncLifetime, IAsyncDisposable
{
private bool _disposed;

Expand Down
11 changes: 9 additions & 2 deletions tests/Ephemerally.Redis.Tests/FixedSizeObjectPoolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

namespace Ephemerally.Redis.Tests;

[Collection(FixedSizeObjectPoolTestCollection.Name)]
public class FixedSizeObjectPoolTests
{
private const int TestTimeout = 2;
private const int TestTimeout = 5;

[Fact]
[Fact(Timeout = TestTimeout)]
public void Get_returns_object_from_pool()
{
// Arrange
Expand Down Expand Up @@ -162,4 +163,10 @@ public async Task GetWhere_should_wait_for_first_available_matching_object()
actual.ShouldBe(2);
await task1;
}
}

[CollectionDefinition(Name, DisableParallelization = true)]
public class FixedSizeObjectPoolTestCollection
{
public const string Name = nameof(FixedSizeObjectPoolTestCollection);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Ephemerally.Redis.Xunit;
using Shouldly;

namespace Ephemerally.Redis.Tests.Fixtures;

[Collection(RedisDatabaseFixtureTestCollection.Name)]
public class RedisDatabaseFixtureTests(RedisDatabaseFixture<RedisDatabaseFixtureTestCollectionFixture> databaseFixture)
: IClassFixture<RedisDatabaseFixture<RedisDatabaseFixtureTestCollectionFixture>>
{
[Fact]
public void Database_should_not_be_null()
{
databaseFixture.Database.ShouldNotBeNull();
}
}

[CollectionDefinition(Name)]
public class RedisDatabaseFixtureTestCollection
: ICollectionFixture<RedisDatabaseFixtureTestCollectionFixture>
{
public const string Name = nameof(RedisDatabaseFixtureTestCollection);
}

public class RedisDatabaseFixtureTestCollectionFixture
: PooledEphemeralRedisMultiplexerFixture<RedisInstanceFixture7>;
Loading