| 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 SynchronizationContextSchedulerTest |
| 22 |
{ |
| 23 |
[TestMethod] |
| 24 |
public void SynchronizationContext_ArgumentChecking() |
| 25 |
{ |
| 26 |
var ms = new MySync(); |
| 27 |
var s = new SynchronizationContextScheduler(ms); |
| 28 |
|
| 29 |
ReactiveAssert.Throws<ArgumentNullException>(() => new SynchronizationContextScheduler(null)); |
| 30 |
ReactiveAssert.Throws<ArgumentNullException>(() => new SynchronizationContextScheduler(null, true)); |
| 31 |
ReactiveAssert.Throws<ArgumentNullException>(() => s.Schedule<int>(42, default(Func<IScheduler, int, IDisposable>))); |
| 32 |
ReactiveAssert.Throws<ArgumentNullException>(() => s.Schedule<int>(42, DateTimeOffset.Now, default(Func<IScheduler, int, IDisposable>))); |
| 33 |
ReactiveAssert.Throws<ArgumentNullException>(() => s.Schedule<int>(42, TimeSpan.Zero, default(Func<IScheduler, int, IDisposable>))); |
| 34 |
} |
| 35 |
|
| 36 |
[TestMethod] |
| 37 |
public void SynchronizationContext_Now() |
| 38 |
{ |
| 39 |
var ms = new MySync(); |
| 40 |
var s = new SynchronizationContextScheduler(ms); |
| 41 |
|
| 42 |
var res = s.Now - DateTime.Now; |
| 43 |
Assert.IsTrue(res.Seconds < 1); |
| 44 |
} |
| 45 |
|
| 46 |
[TestMethod] |
| 47 |
public void SynchronizationContext_ScheduleAction() |
| 48 |
{ |
| 49 |
var ms = new MySync(); |
| 50 |
var s = new SynchronizationContextScheduler(ms); |
| 51 |
|
| 52 |
var ran = false; |
| 53 |
s.Schedule(() => { ran = true; }); |
| 54 |
Assert.IsTrue(ms.Count == 1); |
| 55 |
Assert.IsTrue(ran); |
| 56 |
} |
| 57 |
|
| 58 |
[TestMethod] |
| 59 |
public void SynchronizationContext_ScheduleAction_TimeSpan() |
| 60 |
{ |
| 61 |
var ms = new MySync(); |
| 62 |
var s = new SynchronizationContextScheduler(ms); |
| 63 |
|
| 64 |
var e = new ManualResetEvent(false); |
| 65 |
s.Schedule(TimeSpan.FromMilliseconds(1), () => { e.Set(); }); |
| 66 |
|
| 67 |
e.WaitOne(); |
| 68 |
Assert.IsTrue(ms.Count == 1); |
| 69 |
} |
| 70 |
|
| 71 |
[TestMethod] |
| 72 |
public void SynchronizationContext_ScheduleAction_DateTimeOffset() |
| 73 |
{ |
| 74 |
var ms = new MySync(); |
| 75 |
var s = new SynchronizationContextScheduler(ms); |
| 76 |
|
| 77 |
var e = new ManualResetEvent(false); |
| 78 |
s.Schedule(DateTimeOffset.Now.AddMilliseconds(100), () => { e.Set(); }); |
| 79 |
|
| 80 |
e.WaitOne(); |
| 81 |
Assert.IsTrue(ms.Count >= 1); // Can be > 1 in case of timer queue retry operations. |
| 82 |
} |
| 83 |
|
| 84 |
[TestMethod] |
| 85 |
public void SynchronizationContext_ScheduleActionError() |
| 86 |
{ |
| 87 |
var ms = new MySync(); |
| 88 |
var s = new SynchronizationContextScheduler(ms); |
| 89 |
|
| 90 |
var ex = new Exception(); |
| 91 |
|
| 92 |
try |
| 93 |
{ |
| 94 |
s.Schedule(() => { throw ex; }); |
| 95 |
Assert.Fail(); |
| 96 |
} |
| 97 |
catch (Exception e) |
| 98 |
{ |
| 99 |
Assert.AreSame(e, ex); |
| 100 |
} |
| 101 |
|
| 102 |
Assert.IsTrue(ms.Count == 1); |
| 103 |
} |
| 104 |
|
| 105 |
#if !SILVERLIGHT |
| 106 |
[TestMethod] |
| 107 |
[Ignore] |
| 108 |
public void SynchronizationContext_ScheduleActionDue() |
| 109 |
{ |
| 110 |
var ms = new MySync(); |
| 111 |
var s = new SynchronizationContextScheduler(ms); |
| 112 |
|
| 113 |
var evt = new ManualResetEvent(false); |
| 114 |
var sw = new Stopwatch(); |
| 115 |
sw.Start(); |
| 116 |
s.Schedule(TimeSpan.FromSeconds(0.2), () => { sw.Stop(); evt.Set(); }); |
| 117 |
evt.WaitOne(); |
| 118 |
Assert.IsTrue(sw.ElapsedMilliseconds > 180, "due " + sw.ElapsedMilliseconds); |
| 119 |
Assert.IsTrue(ms.Count == 1); |
| 120 |
} |
| 121 |
#endif |
| 122 |
|
| 123 |
class MySync : SynchronizationContext |
| 124 |
{ |
| 125 |
public int Count { get; private set; } |
| 126 |
|
| 127 |
public override void Post(SendOrPostCallback d, object state) |
| 128 |
{ |
| 129 |
Count++; |
| 130 |
d(state); |
| 131 |
} |
| 132 |
|
| 133 |
public override void Send(SendOrPostCallback d, object state) |
| 134 |
{ |
| 135 |
throw new NotImplementedException(); |
| 136 |
} |
| 137 |
|
| 138 |
public int Started { get; private set; } |
| 139 |
|
| 140 |
public override void OperationStarted() |
| 141 |
{ |
| 142 |
base.OperationStarted(); |
| 143 |
Started++; |
| 144 |
} |
| 145 |
|
| 146 |
public int Completed { get; private set; } |
| 147 |
|
| 148 |
public override void OperationCompleted() |
| 149 |
{ |
| 150 |
base.OperationCompleted(); |
| 151 |
Completed++; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
[TestMethod] |
| 156 |
public void SynchronizationContext_StartedCompleted() |
| 157 |
{ |
| 158 |
var ms = new MySync(); |
| 159 |
var s = new SynchronizationContextScheduler(ms); |
| 160 |
|
| 161 |
var started = 0; |
| 162 |
s.Schedule<int>(42, TimeSpan.Zero, (self, x) => { started = ms.Started; return Disposable.Empty; }); |
| 163 |
|
| 164 |
Assert.IsTrue(started == 1); |
| 165 |
Assert.IsTrue(ms.Count == 1); |
| 166 |
Assert.IsTrue(ms.Completed == 1); |
| 167 |
} |
| 168 |
|
| 169 |
[TestMethod] |
| 170 |
public void SynchronizationContext_DontPost_Different() |
| 171 |
{ |
| 172 |
var ms = new MySync(); |
| 173 |
var s = new SynchronizationContextScheduler(ms, false); |
| 174 |
|
| 175 |
var ran = false; |
| 176 |
s.Schedule(() => { ran = true; }); |
| 177 |
Assert.IsTrue(ms.Count == 1); |
| 178 |
Assert.IsTrue(ran); |
| 179 |
} |
| 180 |
|
| 181 |
[TestMethod] |
| 182 |
public void SynchronizationContext_DontPost_Same() |
| 183 |
{ |
| 184 |
var count = 0; |
| 185 |
var ran = false; |
| 186 |
|
| 187 |
var t = new Thread(() => |
| 188 |
{ |
| 189 |
var ms = new MySync(); |
| 190 |
SynchronizationContext.SetSynchronizationContext(ms); |
| 191 |
|
| 192 |
var s = new SynchronizationContextScheduler(ms, false); |
| 193 |
|
| 194 |
s.Schedule(() => { ran = true; }); |
| 195 |
count = ms.Count; |
| 196 |
}); |
| 197 |
|
| 198 |
t.Start(); |
| 199 |
t.Join(); |
| 200 |
|
| 201 |
Assert.IsTrue(count == 0 /* no post */); |
| 202 |
Assert.IsTrue(ran); |
| 203 |
} |
| 204 |
|
| 205 |
[TestMethod] |
| 206 |
public void SynchronizationContext_AlwaysPost_Different() |
| 207 |
{ |
| 208 |
var ms = new MySync(); |
| 209 |
var s = new SynchronizationContextScheduler(ms, true); |
| 210 |
|
| 211 |
var ran = false; |
| 212 |
s.Schedule(() => { ran = true; }); |
| 213 |
Assert.IsTrue(ms.Count == 1); |
| 214 |
Assert.IsTrue(ran); |
| 215 |
} |
| 216 |
|
| 217 |
[TestMethod] |
| 218 |
public void SynchronizationContext_AlwaysPost_Same() |
| 219 |
{ |
| 220 |
var count = 0; |
| 221 |
var ran = false; |
| 222 |
|
| 223 |
var t = new Thread(() => |
| 224 |
{ |
| 225 |
var ms = new MySync(); |
| 226 |
SynchronizationContext.SetSynchronizationContext(ms); |
| 227 |
|
| 228 |
var s = new SynchronizationContextScheduler(ms, true); |
| 229 |
|
| 230 |
s.Schedule(() => { ran = true; }); |
| 231 |
count = ms.Count; |
| 232 |
}); |
| 233 |
|
| 234 |
t.Start(); |
| 235 |
t.Join(); |
| 236 |
|
| 237 |
Assert.IsTrue(count == 1 /* post */); |
| 238 |
Assert.IsTrue(ran); |
| 239 |
} |
| 240 |
} |
| 241 |
} |