Пример #1
0
        void can_handle_threaded_commands()
        {
            var bus = new Dispatcher("test", 2);

            using (var sub = new TestSubscriber(bus)) {
                sub.Subscribe(
                    new AdHocCommandHandler <TestCommands.OrderedCommand>(cmd => {
                    Interlocked.Increment(ref _cmdCount);
                    return(Interlocked.Read(ref _cmdCount) == cmd.SequenceNumber + 1);
                }));
                var messages = new TestCommands.OrderedCommand[_count];
                for (int i = 0; i < _count; i++)
                {
                    messages[i] = new TestCommands.OrderedCommand(i);
                }

                for (int i = 0; i < _count; i++)
                {
                    bus.Send(messages[i]);
                }

                AssertEx.IsOrBecomesTrue(
                    () => _cmdCount == _count,
                    1000,
                    $"Expected Command count to be {_count} Commands, found {_cmdCount }");
            }
        }
Пример #2
0
        void can_unsubscribe_messages_commands_and_events_by_disposing()
        {
            using (var sub = new TestSubscriber(_bus)) {
                sub.Subscribe(new AdHocHandler <CountedTestMessage>(_ => Interlocked.Increment(ref _messageCount)));
                sub.Subscribe(new AdHocHandler <CountedEvent>(_ => Interlocked.Increment(ref _eventCount)));
                sub.Subscribe(new AdHocCommandHandler <TestCommands.OrderedCommand>(
                                  cmd => {
                    Interlocked.Increment(ref _cmdCount);
                    return(Interlocked.Read(ref _cmdCount) == cmd.SequenceNumber + 1);
                }));

                foreach (var msg in _messages)
                {
                    if (msg is TestCommands.OrderedCommand command)
                    {
                        _bus.Send(command);
                    }
                    else
                    {
                        _bus.Publish(msg);
                    }
                }
                AssertEx.IsOrBecomesTrue(() => _messageCount == _msgCount);
                AssertEx.IsOrBecomesTrue(() => _eventCount == _msgCount);
                AssertEx.IsOrBecomesTrue(() => _cmdCount == _msgCount);
            }

            for (int i = 0; i < 3; i++)
            {
                var msg = _messages[i];
                if (msg is TestCommands.OrderedCommand command)
                {
                    Assert.Throws <CommandNotHandledException>(() => _bus.Send(command));
                }
                else
                {
                    _bus.Publish(msg);
                }
            }

            Assert.True(_messageCount == _msgCount);
            Assert.True(_eventCount == _msgCount);
            Assert.True(_cmdCount == _msgCount);
        }
Пример #3
0
        void can_unsubscribe_messages_and_events_by_disposing()
        {
            using (var sub = new TestSubscriber(_bus)) {
                sub.Subscribe(new AdHocHandler <CountedTestMessage>(_ => Interlocked.Increment(ref _messageCount)));
                sub.Subscribe(new AdHocHandler <CountedEvent>(_ => Interlocked.Increment(ref _eventCount)));

                foreach (var msg in _messages)
                {
                    _bus.Publish(msg);
                }
                AssertEx.IsOrBecomesTrue(() => _messageCount == _msgCount);
                AssertEx.IsOrBecomesTrue(() => _eventCount == _msgCount);
            }

            for (int i = 0; i < _msgCount; i++)
            {
                _bus.Publish(_messages[i]);
            }

            Assert.True(_messageCount == _msgCount);
            Assert.True(_eventCount == _msgCount);
        }
        public void VerifySubscriberCanBeCollected()
        {
            WeakReference wr     = null;
            var           source = new TestEventSource();

            new Action(() =>
            {
                var ts = new TestSubscriber();
                wr     = new WeakReference(ts);
                ts.Subscribe(source);
            })();

            GC.Collect();
            GC.WaitForPendingFinalizers();

            Assert.IsNotNull(wr);
            Assert.IsFalse(wr.IsAlive);

            // The handler for this calls Assert.Fail, so if the subscriber has not been collected
            // the handler will be called and the test will fail
            source.FireTestEvent();
        }
Пример #5
0
        void can_handle_threaded_messages()
        {
            var bus = new Dispatcher("test", 2);

            using (var sub = new TestSubscriber(bus)) {
                sub.Subscribe(
                    new AdHocHandler <CountedTestMessage>(_ => Interlocked.Increment(ref _msgCount)));
                var messages = new IMessage[_count];
                for (int i = 0; i < _count; i++)
                {
                    messages[i] = new CountedTestMessage(i);
                }

                for (int i = 0; i < _count; i++)
                {
                    bus.Publish(messages[i]);
                }

                AssertEx.IsOrBecomesTrue(
                    () => _msgCount == _count,
                    1000,
                    $"Expected message count to be {_count} Messages, found {_msgCount }");
            }
        }