예제 #1
0
        public void Can_cancel_All_handling()
        {
            var source = new EventJournalSource();
            var cancellationTokenSource = new CancellationTokenSource();
            var journal = new EventJournal(source, new Cancellation(() => cancellationTokenSource.Token));

            var task = Task.Run(() =>
            {
                Action action = () =>
                {
                    while (true)
                    {
                        journal.When <SpeechRequestedEvent>(e => { })
                        .All();
                    }
                };

                action.ShouldThrow <OperationCanceledException>();
            });

            journal.AwaitingStarted.WaitOne(100).Should().BeTrue();

            cancellationTokenSource.Cancel();
            task.Wait(TimeSpan.FromMilliseconds(100)).Should()
            .BeTrue("false means timeout - tested task was not cancelled in time");
        }
예제 #2
0
        public void Can_cancel_awaiting()
        {
            ConcurrencyTester.Run(() =>
            {
                var initializedEvent        = new AutoResetEvent(false);
                var source                  = new EventJournalSource();
                var cancellationTokenSource = new CancellationTokenSource();
                var journal                 = new EventJournal(source, new Cancellation(() => cancellationTokenSource.Token));

                var task = Task.Run(() =>
                {
                    Action action = () =>
                    {
                        initializedEvent.Set();
                        journal
                        .When <QuestArrowEvent>(e => { })
                        .WaitAny();
                    };

                    action.Should().Throw <OperationCanceledException>();
                });

                initializedEvent.AssertWaitOneSuccess();
                Thread.Yield();

                cancellationTokenSource.Cancel();

                task.AssertWaitFastSuccess();
            });
        }
예제 #3
0
        public void Can_cancel_awaiting()
        {
            ConcurrencyTester.Run(() =>
            {
                var initializedEvent        = new AutoResetEvent(false);
                var source                  = new EventJournalSource();
                var cancellationTokenSource = new CancellationTokenSource();
                var journal                 = new EventJournal(source, new Cancellation(() => cancellationTokenSource.Token));

                var task = Task.Run(() =>
                {
                    Action action = () =>
                    {
                        initializedEvent.Set();
                        journal
                        .When <QuestArrowEvent>(e => { })
                        .WaitAny();
                    };

                    action.ShouldThrow <OperationCanceledException>();
                });

                initializedEvent.WaitOne(100).Should()
                .BeTrue("awaiting should start immediatelly, false means a timeout");
                Thread.Yield();

                cancellationTokenSource.Cancel();

                task.Wait(TimeSpan.FromMilliseconds(100)).Should()
                .BeTrue("false means timeout - tested task was not executed in time");
            });
        }
예제 #4
0
        public void Can_cancel_All_handling_of_many_events_with_noncancellable_handlers()
        {
            var source = new EventJournalSource();
            var cancellationTokenSource = new CancellationTokenSource();
            var journal = new EventJournal(source, new Cancellation(() => cancellationTokenSource.Token));

            for (int i = 0; i < 10000; i++)
            {
                source.Publish(new SpeechRequestedEvent(i.ToString()));
            }

            var task = Task.Run(() =>
            {
                Action action = () =>
                {
                    journal.When <SpeechRequestedEvent>(e => { Thread.Sleep(25); })
                    .All();
                };

                action.Should().Throw <OperationCanceledException>();
            });

            journal.AwaitingStarted.AssertWaitOneSuccess();

            cancellationTokenSource.Cancel();
            task.AssertWaitFastSuccess();
        }
예제 #5
0
        public void Can_cancel_All_handling()
        {
            var source = new EventJournalSource();
            var cancellationTokenSource = new CancellationTokenSource();
            var journal = new EventJournal(source, new Cancellation(() => cancellationTokenSource.Token));

            var task = Task.Run(() =>
            {
                Action action = () =>
                {
                    while (true)
                    {
                        journal.When <SpeechRequestedEvent>(e => { })
                        .All();
                    }
                };

                action.Should().Throw <OperationCanceledException>();
            });

            journal.AwaitingStarted.AssertWaitOneSuccess();

            cancellationTokenSource.Cancel();
            task.AssertWaitFastSuccess();
        }
