| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
using System; |
| 4 |
using System.Diagnostics; |
| 5 |
using System.Reactive.Concurrency; |
| 6 |
using System.Reactive.Disposables; |
| 7 |
using System.Threading; |
| 8 |
using Microsoft.Reactive.Testing; |
| 9 |
#if NUNIT |
| 10 |
using NUnit.Framework; |
| 11 |
using TestClassAttribute = NUnit.Framework.TestFixtureAttribute; |
| 12 |
using TestMethodAttribute = NUnit.Framework.TestAttribute; |
| 13 |
using TestInitializeAttribute = NUnit.Framework.SetUpAttribute; |
| 14 |
#else |
| 15 |
using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 16 |
#endif |
| 17 |
|
| 18 |
namespace ReactiveTests.Tests |
| 19 |
{ |
| 20 |
[TestClass] |
| 21 |
public class ImmediateSchedulerTest |
| 22 |
{ |
| 23 |
[TestMethod] |
| 24 |
public void Immediate_Now() |
| 25 |
{ |
| 26 |
var res = Scheduler.Immediate.Now - DateTime.Now; |
| 27 |
Assert.IsTrue(res.Seconds < 1); |
| 28 |
} |
| 29 |
|
| 30 |
[TestMethod] |
| 31 |
public void Immediate_ScheduleAction() |
| 32 |
{ |
| 33 |
var id = Thread.CurrentThread.ManagedThreadId; |
| 34 |
var ran = false; |
| 35 |
Scheduler.Immediate.Schedule(() => { Assert.AreEqual(id, Thread.CurrentThread.ManagedThreadId); ran = true; }); |
| 36 |
Assert.IsTrue(ran); |
| 37 |
} |
| 38 |
|
| 39 |
[TestMethod] |
| 40 |
public void Immediate_ScheduleActionError() |
| 41 |
{ |
| 42 |
var ex = new Exception(); |
| 43 |
|
| 44 |
try |
| 45 |
{ |
| 46 |
Scheduler.Immediate.Schedule(() => { throw ex; }); |
| 47 |
Assert.Fail(); |
| 48 |
} |
| 49 |
catch (Exception e) |
| 50 |
{ |
| 51 |
Assert.AreSame(e, ex); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
[TestMethod] |
| 56 |
public void Immediate_ArgumentChecking() |
| 57 |
{ |
| 58 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.Immediate.Schedule<int>(42, default(Func<IScheduler, int, IDisposable>))); |
| 59 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.Immediate.Schedule<int>(42, DateTimeOffset.Now, default(Func<IScheduler, int, IDisposable>))); |
| 60 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.Immediate.Schedule<int>(42, TimeSpan.Zero, default(Func<IScheduler, int, IDisposable>))); |
| 61 |
} |
| 62 |
|
| 63 |
[TestMethod] |
| 64 |
public void Immediate_Simple1() |
| 65 |
{ |
| 66 |
var _x = 0; |
| 67 |
Scheduler.Immediate.Schedule<int>(42, (self, x) => { _x = x; return Disposable.Empty; }); |
| 68 |
Assert.AreEqual(42, _x); |
| 69 |
} |
| 70 |
|
| 71 |
[TestMethod] |
| 72 |
public void Immediate_Simple2() |
| 73 |
{ |
| 74 |
var _x = 0; |
| 75 |
Scheduler.Immediate.Schedule<int>(42, DateTimeOffset.Now, (self, x) => { _x = x; return Disposable.Empty; }); |
| 76 |
Assert.AreEqual(42, _x); |
| 77 |
} |
| 78 |
|
| 79 |
[TestMethod] |
| 80 |
public void Immediate_Simple3() |
| 81 |
{ |
| 82 |
var _x = 0; |
| 83 |
Scheduler.Immediate.Schedule<int>(42, TimeSpan.Zero, (self, x) => { _x = x; return Disposable.Empty; }); |
| 84 |
Assert.AreEqual(42, _x); |
| 85 |
} |
| 86 |
|
| 87 |
[TestMethod] |
| 88 |
public void Immediate_Recursive1() |
| 89 |
{ |
| 90 |
var _x = 0; |
| 91 |
var _y = 0; |
| 92 |
Scheduler.Immediate.Schedule<int>(42, (self, x) => { _x = x; return self.Schedule<int>(43, (self2, y) => { _y = y; return Disposable.Empty; }); }); |
| 93 |
Assert.AreEqual(42, _x); |
| 94 |
Assert.AreEqual(43, _y); |
| 95 |
} |
| 96 |
|
| 97 |
[TestMethod] |
| 98 |
public void Immediate_Recursive2() |
| 99 |
{ |
| 100 |
var _x = 0; |
| 101 |
var _y = 0; |
| 102 |
Scheduler.Immediate.Schedule<int>(42, (self, x) => { _x = x; return self.Schedule<int>(43, DateTimeOffset.Now, (self2, y) => { _y = y; return Disposable.Empty; }); }); |
| 103 |
Assert.AreEqual(42, _x); |
| 104 |
Assert.AreEqual(43, _y); |
| 105 |
} |
| 106 |
|
| 107 |
[TestMethod] |
| 108 |
public void Immediate_Recursive3() |
| 109 |
{ |
| 110 |
var _x = 0; |
| 111 |
var _y = 0; |
| 112 |
Scheduler.Immediate.Schedule<int>(42, (self, x) => { _x = x; return self.Schedule<int>(43, TimeSpan.FromMilliseconds(100), (self2, y) => { _y = y; return Disposable.Empty; }); }); |
| 113 |
Assert.AreEqual(42, _x); |
| 114 |
Assert.AreEqual(43, _y); |
| 115 |
} |
| 116 |
|
| 117 |
[TestMethod] |
| 118 |
public void Immediate_ArgumentChecking_More() |
| 119 |
{ |
| 120 |
Scheduler.Immediate.Schedule(42, (self, state) => |
| 121 |
{ |
| 122 |
ReactiveAssert.Throws<ArgumentNullException>(() => |
| 123 |
{ |
| 124 |
self.Schedule(43, default(Func<IScheduler, int, IDisposable>)); |
| 125 |
}); |
| 126 |
|
| 127 |
return Disposable.Empty; |
| 128 |
}); |
| 129 |
|
| 130 |
Scheduler.Immediate.Schedule(42, (self, state) => |
| 131 |
{ |
| 132 |
ReactiveAssert.Throws<ArgumentNullException>(() => |
| 133 |
{ |
| 134 |
self.Schedule(43, TimeSpan.FromSeconds(1), default(Func<IScheduler, int, IDisposable>)); |
| 135 |
}); |
| 136 |
|
| 137 |
return Disposable.Empty; |
| 138 |
}); |
| 139 |
|
| 140 |
Scheduler.Immediate.Schedule(42, (self, state) => |
| 141 |
{ |
| 142 |
ReactiveAssert.Throws<ArgumentNullException>(() => |
| 143 |
{ |
| 144 |
self.Schedule(43, DateTimeOffset.UtcNow.AddDays(1), default(Func<IScheduler, int, IDisposable>)); |
| 145 |
}); |
| 146 |
|
| 147 |
return Disposable.Empty; |
| 148 |
}); |
| 149 |
} |
| 150 |
|
| 151 |
#if !SILVERLIGHT |
| 152 |
[TestMethod] |
| 153 |
[Ignore] |
| 154 |
public void Immediate_ScheduleActionDue() |
| 155 |
{ |
| 156 |
var id = Thread.CurrentThread.ManagedThreadId; |
| 157 |
var ran = false; |
| 158 |
var sw = new Stopwatch(); |
| 159 |
sw.Start(); |
| 160 |
Scheduler.Immediate.Schedule(TimeSpan.FromSeconds(0.2), () => { sw.Stop(); Assert.AreEqual(id, Thread.CurrentThread.ManagedThreadId); ran = true; }); |
| 161 |
Assert.IsTrue(ran, "ran"); |
| 162 |
Assert.IsTrue(sw.ElapsedMilliseconds > 180, "due " + sw.ElapsedMilliseconds); |
| 163 |
} |
| 164 |
#endif |
| 165 |
} |
| 166 |
} |