forked from Cysharp/MessagePipe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Cysharp#108 from Cysharp/feature/SupportDIForConne…
…ctionMultiplexerFactory Support constructor injection with IConnectionMultiplexerFactory
- Loading branch information
Showing
3 changed files
with
89 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
tests/MessagePipe.Redis.Tests/ConnectionMultiplexerFactoryTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using MessagePipe.Tests; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using StackExchange.Redis; | ||
using Xunit; | ||
|
||
namespace MessagePipe.Redis.Tests; | ||
|
||
public class ConnectionMultiplexerFactoryTest | ||
{ | ||
[Fact] | ||
public async Task CreateFromInstance() | ||
{ | ||
var connection = TestHelper.GetLocalConnectionMultiplexer(); | ||
var services = new ServiceCollection(); | ||
services.AddMessagePipe(); | ||
services.AddMessagePipeRedis(connection); | ||
var sp = services.BuildServiceProvider(); | ||
|
||
var publisher = sp.GetRequiredService<IDistributedPublisher<string, string>>(); | ||
var subscriber = sp.GetRequiredService<IDistributedSubscriber<string, string>>(); | ||
|
||
var results = new List<string>(); | ||
await using var _ = await subscriber.SubscribeAsync("Foo", x => results.Add(x)); | ||
|
||
await publisher.PublishAsync("Foo", "Bar"); | ||
await Task.Delay(250); | ||
results.FirstOrDefault().Should().Be("Bar"); | ||
} | ||
|
||
[Fact] | ||
public async Task CreateFromGenericType() | ||
{ | ||
var connection = TestHelper.GetLocalConnectionMultiplexer(); | ||
var services = new ServiceCollection(); | ||
services.AddSingleton<IConnectionMultiplexer>(connection); | ||
services.AddMessagePipe(); | ||
services.AddMessagePipeRedis<TestConnectionMultiplexerFactory>(); | ||
var sp = services.BuildServiceProvider(); | ||
|
||
var publisher = sp.GetRequiredService<IDistributedPublisher<string, string>>(); | ||
var subscriber = sp.GetRequiredService<IDistributedSubscriber<string, string>>(); | ||
|
||
var results = new List<string>(); | ||
await using var _ = await subscriber.SubscribeAsync("Foo", x => results.Add(x)); | ||
|
||
await publisher.PublishAsync("Foo", "Bar"); | ||
await Task.Delay(250); | ||
results.FirstOrDefault().Should().Be("Bar"); | ||
} | ||
|
||
public class TestConnectionMultiplexerFactory : IConnectionMultiplexerFactory | ||
{ | ||
readonly IConnectionMultiplexer connectionMultiplexer; | ||
|
||
public TestConnectionMultiplexerFactory(IConnectionMultiplexer connectionMultiplexer) | ||
{ | ||
this.connectionMultiplexer = connectionMultiplexer; | ||
} | ||
|
||
public IConnectionMultiplexer GetConnectionMultiplexer<TKey>(TKey key) | ||
{ | ||
return connectionMultiplexer; | ||
} | ||
} | ||
} |