コード例 #1
0
        public void Can_handle_events_published_after_action_but_before_awaiting()
        {
            var canPublishEvent = new AutoResetEvent(false);
            var canAwaitEvent   = new AutoResetEvent(false);
            var source          = new EventJournalSource();
            var journal         = new EventJournal(source);

            string handledMessage = string.Empty;
            var    task           = Task.Run(() =>
            {
                canPublishEvent.Set();
                canAwaitEvent.AssertWaitOneSuccess();

                journal.When <SpeechRequestedEvent>(ev => handledMessage = ev.Message)
                .WaitAny(TimeSpan.FromMilliseconds(100));
            });

            source.NotifyAction();
            canPublishEvent.AssertWaitOneSuccess();
            source.Publish(new SpeechRequestedEvent("message1"));
            canAwaitEvent.Set();

            journal.AwaitingStarted.AssertWaitOneSuccess();
            source.Publish(new SpeechRequestedEvent("message2 - we don't want to get this one"));

            task.AssertWaitFastSuccess();

            handledMessage.Should().Be("message1");
        }
コード例 #2
0
ファイル: IncommingTests.cs プロジェクト: uoinfusion/Infusion
        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_await_one_event()
        {
            var             source        = new EventJournalSource();
            var             journal       = new EventJournal(source);
            var             executedEvent = new AutoResetEvent(false);
            QuestArrowEvent receivedEvent = null;

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

            journal.AwaitingStarted.AssertWaitOneSuccess();

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

            executedEvent.AssertWaitOneSuccess();

            receivedEvent.Should().NotBeNull();
            receivedEvent.Active.Should().BeTrue();
            receivedEvent.Location.Should().Be(new Location2D(123, 321));
        }
コード例 #4
0
        public void Specific_Terminate_waits_for_command_termination()
        {
            var  commandStartedEvent = new AutoResetEvent(false);
            bool commandEndReached   = false;

            var command = new Command("cmd1", executionMode: CommandExecutionMode.Normal, commandAction: () =>
            {
                commandStartedEvent.Set();

                try
                {
                    DoSomeCancellableAction();
                }
                finally
                {
                    Thread.Sleep(10);
                    commandEndReached = true;
                }
            });

            commandHandler.RegisterCommand(command);
            commandHandler.Invoke("cmd1");

            commandStartedEvent.AssertWaitOneSuccess();
            commandHandler.Terminate("cmd1");

            var result = commandEndReached;

            result.Should().BeTrue();
        }
コード例 #5
0
        public void Can_invoke_syntax_with_parameterless_command()
        {
            var ev = new AutoResetEvent(false);

            commandHandler.RegisterCommand("testName", () => ev.Set());

            commandHandler.InvokeSyntax(",testName");
            ev.AssertWaitOneSuccess();
        }
コード例 #6
0
        public void Can_invoke_syntax_with_parametrized_command()
        {
            var actualParameters = string.Empty;
            var ev = new AutoResetEvent(false);

            commandHandler.RegisterCommand("testName", parameters =>
            {
                actualParameters = parameters;
                ev.Set();
            });

            commandHandler.InvokeSyntax(",testName parameter1 parameter2");
            ev.AssertWaitOneSuccess();
            actualParameters.Should().Be("parameter1 parameter2");
        }
コード例 #7
0
        public void Can_invoke_syntax_with_parametrized_command_without_parameters()
        {
            bool executed = false;
            var  finished = new AutoResetEvent(false);

            commandHandler.RegisterCommand("testName", () =>
            {
                executed = true;
                finished.Set();
            });

            commandHandler.InvokeSyntax(",testName");
            finished.AssertWaitOneSuccess();

            executed.Should().BeTrue();
        }
コード例 #8
0
ファイル: IncommingTests.cs プロジェクト: uoinfusion/Infusion
        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");
            });
        }
コード例 #9
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");
            });
        }