예제 #6
0
        public void Can_await_one_event()
        {
            var             source        = new EventJournalSource();
            var             journal       = new EventJournal(source);
            var             executedEvent = new AutoResetEvent(false);
            QuestArrowEvent receivedEvent = null;

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

            journal.AwaitingStarted.WaitOne(100).Should().BeTrue();

            source.Publish(new QuestArrowEvent(true, new Location2D(123, 321)));

            executedEvent.WaitOne(100).Should().BeTrue();

            receivedEvent.Should().NotBeNull();
            receivedEvent.Active.Should().BeTrue();
            receivedEvent.Location.Should().Be(new Location2D(123, 321));
        }
예제 #7
0
        public void Can_handle_event_published_during_handling_all_unprocessed_events()
        {
            ConcurrencyTester.Run(() =>
            {
                var source                 = new EventJournalSource();
                var journal                = new EventJournal(source);
                bool handlerInvoked        = false;
                var waitInsideEventHandler = new AutoResetEvent(false);

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

                var task = Task.Run(() =>
                {
                    journal
                    .When <SpeechRequestedEvent>(e =>
                    {
                        waitInsideEventHandler.WaitOne();
                    })
                    .All();
                });

                journal.AwaitingStarted.WaitOne(100).Should().BeTrue();

                source.Publish(new SpeechRequestedEvent("other text"));
                waitInsideEventHandler.Set();
                task.Wait(100).Should().BeTrue();

                journal
                .When <SpeechRequestedEvent>(e => handlerInvoked = true)
                .All();

                handlerInvoked.Should().BeTrue();
            });
        }
예제 #8
0
        public void Can_cancel_All_handling_of_many_events_with_noncancellable_handlers()
        {
            var source = new EventJournalSource();
            var cancellationTokenSource = new CancellationTokenSource();
            var journal = new EventJournal(source, new Cancellation(() => cancellationTokenSource.Token));

            for (int i = 0; i < 10000; i++)
            {
                source.Publish(new SpeechRequestedEvent(i.ToString()));
            }

            var task = Task.Run(() =>
            {
                Action action = () =>
                {
                    journal.When <SpeechRequestedEvent>(e => { Thread.Sleep(25); })
                    .All();
                };

                action.ShouldThrow <OperationCanceledException>();
            });

            journal.AwaitingStarted.WaitOne(100).Should().BeTrue();

            cancellationTokenSource.Cancel();
            task.Wait(TimeSpan.FromMilliseconds(100)).Should()
            .BeTrue("false means timeout - tested task was not cancelled in time");
        }
예제 #9
0
        public EventJournalBenchmarks()
        {
            emptySource  = new EventJournalSource();
            emptyJournal = new EventJournal(emptySource);

            fullSource  = new EventJournalSource();
            fullJournal = new EventJournal(fullSource);

            for (var i = 0; i <= fullSource.MaximumCapacity + 1; i++)
            {
                fullSource.Publish(new SpeechRequestedEvent("test"));
            }
        }
예제 #10
0
        public void Can_enumerate_all_journal_events()
        {
            var source  = new EventJournalSource();
            var journal = new EventJournal(source);

            source.Publish(new CommandRequestedEvent(",somesyntax"));
            source.Publish(new QuestArrowEvent(true, new Location2D(123, 321)));

            journal.Count().Should().Be(2);
            journal.First().Should().BeOfType <CommandRequestedEvent>()
            .Which.InvocationSyntax.Should().Be(",somesyntax");
            journal.Last().Should().BeOfType <QuestArrowEvent>()
            .Which.Location.Should().Be(new Location2D(123, 321));
        }
예제 #11
0
        public void Can_timeout_when_none_of_awaited_events_published()
        {
            var source  = new EventJournalSource();
            var journal = new EventJournal(source);

            Action actionThrowingTimeoutException = () =>
            {
                journal
                .When <QuestArrowEvent>(e => { })
                .WaitAny(TimeSpan.FromMilliseconds(10));
            };

            actionThrowingTimeoutException.Should().Throw <TimeoutException>();
        }
