Example #1
0
        public async Task OrderOfPluginsRespected()
        {
            await using (var scope = await ServiceBusScope.CreateWithQueue(
                             enablePartitioning: false,
                             enableSession: false))
            {
                var options = new ServiceBusClientOptions();
                options.AddPlugin(new FirstPlugin());
                options.AddPlugin(new SecondPlugin());
                var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString, options);

                var sender      = client.CreateSender(scope.QueueName);
                var receiver    = client.CreateReceiver(scope.QueueName);
                var sendMessage = new ServiceBusMessage();
                await sender.SendMessageAsync(sendMessage);

                var receivedMessage = await receiver.ReceiveMessageAsync();

                var firstSendPluginUserProperty  = (bool)receivedMessage.Properties["FirstSendPlugin"];
                var secondSendPluginUserProperty = (bool)receivedMessage.Properties["SecondSendPlugin"];

                Assert.True(firstSendPluginUserProperty);
                Assert.True(secondSendPluginUserProperty);
            }
        }
        public async Task LogsPluginExceptionEvents()
        {
            await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: false))
            {
                var options = new ServiceBusClientOptions();
                options.AddPlugin(new PluginLiveTests.SendExceptionPlugin());
                var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString, options);
                ServiceBusSender  sender  = client.CreateSender(scope.QueueName);
                ServiceBusMessage message = GetMessage();
                Assert.That(
                    async() => await sender.SendMessageAsync(message),
                    Throws.InstanceOf <NotImplementedException>());
                _listener.SingleEventById(ServiceBusEventSource.PluginStartEvent);
                _listener.SingleEventById(ServiceBusEventSource.PluginExceptionEvent);

                options = new ServiceBusClientOptions();
                options.AddPlugin(new PluginLiveTests.ReceiveExceptionPlugin());
                client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString, options);
                sender = client.CreateSender(scope.QueueName);
                await sender.SendMessageAsync(message);

                Assert.AreEqual(2, _listener.EventsById(ServiceBusEventSource.PluginStartEvent).Count());
                Assert.AreEqual(1, _listener.EventsById(ServiceBusEventSource.PluginExceptionEvent).Count());

                var receiver = client.CreateReceiver(scope.QueueName);
                Assert.That(
                    async() => await receiver.ReceiveMessageAsync(),
                    Throws.InstanceOf <NotImplementedException>());
                Assert.AreEqual(3, _listener.EventsById(ServiceBusEventSource.PluginStartEvent).Count());
                Assert.AreEqual(2, _listener.EventsById(ServiceBusEventSource.PluginExceptionEvent).Count());
            };
        }
Example #3
0
        public async Task PluginsCanAlterMessage(bool schedule)
        {
            await using var scope = await ServiceBusScope.CreateWithQueue(
                            enablePartitioning : false,
                            enableSession : false);

            var plugin  = new SendReceivePlugin();
            var options = new ServiceBusClientOptions();

            options.AddPlugin(plugin);
            var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString, options);

            var sender   = client.CreateSender(scope.QueueName);
            var receiver = client.CreateReceiver(scope.QueueName);

            if (schedule)
            {
                await sender.ScheduleMessageAsync(new ServiceBusMessage(), DateTimeOffset.UtcNow);
            }
            else
            {
                await sender.SendMessageAsync(new ServiceBusMessage());
            }

            Assert.True(plugin.WasCalled);
            var receivedMessage = await receiver.ReceiveMessageAsync();

            Assert.AreEqual("received", receivedMessage.Body.ToString());
        }
Example #4
0
        public async Task PluginCausingExceptionShouldThrow()
        {
            await using (var scope = await ServiceBusScope.CreateWithQueue(
                             enablePartitioning: false,
                             enableSession: false))
            {
                var options = new ServiceBusClientOptions();
                options.AddPlugin(new SendExceptionPlugin());
                var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString, options);
                var sender = client.CreateSender(scope.QueueName);
                Assert.That(
                    async() => await sender.SendMessageAsync(new ServiceBusMessage()),
                    Throws.InstanceOf <NotImplementedException>());

                options = new ServiceBusClientOptions();
                options.AddPlugin(new ReceiveExceptionPlugin());
                client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString, options);
                sender = client.CreateSender(scope.QueueName);
                await sender.SendMessageAsync(new ServiceBusMessage());

                var receiver = client.CreateReceiver(scope.QueueName);
                Assert.That(
                    async() => await receiver.ReceiveMessageAsync(),
                    Throws.InstanceOf <NotImplementedException>());
            }
        }
