예제 #1
0
        public void Executes_only_once_when_action_of_event_received_first()
        {
            ConcurrencyTester.Run(() =>
            {
                var source  = new TestEventJournalSource();
                var journal = new EventJournal(source);
                SpeechRequestedEvent receivedSpeech = null;

                var task = Task.Run(() =>
                {
                    journal
                    .When <SpeechRequestedEvent>(e =>
                    {
                        receivedSpeech = e;
                    })
                    .WaitAny();
                });

                journal.AwaitingStarted.AssertWaitOneSuccess();

                var questArrowEvent = new SpeechRequestedEvent("first message");
                source.Publish(questArrowEvent);

                var speechEvent = new SpeechRequestedEvent("second message");
                source.Publish(speechEvent);

                task.AssertWaitFastSuccess();

                receivedSpeech.Should().NotBeNull();
                receivedSpeech.Message.Should().Be("first message");
            });
        }
예제 #2
0
        public void When_awaiting_any_of_two_events_Then_executes_when_action_of_first_received_event()
        {
            ConcurrencyTester.Run(() =>
            {
                var source                          = new EventJournalSource();
                var journal                         = new EventJournal(source);
                var executedEvent                   = new AutoResetEvent(false);
                QuestArrowEvent receivedQuest       = null;
                SpeechRequestedEvent receivedSpeech = null;

                var task = Task.Run(() =>
                {
                    journal
                    .When <QuestArrowEvent>(e =>
                    {
                        receivedQuest = e;
                        executedEvent.Set();
                    })
                    .When <SpeechRequestedEvent>(e =>
                    {
                        receivedSpeech = e;
                        executedEvent.Set();
                    })
                    .WaitAny();
                });

                journal.AwaitingStarted.AssertWaitOneSuccess();

                source.Publish(new SpeechRequestedEvent("some message"));

                executedEvent.AssertWaitOneSuccess();

                receivedQuest.Should().BeNull();
                receivedSpeech.Should().NotBeNull();
                receivedSpeech.Message.Should().Be("some message");
            });
        }