コード例 #1
0
        public void StopReceiving_NoErrors_ResultIsNotFailure_SubscribersCleared()
        {
            var mocks      = CreateMocks();
            var mockLogger = new Mock <Log>();

            var client = new BrokerClient(
                () => mocks.Item1.Object,
                _mockOpenAsync.Object,
                _mockExeNonQueryAsync.Object,
                mockLogger.Object);

            var mockObserver1 = CreateMockObserver();

            mockObserver1.Setup(f => f.OnCompleted()).Throws <InvalidOperationException>();
            var mockObserver2 = CreateMockObserver();

            client.Subscribe(mockObserver1.Object);
            client.Subscribe(mockObserver2.Object);

            Expect(client.ReceiveMessages(QueueName).StopReceiving().Failed, True);
            Expect(client.Subscribers.Count, EqualTo(0));

            mockObserver1.Verify(o => o.OnCompleted(), Times.Once());
            mockObserver2.Verify(o => o.OnCompleted(), Times.Once());
            mockLogger.Verify(f => f(It.IsRegex("Receiver stopped"), It.IsAny <string>(), It.IsAny <TraceEventType>()), Times.Once());
        }
コード例 #2
0
        public void ReceiveMessages_ExeQueryFirstParamIsDBNull_ObserverOnNextNotCalled()
        {
            var mocks           = CreateMocks();
            var mockConnection  = mocks.Item1;
            var mockTransaction = mocks.Item2;
            var mockCommand     = mocks.Item3;
            var mockParams      = mocks.Item4;
            var mockObserver    = CreateMockObserver();
            var client          = new BrokerClient(() => mockConnection.Object, _mockOpenAsync.Object, _mockExeNonQueryAsync.Object);

            mockConnection.Setup(c => c.Dispose()).Callback(() => _wait.Set());
            mockParams.Setup(p => p[It.IsAny <int>()])
            .Callback(() => new SqlParameter("", DBNull.Value));

            client.Subscribe(mockObserver.Object);
            var running = client.ReceiveMessages(QueueName);

            _wait.WaitOne();
            running.StopReceiving();

            mockObserver.Verify(o => o.OnNext(It.Is <BrokerMessage>(msg => msg == BrokerMessage.Empty)), Times.Never());

            ExpectReceiveMessage(mocks);
            ExpectAllDisposed(mocks);
        }
コード例 #3
0
        public void ReceiveMessages_SBEndDialogMessageReceived_EndDialogCalled()
        {
            var mockObserver   = CreateMockObserver();
            var mocks          = CreateMocks(ServiceBrokerEndDialogMessageType);
            var mockConnection = mocks.Item1;
            var mockCommand    = mocks.Item3;
            var mockParams     = mocks.Item4;

            mockConnection.Setup(c => c.Dispose()).Callback(fun(() =>
            {
                var count = 0;

                return(new Action(() =>
                {
                    if (++count == 2)
                    {
                        _wait.Set();
                    }
                }));
            })());

            var client = new BrokerClient(() => mocks.Item1.Object, _mockOpenAsync.Object, _mockExeNonQueryAsync.Object);

            client.Subscribe(mockObserver.Object);
            var running = client.ReceiveMessages(QueueName);

            _wait.WaitOne();
            running.StopReceiving();

            ExpectEndDialog(mocks);
            ExpectAllDisposed(mocks);
        }
コード例 #4
0
        public static void Main(string[] args)
        {
            Console.WriteLine("New Blog Consumer");

            BrokerClient brokerClient = new BrokerClient(new HostInfo("broker.bk.sapo.pt", 3323));

            Subscription subscription = new Subscription("test@/sapo/blogs/activity/post", NetAction.DestinationType.VIRTUAL_QUEUE);

            subscription.OnMessage += delegate(NetNotification notification)
            {
                if (((++count) % 100) == 0)
                {
                    Console.WriteLine("{0} - New message received. Count: {1}", DateTime.Now.ToLongTimeString(), count);
                }
                brokerClient.Acknowledge(notification.Destination, notification.Message.MessageId);
            };

            brokerClient.Subscribe(subscription);

            Console.WriteLine("Write X to unsbscribe and exit");
            while (!System.Console.Read().Equals('X'))
            {
                ;
            }
            Console.WriteLine();
            Console.WriteLine("Unsubscribe...");

            // Note Subscription instance could other than the one used for subscription as long as it was equivelent (same destination type and subscription pattern). Since the application is ending and therefor the socket will be closed agent's will discard the previous subscription.
            brokerClient.Unsubscribe(subscription);

            Console.WriteLine("Good bye");
        }
