| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
#if !NO_REMOTING |
| 4 |
using System; |
| 5 |
using System.IO; |
| 6 |
using System.Reactive.Concurrency; |
| 7 |
using System.Reactive.PlatformServices; |
| 8 |
using System.Runtime.CompilerServices; |
| 9 |
using System.Threading; |
| 10 |
#if NUNIT |
| 11 |
using NUnit.Framework; |
| 12 |
using TestClassAttribute = NUnit.Framework.TestFixtureAttribute; |
| 13 |
using TestMethodAttribute = NUnit.Framework.TestAttribute; |
| 14 |
using TestInitializeAttribute = NUnit.Framework.SetUpAttribute; |
| 15 |
#else |
| 16 |
using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 17 |
#endif |
| 18 |
|
| 19 |
namespace ReactiveTests.Tests |
| 20 |
{ |
| 21 |
[TestClass] |
| 22 |
[Serializable] |
| 23 |
public class DefaultConcurrencyAbstractionLayerTest |
| 24 |
{ |
| 25 |
private AppDomain _domain; |
| 26 |
|
| 27 |
[TestInitialize] |
| 28 |
public void Init() |
| 29 |
{ |
| 30 |
if (_domain == null) |
| 31 |
{ |
| 32 |
var cur = Utils.GetTestBaseDirectory(); |
| 33 |
var sub = Path.Combine(cur, "NoCAL"); |
| 34 |
|
| 35 |
if (!Directory.Exists(sub)) |
| 36 |
{ |
| 37 |
Directory.CreateDirectory(sub); |
| 38 |
|
| 39 |
foreach (var file in Directory.GetFiles(cur)) |
| 40 |
{ |
| 41 |
var fn = Path.GetFileName(file); |
| 42 |
if (!file.Contains("PlatformServices")) |
| 43 |
File.Copy(Path.Combine(cur, fn), Path.Combine(sub, fn)); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
_domain = AppDomain.CreateDomain("Default_CAL", null, new AppDomainSetup { ApplicationBase = sub }); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
private void Run(CrossAppDomainDelegate a) |
| 52 |
{ |
| 53 |
_domain.DoCallBack(a); |
| 54 |
} |
| 55 |
|
| 56 |
[TestMethod] |
| 57 |
public void Sleep() |
| 58 |
{ |
| 59 |
var ran = new MarshalByRefCell<bool>(); |
| 60 |
_domain.SetData("state", ran); |
| 61 |
|
| 62 |
Run(() => |
| 63 |
{ |
| 64 |
Scheduler.Immediate.Schedule(TimeSpan.FromMilliseconds(1), () => |
| 65 |
{ |
| 66 |
var state = (MarshalByRefCell<bool>)_domain.GetData("state"); |
| 67 |
state.Value = true; |
| 68 |
}); |
| 69 |
}); |
| 70 |
|
| 71 |
Assert.IsTrue(ran.Value); |
| 72 |
} |
| 73 |
|
| 74 |
[TestMethod] |
| 75 |
public void QueueUserWorkItem() |
| 76 |
{ |
| 77 |
var e = new MarshalByRefCell<ManualResetEvent> { Value = new ManualResetEvent(false) }; |
| 78 |
_domain.SetData("state", e); |
| 79 |
|
| 80 |
Run(() => |
| 81 |
{ |
| 82 |
Scheduler.Default.Schedule(() => |
| 83 |
{ |
| 84 |
var state = (MarshalByRefCell<ManualResetEvent>)_domain.GetData("state"); |
| 85 |
state.Value.Set(); |
| 86 |
}); |
| 87 |
}); |
| 88 |
|
| 89 |
e.Value.WaitOne(); |
| 90 |
} |
| 91 |
|
| 92 |
[TestMethod] |
| 93 |
public void StartTimer() |
| 94 |
{ |
| 95 |
var e = new MarshalByRefCell<ManualResetEvent> { Value = new ManualResetEvent(false) }; |
| 96 |
_domain.SetData("state", e); |
| 97 |
|
| 98 |
Run(() => |
| 99 |
{ |
| 100 |
Scheduler.Default.Schedule(TimeSpan.FromMilliseconds(10), () => |
| 101 |
{ |
| 102 |
var state = (MarshalByRefCell<ManualResetEvent>)_domain.GetData("state"); |
| 103 |
state.Value.Set(); |
| 104 |
}); |
| 105 |
}); |
| 106 |
|
| 107 |
e.Value.WaitOne(); |
| 108 |
} |
| 109 |
|
| 110 |
[TestMethod] |
| 111 |
public void StartTimer_Cancel() |
| 112 |
{ |
| 113 |
Run(() => |
| 114 |
{ |
| 115 |
Scheduler.Default.Schedule(TimeSpan.FromSeconds(60), () => |
| 116 |
{ |
| 117 |
throw new InvalidOperationException("This shouldn't have happened!"); |
| 118 |
}).Dispose(); |
| 119 |
}); |
| 120 |
} |
| 121 |
|
| 122 |
[TestMethod] |
| 123 |
public void StartPeriodicTimer() |
| 124 |
{ |
| 125 |
var e = new MarshalByRefCell<ManualResetEvent> { Value = new ManualResetEvent(false) }; |
| 126 |
_domain.SetData("state", e); |
| 127 |
|
| 128 |
Run(() => |
| 129 |
{ |
| 130 |
var n = 0; |
| 131 |
|
| 132 |
Scheduler.Default.SchedulePeriodic(TimeSpan.FromMilliseconds(10), () => |
| 133 |
{ |
| 134 |
var state = (MarshalByRefCell<ManualResetEvent>)_domain.GetData("state"); |
| 135 |
|
| 136 |
if (n++ == 10) |
| 137 |
state.Value.Set(); |
| 138 |
}); |
| 139 |
}); |
| 140 |
|
| 141 |
e.Value.WaitOne(); |
| 142 |
} |
| 143 |
|
| 144 |
[TestMethod] |
| 145 |
public void StartPeriodicTimer_Cancel() |
| 146 |
{ |
| 147 |
Run(() => |
| 148 |
{ |
| 149 |
Scheduler.Default.SchedulePeriodic(TimeSpan.FromSeconds(60), () => |
| 150 |
{ |
| 151 |
throw new InvalidOperationException("This shouldn't have happened!"); |
| 152 |
}).Dispose(); |
| 153 |
}); |
| 154 |
} |
| 155 |
|
| 156 |
[TestMethod] |
| 157 |
public void StartPeriodicTimer_Fast() |
| 158 |
{ |
| 159 |
var e = new MarshalByRefCell<ManualResetEvent> { Value = new ManualResetEvent(false) }; |
| 160 |
_domain.SetData("state", e); |
| 161 |
|
| 162 |
Run(() => |
| 163 |
{ |
| 164 |
var n = 0; |
| 165 |
|
| 166 |
Scheduler.Default.SchedulePeriodic(TimeSpan.Zero, () => |
| 167 |
{ |
| 168 |
var state = (MarshalByRefCell<ManualResetEvent>)_domain.GetData("state"); |
| 169 |
|
| 170 |
if (n++ == 10) |
| 171 |
state.Value.Set(); |
| 172 |
}); |
| 173 |
}); |
| 174 |
|
| 175 |
e.Value.WaitOne(); |
| 176 |
} |
| 177 |
|
| 178 |
[TestMethod] |
| 179 |
public void StartPeriodicTimer_Fast_Cancel() |
| 180 |
{ |
| 181 |
var e = new MarshalByRefCell<ManualResetEvent> { Value = new ManualResetEvent(false) }; |
| 182 |
_domain.SetData("set_cancel", e); |
| 183 |
|
| 184 |
Run(() => |
| 185 |
{ |
| 186 |
var n = 0; |
| 187 |
|
| 188 |
var schedule = Scheduler.Default.SchedulePeriodic(TimeSpan.Zero, () => |
| 189 |
{ |
| 190 |
_domain.SetData("value", n++); |
| 191 |
}); |
| 192 |
|
| 193 |
_domain.SetData("cancel", new MarshalByRefAction(schedule.Dispose)); |
| 194 |
|
| 195 |
var setCancel = (MarshalByRefCell<ManualResetEvent>)_domain.GetData("set_cancel"); |
| 196 |
setCancel.Value.Set(); |
| 197 |
}); |
| 198 |
|
| 199 |
e.Value.WaitOne(); |
| 200 |
|
| 201 |
var value = (int)_domain.GetData("value"); |
| 202 |
|
| 203 |
var cancel = (MarshalByRefAction)_domain.GetData("cancel"); |
| 204 |
cancel.Invoke(); |
| 205 |
|
| 206 |
Thread.Sleep(TimeSpan.FromMilliseconds(50)); |
| 207 |
|
| 208 |
var newValue = (int)_domain.GetData("value"); |
| 209 |
|
| 210 |
Assert.IsTrue(newValue >= value); |
| 211 |
|
| 212 |
Thread.Sleep(TimeSpan.FromMilliseconds(50)); |
| 213 |
|
| 214 |
value = (int)_domain.GetData("value"); |
| 215 |
|
| 216 |
Assert.AreEqual(newValue, value); |
| 217 |
} |
| 218 |
|
| 219 |
[TestMethod] |
| 220 |
public void CreateThread() |
| 221 |
{ |
| 222 |
var e = new MarshalByRefCell<ManualResetEvent> { Value = new ManualResetEvent(false) }; |
| 223 |
_domain.SetData("state", e); |
| 224 |
|
| 225 |
var r = new MarshalByRefCell<string> { Value = "" }; |
| 226 |
_domain.SetData("res", r); |
| 227 |
|
| 228 |
Run(() => |
| 229 |
{ |
| 230 |
var state = (MarshalByRefCell<ManualResetEvent>)_domain.GetData("state"); |
| 231 |
var res = (MarshalByRefCell<string>)_domain.GetData("res"); |
| 232 |
|
| 233 |
var svc = (IServiceProvider)Scheduler.Default; |
| 234 |
|
| 235 |
var per = (ISchedulerPeriodic)svc.GetService(typeof(ISchedulerPeriodic)); |
| 236 |
if (per == null) |
| 237 |
{ |
| 238 |
res.Value = "Failed to get ISchedulerPeriodic."; |
| 239 |
state.Value.Set(); |
| 240 |
return; |
| 241 |
} |
| 242 |
|
| 243 |
var slr = (ISchedulerLongRunning)svc.GetService(typeof(ISchedulerLongRunning)); |
| 244 |
if (slr == null) |
| 245 |
{ |
| 246 |
res.Value = "Failed to get ISchedulerLongRunning."; |
| 247 |
state.Value.Set(); |
| 248 |
return; |
| 249 |
} |
| 250 |
|
| 251 |
var success = false; |
| 252 |
try |
| 253 |
{ |
| 254 |
slr.ScheduleLongRunning(42, null); |
| 255 |
} |
| 256 |
catch (ArgumentNullException) |
| 257 |
{ |
| 258 |
success = true; |
| 259 |
} |
| 260 |
|
| 261 |
if (!success) |
| 262 |
{ |
| 263 |
res.Value = "Failed null check ScheduleLongRunnign."; |
| 264 |
state.Value.Set(); |
| 265 |
return; |
| 266 |
} |
| 267 |
|
| 268 |
state.Value.Set(); |
| 269 |
#if !NO_THREAD |
| 270 |
var w = new ManualResetEvent(false); |
| 271 |
|
| 272 |
var d = slr.ScheduleLongRunning(cancel => |
| 273 |
{ |
| 274 |
while (!cancel.IsDisposed) |
| 275 |
; |
| 276 |
|
| 277 |
w.Set(); |
| 278 |
}); |
| 279 |
|
| 280 |
Thread.Sleep(50); |
| 281 |
d.Dispose(); |
| 282 |
w.WaitOne(); |
| 283 |
#else |
| 284 |
state.Value.Set(); |
| 285 |
#endif |
| 286 |
}); |
| 287 |
|
| 288 |
e.Value.WaitOne(); |
| 289 |
Assert.IsTrue(string.IsNullOrEmpty(r.Value)); |
| 290 |
} |
| 291 |
|
| 292 |
#if !NO_TPL |
| 293 |
[TestMethod] |
| 294 |
public void Cant_Locate_Scheduler() |
| 295 |
{ |
| 296 |
if (!Utils.IsRunningWithPortableLibraryBinaries()) |
| 297 |
{ |
| 298 |
Cant_Locate_Scheduler_NoPlib(); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
[MethodImpl(MethodImplOptions.NoInlining)] |
| 303 |
private void Cant_Locate_Scheduler_NoPlib() |
| 304 |
{ |
| 305 |
var e = new MarshalByRefCell<Exception>(); |
| 306 |
_domain.SetData("state", e); |
| 307 |
|
| 308 |
Run(() => |
| 309 |
{ |
| 310 |
var state = (MarshalByRefCell<Exception>)_domain.GetData("state"); |
| 311 |
try |
| 312 |
{ |
| 313 |
Scheduler.TaskPool.Schedule(() => { }); |
| 314 |
} |
| 315 |
catch (Exception ex) |
| 316 |
{ |
| 317 |
state.Value = ex; |
| 318 |
} |
| 319 |
}); |
| 320 |
|
| 321 |
Assert.IsTrue(e.Value != null && e.Value is NotSupportedException); |
| 322 |
} |
| 323 |
#endif |
| 324 |
|
| 325 |
#if !NO_PERF && !NO_STOPWATCH |
| 326 |
[TestMethod] |
| 327 |
public void Stopwatch() |
| 328 |
{ |
| 329 |
var e = new MarshalByRefCell<bool>(); |
| 330 |
_domain.SetData("state", e); |
| 331 |
|
| 332 |
Run(() => |
| 333 |
{ |
| 334 |
var state = (MarshalByRefCell<bool>)_domain.GetData("state"); |
| 335 |
|
| 336 |
var sw = Scheduler.Default.StartStopwatch(); |
| 337 |
|
| 338 |
var fst = sw.Elapsed; |
| 339 |
Thread.Sleep(100); |
| 340 |
var snd = sw.Elapsed; |
| 341 |
|
| 342 |
state.Value = snd > fst; |
| 343 |
}); |
| 344 |
|
| 345 |
Assert.IsTrue(e.Value); |
| 346 |
} |
| 347 |
#endif |
| 348 |
|
| 349 |
[TestMethod] |
| 350 |
public void EnsureLoaded() |
| 351 |
{ |
| 352 |
Assert.IsTrue(EnlightenmentProvider.EnsureLoaded()); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
public class MarshalByRefCell<T> : MarshalByRefObject |
| 357 |
{ |
| 358 |
public T Value; |
| 359 |
} |
| 360 |
|
| 361 |
public class MarshalByRefAction : MarshalByRefObject |
| 362 |
{ |
| 363 |
private readonly Action _action; |
| 364 |
|
| 365 |
public MarshalByRefAction(Action action) |
| 366 |
{ |
| 367 |
_action = action; |
| 368 |
} |
| 369 |
|
| 370 |
public void Invoke() |
| 371 |
{ |
| 372 |
_action(); |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
#endif |