|
| 1 | +namespace SlimMessageBus.Host.GooglePubSub.Test; |
| 2 | + |
| 3 | +[Trait("Category", "Integration")] |
| 4 | +[Trait("Transport", "GooglePubSub")] |
| 5 | +[Collection(nameof(GooglePubSubCollection))] |
| 6 | +public class GooglePubSubMessageBusIt(ITestOutputHelper output, GooglePubSubFixture fixture) |
| 7 | + : BaseIntegrationTest<GooglePubSubMessageBusIt>(output) |
| 8 | +{ |
| 9 | + private readonly string _topic = $"orders-{Guid.NewGuid():N}"; |
| 10 | + private readonly string _subscription = $"orders-worker-{Guid.NewGuid():N}"; |
| 11 | + |
| 12 | + protected override void SetupServices(ServiceCollection services, IConfigurationRoot configuration) |
| 13 | + { |
| 14 | + services.AddSlimMessageBus(mbb => |
| 15 | + { |
| 16 | + mbb.WithProviderGooglePubSub(settings => |
| 17 | + { |
| 18 | + settings.ProjectId = "slim-message-bus-tests"; |
| 19 | + ConfigureEmulatorClients(settings, fixture.EmulatorEndpoint); |
| 20 | + }); |
| 21 | + mbb.Produce<OrderSubmitted>(x => x.DefaultTopic(_topic)); |
| 22 | + mbb.Consume<OrderSubmitted>(x => x.Topic(_topic).SubscriptionName(_subscription)); |
| 23 | + mbb.AddServicesFromAssemblyContaining<OrderSubmittedConsumer>(); |
| 24 | + mbb.AddJsonSerializer(); |
| 25 | + }); |
| 26 | + |
| 27 | + services.AddSingleton<TestEventCollector<OrderSubmitted>>(); |
| 28 | + } |
| 29 | + |
| 30 | + [Fact] |
| 31 | + public async Task ProvisionsTopologyAndDeliversPublishedMessage() |
| 32 | + { |
| 33 | + var messageBus = ServiceProvider.GetRequiredService<IMessageBus>(); |
| 34 | + var consumedMessages = ServiceProvider.GetRequiredService<TestEventCollector<OrderSubmitted>>(); |
| 35 | + var message = new OrderSubmitted(Guid.NewGuid(), 42); |
| 36 | + |
| 37 | + await EnsureConsumersStarted(); |
| 38 | + await messageBus.Publish(message, headers: new Dictionary<string, object> { ["source"] = "integration-test" }); |
| 39 | + await consumedMessages.WaitUntilArriving(newMessagesTimeout: 30, expectedCount: 1); |
| 40 | + |
| 41 | + consumedMessages.Snapshot().Should().ContainSingle().Which.Should().Be(message); |
| 42 | + } |
| 43 | + |
| 44 | + private static void ConfigureEmulatorClients(GooglePubSubMessageBusSettings settings, string endpoint) |
| 45 | + { |
| 46 | + settings.PublisherClientFactory = (_, topic, cancellationToken) => new PublisherClientBuilder |
| 47 | + { |
| 48 | + TopicName = topic, |
| 49 | + Endpoint = endpoint, |
| 50 | + ChannelCredentials = ChannelCredentials.Insecure |
| 51 | + }.BuildAsync(cancellationToken); |
| 52 | + settings.SubscriberClientFactory = (_, subscription, cancellationToken) => new SubscriberClientBuilder |
| 53 | + { |
| 54 | + SubscriptionName = subscription, |
| 55 | + Endpoint = endpoint, |
| 56 | + ChannelCredentials = ChannelCredentials.Insecure |
| 57 | + }.BuildAsync(cancellationToken); |
| 58 | + settings.PublisherServiceApiClientFactory = (_, cancellationToken) => new PublisherServiceApiClientBuilder |
| 59 | + { |
| 60 | + Endpoint = endpoint, |
| 61 | + ChannelCredentials = ChannelCredentials.Insecure |
| 62 | + }.BuildAsync(cancellationToken); |
| 63 | + settings.SubscriberServiceApiClientFactory = (_, cancellationToken) => new SubscriberServiceApiClientBuilder |
| 64 | + { |
| 65 | + Endpoint = endpoint, |
| 66 | + ChannelCredentials = ChannelCredentials.Insecure |
| 67 | + }.BuildAsync(cancellationToken); |
| 68 | + } |
| 69 | + |
| 70 | + public record OrderSubmitted(Guid OrderId, int Quantity); |
| 71 | + |
| 72 | + public class OrderSubmittedConsumer(TestEventCollector<OrderSubmitted> messages) : IConsumer<OrderSubmitted> |
| 73 | + { |
| 74 | + public Task OnHandle(OrderSubmitted message, CancellationToken cancellationToken) |
| 75 | + { |
| 76 | + messages.Add(message); |
| 77 | + return Task.CompletedTask; |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments