public async Task Receiver_CompletesDelivery_WhenBodyIsValid() { var utcNow = DateTime.UtcNow; var ttl = TimeSpan.FromMinutes(5); IDelivery delivery; var simulator = _simulation.AddServiceBus() .SetCertificate(_testCertificate) .AddQueue(TestInQueue) .Build(); await simulator .StartAsync() .ConfigureAwait(false); var settings = new AzureServiceBusSettings { InConnectionString = simulator.ConnectionString, InQueueName = TestInQueue }; var receiver = new AzureServiceBusMessageReceiver(settings, new AzureServiceBusMessageHandler()); using var cts = new CancellationTokenSource(); var runTask = receiver.RunAsync(cts.Token); delivery = simulator.Queues[TestInQueue].Post(new Amqp.Message() { Header = new Amqp.Framing.Header { DeliveryCount = 3, Ttl = (uint)ttl.TotalMilliseconds }, BodySection = new Amqp.Framing.Data { Binary = Encoding.ASCII.GetBytes("body1abc") }, Properties = new Amqp.Framing.Properties { MessageId = "8457986", CorrelationId = Guid.NewGuid().ToString(), CreationTime = utcNow }, MessageAnnotations = new Amqp.Framing.MessageAnnotations { [(Symbol)"x-opt-enqueue-sequence-number"] = 1234L, [(Symbol)"x-opt-sequence-number"] = 1234L, [(Symbol)"x-opt-enqueued-time"] = utcNow, [(Symbol)"x-opt-locked-until"] = utcNow.Add(ttl), [(Symbol)"x-opt-scheduled-enqueue-time"] = utcNow.AddMinutes(10), [(Symbol)"x-opt-partition-id"] = (short)17, [(Symbol)"x-opt-partition-key"] = "Key1", [(Symbol)"x-opt-via-partition-key"] = "VKey1", [(Symbol)"x-opt-deadletter-source"] = "$deadletter" } }); var deliveryTask = delivery.WaitAsync(TimeSpan.FromSeconds(10)); await Task.WhenAny(runTask, deliveryTask).ConfigureAwait(false); cts.Cancel(); Assert.AreEqual(DeliveryResult.Completed, delivery.Result); }
public void AddServiceBus_CreatesNewInstanceOfServiceBusSimulator() { ISimulation simulation = Substitute.For <ISimulation, IAddSimulator>(); ServiceBusBuilder serviceBusBuilder1 = simulation.AddServiceBus(); ServiceBusBuilder serviceBusBuilder2 = simulation.AddServiceBus(); serviceBusBuilder1.Build(); serviceBusBuilder2.Build(); simulation.ShouldSatisfyAllConditions( () => serviceBusBuilder1.ShouldNotBeNull(), () => serviceBusBuilder2.ShouldNotBeNull(), () => serviceBusBuilder1.ShouldNotBeSameAs(serviceBusBuilder2), () => ((IAddSimulator)simulation).Received(2).Add(Arg.Any <ServiceBusSimulator>()) ); }
public async Task TestAzureServiceBusQueueSend() { X509Certificate2 testCertificate = TestCertificate.Find(); if (testCertificate == null) { Assert.Inconclusive("The test SSL certificate is not available."); } using ISimulation simulation = Simulation.Create(); const string queueName = "my-queue-3278"; var messageBody = Encoding.ASCII.GetBytes("DTESTa"); IServiceBusSimulator bus = simulation.AddServiceBus() .SetCertificate(testCertificate) .AddQueue(queueName) .Build(); await bus.StartAsync(); var queueClient = new QueueClient(bus.ConnectionString, queueName); var message = new Message(messageBody) { CorrelationId = "3278" }; message.UserProperties["BoldManTrue"] = 32; await queueClient.SendAsync(message); await queueClient.CloseAsync(); await bus.StopAsync(); IReadOnlyList <IDelivery> deliveries = bus.Queues[queueName].Deliveries; bus.ShouldSatisfyAllConditions( () => deliveries.Count.ShouldBe(1), () => deliveries[0].Message.Body.ShouldBe(messageBody), () => deliveries[0].Message.ApplicationProperties["BoldManTrue"].ShouldBe(32) ); }
public async Task TestAzureServiceBusQueueReceive() { X509Certificate2 testCertificate = TestCertificate.Find(); using ISimulation simulation = Simulation.Create(); const string queueName = "sb-queue-x"; Exception busException = null; IDelivery[] deliveries = null; IServiceBusSimulator bus = simulation.AddServiceBus() .SetCertificate(testCertificate) .AddQueue(queueName) .Build(); await bus.StartAsync(); IQueue busQueue = bus.Queues[queueName]; var receiver = new MessageReceiver(bus.ConnectionString, queueName); try { receiver.ServiceBusConnection.OperationTimeout = TimeSpan.FromHours(1); receiver.RegisterMessageHandler(async(message, cancellationToken) => { cancellationToken.ThrowIfCancellationRequested(); if (message.MessageId == "2481") { await receiver.DeadLetterAsync(message.SystemProperties.LockToken); } else if (message.MessageId == "51847") { await receiver.AbandonAsync(message.SystemProperties.LockToken); } else { await receiver.CompleteAsync(message.SystemProperties.LockToken); } }, new MessageHandlerOptions(e => { if (!(e.Exception is OperationCanceledException)) { busException = e.Exception; } return(Task.CompletedTask); }) { AutoComplete = false }); busQueue.Post(new Amqp.Message { Properties = new Amqp.Framing.Properties { MessageId = "2481" } }); busQueue.Post(new Amqp.Message { Properties = new Amqp.Framing.Properties { MessageId = "51847" }, }); busQueue.Post(new Amqp.Message { Properties = new Amqp.Framing.Properties { MessageId = "33782" }, }); busQueue.Post(new Amqp.Message()); deliveries = busQueue.Deliveries.ToArray(); IEnumerable <Task <bool> > deliveryTasks = deliveries.Select(d => d.WaitAsync(TimeSpan.FromSeconds(5))); await Task.WhenAll(deliveryTasks); } finally { await receiver.CloseAsync(); await bus.StopAsync(); } bus.ShouldSatisfyAllConditions( () => deliveries.Length.ShouldBe(4), () => deliveries[0].Result.ShouldBe(DeliveryResult.DeadLettered), () => deliveries[1].Result.ShouldBe(DeliveryResult.Abandoned), () => deliveries[2].Result.ShouldBe(DeliveryResult.Completed), () => deliveries[3].Result.ShouldBe(DeliveryResult.Completed) ); }