Example #1
0
        public override void Start()
        {
            base.Start();

            #region init process command reply worker

            try
            {
                if (!string.IsNullOrWhiteSpace(_replyTopicName))
                {
                    _internalConsumer = MessageQueueClient.StartSubscriptionClient(_replyTopicName,
                                                                                   _replySubscriptionName,
                                                                                   _consumerId,
                                                                                   OnMessagesReceived,
                                                                                   _consumerConfig);
                }
            }
            catch (Exception e)
            {
                Logger.LogError(e, $"command bus started faield");
            }

            #endregion

            _messageProcessor.Start();
        }
Example #2
0
        public void TryReceivePostAndReply_with_TryScan_timeout(int timeout)
        {
            // Given
            var mb = MailboxProcessor.Start <IReplyChannel <int?> >(async inbox =>
            {
                var replyChannel = await inbox.TryReceive();
                if (replyChannel == null)
                {
                    Assert.True(false);
                    return;
                }
                var scanRes = await inbox.TryScan(__ =>
                {
                    Assert.True(false, "Should have timedout");
                    return(Task.FromResult(inbox));
                }, timeout);

                if (scanRes == null)
                {
                    replyChannel.Reply(200);
                }
                else
                {
                    Assert.True(false, "TryScan should have failed");
                }
            });

            // When
            var result1 = mb.PostAndReply <int?>(channel => channel);

            // Then
            Assert.Equal(200, result1);
        }
Example #3
0
        public void PostReceive(int n)
        {
            // Given
            var received = 0L;

            var mb = MailboxProcessor.Start <Option <int> >(async inbox =>
            {
                for (var i = 0; i < n; i++)
                {
                    var _ = await inbox.Receive();
                    Interlocked.Increment(ref received);
                }
            });


            // When
            for (var i = 0; i < n; i++)
            {
                mb.Post(Some(i));
            }

            // Then
            while (received < n)
            {
                var numReceived = Interlocked.Read(ref received);
                if (numReceived % 100 == 0)
                {
                    Trace.WriteLine(string.Format("received = {0}", numReceived));
                }
                Thread.Sleep(1);
            }

            Assert.Equal(n, received);
        }
Example #4
0
        public void TryReceivePostAndReply_with_Scan_timeout(int timeout)
        {
            // Given
            var mb = MailboxProcessor.Start <IReplyChannel <int?> >(async inbox =>
            {
                var replyChannel = await inbox.TryReceive();
                if (replyChannel == null)
                {
                    Assert.True(false);
                    return;
                }
                try
                {
                    var _ = await inbox.Scan(__ =>
                    {
                        Assert.True(false, "Should have timedout");
                        return(Task.FromResult(inbox));
                    }, timeout);
                    Assert.True(false, "should have received timeout");
                }
                catch (TimeoutException)
                {
                    replyChannel.Reply(200);
                }
            });

            // When
            var result1 = mb.PostAndReply <int?>(channel => channel);

            // Then
            Assert.Equal(200, result1);
        }
Example #5
0
        public void TryReceive_PostAndReply()
        {
            // Given
            var mb = MailboxProcessor.Start <IReplyChannel <int?> >(async inbox =>
            {
                var msg = await inbox.TryReceive();
                if (msg == null)
                {
                    Assert.True(false);
                    return;
                }

                msg.Reply(100);

                var msg2 = await inbox.TryReceive();
                if (msg2 == null)
                {
                    Assert.True(false);
                    return;
                }

                msg2.Reply(200);
            });

            // When
            var result1 = mb.PostAndReply <int?>(channel => channel);
            var result2 = mb.PostAndReply <int?>(channel => channel);


            // Then
            Assert.Equal(100, result1);
            Assert.Equal(200, result2);
        }
        public void MailboxProcessor_null()
        {
            // Given
            var mb = new MailboxProcessor<IReplyChannel<int?>>(async inbox => { });

            // When
            mb.Start();

            // Then
            // no exceptions
        }
Example #7
0
        public void MailboxProcessor_null()
        {
            // Given
            var mb = new MailboxProcessor <IReplyChannel <int?> >(async inbox => { });

            // When
            mb.Start();

            // Then
            // no exceptions
        }
 internal RealTimeAgent_Mailbox(Agent agent)
     : base(agent)
 {
     mailbox = MailboxProcessor.Start <Message>(async mb =>
     {
         while (true)
         {
             var message = await mb.Receive();
             agent.HandleMessage(message);
         }
     });
 }
Example #9
0
        public void Receive_PostAndReply()
        {
            // Given
            var mb = MailboxProcessor.Start <IReplyChannel <int?> >(async inbox =>
            {
                var msg = await inbox.Receive();
                msg.Reply(100);
            });

            // When
            var result = mb.PostAndReply <int?>(channel => channel);

            Assert.Equal(100, result);
        }
Example #10
0
        public void PostTryReceive(int timeout, int n)
        {
            // Given
            var received = 0L;

            var mb = MailboxProcessor.Start <Option <int> >(async inbox =>
            {
                while (Interlocked.Read(ref received) < n)
                {
                    var msgOpt = await inbox.TryReceive(timeout);
                    if (msgOpt == null)
                    {
                        var numReceived = Interlocked.Read(ref received);
                        if (numReceived % 100 == 0)
                        {
                            Trace.WriteLine(string.Format("timeout!, received = {0}", numReceived));
                        }
                    }
                    else
                    {
                        Interlocked.Increment(ref received);
                    }
                }
            });


            // When
            for (var i = 0; i < n; i++)
            {
                Thread.Sleep(1);
                mb.Post(Some(i));
            }

            // Then
            while (Interlocked.Read(ref received) < n)
            {
                var numReceived = Interlocked.Read(ref received);
                if (numReceived % 100 == 0)
                {
                    Trace.WriteLine(string.Format("received = {0}", numReceived));
                }

                Thread.Sleep(1);
            }

            Assert.Equal(n, received);
        }
Example #11
0
        public void Start()
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(CommandQueueName))
                {
                    InternalConsumer = MessageQueueClient.StartQueueClient(CommandQueueName, ConsumerId,
                                                                           OnMessageReceived,
                                                                           ConsumerConfig);
                }

                MessageProcessor.Start();
            }
            catch (Exception e)
            {
                Logger.LogError(e, $"Command Consumer {ConsumerId} Start Failed");
            }
        }
Example #12
0
        public void Start()
        {
            try
            {
                if (_topicSubscriptions?.Length > 0)
                {
                    InternalConsumer =
                        MessageQueueClient.StartSubscriptionClient(_topicSubscriptions.Select(ts => ts.Topic)
                                                                   .ToArray(),
                                                                   SubscriptionName,
                                                                   ConsumerId,
                                                                   OnMessagesReceived,
                                                                   ConsumerConfig);
                }

                MessageProcessor.Start();
            }
            catch (Exception e)
            {
                Logger.LogError(e, $"Event Subscriber {string.Join(",", _topicSubscriptions?.Select(ts => ts.Topic) ?? new string[0])} start faield");
            }
        }
Example #13
0
        public void TryReceive_timeout(int timeout)
        {
            // Given
            var mb = MailboxProcessor.Start <IReplyChannel <int?> >(async inbox =>
            {
                var replyChannel = await inbox.TryReceive();
                if (replyChannel == null)
                {
                    Assert.True(false);
                    return;
                }

                var msg2 = await inbox.TryReceive(timeout);
                replyChannel.Reply(msg2 == null ? 100 : 200);
            });

            // When
            var result1 = mb.PostAndReply <int?>(channel => channel);

            // Then
            Assert.Equal(100, result1);
        }