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(); }); }
public void Can_use_same_journal_concurrently() { ConcurrencyTester.Run(() => { var source = new EventJournalSource(); var journal = new EventJournal(source); string task1Result = string.Empty; string task2Result = string.Empty; var task1 = Task.Run(() => { journal.When <SpeechRequestedEvent>(e => { task1Result = "SpeechRequestedEvent"; }) .WaitAny(); }); journal.AwaitingStarted.AssertWaitOneSuccess(); var task2 = Task.Run(() => { journal.When <DialogBoxOpenedEvent>(e => { task2Result = "DialogBoxOpenedEvent"; }) .WaitAny(); }); journal.AwaitingStarted.AssertWaitOneSuccess(); source.Publish(new SpeechRequestedEvent("test")); source.Publish(new DialogBoxOpenedEvent(new DialogBox(1, 1, "", null))); task1.AssertWaitFastSuccess(); task2.AssertWaitFastSuccess(); task1Result.Should().Be("SpeechRequestedEvent"); task2Result.Should().Be("DialogBoxOpenedEvent"); }); }
public void MakeStepByKey(int key) { DateTime startTime; int attempts = 0; int maxAttempts = 10; bool moveRequestFailed = false; do { bool moveRequestSent = false; do { api.NotifyAction(); this.api.ClientWindow.PressKey((KeyCode)key); journal.When <Events.PlayerMoveRequestedEvent>(request => { moveRequestSent = true; }) .WhenTimeout(() => { moveRequestSent = false; }) .WaitAny(TimeSpan.FromSeconds(1)); } while (!moveRequestSent); startTime = DateTime.UtcNow; journal.When <Events.PlayerMoveRejectedEvent>(p => { moveRequestFailed = true; }) .When <Events.PlayerMoveAcceptedEvent>(p => { moveRequestFailed = false; }) .WhenTimeout(() => { if (attempts > maxAttempts) { throw new InjectionException("Cannot walk"); } moveRequestFailed = true; }) .WaitAny(TimeSpan.FromSeconds(30)); api.Wait(25); attempts++; } while (moveRequestFailed); var endTime = DateTime.UtcNow; var minDuration = TimeSpan.FromMilliseconds(150); var duration = endTime - startTime; if (duration < minDuration) { api.Wait((int)(minDuration - duration).TotalMilliseconds); } }
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(); }
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(); }
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(); }); }
public void First_unconditional_when_is_executed() { bool conditionalWhenExecuted = false; bool unconditionalWhenExecuted = false; var source = new EventJournalSource(); var journal = new EventJournal(source); var task = Task.Run(() => { journal .When <SpeechRequestedEvent>(e => { unconditionalWhenExecuted = true; }) .When <SpeechRequestedEvent>(e => { // another unconditional when, that cannot be executed }) .When <SpeechRequestedEvent>(e => e.Message == "I handle this message", e => { conditionalWhenExecuted = true; }) .WaitAny(); }); journal.AwaitingStarted.AssertWaitOneSuccess(); source.Publish(new SpeechRequestedEvent("I handle this message")); task.AssertWaitFastSuccess(); conditionalWhenExecuted.Should().BeFalse(); unconditionalWhenExecuted.Should().BeTrue(); }
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"); }); }
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"); }); }
public void When_can_refuse_to_handle_message() { bool conditionalWhenExecuted = false; bool unconditionalWhenExecuted = false; var source = new EventJournalSource(); var journal = new EventJournal(source); var task = Task.Run(() => { journal .When <SpeechRequestedEvent>(e => e.Message == "I refuse 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(); conditionalWhenExecuted.Should().BeFalse(); unconditionalWhenExecuted.Should().BeTrue(); }
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"); }
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"); }
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"); }
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)); }
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"); }
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(); }
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>(); }
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"); }
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"); }); }
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(); }
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(); }
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"); }); }
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(); }
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(); }
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(); }
public void Receive(int id, int timeout) { var target = api.GameObjects[(ObjectId)id]; if (target == null) { return; } api.Click(target); if (string.IsNullOrEmpty(target.Name) && timeout > 0) { journal.When <Events.SpeechReceivedEvent>( ev => ev.Speech.SpeakerId == target.Id, ev => { }) .WhenTimeout(() => { }) .WaitAny(TimeSpan.FromMilliseconds(timeout)); } }
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"); }); }
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(); }
public void HandlingAllWithEmptyJournal() { emptyJournal.When <SpeechRequestedEvent>(e => { }) .All(); }
public void HandlingAllWithFullJournal_NoneEventsHandled() { fullJournal.When <DialogBoxOpenedEvent>(e => { }) .All(); }