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_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 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 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_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 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 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 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"); }); }