public async Task Throws_an_exception_when_message_could_not_be_parsed() { var handlerMock = new Mock <IHandler>(); var streamMock = new Mock <IClientStreamWriter <ChaincodeMessage> >(); handlerMock.SetupGet(m => m.WriteStream).Returns(streamMock.Object); handlerMock.Setup <object>(m => m.ParseResponse(It.IsAny <ChaincodeMessage>(), It.IsAny <MessageMethod>())) .Throws <Exception>(); var sut = new MessageQueue(handlerMock.Object, new Mock <ILogger <MessageQueue> >().Object); var taskCompletionSource = new TaskCompletionSource <string>(); var queueMessage = new QueueMessage <string>(new ChaincodeMessage { ChannelId = "chaincode", Txid = "message" }, 0, taskCompletionSource); await sut.QueueMessage(queueMessage); sut.HandleMessageResponse(new ChaincodeMessage { ChannelId = "chaincode", Txid = "message" }); taskCompletionSource.Awaiting(t => t.Task) .Should().Throw <Exception>(); }
public void Fail_should_set_exception_of_TaskCompletionSource() { var taskCompletionSource = new TaskCompletionSource <int>(); var sut = new QueueMessage <int>(null, 0, taskCompletionSource); sut.Fail(new Exception("something went wrong")); taskCompletionSource.Awaiting(t => t.Task) .Should().Throw <Exception>() .WithMessage("something went wrong"); }
public async Task When_task_completes_on_UI_thread_slow_async_it_should_fail() { // Arrange var timer = new FakeClock(); var taskFactory = new TaskCompletionSource <bool>(); // Act Func <Task> action = () => taskFactory.Awaiting(t => (Task)t.Task).Should(timer).CompleteWithinAsync(100.Milliseconds()); timer.Complete(); // Assert await action.Should().ThrowAsync <XunitException>(); }
public void When_task_completes_slow_it_should_fail() { // Arrange var timer = new FakeClock(); var taskFactory = new TaskCompletionSource <bool>(); // Act Action action = () => taskFactory.Awaiting(t => (Task)t.Task).Should(timer).CompleteWithin(100.Milliseconds()); timer.RunsIntoTimeout(); // Assert action.Should().Throw <XunitException>(); }
public async Task When_task_completes_fast_async_it_should_succeed() { // Arrange var timer = new FakeClock(); var taskFactory = new TaskCompletionSource <bool>(); // Act Func <Task> action = () => taskFactory.Awaiting(t => (Task)t.Task).Should(timer).CompleteWithinAsync(100.Milliseconds()); taskFactory.SetResult(true); timer.Complete(); // Assert await action.Should().NotThrowAsync(); }
public void When_task_completes_fast_it_should_succeed() { // Arrange var timer = new FakeClock(); var taskFactory = new TaskCompletionSource <bool>(); // Act Action action = () => taskFactory.Awaiting(t => (Task)t.Task).Should(timer).CompleteWithin(100.Milliseconds()); taskFactory.SetResult(true); timer.CompletesBeforeTimeout(); // Assert action.Should().NotThrow(); }
public async Task OnActivate_RecentProject_ProjectLoadedAndModifiedProjectAutoSaved() { var sut = CreateSut(out IFileSystem fileSystem); sut.ProjectData.AutoSaveInterval = 100.Milliseconds(); sut.ProjectData.Settings.RecentProject = "recent.project"; var sample = new AccountingData { Accounts = new List <AccountingDataAccountGroup> { new AccountingDataAccountGroup { Account = new List <AccountDefinition> { new AccountDefinition { ID = 1, Name = "TheAccount" } } } } }; fileSystem.FileExists("recent.project").Returns(true); fileSystem.ReadAllTextFromFile("recent.project").Returns(sample.Serialize()); var fileSaved = new TaskCompletionSource <bool>(); fileSystem .When(x => x.WriteAllTextIntoFile(Arg.Any <string>(), Arg.Any <string>())) .Do(x => fileSaved.SetResult(true)); ((IActivate)sut).Activate(); await sut.Awaiting(x => x.LoadingTask).Should().CompleteWithinAsync(1.Seconds()); sut.ProjectData.IsModified = true; await fileSaved.Awaiting(x => x.Task).Should().CompleteWithinAsync( 1.Seconds(), "file should be saved by auto-save task"); using var _ = new AssertionScope(); sut.ProjectData.IsModified.Should() .BeTrue("the project is ONLY auto-saved and not saved to real project file"); sut.Accounts.AccountList.Should().BeEquivalentTo(new[] { new { Name = "TheAccount" } }); fileSystem.DidNotReceive().WriteAllTextIntoFile("recent.project", Arg.Any <string>()); fileSystem.Received(1).WriteAllTextIntoFile("recent.project~", Arg.Any <string>()); }
public void When_task_completes_slow_async_it_should_fail() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- var timer = new TestingTimer(); var taskFactory = new TaskCompletionSource <bool>(); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- Func <Task> action = () => taskFactory.Awaiting(t => t.Task).Should(timer).CompleteWithinAsync(100.Milliseconds()); timer.RunsIntoTimeout(); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- action.Should().Throw <XunitException>(); }
public void ShouldBeAbleToSpecifyThatSyncWorkIsLongRunning() { var taskCompletionSource = new TaskCompletionSource <object>(); _scheduler.Schedule( () => { try { ActorThreadAssertions.CurrentThreadShouldNotBeThreadPoolThread(); taskCompletionSource.TrySetResult(null); } catch (Exception exception) { taskCompletionSource.TrySetException(exception); } }, TimeSpan.FromMilliseconds(1000), ActorScheduleOptions.NoInitialDelay | ActorScheduleOptions.WorkIsLongRunning); taskCompletionSource.Awaiting(x => x.Task).Should().NotThrow(); }
public void When_task_completes_fast_async_it_should_succeed() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- var timer = new TestingTimer(); var taskFactory = new TaskCompletionSource <bool>(); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- Func <Task> action = () => taskFactory.Awaiting(t => t.Task).Should(timer).CompleteWithinAsync(100.Milliseconds()); taskFactory.SetResult(true); timer.CompletesBeforeTimeout(); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- action.Should().NotThrow(); }
public async Task Sync_work_in_async_method_is_taken_into_account() { // Arrange var timer = new FakeClock(); var taskFactory = new TaskCompletionSource <bool>(); // Act Func <Task> action = () => taskFactory .Awaiting(t => { timer.Delay(101.Milliseconds()); return((Task)t.Task); }) .Should(timer) .CompleteWithinAsync(100.Milliseconds()); taskFactory.SetResult(true); timer.Complete(); // Assert await action.Should().ThrowAsync <XunitException>(); }
public async Task OnActivate_NewProject_ProjectLoadedAndAutoSaveActive() { var sut = CreateSut(out IFileSystem fileSystem); sut.ProjectData.AutoSaveInterval = 100.Milliseconds(); sut.ProjectData.FileName = "new.project"; var fileSaved = new TaskCompletionSource <bool>(); fileSystem .When(x => x.WriteAllTextIntoFile(Arg.Any <string>(), Arg.Any <string>())) .Do(x => fileSaved.SetResult(true)); ((IActivate)sut).Activate(); sut.LoadingTask.Status.Should().Be(TaskStatus.RanToCompletion); sut.ProjectData.Load(new AccountingData()); sut.ProjectData.IsModified = true; await fileSaved.Awaiting(x => x.Task).Should().CompleteWithinAsync(1.Seconds()); using var _ = new AssertionScope(); sut.ProjectData.IsModified.Should().BeTrue(); fileSystem.Received(1).WriteAllTextIntoFile("new.project~", Arg.Any <string>()); }
public async Task OpenProjectCommand_HappyPath_BusyIndicatorChanged() { var settings = new Settings(); var fileSystem = Substitute.For <IFileSystem>(); var projectData = new ProjectData(settings, null !, null !, fileSystem, null !); var dialogs = Substitute.For <IDialogs>(); var busy = new BusyControlModel(); var sut = new MenuViewModel(projectData, busy, null !, null !, dialogs); long counter = 0; var tcs = new TaskCompletionSource <bool>(); dialogs.ShowOpenFileDialog(Arg.Any <string>()).Returns((DialogResult.OK, "dummy")); // Because awaiting "ExecuteUIThread" does not really await the action // we need to wait for two property changed events. var values = new List <bool>(); busy.PropertyChanged += (sender, args) => { if (args.PropertyName != "IsBusy") { return; } values.Add(busy.IsBusy); if (Interlocked.Increment(ref counter) == 2) { tcs.SetResult(true); } }; sut.OpenProjectCommand.Execute(null); await tcs.Awaiting(x => x.Task).Should().CompleteWithinAsync(1.Seconds(), "IsBusy should change twice"); values.Should().Equal(true, false); }
public async Task Uncompleted_TaskOfInt_WithTimeout_can_be_canceled_afterwards() { var tcs = new TaskCompletionSource<int>(); tcs .Awaiting(_ => _.Task.WithTimeout(TimeSpan.FromMilliseconds(500))) .ShouldThrowExactly<TimeoutException>(); }
public async Task Uncompleted_TaskOfInt_WithTimeout_can_fault_afterwards() { var tcs = new TaskCompletionSource<int>(); tcs .Awaiting(_ => _.Task.WithTimeout(TimeSpan.FromMilliseconds(500))) .ShouldThrowExactly<TimeoutException>(); tcs.SetException(new InvalidOperationException()); tcs .Awaiting(_ => _.Task.WithTimeout(TimeSpan.FromMilliseconds(500))) .ShouldThrowExactly<InvalidOperationException>(); }