コード例 #5
0
        public static void Main(string[] args)
        {
            Console.WriteLine("ConsumerWithAutoAck test");

            if (args.Length == 0)
            {
                System.Console.WriteLine(CommandLineArguments.Usage());
                return;
            }

            CommandLineArguments cliArgs = new CommandLineArguments();
            Parser parser = new Parser(System.Environment.CommandLine, cliArgs);

            parser.Parse();

            BrokerClient brokerClient = new BrokerClient(new HostInfo(cliArgs.Hostname, cliArgs.PortNumber));

            Subscription subscription = new Subscription(cliArgs.DestinationName, cliArgs.DestinationType);

            if (cliArgs.DestinationType != NetAction.DestinationType.TOPIC)
            {
                // Set AutoAcknowledge
                subscription.AutoAcknowledge = true;
            }

            int i = 0;

            subscription.OnMessage += delegate(NetNotification notification)
            {
                System.Console.WriteLine("Message received: {0}, Total: {1}",
                                         System.Text.Encoding.UTF8.GetString(notification.Message.Payload), (++i).ToString());

                /*
                 *  AutoAcknowledge is enable, so, there is no need to excplicit acknowledge message
                 *
                 * if (notification.DestinationType != NetAction.DestinationType.TOPIC)
                 * {
                 *  brokerClient.Acknowledge(notification);
                 * }
                 */
            };

            brokerClient.Subscribe(subscription);

            Console.WriteLine("Write X to unsbscribe and exit");
            while (!System.Console.Read().Equals('X'))
            {
                ;
            }
            Console.WriteLine();
            Console.WriteLine("Unsubscribe...");

            // Note Subscription instance could other than the one used for subscription as long as it was equivelent (same destination type and subscription pattern). Since the application is ending and therefor the socket will be closed agent's will discard the previous subscription.
            brokerClient.Unsubscribe(subscription);

            Console.WriteLine("Good bye");
        }
コード例 #6
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Consumer with failover test");

            if (args.Length == 0)
            {
                System.Console.WriteLine(CommandLineArguments.Usage());
                return;
            }

            CommandLineArguments cliArgs = new CommandLineArguments();
            Parser parser = new Parser(System.Environment.CommandLine, cliArgs);

            parser.Parse();

            List <HostInfo> hosts = new List <HostInfo>();

            hosts.Add(new HostInfo(cliArgs.Hostname, cliArgs.PortNumber));
            //hosts.Add(new HostInfo(cliArgs.Hostname, 3423)); // Add an alternative

            BrokerClient brokerClient = new BrokerClient(hosts);
            Subscription subscription = new Subscription(cliArgs.DestinationName, cliArgs.DestinationType);

            subscription.OnMessage += delegate(NetNotification notification)
            {
                System.Console.WriteLine("Message received: {0}",
                                         System.Text.Encoding.UTF8.GetString(notification.Message.Payload));
                if (notification.DestinationType != NetAction.DestinationType.TOPIC)
                {
                    brokerClient.Acknowledge(notification);
                }
            };

            brokerClient.Subscribe(subscription);

            Console.WriteLine("Write X to unsbscribe and exit");
            while (!System.Console.Read().Equals('X'))
            {
                ;
            }
            Console.WriteLine();
            Console.WriteLine("Unsubscribe...");

            // Note Subscription instance could other than the one used for subscription as long as it was equivelent (same destination type and subscription pattern). Since the application is ending and therefor the socket will be closed agent's will discard the previous subscription.
            brokerClient.Unsubscribe(subscription);

            Console.WriteLine("Good bye");
        }
コード例 #7
0
        public void ReceiveMessages_SBErrorMessageReceived_ObserversOnErrorsCalled()
        {
            var mockObserver = CreateMockObserver();

            mockObserver.Setup(o => o.OnError(It.IsAny <Exception>(), It.IsAny <Maybe <BrokerMessage> >())).Callback(() => _wait.Set());

            var mocks  = CreateMocks(ServiceBrokerErrorMessageType);
            var client = new BrokerClient(() => mocks.Item1.Object, _mockOpenAsync.Object, _mockExeNonQueryAsync.Object);

            client.Subscribe(mockObserver.Object);
            var running = client.ReceiveMessages(QueueName);

            _wait.WaitOne();
            running.StopReceiving();

            mockObserver.Verify(o => o.OnError(It.Is <Exception>(e => e.Message == MessageText), It.IsAny <Maybe <BrokerMessage> >()));

            ExpectAll(mocks);
        }
