Skip to content

Commit

Permalink
Add simpler Redis DB fixture (#18)
Browse files Browse the repository at this point in the history
* feat: add redis db fixture

* test: add test for redis database fixture

* chore: ci changes

* chore: adjust FixedSizeObjectPool test timeouts

* chore: disable parallelization for FixedSizeObjectPool tests
  • Loading branch information
Confusingboat authored Mar 29, 2024
1 parent 9c6327a commit 998bb21
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 5 deletions.
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>;

0 comments on commit 998bb21

Please sign in to comment.