Skip to content

Commit

Permalink
Add Redis DB Fixture (#16)
Browse files Browse the repository at this point in the history
* make redis db fixture public + tests

* public redis extensions now use interface
  • Loading branch information
Confusingboat authored Mar 27, 2024
1 parent 3a08815 commit 9c6327a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public EphemeralRedisMultiplexerFixture() { }
protected EphemeralRedisMultiplexerFixture(IRedisInstanceFixture redisInstanceFixture)
: base(redisInstanceFixture) { }

public IEphemeralRedisDatabase GetDatabase(int db = -1) => Multiplexer.GetDatabase() as IEphemeralRedisDatabase;

protected override async Task<IConnectionMultiplexer> CreateMultiplexerAsync()
{
var implementation = await base.CreateMultiplexerAsync();
Expand Down
14 changes: 12 additions & 2 deletions src/Ephemerally.Redis.Xunit/PooledEphemeralRedisDatabaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

namespace Ephemerally.Redis.Xunit;

internal class PooledEphemeralRedisDatabaseFixture : PooledEphemeralRedisMultiplexerFixture
public class PooledEphemeralRedisDatabaseFixture<TRedisInstance>()
: PooledEphemeralRedisDatabaseFixture(new TRedisInstance())
where TRedisInstance : IRedisInstanceFixture, new();

public class PooledEphemeralRedisDatabaseFixture : PooledEphemeralRedisMultiplexerFixture
{
private readonly Lazy<Task<IEphemeralRedisDatabase>> _database;

Expand All @@ -13,7 +17,13 @@ internal class PooledEphemeralRedisDatabaseFixture : PooledEphemeralRedisMultipl
public PooledEphemeralRedisDatabaseFixture()
{
_database = new(CreateDatabaseAsync);
}
}

protected PooledEphemeralRedisDatabaseFixture(IRedisInstanceFixture redisInstanceFixture)
: base(redisInstanceFixture)
{
_database = new(CreateDatabaseAsync);
}

protected virtual async Task<IEphemeralRedisDatabase> CreateDatabaseAsync()
{
Expand Down
17 changes: 9 additions & 8 deletions src/Ephemerally.Redis/PublicExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ namespace Ephemerally;

public static class PublicExtensions
{
public static EphemeralRedisDatabase AsEphemeral(this IDatabase database) =>
database is null or EphemeralRedisDatabase
? (EphemeralRedisDatabase)database
public static IEphemeralRedisDatabase AsEphemeral(this IDatabase database) =>
database is null or IEphemeralRedisDatabase
? (IEphemeralRedisDatabase)database
: database.ToEphemeral();

public static EphemeralRedisDatabase ToEphemeral(this IDatabase database) =>
new(new RedisDatabaseEphemeral(database));
public static IEphemeralRedisDatabase ToEphemeral(this IDatabase database) =>
new EphemeralRedisDatabase(new RedisDatabaseEphemeral(database));

public static EphemeralRedisDatabase GetEphemeralDatabase(
public static IEphemeralRedisDatabase GetEphemeralDatabase(
this IConnectionMultiplexer multiplexer,
int db = -1) =>
multiplexer.GetDatabase(db).AsEphemeral();
int db = -1,
object asyncState = null) =>
multiplexer.GetDatabase(db, asyncState).AsEphemeral();

#region EphemeralConnectionMultiplexer

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Ephemerally.Redis.Xunit;
using Shouldly;

namespace Ephemerally.Redis.Tests.Fixtures;

public class PooledEphemeralRedisDatabaseFixtureTests6 : PooledEphemeralRedisDatabaseFixtureTests<RedisInstanceFixture6>;

public class PooledEphemeralRedisDatabaseFixtureTests7 : PooledEphemeralRedisDatabaseFixtureTests<RedisInstanceFixture7>;

[Collection(RedisTestCollection.Name)]
public abstract class PooledEphemeralRedisDatabaseFixtureTests<TRedisFixture> : IAsyncLifetime
where TRedisFixture : class, IRedisInstanceFixture, new()
{
// Arrange
private readonly PooledEphemeralRedisDatabaseFixture<TRedisFixture> _fixture = new();

[RedisFact]
public async Task Fixture_database_should_not_be_null()

Check warning on line 18 in tests/Ephemerally.Redis.Tests/Fixtures/PooledEphemeralRedisDatabaseFixtureTests.cs

View workflow job for this annotation

GitHub Actions / build_and_test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 18 in tests/Ephemerally.Redis.Tests/Fixtures/PooledEphemeralRedisDatabaseFixtureTests.cs

View workflow job for this annotation

GitHub Actions / build_and_test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 18 in tests/Ephemerally.Redis.Tests/Fixtures/PooledEphemeralRedisDatabaseFixtureTests.cs

View workflow job for this annotation

GitHub Actions / build_and_test_cosmos

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 18 in tests/Ephemerally.Redis.Tests/Fixtures/PooledEphemeralRedisDatabaseFixtureTests.cs

View workflow job for this annotation

GitHub Actions / build_and_test_cosmos

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 18 in tests/Ephemerally.Redis.Tests/Fixtures/PooledEphemeralRedisDatabaseFixtureTests.cs

View workflow job for this annotation

GitHub Actions / build_and_test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 18 in tests/Ephemerally.Redis.Tests/Fixtures/PooledEphemeralRedisDatabaseFixtureTests.cs

View workflow job for this annotation

GitHub Actions / build_and_test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 18 in tests/Ephemerally.Redis.Tests/Fixtures/PooledEphemeralRedisDatabaseFixtureTests.cs

View workflow job for this annotation

GitHub Actions / build_and_test_cosmos

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 18 in tests/Ephemerally.Redis.Tests/Fixtures/PooledEphemeralRedisDatabaseFixtureTests.cs

View workflow job for this annotation

GitHub Actions / build_and_test_cosmos

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
// Act
var database = _fixture.Database;

// Assert
database.ShouldNotBeNull();
}

#region IAsyncLifetime Members

public Task InitializeAsync() => _fixture.InitializeAsync();

public Task DisposeAsync() => _fixture.DisposeAsync();

#endregion
}

0 comments on commit 9c6327a

Please sign in to comment.