コード例 #8
0
        public void ReceiveMessages_ObserverOnErrorErrors_ErrorLogged()
        {
            var mockLogger = new Mock <Log>();

            mockLogger
            .Setup(f => f(It.IsAny <string>(), GeneralLogCategory, It.Is <TraceEventType>(t => t == TraceEventType.Error)))
            .Callback(() => _wait.Set());

            var mocks          = CreateMocks();
            var mockConnection = mocks.Item1;

            var client = new BrokerClient(
                () => mockConnection.Object,
                _mockOpenAsync.Object,
                _mockExeNonQueryAsync.Object,
                mockLogger.Object);

            var mockObserver = CreateMockObserver();

            mockObserver.SetupSequence(o => o.OnNext(It.IsAny <BrokerMessage>()))
            .Throws(new Exception(FailVal))
            .Throws(new Exception($"{FailVal} 2"))
            .Pass();

            mockObserver.SetupSequence(o => o.OnError(It.IsAny <Exception>(), It.IsAny <Maybe <BrokerMessage> >()))
            .Pass()
            .Throws(new Exception("OnError Fail"));

            client.Subscribe(mockObserver.Object);
            var running = client.ReceiveMessages("TestQueue");

            _wait.WaitOne();
            running.StopReceiving();

            mockObserver.Verify(o => o.OnError(It.Is <Exception>(e => e.Message == FailVal), It.IsAny <Maybe <BrokerMessage> >()));
            mockLogger.Verify(l => l(It.IsRegex($"{FailVal} 2"), GeneralLogCategory, TraceEventType.Error));

            ExpectAll(mocks);
        }
コード例 #9
0
        public void ReceiveMessages_CommandCorrect_ObserversOnNextCalled()
        {
            var mocks          = CreateMocks(MessageType);
            var mockConnection = mocks.Item1;
            var mockObserver   = CreateMockObserver();

            mockObserver.Setup(o => o.OnNext(It.IsAny <BrokerMessage>()))
            .Callback(() => _wait.Set());

            var client = new BrokerClient(
                () => mockConnection.Object,
                _mockOpenAsync.Object,
                _mockExeNonQueryAsync.Object);

            client.Subscribe(mockObserver.Object);
            var running = client.ReceiveMessages(QueueName);

            _wait.WaitOne();
            running.StopReceiving();

            mockObserver.Verify(o => o.OnNext(It.Is <BrokerMessage>(m => m.MessageType == MessageType)));

            ExpectAll(mocks);
        }
コード例 #10
0
 protected IDisposable AddSubscriber() =>
 _client.Subscribe(CreateObserver());
コード例 #11
0
        public static void Main(string[] args)
        {
            Console.WriteLine("ConsumerWithNoAckRequired test");

            if (args.Length == 0)
            {
                System.Console.WriteLine(CommandLineArguments.Usage());
                return;
            }

            CommandLineArguments cliArgs = new CommandLineArguments();
            Parser parser = new Parser(System.Environment.CommandLine, cliArgs);

            parser.Parse();

            BrokerClient brokerClient = new BrokerClient(new HostInfo(cliArgs.Hostname, cliArgs.PortNumber));

            Subscription subscription = new Subscription(cliArgs.DestinationName, cliArgs.DestinationType);

            if (cliArgs.DestinationType != NetAction.DestinationType.TOPIC)
            {
                subscription.SetHeader("ACK_REQUIRED", "false");
            }

            int i = 0;

            subscription.OnMessage += delegate(NetNotification notification)
            {
                System.Console.WriteLine("Message received: {0}, Total: {1}",
                                         System.Text.Encoding.UTF8.GetString(notification.Message.Payload), (++i).ToString());

                IDictionary <string, string> headers = notification.Headers;
                if (headers.Keys != null)
                {
                    System.Console.WriteLine("Headers:");

                    foreach (string header in headers.Keys)
                    {
                        System.Console.WriteLine("{0} - {1}", header, headers[header]);
                    }
                }

                /*
                 *  ACK IS NOT REQUIRED because ACK_REQUIRED was set to false.
                 *
                 * if (notification.DestinationType != NetAction.DestinationType.TOPIC)
                 * {
                 *  brokerClient.Acknowledge(notification);
                 * }
                 */
            };

            brokerClient.Subscribe(subscription);

            Console.WriteLine("Write X to unsbscribe and exit");
            while (!System.Console.Read().Equals('X'))
            {
                ;
            }
            Console.WriteLine();
            Console.WriteLine("Unsubscribe...");

            // Note Subscription instance could other than the one used for subscription as long as it was equivelent (same destination type and subscription pattern). Since the application is ending and therefor the socket will be closed agent's will discard the previous subscription.
            brokerClient.Unsubscribe(subscription);

            Console.WriteLine("Good bye");
        }