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