예제 #12
0
        public void Cannot_see_events_published_before_journal_creation()
        {
            var source = new EventJournalSource();

            source.Publish(new CommandRequestedEvent(",before1"));
            source.Publish(new CommandRequestedEvent(",before2"));

            var journal = new EventJournal(source);

            source.Publish(new CommandRequestedEvent(",after"));

            journal.Count().Should().Be(1, "1 event was added to event source before journal creation");
            journal.Single().Should().BeOfType <CommandRequestedEvent>()
            .Which.InvocationSyntax.Should().Be(",after");
        }
예제 #13
0
        public void Can_handle_incomming_events()
        {
            ConcurrencyTester.Run(() =>
            {
                var finishedEvent           = new AutoResetEvent(false);
                int whenExecutedCount       = 0;
                var source                  = new EventJournalSource();
                var cancellationTokenSource = new CancellationTokenSource();
                var journal                 = new EventJournal(source, new Cancellation(() => cancellationTokenSource.Token));
                var resultBuilder           = new StringBuilder();

                var task = Task.Run(() =>
                {
                    Action testedAction = () =>
                    {
                        journal
                        .When <SpeechRequestedEvent>(e =>
                        {
                            resultBuilder.Append(e.Message);
                            whenExecutedCount++;
                            if (whenExecutedCount >= 3)
                            {
                                finishedEvent.Set();
                            }
                        })
                        .Incomming();
                    };

                    testedAction.ShouldThrow <OperationCanceledException>();
                });

                journal.AwaitingStarted.WaitOne(100).Should().BeTrue();

                source.Publish(new SpeechRequestedEvent("message1"));
                source.Publish(new SpeechRequestedEvent("message2"));
                source.Publish(new SpeechRequestedEvent("message3"));

                finishedEvent.WaitOne(TimeSpan.FromMilliseconds(100)).Should()
                .BeTrue("the test didn't finished in time");

                cancellationTokenSource.Cancel();
                task.Wait(TimeSpan.FromMilliseconds(100)).Should()
                .BeTrue("false means timeout - tested task was not executed in time");

                resultBuilder.ToString().Should().Be("message1message2message3");
            });
        }
예제 #14
0
        public void Can_handle_all_unprocessed_events_with_multiple_subscriptions_to_one_event()
        {
            var  source               = new EventJournalSource();
            var  journal              = new EventJournal(source);
            bool firstHandlerInvoked  = false;
            bool secondHandlerInvoked = false;

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

            journal
            .When <SpeechRequestedEvent>(e => firstHandlerInvoked  = true)
            .When <SpeechRequestedEvent>(e => secondHandlerInvoked = true)
            .All();

            firstHandlerInvoked.Should().BeTrue();
            secondHandlerInvoked.Should().BeTrue();
        }
예제 #15
0
        public void All_events_handling_respects_when_predicate()
        {
            var  source  = new EventJournalSource();
            var  journal = new EventJournal(source);
            bool handlerWithFalsePredicate = false;
            bool handlerWithTruePredicate  = false;

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

            journal
            .When <SpeechRequestedEvent>(e => e.Message == "something else", e => handlerWithFalsePredicate = true)
            .When <SpeechRequestedEvent>(e => e.Message == "some text", e => handlerWithTruePredicate       = true)
            .All();

            handlerWithFalsePredicate.Should().BeFalse();
            handlerWithTruePredicate.Should().BeTrue();
        }
