diff --git a/ReactiveUI.Testing/TestUtils.cs b/ReactiveUI.Testing/TestUtils.cs index 504bc8ff96..22aac1d6a4 100644 --- a/ReactiveUI.Testing/TestUtils.cs +++ b/ReactiveUI.Testing/TestUtils.cs @@ -38,6 +38,28 @@ public static IDisposable WithScheduler(IScheduler sched) }); } + /// + /// WithMessageBus allows you to override the default Message Bus + /// implementation until the object returned is disposed. If a + /// message bus is not specified, a default empty one is created. + /// + /// The message bus to use, or null to create + /// a new one using the default implementation. + /// An object that when disposed, restores the original + /// message bus. + public static IDisposable WithMessageBus(this IMessageBus messageBus) + { + var origMessageBus = RxApp.MessageBus; + + Monitor.Enter(mbGate); + RxApp.MessageBus = messageBus ?? new MessageBus(); + return Disposable.Create(() => + { + RxApp.MessageBus = origMessageBus; + Monitor.Exit(mbGate); + }); + } + /// /// With is an extension method that uses the given scheduler as the /// default Deferred and Taskpool schedulers for the given Func. Use @@ -47,7 +69,8 @@ public static IDisposable WithScheduler(IScheduler sched) /// The scheduler to use. /// The function to execute. /// The return value of the function. - public static TRet With(this IScheduler sched, Func block) + public static TRet With(this T sched, Func block) + where T : IScheduler { TRet ret; using (WithScheduler(sched)) { @@ -62,87 +85,60 @@ public static TRet With(this IScheduler sched, Func bloc /// /// The scheduler to use. /// The action to execute. - public static void With(this IScheduler sched, Action block) + public static void With(this T sched, Action block) + where T : IScheduler { sched.With(x => { block(x); return 0; }); } /// - /// With is an extension method that uses the given scheduler as the - /// default Deferred and Taskpool schedulers for the given Func. Use - /// this to initialize objects that store the default scheduler (most - /// RxXaml objects). + /// Override the default Message Bus during the specified block. /// - /// The scheduler to use. + /// The message bus to use for the block. /// The function to execute. /// The return value of the function. - public static TRet With(this TestScheduler sched, Func block) + public static TRet With(this IMessageBus messageBus, Func block) { - TRet ret; - using (WithScheduler(sched)) { - ret = block(sched); + using (messageBus.WithMessageBus()) { + return block(); } - return ret; } /// - /// With is an extension method that uses the given scheduler as the - /// default Deferred and Taskpool schedulers for the given Action. + /// Override the default Message Bus during the specified block. /// + /// The message bus to use for the block. /// The scheduler to use. /// The action to execute. - public static void With(this TestScheduler sched, Action block) + public static void With(this IMessageBus messageBus, Action block) { - sched.With(x => { block(x); return 0; }); + using (messageBus.WithMessageBus()) { + block(); + } } /// - /// WithMessageBus allows you to override the default Message Bus - /// implementation until the object returned is disposed. If a - /// message bus is not specified, a default empty one is created. + /// AdvanceToMs moves the TestScheduler to the specified time in + /// milliseconds. /// - /// The message bus to use, or null to create - /// a new one using the default implementation. - /// An object that when disposed, restores the original - /// message bus. - public static IDisposable WithMessageBus(this TestScheduler sched, IMessageBus messageBus = null) - { - var origMessageBus = RxApp.MessageBus; - - Monitor.Enter(mbGate); - RxApp.MessageBus = messageBus ?? new MessageBus(); - return Disposable.Create(() => - { - RxApp.MessageBus = origMessageBus; - Monitor.Exit(mbGate); - }); - } - - /// - /// WithMessageBus allows you to override the default Message Bus - /// implementation for a specified action. If a message bus is not - /// specified, a default empty one is created. - /// The action to execute. - /// The message bus to use, or null to create - /// a new one using the default implementation. - public static void WithMessageBus(this TestScheduler sched, Action block, IMessageBus messageBus = null) + /// The time offset to set the TestScheduler + /// to, in milliseconds. Note that this is *not* additive or + /// incremental, it sets the time. + public static void AdvanceToMs(this TestScheduler sched, double milliseconds) { - messageBus = messageBus ?? new MessageBus(); - using(var _ = sched.WithMessageBus(messageBus)) { - block(messageBus); - } + sched.AdvanceTo(sched.FromTimeSpan(TimeSpan.FromMilliseconds(milliseconds))); } /// - /// RunToMilliseconds moves the TestScheduler to the specified time in + /// AdvanceToMs moves the TestScheduler along by the specified time in /// milliseconds. /// /// The time offset to set the TestScheduler /// to, in milliseconds. Note that this is *not* additive or /// incremental, it sets the time. - public static void RunToMilliseconds(this TestScheduler sched, double milliseconds) + public static void AdvanceByMs(this TestScheduler sched, double milliseconds) { - sched.AdvanceTo(sched.FromTimeSpan(TimeSpan.FromMilliseconds(milliseconds))); + sched.AdvanceBy(sched.FromTimeSpan(TimeSpan.FromMilliseconds(milliseconds))); } /// @@ -200,4 +196,4 @@ public static long FromTimeSpan(this TestScheduler sched, TimeSpan span) } } -// vim: tw=120 ts=4 sw=4 et : +// vim: tw=120 ts=4 sw=4 et : \ No newline at end of file diff --git a/ReactiveUI.Tests/ObservableAsPropertyHelperTest.cs b/ReactiveUI.Tests/ObservableAsPropertyHelperTest.cs index bb5fd73d2d..8123f083e5 100644 --- a/ReactiveUI.Tests/ObservableAsPropertyHelperTest.cs +++ b/ReactiveUI.Tests/ObservableAsPropertyHelperTest.cs @@ -95,7 +95,7 @@ public void OAPHShouldBeObservable() var fixture = new ObservableAsPropertyHelper(inputOaph.Select(x => x.ToString()), result.Add, "0"); - sched.RunToMilliseconds(500); + sched.AdvanceToMs(500); new[] {"0", "5", "10", "15", "20"}.AssertAreEqual(result); }); diff --git a/ReactiveUI.Tests/ObservableAsyncMRUCacheTest.cs b/ReactiveUI.Tests/ObservableAsyncMRUCacheTest.cs index b23b3f46a8..0ecda6b4b9 100644 --- a/ReactiveUI.Tests/ObservableAsyncMRUCacheTest.cs +++ b/ReactiveUI.Tests/ObservableAsyncMRUCacheTest.cs @@ -31,13 +31,13 @@ public void GetTest() t.Start(); sched.Start(); - sched.RunToMilliseconds(500); + sched.AdvanceToMs(500); // NB: The Thread.Sleep is to let our other thread catch up Thread.Sleep(100); Assert.Equal(0, result); - sched.RunToMilliseconds(1200); + sched.AdvanceToMs(1200); Thread.Sleep(100); Assert.Equal(25, result); diff --git a/ReactiveUI.Tests/ObservedChangedMixinTest.cs b/ReactiveUI.Tests/ObservedChangedMixinTest.cs index 940cff3e03..a2c47c68c7 100644 --- a/ReactiveUI.Tests/ObservedChangedMixinTest.cs +++ b/ReactiveUI.Tests/ObservedChangedMixinTest.cs @@ -33,7 +33,7 @@ public void GetValueShouldActuallyReturnTheValue() foreach (var v in input) { fixture.IsOnlyOneWord = v; } - sched.RunToMilliseconds(1000); + sched.AdvanceToMs(1000); input.AssertAreEqual(output); input.AssertAreEqual(output2); @@ -89,7 +89,7 @@ public void ValueTest() foreach (var v in input) { fixture.IsOnlyOneWord = v; } - sched.RunToMilliseconds(1000); + sched.AdvanceToMs(1000); input.AssertAreEqual(output); input.AssertAreEqual(output2); diff --git a/ReactiveUI.Tests/ReactiveCollectionTest.cs b/ReactiveUI.Tests/ReactiveCollectionTest.cs index efe00d0d36..cf0df0c565 100644 --- a/ReactiveUI.Tests/ReactiveCollectionTest.cs +++ b/ReactiveUI.Tests/ReactiveCollectionTest.cs @@ -237,13 +237,13 @@ public void CreateCollectionWithTimer() ReactiveCollection fixture; fixture = input.ToObservable(sched).CreateCollection(TimeSpan.FromSeconds(0.5)); - sched.RunToMilliseconds(1005); + sched.AdvanceToMs(1005); fixture.AssertAreEqual(input.Take(2)); - sched.RunToMilliseconds(1505); + sched.AdvanceToMs(1505); fixture.AssertAreEqual(input.Take(3)); - sched.RunToMilliseconds(10000); + sched.AdvanceToMs(10000); fixture.AssertAreEqual(input); } } diff --git a/ReactiveUI.Tests/ReactiveCommandTest.cs b/ReactiveUI.Tests/ReactiveCommandTest.cs index f209d2cd22..8e1f54752b 100644 --- a/ReactiveUI.Tests/ReactiveCommandTest.cs +++ b/ReactiveUI.Tests/ReactiveCommandTest.cs @@ -57,7 +57,7 @@ public void ObservableCanExecuteShouldShowUpInCommand() // N.B. We check against '5' instead of 6 because we're supposed to // suppress changes that aren't actually changes i.e. false => false - sched.RunToMilliseconds(10 * 1000); + sched.AdvanceToMs(10 * 1000); return changes_as_observable; }); @@ -115,7 +115,7 @@ public void MultipleSubscribesShouldntResultInMultipleNotifications() fixture.Where(x => ((int)x) % 2 == 0).Subscribe(x => even_list.Add((int)x)); input.Run(x => fixture.Execute(x)); - sched.RunToMilliseconds(1000); + sched.AdvanceToMs(1000); new[]{1,1}.AssertAreEqual(odd_list); new[]{2,2}.AssertAreEqual(even_list); @@ -196,14 +196,14 @@ public void RegisterAsyncFunctionSmokeTest() Observable.Return(5).Delay(TimeSpan.FromSeconds(5), sched)).CreateCollection(); var inflightResults = fixture.ItemsInflight.CreateCollection(); - sched.RunToMilliseconds(10); + sched.AdvanceToMs(10); Assert.True(fixture.CanExecute(null)); fixture.Execute(null); - sched.RunToMilliseconds(1005); + sched.AdvanceToMs(1005); Assert.False(fixture.CanExecute(null)); - sched.RunToMilliseconds(5100); + sched.AdvanceToMs(5100); Assert.True(fixture.CanExecute(null)); new[] {0,1,0}.AssertAreEqual(inflightResults); @@ -264,10 +264,10 @@ public void MultipleSubscribersShouldntDecrementRefcountBelowZero() Assert.True(fixture.CanExecute(null)); fixture.Execute(null); - sched.RunToMilliseconds(2000); + sched.AdvanceToMs(2000); Assert.False(fixture.CanExecute(null)); - sched.RunToMilliseconds(6000); + sched.AdvanceToMs(6000); Assert.True(fixture.CanExecute(null)); Assert.True(results.Count == 1); @@ -373,7 +373,7 @@ public void CanExecuteShouldChangeOnInflightOp() // CanExecute should be true, both input observable is true // and we don't have anything inflight - sched.RunToMilliseconds(10); + sched.AdvanceToMs(10); Assert.True(fixture.CanExecute(1)); Assert.True(latestCanExecute); @@ -381,28 +381,28 @@ public void CanExecuteShouldChangeOnInflightOp() fixture.Execute(1); // At 300ms, input is false - sched.RunToMilliseconds(300); + sched.AdvanceToMs(300); Assert.False(fixture.CanExecute(1)); Assert.False(latestCanExecute); // At 600ms, input is true, but the command is still running - sched.RunToMilliseconds(600); + sched.AdvanceToMs(600); Assert.False(fixture.CanExecute(1)); Assert.False(latestCanExecute); // After we've completed, we should still be false, since from // 750ms-1000ms the input observable is false - sched.RunToMilliseconds(900); + sched.AdvanceToMs(900); Assert.False(fixture.CanExecute(1)); Assert.False(latestCanExecute); Assert.Equal(-1, calculatedResult); - sched.RunToMilliseconds(1010); + sched.AdvanceToMs(1010); Assert.True(fixture.CanExecute(1)); Assert.True(latestCanExecute); Assert.Equal(calculatedResult, 5); - sched.RunToMilliseconds(1200); + sched.AdvanceToMs(1200); Assert.False(fixture.CanExecute(1)); Assert.False(latestCanExecute); });