Permalink
Browse files

Add tests for Mono.Reactive.Testing.TestScheduler (almost copy of His…

…toricalSchedulerTest).
  • Loading branch information...
1 parent d3be46e commit 8a52fa66a25731a67ded70417649623ee13e04f2 @atsushieno atsushieno committed Feb 5, 2012
View
22 Mono.Reactive.Testing/Mono.Reactive.Testing.cs
@@ -314,11 +314,6 @@ public override string ToString ()
public class TestScheduler : VirtualTimeScheduler<long, long>
{
- static readonly Comparer<long> long_comparer = Comparer<long>.Default;
- static readonly Comparison<Task> comparison = (t1, t2) => long_comparer.Compare (t1.DueTime, t2.DueTime);
-
- List<Task> tasks = new List<Task> ();
-
// VirtualTimeScheduler members.
protected override long Add (long absolute, long relative)
@@ -328,29 +323,14 @@ protected override long Add (long absolute, long relative)
protected override DateTimeOffset ToDateTimeOffset (long absolute)
{
- return new DateTimeOffset (new DateTime (absolute));
+ return new DateTimeOffset (DateTime.MinValue.AddTicks (absolute), TimeSpan.Zero);
}
protected override long ToRelative (TimeSpan timeSpan)
{
return timeSpan.Ticks;
}
- class Task
- {
- public long DueTime;
- public Func<IDisposable> Action;
- }
-
- public override IDisposable ScheduleAbsolute<TState> (TState state, long dueTime, Func<IScheduler, TState, IDisposable> action)
- {
- Task t = default (Task);
- t = new Task () { DueTime = dueTime, Action = () => { tasks.Remove (t); return action (this, state); }};
- tasks.Add (t);
- tasks.Sort (comparison);
- return new CompositeDisposable (Disposable.Create (() => tasks.Remove (t)));
- }
-
// TestScheduler specific.
public ITestableObservable<T> CreateColdObservable<T> (params Recorded<Notification<T>> [] messages)
View
67 System.Reactive.Tests/Mono.Reactive.Testing/TestSchedulerTest.cs
@@ -0,0 +1,67 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Reactive;
+using System.Reactive.Concurrency;
+using System.Reactive.Disposables;
+using System.Reactive.Linq;
+using System.Threading;
+using NUnit.Framework;
+
+namespace Mono.Reactive.Testing.Tests
+{
+ [TestFixture]
+ public class TestSchedulerTest
+ {
+ class MyTestScheduler : TestScheduler
+ {
+ public long PublicAdd (long time)
+ {
+ return Add (Clock, time);
+ }
+
+ public long PublicToRelative (TimeSpan time)
+ {
+ return ToRelative (time);
+ }
+
+ public IScheduledItem<long> PublicGetNext ()
+ {
+ return GetNext ();
+ }
+ }
+
+ [Test]
+ public void Clock ()
+ {
+ var scheduler = new TestScheduler ();
+ Assert.AreEqual (0, scheduler.Clock, "#1"); // default
+ scheduler.AdvanceBy (TimeSpan.FromDays (1).Ticks);
+ Assert.AreEqual (TimeSpan.FromDays (1).Ticks, scheduler.Clock, "#2");
+ var dt = new DateTimeOffset (2012, 1, 1, 0, 0, 0, TimeSpan.Zero);
+ scheduler.AdvanceTo (dt.Ticks);
+ Assert.AreEqual (dt, new DateTimeOffset (scheduler.Clock, TimeSpan.Zero), "#3");
+ }
+
+ [Test]
+ public void AdvanceByRaisesEvent ()
+ {
+ var scheduler = new MyTestScheduler ();
+ Assert.AreEqual (TimeSpan.FromDays (1).Ticks, scheduler.PublicAdd (TimeSpan.FromDays (1).Ticks), "#0");
+ Assert.AreEqual (TimeSpan.FromDays (1).Ticks, scheduler.PublicToRelative (TimeSpan.FromDays (1)), "#0-2");
+ var source = Observable.Interval (TimeSpan.FromDays (1), scheduler);
+ int x = 0;
+ var dis = source.Subscribe (v => x++);
+ Assert.IsNotNull (scheduler.PublicGetNext (), "#1");
+ Assert.AreEqual (0, x, "#2");
+ scheduler.AdvanceBy (TimeSpan.FromHours (1).Ticks);
+ Assert.AreEqual (0, x, "#3");
+ scheduler.AdvanceBy (TimeSpan.FromDays (1).Ticks);
+ var item = scheduler.PublicGetNext ();
+ Assert.IsNotNull (item, "#5");
+ Assert.AreEqual (1, x, "#5");
+ dis.Dispose ();
+ }
+ }
+}
View
3 System.Reactive.Tests/System.Reactive.Concurrency/HistoricalSchedulerTest.cs
@@ -48,7 +48,7 @@ public void AdvanceByRaisesEvent ()
{
var scheduler = new MyHistoricalScheduler ();
Assert.AreEqual (DateTimeOffset.MinValue.AddDays (1), scheduler.PublicAdd (TimeSpan.FromDays (1)), "#0");
- Assert.AreEqual (TimeSpan.FromDays (1), scheduler.PublicToRelative (TimeSpan.FromDays (1)), "#0");
+ Assert.AreEqual (TimeSpan.FromDays (1), scheduler.PublicToRelative (TimeSpan.FromDays (1)), "#0-2");
var source = Observable.Interval (TimeSpan.FromDays (1), scheduler);
int x = 0;
var dis = source.Subscribe (v => x++);
@@ -60,6 +60,7 @@ public void AdvanceByRaisesEvent ()
var item = scheduler.PublicGetNext ();
Assert.IsNotNull (item, "#5");
Assert.AreEqual (1, x, "#5");
+ dis.Dispose ();
}
}
}
View
5 System.Reactive.Tests/System.Reactive.Tests.csproj
@@ -49,6 +49,7 @@
<Folder Include="System.Reactive.Subjects\" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="Mono.Reactive.Testing\TestSchedulerTest.cs" />
<Compile Include="System\MyException.cs" />
<Compile Include="System\ErrorScheduler.cs" />
<Compile Include="System\ObservableExtensionTest.cs" />
@@ -83,5 +84,9 @@
<Project>{0D7D238E-9FB4-4923-99EA-53CC34640235}</Project>
<Name>System.Reactive</Name>
</ProjectReference>
+ <ProjectReference Include="..\Mono.Reactive.Testing\Mono.Reactive.Testing.csproj">
+ <Project>{9FE08AE0-EF24-4498-A50F-6D314F51F18C}</Project>
+ <Name>Mono.Reactive.Testing</Name>
+ </ProjectReference>
</ItemGroup>
</Project>
View
2 mono-reactive.sln
@@ -38,7 +38,7 @@ Global
{C3403D3D-408A-4FD8-8C34-A8CD818D489A}.Release|Any CPU.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
- StartupItem = System.Reactive\System.Reactive.csproj
+ StartupItem = System.Reactive.Tests\System.Reactive.Tests.csproj
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

0 comments on commit 8a52fa6

Please sign in to comment.