예제 #16
0
        public void Can_handle_incomming_events()
        {
            ConcurrencyTester.Run(() =>
            {
                var finishedEvent           = new AutoResetEvent(false);
                int whenExecutedCount       = 0;
                var source                  = new EventJournalSource();
                var cancellationTokenSource = new CancellationTokenSource();
                var journal                 = new EventJournal(source, new Cancellation(() => cancellationTokenSource.Token));
                var resultBuilder           = new StringBuilder();

                var task = Task.Run(() =>
                {
                    Action testedAction = () =>
                    {
                        journal
                        .When <SpeechRequestedEvent>(e =>
                        {
                            resultBuilder.Append(e.Message);
                            whenExecutedCount++;
                            if (whenExecutedCount >= 3)
                            {
                                finishedEvent.Set();
                            }
                        })
                        .Incomming();
                    };

                    testedAction.Should().Throw <OperationCanceledException>();
                });

                journal.AwaitingStarted.AssertWaitOneSuccess();

                source.Publish(new SpeechRequestedEvent("message1"));
                source.Publish(new SpeechRequestedEvent("message2"));
                source.Publish(new SpeechRequestedEvent("message3"));

                finishedEvent.AssertWaitOneSuccess();

                cancellationTokenSource.Cancel();
                task.AssertWaitFastSuccess();

                resultBuilder.ToString().Should().Be("message1message2message3");
            });
        }
예제 #17
0
        public void Can_handle_all_unprocessed_events()
        {
            var  source  = new EventJournalSource();
            var  journal = new EventJournal(source);
            bool speechRequestedEventHandled = false;
            bool questArrowEventHandled      = false;

            source.Publish(new SpeechRequestedEvent("some text"));
            source.Publish(new QuestArrowEvent(true, new Location2D(123, 321)));

            journal
            .When <SpeechRequestedEvent>(e => speechRequestedEventHandled = true)
            .When <QuestArrowEvent>(e => questArrowEventHandled           = true)
            .All();

            speechRequestedEventHandled.Should().BeTrue();
            questArrowEventHandled.Should().BeTrue();
        }
예제 #18
0
        public void Unprocessed_events_by_first_WaitAny_are_visible_to_second_WaitAny()
        {
            // It covers a case when script waits for an event and then has to wait for a second event.
            // The second event may arrive nearly at the same time as the first event.
            // There are two WaitAny calls in a single code block. There is just one action,
            // before the first WaitAny.
            // In other words, two consecutive calls to WaitAny have to process all events, no unhandled "holes"
            // are allowed.
            var    source  = new EventJournalSource();
            var    journal = new EventJournal(source);
            string result  = string.Empty;

            var task = Task.Run(() =>
            {
                journal
                .When <ContainerOpenedEvent>(e => result = "y")
                .When <SpeechReceivedEvent>(e => e.Speech.Message == "qwer", e => { result = "x"; })
                .WaitAny();
            });

            journal.AwaitingStarted.AssertWaitOneSuccess();
            source.Publish(new SpeechReceivedEvent(new JournalEntry(1, "qwer", "qwer", 0, 0, (Color)0)));
            source.Publish(new SpeechReceivedEvent(new JournalEntry(1, "asdf", "asdf", 0, 0, (Color)0)));
            task.AssertWaitFastSuccess();
            result.Should().Be("x");


            result = "z";
            task   = Task.Run(() =>
            {
                journal
                .When <ContainerOpenedEvent>(e => result = "published after the second WaitAny started")
                .When <SpeechReceivedEvent>(e => e.Speech.Message == "qwer", e => { result = "processed by the first WaitAny"; })
                .When <SpeechReceivedEvent>(e => e.Speech.Message == "asdf", e => { result = "not processed by the first WaitAny"; })
                .WaitAny();
            });

            journal.AwaitingStarted.AssertWaitOneSuccess();
            source.Publish(new ContainerOpenedEvent(1));

            task.AssertWaitFastSuccess();
            result.Should().Be("not processed by the first WaitAny");
        }
예제 #19
0
        public void Can_handle_all_unprocessed_speech_events()
        {
            var source  = new EventJournalSource();
            var journal = new EventJournal(source);

            source.Publish(new SpeechReceivedEvent(new JournalEntry(0, "name", "test1", 0, 0)));
            source.Publish(new SpeechReceivedEvent(new JournalEntry(0, "name", "test2", 0, 0)));

            bool test1Handled = false;
            bool test2Handled = false;

            journal
            .When("test1", () => test1Handled = true)
            .When("test2", () => test2Handled = true)
            .All();

            test1Handled.Should().BeTrue();
            test2Handled.Should().BeTrue();
        }
