|
| 1 | +namespace SlimMessageBus.Host.RabbitMQ.Test.IntegrationTests; |
| 2 | + |
| 3 | +using System.Linq; |
| 4 | +using System.Net.Mime; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +using Microsoft.Extensions.Configuration; |
| 8 | + |
| 9 | +using SlimMessageBus.Host.Serialization.Json; |
| 10 | +using SlimMessageBus.Host.Test.Common.IntegrationTest; |
| 11 | + |
| 12 | +[Trait("Category", "Integration")] |
| 13 | +[Trait("Transport", "RabbitMQ")] |
| 14 | +public class RabbitMqDefaultExchangeIt(ITestOutputHelper output) : BaseIntegrationTest<RabbitMqDefaultExchangeIt>(output) |
| 15 | +{ |
| 16 | + [Fact] |
| 17 | + public async Task PublishDirectlyToQueueUsingDefaultExchange() |
| 18 | + { |
| 19 | + const string queueName = "default-exchange-queue"; |
| 20 | + |
| 21 | + AddBusConfiguration(mbb => |
| 22 | + { |
| 23 | + mbb.Produce<PingMessage>(x => x |
| 24 | + .DefaultPath(string.Empty) |
| 25 | + .RoutingKeyProvider((m, ctx) => queueName) |
| 26 | + .MessagePropertiesModifier((m, p) => |
| 27 | + { |
| 28 | + p.MessageId = $"ID_{m.Counter}"; |
| 29 | + p.ContentType = MediaTypeNames.Application.Json; |
| 30 | + })); |
| 31 | + |
| 32 | + mbb.Consume<PingMessage>(x => x |
| 33 | + .Path(string.Empty) // default exchange |
| 34 | + .Queue(queueName) |
| 35 | + .WithConsumer<PingConsumer>()); |
| 36 | + }); |
| 37 | + |
| 38 | + var messageBus = ServiceProvider.GetRequiredService<IMessageBus>(); |
| 39 | + var consumedMessages = ServiceProvider.GetRequiredService<TestEventCollector<TestEvent>>(); |
| 40 | + |
| 41 | + var ping = new PingMessage { Counter = 42 }; |
| 42 | + |
| 43 | + // act |
| 44 | + await messageBus.Publish(ping); |
| 45 | + |
| 46 | + await consumedMessages.WaitUntilArriving(); |
| 47 | + |
| 48 | + // assert |
| 49 | + var received = consumedMessages.Snapshot().Single(); |
| 50 | + received.Message.Counter.Should().Be(42); |
| 51 | + received.Message.Value.Should().Be(ping.Value); |
| 52 | + } |
| 53 | + |
| 54 | + protected override void SetupServices(ServiceCollection services, IConfigurationRoot configuration) |
| 55 | + { |
| 56 | + services.AddSlimMessageBus((mbb) => |
| 57 | + { |
| 58 | + mbb.WithProviderRabbitMQ(cfg => |
| 59 | + { |
| 60 | + cfg.ConnectionString = Secrets.Service.PopulateSecrets(configuration["RabbitMQ:ConnectionString"]); |
| 61 | + |
| 62 | + cfg.ConnectionFactory.ClientProvidedName = $"MyService_{Environment.MachineName}"; |
| 63 | + |
| 64 | + cfg.UseMessagePropertiesModifier((m, p) => |
| 65 | + { |
| 66 | + p.ContentType = MediaTypeNames.Application.Json; |
| 67 | + }); |
| 68 | + cfg.UseQueueDefaults(durable: false); |
| 69 | + cfg.UseTopologyInitializer((channel, applyDefaultTopology) => |
| 70 | + { |
| 71 | + // before test clean up |
| 72 | + channel.QueueDelete("default-exchange-queue", ifUnused: true, ifEmpty: false); |
| 73 | + |
| 74 | + // apply default SMB inferred topology |
| 75 | + applyDefaultTopology(); |
| 76 | + |
| 77 | + // after |
| 78 | + }); |
| 79 | + }); |
| 80 | + mbb.AddServicesFromAssemblyContaining<PingConsumer>(); |
| 81 | + mbb.AddJsonSerializer(); |
| 82 | + ApplyBusConfiguration(mbb); |
| 83 | + }); |
| 84 | + |
| 85 | + // Custom error handler |
| 86 | + services.AddTransient(typeof(IRabbitMqConsumerErrorHandler<>), typeof(CustomRabbitMqConsumerErrorHandler<>)); |
| 87 | + |
| 88 | + services.AddSingleton<TestEventCollector<TestEvent>>(); |
| 89 | + } |
| 90 | +} |
0 commit comments