|
| 1 | +using HotChocolate.Execution; |
| 2 | +using HotChocolate.Execution.Configuration; |
| 3 | +using Microsoft.Extensions.DependencyInjection; |
| 4 | +using Squadron; |
| 5 | +using StackExchange.Redis; |
| 6 | +using Xunit.Abstractions; |
| 7 | + |
| 8 | +namespace HotChocolate.Subscriptions.Redis; |
| 9 | + |
| 10 | +public class RedisTopicPrefixIntegrationTests(RedisResource redisResource, ITestOutputHelper output) |
| 11 | + : SubscriptionIntegrationTestBase(output), IClassFixture<RedisResource> |
| 12 | +{ |
| 13 | + private const string TopicPrefix = "prefix:"; |
| 14 | + |
| 15 | + [Fact] |
| 16 | + public override Task Subscribe_Infer_Topic() |
| 17 | + => base.Subscribe_Infer_Topic(); |
| 18 | + |
| 19 | + [Fact] |
| 20 | + public override Task Subscribe_Static_Topic() |
| 21 | + => base.Subscribe_Static_Topic(); |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public override Task Subscribe_Topic_With_Arguments() |
| 25 | + => base.Subscribe_Topic_With_Arguments(); |
| 26 | + |
| 27 | + [Fact] |
| 28 | + public override Task Subscribe_Topic_With_Arguments_2_Subscriber() |
| 29 | + => base.Subscribe_Topic_With_Arguments_2_Subscriber(); |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public override Task Subscribe_Topic_With_Arguments_2_Topics() |
| 33 | + => base.Subscribe_Topic_With_Arguments_2_Topics(); |
| 34 | + |
| 35 | + [Fact] |
| 36 | + public override Task Subscribe_Topic_With_2_Arguments() |
| 37 | + => base.Subscribe_Topic_With_2_Arguments(); |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public override Task Subscribe_And_Complete_Topic() |
| 41 | + => base.Subscribe_And_Complete_Topic(); |
| 42 | + |
| 43 | + [Fact] |
| 44 | + public override Task Subscribe_And_Complete_Topic_With_ValueTypeMessage() |
| 45 | + => base.Subscribe_And_Complete_Topic_With_ValueTypeMessage(); |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public async Task Subscribe_Should_Create_Channel_With_Prefix() |
| 49 | + { |
| 50 | + using var cts = new CancellationTokenSource(Timeout); |
| 51 | + await using var services = CreateServer<Subscription>(); |
| 52 | + |
| 53 | + await using var result = await services.ExecuteRequestAsync( |
| 54 | + "subscription { onMessage }", |
| 55 | + cancellationToken: cts.Token); |
| 56 | + |
| 57 | + var activeChannels = await GetActiveChannelsAsync(); |
| 58 | + |
| 59 | + Assert.Contains(activeChannels, channel => channel.ToString()!.StartsWith(TopicPrefix)); |
| 60 | + } |
| 61 | + |
| 62 | + private async Task<RedisResult[]> GetActiveChannelsAsync() |
| 63 | + { |
| 64 | + return (RedisResult[])(await redisResource.GetConnection().GetDatabase().ExecuteAsync("PUBSUB", "CHANNELS"))!; |
| 65 | + } |
| 66 | + |
| 67 | + protected override void ConfigurePubSub(IRequestExecutorBuilder graphqlBuilder) |
| 68 | + => graphqlBuilder.AddRedisSubscriptions(_ => redisResource.GetConnection(), new SubscriptionOptions { TopicPrefix = TopicPrefix }); |
| 69 | +} |
0 commit comments