| 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.Collections.Generic; |
| 5 |
using System.Linq; |
| 6 |
using System.Reactive.Concurrency; |
| 7 |
using System.Reactive.Disposables; |
| 8 |
using System.Reactive.Linq; |
| 9 |
using System.Reactive.PlatformServices; |
| 10 |
using System.Runtime.CompilerServices; |
| 11 |
using System.Threading; |
| 12 |
#if NUNIT |
| 13 |
using NUnit.Framework; |
| 14 |
using TestClassAttribute = NUnit.Framework.TestFixtureAttribute; |
| 15 |
using TestMethodAttribute = NUnit.Framework.TestAttribute; |
| 16 |
using TestInitializeAttribute = NUnit.Framework.SetUpAttribute; |
| 17 |
#else |
| 18 |
using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 19 |
#endif |
| 20 |
using Microsoft.Reactive.Testing; |
| 21 |
|
| 22 |
#if HAS_WINFORMS |
| 23 |
using System.Windows.Forms; |
| 24 |
#endif |
| 25 |
|
| 26 |
namespace ReactiveTests.Tests |
| 27 |
{ |
| 28 |
[TestClass] |
| 29 |
public class SchedulerTest |
| 30 |
{ |
| 31 |
#region IScheduler |
| 32 |
|
| 33 |
[TestMethod] |
| 34 |
public void Scheduler_ArgumentChecks() |
| 35 |
{ |
| 36 |
var ms = new MyScheduler(); |
| 37 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.Schedule(default(IScheduler), a => { })); |
| 38 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.Schedule(default(IScheduler), () => { })); |
| 39 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.Schedule(default(IScheduler), 1, (a, s) => { })); |
| 40 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.Schedule(ms, default(Action<Action>))); |
| 41 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.Schedule(ms, 1, default(Action<int, Action<int>>))); |
| 42 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.Schedule(default(IScheduler), DateTimeOffset.Now, a => { })); |
| 43 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.Schedule(default(IScheduler), DateTimeOffset.Now, () => { })); |
| 44 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.Schedule(default(IScheduler), 1, DateTimeOffset.Now, (a, s) => { })); |
| 45 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.Schedule(ms, DateTimeOffset.Now, default(Action<Action<DateTimeOffset>>))); |
| 46 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.Schedule(ms, 1, DateTimeOffset.Now, default(Action<int, Action<int, DateTimeOffset>>))); |
| 47 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.Schedule(default(IScheduler), TimeSpan.Zero, a => { })); |
| 48 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.Schedule(default(IScheduler), TimeSpan.Zero, () => { })); |
| 49 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.Schedule(default(IScheduler), 1, TimeSpan.Zero, (a, s) => { })); |
| 50 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.Schedule(ms, TimeSpan.Zero, default(Action<Action<TimeSpan>>))); |
| 51 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.Schedule(ms, 1, TimeSpan.Zero, default(Action<int, Action<int, TimeSpan>>))); |
| 52 |
} |
| 53 |
|
| 54 |
[TestMethod] |
| 55 |
public void Schedulers_ArgumentChecks() |
| 56 |
{ |
| 57 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.CurrentThread.Schedule(default(Action))); |
| 58 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.CurrentThread.Schedule(TimeSpan.Zero, default(Action))); |
| 59 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.CurrentThread.Schedule(DateTimeOffset.MaxValue, default(Action))); |
| 60 |
#if !NO_WINDOWS_THREADING |
| 61 |
ReactiveAssert.Throws<ArgumentNullException>(() => DispatcherScheduler.Instance.Schedule(default(Action))); |
| 62 |
ReactiveAssert.Throws<ArgumentNullException>(() => DispatcherScheduler.Instance.Schedule(TimeSpan.Zero, default(Action))); |
| 63 |
ReactiveAssert.Throws<ArgumentNullException>(() => DispatcherScheduler.Instance.Schedule(DateTimeOffset.MaxValue, default(Action))); |
| 64 |
#endif |
| 65 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.Immediate.Schedule(default(Action))); |
| 66 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.Immediate.Schedule(TimeSpan.Zero, default(Action))); |
| 67 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.Immediate.Schedule(DateTimeOffset.MaxValue, default(Action))); |
| 68 |
ReactiveAssert.Throws<ArgumentNullException>(() => NewThreadScheduler.Default.Schedule(default(Action))); |
| 69 |
ReactiveAssert.Throws<ArgumentNullException>(() => NewThreadScheduler.Default.Schedule(TimeSpan.Zero, default(Action))); |
| 70 |
ReactiveAssert.Throws<ArgumentNullException>(() => NewThreadScheduler.Default.Schedule(DateTimeOffset.MaxValue, default(Action))); |
| 71 |
#if !NO_TPL |
| 72 |
ReactiveAssert.Throws<ArgumentNullException>(() => TaskPoolScheduler.Default.Schedule(default(Action))); |
| 73 |
ReactiveAssert.Throws<ArgumentNullException>(() => TaskPoolScheduler.Default.Schedule(TimeSpan.Zero, default(Action))); |
| 74 |
ReactiveAssert.Throws<ArgumentNullException>(() => TaskPoolScheduler.Default.Schedule(DateTimeOffset.MaxValue, default(Action))); |
| 75 |
#endif |
| 76 |
ReactiveAssert.Throws<ArgumentNullException>(() => DefaultScheduler.Instance.Schedule(default(Action))); |
| 77 |
ReactiveAssert.Throws<ArgumentNullException>(() => DefaultScheduler.Instance.Schedule(TimeSpan.Zero, default(Action))); |
| 78 |
ReactiveAssert.Throws<ArgumentNullException>(() => DefaultScheduler.Instance.Schedule(DateTimeOffset.MaxValue, default(Action))); |
| 79 |
ReactiveAssert.Throws<ArgumentNullException>(() => DefaultScheduler.Instance.SchedulePeriodic(42, TimeSpan.FromSeconds(1), default(Func<int, int>))); |
| 80 |
ReactiveAssert.Throws<ArgumentOutOfRangeException>(() => DefaultScheduler.Instance.SchedulePeriodic(42, TimeSpan.FromSeconds(-1), _ => _)); |
| 81 |
#if HAS_WINFORMS |
| 82 |
var lbl = new Label(); |
| 83 |
ReactiveAssert.Throws<ArgumentNullException>(() => new ControlScheduler(lbl).Schedule(default(Action))); |
| 84 |
ReactiveAssert.Throws<ArgumentNullException>(() => new ControlScheduler(lbl).Schedule(TimeSpan.Zero, default(Action))); |
| 85 |
ReactiveAssert.Throws<ArgumentNullException>(() => new ControlScheduler(lbl).Schedule(DateTimeOffset.MaxValue, default(Action))); |
| 86 |
#endif |
| 87 |
var ctx = new SynchronizationContext(); |
| 88 |
ReactiveAssert.Throws<ArgumentNullException>(() => new SynchronizationContextScheduler(ctx).Schedule(default(Action))); |
| 89 |
ReactiveAssert.Throws<ArgumentNullException>(() => new SynchronizationContextScheduler(ctx).Schedule(TimeSpan.Zero, default(Action))); |
| 90 |
ReactiveAssert.Throws<ArgumentNullException>(() => new SynchronizationContextScheduler(ctx).Schedule(DateTimeOffset.MaxValue, default(Action))); |
| 91 |
} |
| 92 |
|
| 93 |
[TestMethod] |
| 94 |
public void Scheduler_ScheduleNonRecursive() |
| 95 |
{ |
| 96 |
var ms = new MyScheduler(); |
| 97 |
var res = false; |
| 98 |
Scheduler.Schedule(ms, a => { res = true; }); |
| 99 |
Assert.IsTrue(res); |
| 100 |
} |
| 101 |
|
| 102 |
[TestMethod] |
| 103 |
public void Scheduler_ScheduleRecursive() |
| 104 |
{ |
| 105 |
var ms = new MyScheduler(); |
| 106 |
var i = 0; |
| 107 |
Scheduler.Schedule(ms, a => { if (++i < 10) a(); }); |
| 108 |
Assert.AreEqual(10, i); |
| 109 |
} |
| 110 |
|
| 111 |
[TestMethod] |
| 112 |
public void Scheduler_ScheduleWithTimeNonRecursive() |
| 113 |
{ |
| 114 |
var now = DateTimeOffset.Now; |
| 115 |
var ms = new MyScheduler(now) { Check = (a, s, t) => { Assert.IsTrue(t == TimeSpan.Zero); } }; |
| 116 |
var res = false; |
| 117 |
Scheduler.Schedule(ms, now, a => { res = true; }); |
| 118 |
Assert.IsTrue(res); |
| 119 |
Assert.IsTrue(ms.WaitCycles == 0); |
| 120 |
} |
| 121 |
|
| 122 |
[TestMethod] |
| 123 |
public void Scheduler_ScheduleWithTimeRecursive() |
| 124 |
{ |
| 125 |
var now = DateTimeOffset.Now; |
| 126 |
var i = 0; |
| 127 |
var ms = new MyScheduler(now) { Check = (a, s, t) => { Assert.IsTrue(t == TimeSpan.Zero); } }; |
| 128 |
Scheduler.Schedule(ms, now, a => { if (++i < 10) a(now); }); |
| 129 |
Assert.IsTrue(ms.WaitCycles == 0); |
| 130 |
Assert.AreEqual(10, i); |
| 131 |
} |
| 132 |
|
| 133 |
[TestMethod] |
| 134 |
public void Scheduler_ScheduleWithTimeSpanNonRecursive() |
| 135 |
{ |
| 136 |
var now = DateTimeOffset.Now; |
| 137 |
var ms = new MyScheduler(now) { Check = (a, s, t) => { Assert.IsTrue(t == TimeSpan.Zero); } }; |
| 138 |
var res = false; |
| 139 |
Scheduler.Schedule(ms, TimeSpan.Zero, a => { res = true; }); |
| 140 |
Assert.IsTrue(res); |
| 141 |
Assert.IsTrue(ms.WaitCycles == 0); |
| 142 |
} |
| 143 |
|
| 144 |
[TestMethod] |
| 145 |
public void Scheduler_ScheduleWithTimeSpanRecursive() |
| 146 |
{ |
| 147 |
var now = DateTimeOffset.Now; |
| 148 |
var ms = new MyScheduler(now) { Check = (a, s, t) => { Assert.IsTrue(t < TimeSpan.FromTicks(10)); } }; |
| 149 |
var i = 0; |
| 150 |
Scheduler.Schedule(ms, TimeSpan.Zero, a => { if (++i < 10) a(TimeSpan.FromTicks(i)); }); |
| 151 |
Assert.IsTrue(ms.WaitCycles == Enumerable.Range(1, 9).Sum()); |
| 152 |
Assert.AreEqual(10, i); |
| 153 |
} |
| 154 |
|
| 155 |
[TestMethod] |
| 156 |
public void Scheduler_StateThreading() |
| 157 |
{ |
| 158 |
var lst = new List<int>(); |
| 159 |
Scheduler.Immediate.Schedule(0, (i, a) => |
| 160 |
{ |
| 161 |
lst.Add(i); |
| 162 |
if (i < 9) |
| 163 |
a(i + 1); |
| 164 |
}); |
| 165 |
|
| 166 |
Assert.IsTrue(lst.SequenceEqual(Enumerable.Range(0, 10))); |
| 167 |
} |
| 168 |
|
| 169 |
[TestMethod] |
| 170 |
public void Scheduler_Builtins() |
| 171 |
{ |
| 172 |
// Default |
| 173 |
{ |
| 174 |
var e = new ManualResetEvent(false); |
| 175 |
Scheduler.Default.Schedule(() => e.Set()); |
| 176 |
e.WaitOne(); |
| 177 |
} |
| 178 |
|
| 179 |
if (!Utils.IsRunningWithPortableLibraryBinaries()) |
| 180 |
{ |
| 181 |
Scheduler_Builtins_NoPlib(); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
[MethodImpl(MethodImplOptions.NoInlining)] |
| 186 |
private void Scheduler_Builtins_NoPlib() |
| 187 |
{ |
| 188 |
#if !PLIB |
| 189 |
// ThreadPool |
| 190 |
{ |
| 191 |
var e = new ManualResetEvent(false); |
| 192 |
Scheduler.ThreadPool.Schedule(() => e.Set()); |
| 193 |
e.WaitOne(); |
| 194 |
} |
| 195 |
#endif |
| 196 |
|
| 197 |
#if !NO_THREAD |
| 198 |
// NewThread |
| 199 |
{ |
| 200 |
var e = new ManualResetEvent(false); |
| 201 |
Scheduler.NewThread.Schedule(() => e.Set()); |
| 202 |
e.WaitOne(); |
| 203 |
} |
| 204 |
#endif |
| 205 |
|
| 206 |
#if !NO_TPL |
| 207 |
// TaskPool |
| 208 |
{ |
| 209 |
var e = new ManualResetEvent(false); |
| 210 |
Scheduler.TaskPool.Schedule(() => e.Set()); |
| 211 |
e.WaitOne(); |
| 212 |
} |
| 213 |
#endif |
| 214 |
} |
| 215 |
|
| 216 |
#endregion |
| 217 |
|
| 218 |
#region ISchedulerLongRunning |
| 219 |
|
| 220 |
#if !NO_PERF |
| 221 |
[TestMethod] |
| 222 |
public void Scheduler_LongRunning_ArgumentChecking() |
| 223 |
{ |
| 224 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.ScheduleLongRunning(null, c => { })); |
| 225 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.ScheduleLongRunning(ThreadPoolScheduler.Instance, default(Action<ICancelable>))); |
| 226 |
} |
| 227 |
|
| 228 |
[TestMethod] |
| 229 |
public void Scheduler_Periodic_ArgumentChecking() |
| 230 |
{ |
| 231 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.SchedulePeriodic(null, TimeSpan.FromSeconds(1), () => { })); |
| 232 |
ReactiveAssert.Throws<ArgumentOutOfRangeException>(() => Scheduler.SchedulePeriodic(ThreadPoolScheduler.Instance, TimeSpan.FromSeconds(-1), () => { })); |
| 233 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.SchedulePeriodic(ThreadPoolScheduler.Instance, TimeSpan.FromSeconds(1), default(Action))); |
| 234 |
|
| 235 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.SchedulePeriodic<int>(null, 42, TimeSpan.FromSeconds(1), _ => { })); |
| 236 |
ReactiveAssert.Throws<ArgumentOutOfRangeException>(() => Scheduler.SchedulePeriodic<int>(ThreadPoolScheduler.Instance, 42, TimeSpan.FromSeconds(-1), _ => { })); |
| 237 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.SchedulePeriodic<int>(ThreadPoolScheduler.Instance, 42, TimeSpan.FromSeconds(1), default(Action<int>))); |
| 238 |
|
| 239 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.SchedulePeriodic<int>(null, 42, TimeSpan.FromSeconds(1), _ => _)); |
| 240 |
ReactiveAssert.Throws<ArgumentOutOfRangeException>(() => Scheduler.SchedulePeriodic<int>(ThreadPoolScheduler.Instance, 42, TimeSpan.FromSeconds(-1), _ => _)); |
| 241 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.SchedulePeriodic<int>(ThreadPoolScheduler.Instance, 42, TimeSpan.FromSeconds(1), default(Func<int, int>))); |
| 242 |
} |
| 243 |
|
| 244 |
[TestMethod] |
| 245 |
public void Scheduler_Stopwatch_Emulation() |
| 246 |
{ |
| 247 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.StartStopwatch(null)); |
| 248 |
} |
| 249 |
|
| 250 |
#if !NO_TPL |
| 251 |
[TestMethod] |
| 252 |
public void Scheduler_LongRunning1() |
| 253 |
{ |
| 254 |
var s = TaskPoolScheduler.Default; |
| 255 |
|
| 256 |
var x = new ManualResetEvent(false); |
| 257 |
var e = new ManualResetEvent(false); |
| 258 |
|
| 259 |
var d = s.ScheduleLongRunning(42, (state, cancel) => |
| 260 |
{ |
| 261 |
while (!cancel.IsDisposed) |
| 262 |
x.Set(); |
| 263 |
e.Set(); |
| 264 |
}); |
| 265 |
|
| 266 |
x.WaitOne(); |
| 267 |
d.Dispose(); |
| 268 |
|
| 269 |
e.WaitOne(); |
| 270 |
} |
| 271 |
|
| 272 |
[TestMethod] |
| 273 |
public void Scheduler_LongRunning2() |
| 274 |
{ |
| 275 |
var s = TaskPoolScheduler.Default; |
| 276 |
|
| 277 |
var x = new ManualResetEvent(false); |
| 278 |
var e = new ManualResetEvent(false); |
| 279 |
|
| 280 |
var d = s.ScheduleLongRunning(cancel => |
| 281 |
{ |
| 282 |
while (!cancel.IsDisposed) |
| 283 |
x.Set(); |
| 284 |
e.Set(); |
| 285 |
}); |
| 286 |
|
| 287 |
x.WaitOne(); |
| 288 |
d.Dispose(); |
| 289 |
|
| 290 |
e.WaitOne(); |
| 291 |
} |
| 292 |
#endif |
| 293 |
#endif |
| 294 |
|
| 295 |
#endregion |
| 296 |
|
| 297 |
#region ISchedulerPeriodic |
| 298 |
|
| 299 |
#if !NO_PERF |
| 300 |
[TestMethod] |
| 301 |
public void Scheduler_Periodic1() |
| 302 |
{ |
| 303 |
var n = 0; |
| 304 |
var e = new ManualResetEvent(false); |
| 305 |
|
| 306 |
var d = ThreadPoolScheduler.Instance.SchedulePeriodic(TimeSpan.FromMilliseconds(50), () => |
| 307 |
{ |
| 308 |
if (n++ == 10) |
| 309 |
e.Set(); |
| 310 |
}); |
| 311 |
|
| 312 |
e.WaitOne(); |
| 313 |
d.Dispose(); |
| 314 |
} |
| 315 |
|
| 316 |
[TestMethod] |
| 317 |
public void Scheduler_Periodic2() |
| 318 |
{ |
| 319 |
var n = 0; |
| 320 |
var e = new ManualResetEvent(false); |
| 321 |
|
| 322 |
var d = ThreadPoolScheduler.Instance.SchedulePeriodic(42, TimeSpan.FromMilliseconds(50), x => |
| 323 |
{ |
| 324 |
Assert.AreEqual(42, x); |
| 325 |
|
| 326 |
if (n++ == 10) |
| 327 |
e.Set(); |
| 328 |
}); |
| 329 |
|
| 330 |
e.WaitOne(); |
| 331 |
d.Dispose(); |
| 332 |
} |
| 333 |
|
| 334 |
#if DESKTOPCLR |
| 335 |
[TestMethod] |
| 336 |
public void Scheduler_Periodic_HostLifecycleManagement() |
| 337 |
{ |
| 338 |
var cur = Utils.GetTestBaseDirectory(); |
| 339 |
|
| 340 |
var domain = AppDomain.CreateDomain("HLN", null, new AppDomainSetup { ApplicationBase = cur }); |
| 341 |
|
| 342 |
domain.DoCallBack(() => |
| 343 |
{ |
| 344 |
var pep = PlatformEnlightenmentProvider.Current; |
| 345 |
|
| 346 |
try |
| 347 |
{ |
| 348 |
var hln = new HLN(); |
| 349 |
PlatformEnlightenmentProvider.Current = new PEP(hln); |
| 350 |
|
| 351 |
var s = ThreadPoolScheduler.Instance.DisableOptimizations(typeof(ISchedulerPeriodic)); |
| 352 |
|
| 353 |
var n = 0; |
| 354 |
var e = new ManualResetEvent(false); |
| 355 |
|
| 356 |
var d = Observable.Interval(TimeSpan.FromMilliseconds(100), s).Subscribe(_ => |
| 357 |
{ |
| 358 |
if (n++ == 10) |
| 359 |
e.Set(); |
| 360 |
}); |
| 361 |
|
| 362 |
hln.OnSuspending(); |
| 363 |
hln.OnResuming(); |
| 364 |
|
| 365 |
Thread.Sleep(250); |
| 366 |
hln.OnSuspending(); |
| 367 |
|
| 368 |
Thread.Sleep(150); |
| 369 |
hln.OnResuming(); |
| 370 |
|
| 371 |
Thread.Sleep(50); |
| 372 |
hln.OnSuspending(); |
| 373 |
hln.OnResuming(); |
| 374 |
|
| 375 |
e.WaitOne(); |
| 376 |
d.Dispose(); |
| 377 |
} |
| 378 |
finally |
| 379 |
{ |
| 380 |
PlatformEnlightenmentProvider.Current = pep; |
| 381 |
} |
| 382 |
}); |
| 383 |
} |
| 384 |
|
| 385 |
class PEP : IPlatformEnlightenmentProvider |
| 386 |
{ |
| 387 |
private readonly IPlatformEnlightenmentProvider _old; |
| 388 |
private readonly IHostLifecycleNotifications _hln; |
| 389 |
|
| 390 |
public PEP(HLN hln) |
| 391 |
{ |
| 392 |
_old = PlatformEnlightenmentProvider.Current; |
| 393 |
_hln = hln; |
| 394 |
} |
| 395 |
|
| 396 |
public T GetService<T>(params object[] args) where T : class |
| 397 |
{ |
| 398 |
if (typeof(T) == typeof(IHostLifecycleNotifications)) |
| 399 |
{ |
| 400 |
return (T)(object)_hln; |
| 401 |
} |
| 402 |
|
| 403 |
return _old.GetService<T>(args); |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
class HLN : IHostLifecycleNotifications |
| 408 |
{ |
| 409 |
public event EventHandler<HostSuspendingEventArgs> Suspending; |
| 410 |
public event EventHandler<HostResumingEventArgs> Resuming; |
| 411 |
|
| 412 |
public void OnSuspending() |
| 413 |
{ |
| 414 |
var s = Suspending; |
| 415 |
if (s != null) |
| 416 |
s(this, null); |
| 417 |
} |
| 418 |
|
| 419 |
public void OnResuming() |
| 420 |
{ |
| 421 |
var s = Resuming; |
| 422 |
if (s != null) |
| 423 |
s(this, null); |
| 424 |
} |
| 425 |
} |
| 426 |
#endif |
| 427 |
|
| 428 |
#endif |
| 429 |
|
| 430 |
#endregion |
| 431 |
|
| 432 |
#region DisableOptimizations |
| 433 |
|
| 434 |
#if !NO_PERF |
| 435 |
[TestMethod] |
| 436 |
public void DisableOptimizations_ArgumentChecking() |
| 437 |
{ |
| 438 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.DisableOptimizations(default(IScheduler))); |
| 439 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.DisableOptimizations(default(IScheduler), new Type[0])); |
| 440 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.DisableOptimizations(ThreadPoolScheduler.Instance, default(Type[]))); |
| 441 |
|
| 442 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.DisableOptimizations(Scheduler.Default).Schedule(42, default(Func<IScheduler, int, IDisposable>))); |
| 443 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.DisableOptimizations(Scheduler.Default).Schedule(42, TimeSpan.FromSeconds(1), default(Func<IScheduler, int, IDisposable>))); |
| 444 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.DisableOptimizations(Scheduler.Default).Schedule(42, DateTimeOffset.Now, default(Func<IScheduler, int, IDisposable>))); |
| 445 |
} |
| 446 |
|
| 447 |
#if !NO_TPL |
| 448 |
[TestMethod] |
| 449 |
public void DisableOptimizations1() |
| 450 |
{ |
| 451 |
var s = TaskPoolScheduler.Default; |
| 452 |
Assert.IsTrue(s is IServiceProvider); |
| 453 |
|
| 454 |
var t = s.DisableOptimizations(); |
| 455 |
|
| 456 |
var d = t.Now - s.Now; |
| 457 |
Assert.IsTrue(d.TotalSeconds < 1); |
| 458 |
|
| 459 |
var e1 = new ManualResetEvent(false); |
| 460 |
t.Schedule(42, (self, state) => |
| 461 |
{ |
| 462 |
e1.Set(); |
| 463 |
return Disposable.Empty; |
| 464 |
}); |
| 465 |
e1.WaitOne(); |
| 466 |
|
| 467 |
var e2 = new ManualResetEvent(false); |
| 468 |
t.Schedule(42, TimeSpan.FromMilliseconds(1), (self, state) => |
| 469 |
{ |
| 470 |
e2.Set(); |
| 471 |
return Disposable.Empty; |
| 472 |
}); |
| 473 |
e2.WaitOne(); |
| 474 |
|
| 475 |
var e3 = new ManualResetEvent(false); |
| 476 |
t.Schedule(42, DateTimeOffset.Now.AddMilliseconds(10), (self, state) => |
| 477 |
{ |
| 478 |
e3.Set(); |
| 479 |
return Disposable.Empty; |
| 480 |
}); |
| 481 |
e3.WaitOne(); |
| 482 |
} |
| 483 |
|
| 484 |
[TestMethod] |
| 485 |
public void DisableOptimizations2() |
| 486 |
{ |
| 487 |
var s = TaskPoolScheduler.Default; |
| 488 |
Assert.IsTrue(s is IServiceProvider); |
| 489 |
|
| 490 |
var lr1 = ((IServiceProvider)s).GetService(typeof(ISchedulerLongRunning)); |
| 491 |
Assert.IsNotNull(lr1); |
| 492 |
|
| 493 |
var e1 = new ManualResetEvent(false); |
| 494 |
s.Schedule(42, (self, state) => |
| 495 |
{ |
| 496 |
Assert.IsTrue(self is IServiceProvider); |
| 497 |
|
| 498 |
var lrr1 = ((IServiceProvider)self).GetService(typeof(ISchedulerLongRunning)); |
| 499 |
Assert.IsNotNull(lrr1); |
| 500 |
|
| 501 |
e1.Set(); |
| 502 |
return Disposable.Empty; |
| 503 |
}); |
| 504 |
e1.WaitOne(); |
| 505 |
|
| 506 |
var t = s.DisableOptimizations(); |
| 507 |
Assert.IsTrue(t is IServiceProvider); |
| 508 |
|
| 509 |
var lr2 = ((IServiceProvider)t).GetService(typeof(ISchedulerLongRunning)); |
| 510 |
Assert.IsNull(lr2); |
| 511 |
|
| 512 |
var e2 = new ManualResetEvent(false); |
| 513 |
t.Schedule(42, (self, state) => |
| 514 |
{ |
| 515 |
Assert.IsTrue(self is IServiceProvider); |
| 516 |
|
| 517 |
var lrr2 = ((IServiceProvider)self).GetService(typeof(ISchedulerLongRunning)); |
| 518 |
Assert.IsNull(lrr2); |
| 519 |
|
| 520 |
e2.Set(); |
| 521 |
return Disposable.Empty; |
| 522 |
}); |
| 523 |
e2.WaitOne(); |
| 524 |
} |
| 525 |
|
| 526 |
[TestMethod] |
| 527 |
public void DisableOptimizations3() |
| 528 |
{ |
| 529 |
var s = TaskPoolScheduler.Default; |
| 530 |
Assert.IsTrue(s is IServiceProvider); |
| 531 |
|
| 532 |
var lr1 = ((IServiceProvider)s).GetService(typeof(ISchedulerLongRunning)); |
| 533 |
Assert.IsNotNull(lr1); |
| 534 |
|
| 535 |
var p1 = ((IServiceProvider)s).GetService(typeof(ISchedulerPeriodic)); |
| 536 |
Assert.IsNotNull(p1); |
| 537 |
|
| 538 |
var e1 = new ManualResetEvent(false); |
| 539 |
s.Schedule(42, (self, state) => |
| 540 |
{ |
| 541 |
Assert.IsTrue(self is IServiceProvider); |
| 542 |
|
| 543 |
var lrr1 = ((IServiceProvider)self).GetService(typeof(ISchedulerLongRunning)); |
| 544 |
Assert.IsNotNull(lrr1); |
| 545 |
|
| 546 |
var pr1 = ((IServiceProvider)self).GetService(typeof(ISchedulerPeriodic)); |
| 547 |
Assert.IsNotNull(pr1); |
| 548 |
|
| 549 |
e1.Set(); |
| 550 |
return Disposable.Empty; |
| 551 |
}); |
| 552 |
e1.WaitOne(); |
| 553 |
|
| 554 |
var t = s.DisableOptimizations(typeof(ISchedulerLongRunning)); |
| 555 |
Assert.IsTrue(t is IServiceProvider); |
| 556 |
|
| 557 |
var lr2 = ((IServiceProvider)t).GetService(typeof(ISchedulerLongRunning)); |
| 558 |
Assert.IsNull(lr2); |
| 559 |
|
| 560 |
var p2 = ((IServiceProvider)t).GetService(typeof(ISchedulerPeriodic)); |
| 561 |
Assert.IsNotNull(p2); |
| 562 |
|
| 563 |
var e2 = new ManualResetEvent(false); |
| 564 |
t.Schedule(42, (self, state) => |
| 565 |
{ |
| 566 |
Assert.IsTrue(self is IServiceProvider); |
| 567 |
|
| 568 |
var lrr2 = ((IServiceProvider)self).GetService(typeof(ISchedulerLongRunning)); |
| 569 |
Assert.IsNull(lrr2); |
| 570 |
|
| 571 |
var pr2 = ((IServiceProvider)self).GetService(typeof(ISchedulerPeriodic)); |
| 572 |
Assert.IsNotNull(pr2); |
| 573 |
|
| 574 |
e2.Set(); |
| 575 |
return Disposable.Empty; |
| 576 |
}); |
| 577 |
e2.WaitOne(); |
| 578 |
} |
| 579 |
#endif |
| 580 |
#endif |
| 581 |
|
| 582 |
[TestMethod] |
| 583 |
public void DisableOptimizations_UnknownService() |
| 584 |
{ |
| 585 |
var s = new MyScheduler(); |
| 586 |
Assert.IsFalse(s is IServiceProvider); |
| 587 |
|
| 588 |
var d = s.DisableOptimizations(); |
| 589 |
Assert.IsTrue(d is IServiceProvider); |
| 590 |
Assert.IsNull(((IServiceProvider)d).GetService(typeof(bool))); |
| 591 |
} |
| 592 |
|
| 593 |
class MyScheduler : IScheduler |
| 594 |
{ |
| 595 |
public MyScheduler() |
| 596 |
: this(DateTimeOffset.Now) |
| 597 |
{ |
| 598 |
} |
| 599 |
|
| 600 |
public MyScheduler(DateTimeOffset now) |
| 601 |
{ |
| 602 |
Now = now; |
| 603 |
} |
| 604 |
|
| 605 |
public DateTimeOffset Now |
| 606 |
{ |
| 607 |
get; |
| 608 |
private set; |
| 609 |
} |
| 610 |
|
| 611 |
public IDisposable Schedule<T>(T state, Func<IScheduler, T, IDisposable> action) |
| 612 |
{ |
| 613 |
return action(this, state); |
| 614 |
} |
| 615 |
|
| 616 |
public Action<Action<object>, object, TimeSpan> Check { get; set; } |
| 617 |
public long WaitCycles { get; set; } |
| 618 |
|
| 619 |
public IDisposable Schedule<T>(T state, TimeSpan dueTime, Func<IScheduler, T, IDisposable> action) |
| 620 |
{ |
| 621 |
Check(o => action(this, (T)o), state, dueTime); |
| 622 |
WaitCycles += dueTime.Ticks; |
| 623 |
return action(this, state); |
| 624 |
} |
| 625 |
|
| 626 |
public IDisposable Schedule<T>(T state, DateTimeOffset dueTime, Func<IScheduler, T, IDisposable> action) |
| 627 |
{ |
| 628 |
return Schedule(state, dueTime - Now, action); |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
#endregion |
| 633 |
|
| 634 |
#region Catch |
| 635 |
|
| 636 |
[TestMethod] |
| 637 |
public void Catch_ArgumentChecking() |
| 638 |
{ |
| 639 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.Catch<Exception>(default(IScheduler), _ => true)); |
| 640 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.Catch<Exception>(Scheduler.Default, default(Func<Exception, bool>))); |
| 641 |
|
| 642 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.Catch<Exception>(Scheduler.Default, _ => true).Schedule(42, default(Func<IScheduler, int, IDisposable>))); |
| 643 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.Catch<Exception>(Scheduler.Default, _ => true).Schedule(42, TimeSpan.FromSeconds(1), default(Func<IScheduler, int, IDisposable>))); |
| 644 |
ReactiveAssert.Throws<ArgumentNullException>(() => Scheduler.Catch<Exception>(Scheduler.Default, _ => true).Schedule(42, DateTimeOffset.Now, default(Func<IScheduler, int, IDisposable>))); |
| 645 |
} |
| 646 |
|
| 647 |
[TestMethod] |
| 648 |
public void Catch_Builtin_Swallow_Shallow() |
| 649 |
{ |
| 650 |
var done = new ManualResetEvent(false); |
| 651 |
|
| 652 |
var swallow = Scheduler.Default.Catch<InvalidOperationException>(_ => { done.Set(); return true; }); |
| 653 |
|
| 654 |
swallow.Schedule(() => |
| 655 |
{ |
| 656 |
throw new InvalidOperationException(); |
| 657 |
}); |
| 658 |
|
| 659 |
done.WaitOne(); |
| 660 |
} |
| 661 |
|
| 662 |
[TestMethod] |
| 663 |
public void Catch_Builtin_Swallow_Recursive() |
| 664 |
{ |
| 665 |
var done = new ManualResetEvent(false); |
| 666 |
|
| 667 |
var swallow = Scheduler.Default.Catch<InvalidOperationException>(_ => { done.Set(); return true; }); |
| 668 |
|
| 669 |
swallow.Schedule(42, (self, state) => |
| 670 |
{ |
| 671 |
return self.Schedule(() => |
| 672 |
{ |
| 673 |
throw new InvalidOperationException(); |
| 674 |
}); |
| 675 |
}); |
| 676 |
|
| 677 |
done.WaitOne(); |
| 678 |
} |
| 679 |
|
| 680 |
[TestMethod] |
| 681 |
public void Catch_Custom_Unhandled() |
| 682 |
{ |
| 683 |
var err = default(Exception); |
| 684 |
var scheduler = new MyExceptionScheduler(ex_ => err = ex_); |
| 685 |
|
| 686 |
scheduler.Catch<InvalidOperationException>(_ => true).Schedule(() => |
| 687 |
{ |
| 688 |
throw new InvalidOperationException(); |
| 689 |
}); |
| 690 |
Assert.IsNull(err); |
| 691 |
|
| 692 |
var ex = new ArgumentException(); |
| 693 |
scheduler.Catch<InvalidOperationException>(_ => true).Schedule(() => |
| 694 |
{ |
| 695 |
throw ex; |
| 696 |
}); |
| 697 |
Assert.AreSame(ex, err); |
| 698 |
} |
| 699 |
|
| 700 |
[TestMethod] |
| 701 |
public void Catch_Custom_Rethrow() |
| 702 |
{ |
| 703 |
var err = default(Exception); |
| 704 |
var scheduler = new MyExceptionScheduler(ex_ => err = ex_); |
| 705 |
|
| 706 |
var ex = new InvalidOperationException(); |
| 707 |
scheduler.Catch<InvalidOperationException>(_ => false).Schedule(() => |
| 708 |
{ |
| 709 |
throw ex; |
| 710 |
}); |
| 711 |
Assert.AreSame(ex, err); |
| 712 |
} |
| 713 |
|
| 714 |
[TestMethod] |
| 715 |
public void Catch_Custom_LongRunning_Caught() |
| 716 |
{ |
| 717 |
var err = default(Exception); |
| 718 |
var scheduler = new MyExceptionScheduler(ex_ => err = ex_); |
| 719 |
|
| 720 |
var caught = false; |
| 721 |
var @catch = scheduler.Catch<InvalidOperationException>(_ => caught = true); |
| 722 |
var slr = (ISchedulerLongRunning)((IServiceProvider)@catch).GetService(typeof(ISchedulerLongRunning)); |
| 723 |
|
| 724 |
slr.ScheduleLongRunning(cancel => |
| 725 |
{ |
| 726 |
throw new InvalidOperationException(); |
| 727 |
}); |
| 728 |
Assert.IsTrue(caught); |
| 729 |
Assert.IsNull(err); |
| 730 |
|
| 731 |
var ex = new ArgumentException(); |
| 732 |
slr.ScheduleLongRunning(cancel => |
| 733 |
{ |
| 734 |
throw ex; |
| 735 |
}); |
| 736 |
Assert.AreSame(ex, err); |
| 737 |
} |
| 738 |
|
| 739 |
[TestMethod] |
| 740 |
public void Catch_Custom_LongRunning_Rethrow() |
| 741 |
{ |
| 742 |
var err = default(Exception); |
| 743 |
var scheduler = new MyExceptionScheduler(ex_ => err = ex_); |
| 744 |
|
| 745 |
var @catch = scheduler.Catch<InvalidOperationException>(_ => false); |
| 746 |
var slr = (ISchedulerLongRunning)((IServiceProvider)@catch).GetService(typeof(ISchedulerLongRunning)); |
| 747 |
|
| 748 |
var done = false; |
| 749 |
slr.ScheduleLongRunning(cancel => |
| 750 |
{ |
| 751 |
done = true; |
| 752 |
}); |
| 753 |
Assert.IsTrue(done); |
| 754 |
|
| 755 |
var ex = new InvalidOperationException(); |
| 756 |
slr.ScheduleLongRunning(cancel => |
| 757 |
{ |
| 758 |
throw ex; |
| 759 |
}); |
| 760 |
Assert.AreSame(ex, err); |
| 761 |
} |
| 762 |
|
| 763 |
[TestMethod] |
| 764 |
public void Catch_Custom_Periodic_Regular() |
| 765 |
{ |
| 766 |
var scheduler = new MyExceptionScheduler(_ => { }); |
| 767 |
scheduler.PeriodicStopped = new ManualResetEvent(false); |
| 768 |
|
| 769 |
var @catch = scheduler.Catch<InvalidOperationException>(_ => true); |
| 770 |
var per = (ISchedulerPeriodic)((IServiceProvider)@catch).GetService(typeof(ISchedulerPeriodic)); |
| 771 |
|
| 772 |
var madeProgress = new ManualResetEvent(false); |
| 773 |
var d = per.SchedulePeriodic(0, TimeSpan.Zero, x => |
| 774 |
{ |
| 775 |
if (x > 10) |
| 776 |
madeProgress.Set(); |
| 777 |
|
| 778 |
return x + 1; |
| 779 |
}); |
| 780 |
|
| 781 |
madeProgress.WaitOne(); |
| 782 |
d.Dispose(); |
| 783 |
scheduler.PeriodicStopped.WaitOne(); |
| 784 |
} |
| 785 |
|
| 786 |
[TestMethod] |
| 787 |
public void Catch_Custom_Periodic_Uncaught1() |
| 788 |
{ |
| 789 |
var err = default(Exception); |
| 790 |
var done = new ManualResetEvent(false); |
| 791 |
var scheduler = new MyExceptionScheduler(ex_ => { err = ex_; done.Set(); }); |
| 792 |
scheduler.PeriodicStopped = new ManualResetEvent(false); |
| 793 |
|
| 794 |
var @catch = scheduler.Catch<InvalidOperationException>(_ => true); |
| 795 |
var per = (ISchedulerPeriodic)((IServiceProvider)@catch).GetService(typeof(ISchedulerPeriodic)); |
| 796 |
|
| 797 |
var ex = new ArgumentException(); |
| 798 |
per.SchedulePeriodic(42, TimeSpan.Zero, x => |
| 799 |
{ |
| 800 |
throw ex; |
| 801 |
}); |
| 802 |
|
| 803 |
scheduler.PeriodicStopped.WaitOne(); |
| 804 |
done.WaitOne(); |
| 805 |
Assert.AreSame(ex, err); |
| 806 |
} |
| 807 |
|
| 808 |
[TestMethod] |
| 809 |
public void Catch_Custom_Periodic_Uncaught2() |
| 810 |
{ |
| 811 |
var err = default(Exception); |
| 812 |
var done = new ManualResetEvent(false); |
| 813 |
var scheduler = new MyExceptionScheduler(ex_ => { err = ex_; done.Set(); }); |
| 814 |
scheduler.PeriodicStopped = new ManualResetEvent(false); |
| 815 |
|
| 816 |
var @catch = scheduler.Catch<InvalidOperationException>(_ => false); |
| 817 |
var per = (ISchedulerPeriodic)((IServiceProvider)@catch).GetService(typeof(ISchedulerPeriodic)); |
| 818 |
|
| 819 |
var ex = new InvalidOperationException(); |
| 820 |
per.SchedulePeriodic(42, TimeSpan.Zero, x => |
| 821 |
{ |
| 822 |
throw ex; |
| 823 |
}); |
| 824 |
|
| 825 |
scheduler.PeriodicStopped.WaitOne(); |
| 826 |
done.WaitOne(); |
| 827 |
Assert.AreSame(ex, err); |
| 828 |
} |
| 829 |
|
| 830 |
[TestMethod] |
| 831 |
public void Catch_Custom_Periodic_Caught() |
| 832 |
{ |
| 833 |
var err = default(Exception); |
| 834 |
var scheduler = new MyExceptionScheduler(ex_ => err = ex_); |
| 835 |
scheduler.PeriodicStopped = new ManualResetEvent(false); |
| 836 |
|
| 837 |
var caught = new ManualResetEvent(false); |
| 838 |
var @catch = scheduler.Catch<InvalidOperationException>(_ => { caught.Set(); return true; }); |
| 839 |
var per = (ISchedulerPeriodic)((IServiceProvider)@catch).GetService(typeof(ISchedulerPeriodic)); |
| 840 |
|
| 841 |
per.SchedulePeriodic(42, TimeSpan.Zero, x => |
| 842 |
{ |
| 843 |
throw new InvalidOperationException(); |
| 844 |
}); |
| 845 |
|
| 846 |
scheduler.PeriodicStopped.WaitOne(); |
| 847 |
caught.WaitOne(); |
| 848 |
Assert.IsNull(err); |
| 849 |
} |
| 850 |
|
| 851 |
class MyExceptionScheduler : LocalScheduler, ISchedulerLongRunning, ISchedulerPeriodic |
| 852 |
{ |
| 853 |
private readonly Action<Exception> _onError; |
| 854 |
|
| 855 |
public MyExceptionScheduler(Action<Exception> onError) |
| 856 |
{ |
| 857 |
_onError = onError; |
| 858 |
} |
| 859 |
|
| 860 |
public override IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action) |
| 861 |
{ |
| 862 |
try |
| 863 |
{ |
| 864 |
return action(this, state); |
| 865 |
} |
| 866 |
catch (Exception exception) |
| 867 |
{ |
| 868 |
_onError(exception); |
| 869 |
return Disposable.Empty; |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action) |
| 874 |
{ |
| 875 |
throw new NotImplementedException(); |
| 876 |
} |
| 877 |
|
| 878 |
public IDisposable ScheduleLongRunning<TState>(TState state, Action<TState, ICancelable> action) |
| 879 |
{ |
| 880 |
var b = new BooleanDisposable(); |
| 881 |
|
| 882 |
try |
| 883 |
{ |
| 884 |
action(state, b); |
| 885 |
} |
| 886 |
catch (Exception exception) |
| 887 |
{ |
| 888 |
_onError(exception); |
| 889 |
return Disposable.Empty; |
| 890 |
} |
| 891 |
|
| 892 |
return b; |
| 893 |
} |
| 894 |
|
| 895 |
public ManualResetEvent PeriodicStopped { get; set; } |
| 896 |
|
| 897 |
public IDisposable SchedulePeriodic<TState>(TState state, TimeSpan period, Func<TState, TState> action) |
| 898 |
{ |
| 899 |
var b = new BooleanDisposable(); |
| 900 |
|
| 901 |
Scheduler.Default.Schedule(() => |
| 902 |
{ |
| 903 |
try |
| 904 |
{ |
| 905 |
var s = state; |
| 906 |
for (int i = 0; true; i++) |
| 907 |
{ |
| 908 |
if (i > 100 /* mimic delayed cancellation */ && b.IsDisposed) |
| 909 |
break; |
| 910 |
s = action(s); |
| 911 |
} |
| 912 |
} |
| 913 |
catch (Exception exception) |
| 914 |
{ |
| 915 |
_onError(exception); |
| 916 |
} |
| 917 |
finally |
| 918 |
{ |
| 919 |
PeriodicStopped.Set(); |
| 920 |
} |
| 921 |
}); |
| 922 |
|
| 923 |
return b; |
| 924 |
} |
| 925 |
} |
| 926 |
|
| 927 |
#endregion |
| 928 |
|
| 929 |
#region Services |
| 930 |
|
| 931 |
[TestMethod] |
| 932 |
public void InvalidService_Null() |
| 933 |
{ |
| 934 |
var s = new MySchedulerWithoutServices(); |
| 935 |
Assert.IsNull(((IServiceProvider)s).GetService(typeof(IAsyncResult))); |
| 936 |
} |
| 937 |
|
| 938 |
class MySchedulerWithoutServices : LocalScheduler |
| 939 |
{ |
| 940 |
public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action) |
| 941 |
{ |
| 942 |
throw new NotImplementedException(); |
| 943 |
} |
| 944 |
} |
| 945 |
|
| 946 |
[TestMethod] |
| 947 |
public void DetectServices_Null_1() |
| 948 |
{ |
| 949 |
var s = new MyDumbScheduler1(); |
| 950 |
Assert.IsNull(Scheduler.AsLongRunning(s)); |
| 951 |
Assert.IsNull(Scheduler.AsPeriodic(s)); |
| 952 |
Assert.IsNull(Scheduler.AsStopwatchProvider(s)); |
| 953 |
} |
| 954 |
|
| 955 |
class MyDumbScheduler1 : IScheduler |
| 956 |
{ |
| 957 |
public DateTimeOffset Now |
| 958 |
{ |
| 959 |
get { throw new NotImplementedException(); } |
| 960 |
} |
| 961 |
|
| 962 |
public IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action) |
| 963 |
{ |
| 964 |
throw new NotImplementedException(); |
| 965 |
} |
| 966 |
|
| 967 |
public IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action) |
| 968 |
{ |
| 969 |
throw new NotImplementedException(); |
| 970 |
} |
| 971 |
|
| 972 |
public IDisposable Schedule<TState>(TState state, DateTimeOffset dueTime, Func<IScheduler, TState, IDisposable> action) |
| 973 |
{ |
| 974 |
throw new NotImplementedException(); |
| 975 |
} |
| 976 |
} |
| 977 |
|
| 978 |
[TestMethod] |
| 979 |
public void DetectServices_Null_2() |
| 980 |
{ |
| 981 |
var s = new MyDumbScheduler2(new Dictionary<Type, object>()); |
| 982 |
Assert.IsNull(Scheduler.AsLongRunning(s)); |
| 983 |
Assert.IsNull(Scheduler.AsPeriodic(s)); |
| 984 |
Assert.IsNull(Scheduler.AsStopwatchProvider(s)); |
| 985 |
} |
| 986 |
|
| 987 |
[TestMethod] |
| 988 |
public void DetectServices_Found() |
| 989 |
{ |
| 990 |
{ |
| 991 |
var x = new MyLongRunning(); |
| 992 |
|
| 993 |
var s = new MyDumbScheduler2(new Dictionary<Type, object> |
| 994 |
{ |
| 995 |
{ typeof(ISchedulerLongRunning), x } |
| 996 |
}); |
| 997 |
|
| 998 |
Assert.AreEqual(x, Scheduler.AsLongRunning(s)); |
| 999 |
} |
| 1000 |
|
| 1001 |
{ |
| 1002 |
var x = new MyStopwatchProvider(); |
| 1003 |
|
| 1004 |
var s = new MyDumbScheduler2(new Dictionary<Type, object> |
| 1005 |
{ |
| 1006 |
{ typeof(IStopwatchProvider), x } |
| 1007 |
}); |
| 1008 |
|
| 1009 |
Assert.AreEqual(x, Scheduler.AsStopwatchProvider(s)); |
| 1010 |
} |
| 1011 |
|
| 1012 |
{ |
| 1013 |
var x = new MyPeriodic(); |
| 1014 |
|
| 1015 |
var s = new MyDumbScheduler2(new Dictionary<Type, object> |
| 1016 |
{ |
| 1017 |
{ typeof(ISchedulerPeriodic), x } |
| 1018 |
}); |
| 1019 |
|
| 1020 |
Assert.AreEqual(x, Scheduler.AsPeriodic(s)); |
| 1021 |
} |
| 1022 |
} |
| 1023 |
|
| 1024 |
class MyDumbScheduler2 : IScheduler, IServiceProvider |
| 1025 |
{ |
| 1026 |
private readonly Dictionary<Type, object> _services; |
| 1027 |
|
| 1028 |
public MyDumbScheduler2(Dictionary<Type, object> services) |
| 1029 |
{ |
| 1030 |
_services = services; |
| 1031 |
} |
| 1032 |
|
| 1033 |
public DateTimeOffset Now |
| 1034 |
{ |
| 1035 |
get { throw new NotImplementedException(); } |
| 1036 |
} |
| 1037 |
|
| 1038 |
public IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action) |
| 1039 |
{ |
| 1040 |
throw new NotImplementedException(); |
| 1041 |
} |
| 1042 |
|
| 1043 |
public IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action) |
| 1044 |
{ |
| 1045 |
throw new NotImplementedException(); |
| 1046 |
} |
| 1047 |
|
| 1048 |
public IDisposable Schedule<TState>(TState state, DateTimeOffset dueTime, Func<IScheduler, TState, IDisposable> action) |
| 1049 |
{ |
| 1050 |
throw new NotImplementedException(); |
| 1051 |
} |
| 1052 |
|
| 1053 |
public object GetService(Type serviceType) |
| 1054 |
{ |
| 1055 |
var res = default(object); |
| 1056 |
if (_services.TryGetValue(serviceType, out res)) |
| 1057 |
return res; |
| 1058 |
|
| 1059 |
return null; |
| 1060 |
} |
| 1061 |
} |
| 1062 |
|
| 1063 |
class MyLongRunning : ISchedulerLongRunning |
| 1064 |
{ |
| 1065 |
public IDisposable ScheduleLongRunning<TState>(TState state, Action<TState, ICancelable> action) |
| 1066 |
{ |
| 1067 |
throw new NotImplementedException(); |
| 1068 |
} |
| 1069 |
} |
| 1070 |
|
| 1071 |
class MyStopwatchProvider : IStopwatchProvider |
| 1072 |
{ |
| 1073 |
public IStopwatch StartStopwatch() |
| 1074 |
{ |
| 1075 |
throw new NotImplementedException(); |
| 1076 |
} |
| 1077 |
} |
| 1078 |
|
| 1079 |
class MyPeriodic : ISchedulerPeriodic |
| 1080 |
{ |
| 1081 |
public IDisposable SchedulePeriodic<TState>(TState state, TimeSpan period, Func<TState, TState> action) |
| 1082 |
{ |
| 1083 |
throw new NotImplementedException(); |
| 1084 |
} |
| 1085 |
} |
| 1086 |
|
| 1087 |
#endregion |
| 1088 |
} |
| 1089 |
} |