Example #5
0
        public async Task PluginsCanAlterMessageUsingSessionProcessor()
        {
            await using (var scope = await ServiceBusScope.CreateWithQueue(
                             enablePartitioning: false,
                             enableSession: true))
            {
                var plugin  = new SendReceivePlugin();
                var options = new ServiceBusClientOptions();
                options.AddPlugin(plugin);
                var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString, options);
                var sender = client.CreateSender(scope.QueueName);

                await sender.SendMessageAsync(GetMessage("sessionId"));

                Assert.True(plugin.WasCalled);
                var processor = client.CreateSessionProcessor(scope.QueueName, new ServiceBusSessionProcessorOptions
                {
                    MaxConcurrentSessions = 1
                });
                processor.ProcessErrorAsync += ExceptionHandler;
                var tcs = new TaskCompletionSource <bool>();
                processor.ProcessMessageAsync += args =>
                {
                    Assert.AreEqual("received", args.Message.Body.ToString());
                    tcs.SetResult(true);
                    return(Task.CompletedTask);
                };
                await processor.StartProcessingAsync();

                await tcs.Task;
                await processor.StopProcessingAsync();
            }
        }
Example #6
0
        public async Task PluginsCanAlterTopicSessionMessage()
        {
            await using (var scope = await ServiceBusScope.CreateWithTopic(
                             enablePartitioning: false,
                             enableSession: true))
            {
                var plugin  = new SendReceivePlugin();
                var options = new ServiceBusClientOptions();
                options.AddPlugin(plugin);
                var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString, options);
                var sender = client.CreateSender(scope.TopicName);

                await sender.SendMessageAsync(GetMessage("sessionId"));

                Assert.True(plugin.WasCalled);
                var receiver = await client.CreateSessionReceiverAsync(scope.TopicName, scope.SubscriptionNames.First());

                var receivedMessage = await receiver.ReceiveMessageAsync();

                Assert.AreEqual("received", receivedMessage.Body.ToString());
            }
        }
Example #7
0
        public async Task PluginsCanAlterSessionMessage()
        {
            await using (var scope = await ServiceBusScope.CreateWithQueue(
                             enablePartitioning: false,
                             enableSession: true))
            {
                var plugin  = new SendReceivePlugin();
                var options = new ServiceBusClientOptions();
                options.AddPlugin(plugin);
                var client = new ServiceBusClient(TestEnvironment.FullyQualifiedNamespace, TestEnvironment.Credential, options);
                var sender = client.CreateSender(scope.QueueName);

                await sender.SendMessageAsync(GetMessage("sessionId"));

                Assert.True(plugin.WasCalled);
                var receiver = await client.CreateSessionReceiverAsync(scope.QueueName);

                var receivedMessage = await receiver.ReceiveMessageAsync();

                Assert.AreEqual("received", receivedMessage.Body.ToString());
            }
        }
Example #8
0
        public async Task PluginsCanAlterSetOfMessages(bool schedule)
        {
            await using (var scope = await ServiceBusScope.CreateWithQueue(
                             enablePartitioning: false,
                             enableSession: false))
            {
                var plugin  = new SendReceivePlugin();
                var options = new ServiceBusClientOptions();
                options.AddPlugin(plugin);
                var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString, options);

                var sender      = client.CreateSender(scope.QueueName);
                var receiver    = client.CreateReceiver(scope.QueueName);
                int numMessages = 5;
                if (schedule)
                {
                    await sender.ScheduleMessagesAsync(GetMessages(numMessages), DateTimeOffset.UtcNow);
                }
                else
                {
                    await sender.SendMessagesAsync(GetMessages(numMessages));
                }

                Assert.True(plugin.WasCalled);

                var remaining = numMessages;
                while (remaining > 0)
                {
                    var receivedMessages = await receiver.ReceiveMessagesAsync(remaining);

                    remaining = -receivedMessages.Count;
                    foreach (var receivedMessage in receivedMessages)
                    {
                        Assert.AreEqual("received", receivedMessage.Body.ToString());
                    }
                }
            }
        }