예제 #20
0
        public void Can_delete_journal_and_All_handles_next_event()
        {
            var source  = new EventJournalSource();
            var journal = new EventJournal(source);

            bool eventBeforeDeleteHandled = false;
            bool eventAfterDeleteHandled  = false;

            source.Publish(new SpeechRequestedEvent("event that should be deleted"));
            journal.Delete();
            source.Publish(new QuestArrowEvent(true, new Location2D(123, 321)));

            journal.When <SpeechRequestedEvent>(e => eventBeforeDeleteHandled = true)
            .When <QuestArrowEvent>(e => eventAfterDeleteHandled = true)
            .All();

            eventBeforeDeleteHandled.Should().BeFalse();
            eventAfterDeleteHandled.Should().BeTrue();
        }
예제 #21
0
        public void All_event_handling_doesnt_handle_handled_events()
        {
            var  source         = new EventJournalSource();
            var  journal        = new EventJournal(source);
            bool handlerInvoked = false;

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

            journal
            .When <SpeechRequestedEvent>(e => handlerInvoked = true)
            .All();

            handlerInvoked.Should().BeTrue();

            handlerInvoked = false;
            journal
            .When <SpeechRequestedEvent>(e => handlerInvoked = true)
            .All();

            handlerInvoked.Should().BeFalse();
        }
예제 #22
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");
            });
        }
예제 #23
0
        public void First_satisfied_conditional_when_is_executed()
        {
            bool conditionalWhenExecuted     = false;
            bool notSatisfiedConditionalWhen = false;
            bool unconditionalWhenExecuted   = false;
            var  source  = new EventJournalSource();
            var  journal = new EventJournal(source);

            var task = Task.Run(() =>
            {
                journal
                .When <SpeechRequestedEvent>(e => e.Message == "I don't handle this message", e =>
                {
                    notSatisfiedConditionalWhen = true;
                })
                .When <SpeechRequestedEvent>(e => e.Message == "I handle this message", e =>
                {
                    conditionalWhenExecuted = true;
                })
                .When <SpeechRequestedEvent>(e =>
                {
                    unconditionalWhenExecuted = true;
                })
                .WaitAny();
            });

            journal.AwaitingStarted.AssertWaitOneSuccess();

            source.Publish(new SpeechRequestedEvent("something else than I refuse this message"));

            task.AssertWaitFastSuccess();

            unconditionalWhenExecuted.Should().BeTrue();
            conditionalWhenExecuted.Should().BeFalse();
            notSatisfiedConditionalWhen.Should().BeFalse();
        }
예제 #24
0
        public void Publish_after_WaitAny_doesnt_influence_next_call_to_WaitAny()
        {
            var    source  = new EventJournalSource();
            var    journal = new EventJournal(source);
            string result  = string.Empty;

            var task = Task.Run(() =>
            {
                journal
                .When <ContainerOpenedEvent>(e => result = "y")
                .When <SpeechReceivedEvent>(e => e.Speech.Message == "qwer", e => { result = "x"; })
                .WaitAny();
            });

            journal.AwaitingStarted.WaitOne(100).Should().BeTrue();
            source.Publish(new SpeechReceivedEvent(new JournalEntry(1, "qwer", "qwer", 0, 0)));
            task.Wait(100).Should().BeTrue();
            result.Should().Be("x");

            source.Publish(new SpeechReceivedEvent(new JournalEntry(1, "qwer", "qwer", 0, 0)));

            result = "z";
            task   = Task.Run(() =>
            {
                journal
                .When <ContainerOpenedEvent>(e => result = "y")
                .When <SpeechReceivedEvent>(e => e.Speech.Message == "qwer", e => { result = "x"; })
                .WaitAny();
            });

            journal.AwaitingStarted.WaitOne(100).Should().BeTrue();
            source.Publish(new ContainerOpenedEvent(1));

            task.Wait(100).Should().BeTrue();
            result.Should().Be("y");
        }