public async Task RegisterMessageHandler_MessageHandlerInvokedAcrossInstances()
        {
            // Arrange
            var queueName = Guid.NewGuid().ToString();

            var queueFactory = new InMemoryQueueClientFactory();

            using (var instance1 = queueFactory.GetQueueReference(queueName))
                using (var instance2 = queueFactory.GetQueueReference(queueName))
                {
                    var content = Guid.NewGuid().ToString();

                    var messageHandled = false;

                    Func <IQueueMessage, CancellationToken, Task> handler
                        = (message, cancellationToken) =>
                        {
                        messageHandled = true;
                        return(Task.CompletedTask);
                        };

                    await instance2.RegisterMessageHandler(handler);

                    // Act
                    await instance1.EnqueueAsync(content, CancellationToken.None);

                    // Assert
                    Assert.True(messageHandled);
                }
        }
        public async Task DiscoverMessageHandlers_CallsMessageHandler()
        {
            // Arrange
            var fixture = new Fixture();

            IQueueClientFactory queueClientFactory = new InMemoryQueueClientFactory();
            var queueClient = queueClientFactory.GetQueueReference(QueueName);

            var message = new TestDto
            {
                TestProperty = fixture.Create <string>()
            };

            var job = new Mock <TestJob>();

            job.Setup(o => o.RunAsync(It.IsAny <TestDto>()));

            var services = new ServiceCollection();

            services.AddSingleton(job.Object);
            services.AddSingleton(queueClientFactory);
            services.AddSingleton <QueueManager>();
            var serviceProvider = services.BuildServiceProvider();

            await serviceProvider.DiscoverMessageHandlers(Assembly.GetAssembly(typeof(TestJob)));

            // Act
            await queueClient.EnqueueAsync(message);

            // Assert
            job.Verify(o => o.RunAsync(It.Is <TestDto>(p => p.Equals(message))), Times.Once);
        }
        public InMemoryQueueClientTests(InMemoryTestFixture fixture)
        {
            var queueFactory = new InMemoryQueueClientFactory();

            _sut = queueFactory.GetQueueReference(fixture.QueueName);

            _fixture = fixture;
        }
        public void Does_Process_All_Published_Messages_InMemory()
        {
            var queueClientFactory = new InMemoryQueueClientFactory();
            var server             = new AzureMessageService(queueClientFactory);
            var client             = server.MessageFactory.CreateMessageQueueClient();
            var handlerCallCount   = 0;
            var settings           = new MessageHandlerSettings();

            server.RegisterHandler <GreetTestMessage>(x =>
            {
                handlerCallCount++;
                Trace.WriteLine("GreetTestMessage: " + x.GetBody().Name);

                return(null);
            }, settings);

            server.RegisterHandler <GreetWorldTestMessage>(x =>
            {
                handlerCallCount++;
                Trace.WriteLine("GreetWorldTestMessage: " + x.GetBody().Name);

                return(null);
            }, settings);

            server.Start();

            client.Publish(new GreetTestMessage {
                Name = "1"
            });
            client.Publish(new GreetTestMessage {
                Name = "2"
            });
            client.Publish(new GreetWorldTestMessage {
                Name = "3"
            });
            client.Publish(new GreetWorldTestMessage {
                Name = "4"
            });

            Thread.Sleep(3000);

            Assert.That(server.GetStats().TotalMessagesProcessed, Is.EqualTo(4));
            Assert.That(handlerCallCount, Is.EqualTo(4));

            server.Dispose();
        }
Beispiel #5
0
        public async Task ClearQueue()
        {
            var queueFactory = new InMemoryQueueClientFactory();

            using (var queueClient = queueFactory.GetQueueReference(QueueName))
            {
                try
                {
                    IQueueMessage message;

                    while ((message = await queueClient.DequeueAsync()) != null)
                    {
                        await queueClient.CompleteAsync(message);
                    }
                }
                catch (OperationCanceledException)
                {
                }
            }
        }
        public async Task CanDequeueAcrossInstances()
        {
            // Arrange
            var queueName = Guid.NewGuid().ToString();

            var queueFactory = new InMemoryQueueClientFactory();

            using (var instance1 = queueFactory.GetQueueReference(queueName))
                using (var instance2 = queueFactory.GetQueueReference(queueName))
                {
                    var content = Guid.NewGuid().ToString();

                    await instance1.EnqueueAsync(content, CancellationToken.None);

                    // Act
                    var message = await instance2.DequeueAsync(CancellationToken.None);

                    // Assert
                    Assert.Equal(content, message.GetBody <string>());

                    // Clean up
                    await _sut.CompleteAsync(message, CancellationToken.None);
                }
        }