| 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.Reactive.Concurrency; |
| 5 |
using System.Threading; |
| 6 |
#if NUNIT |
| 7 |
using NUnit.Framework; |
| 8 |
using TestClassAttribute = NUnit.Framework.TestFixtureAttribute; |
| 9 |
using TestMethodAttribute = NUnit.Framework.TestAttribute; |
| 10 |
using TestInitializeAttribute = NUnit.Framework.SetUpAttribute; |
| 11 |
#else |
| 12 |
using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 13 |
#endif |
| 14 |
|
| 15 |
namespace ReactiveTests.Tests |
| 16 |
{ |
| 17 |
[TestClass] |
| 18 |
public class ConcurrencyTest |
| 19 |
{ |
| 20 |
[TestMethod] |
| 21 |
public void CurrentScheduler_EnsureTrampoline() |
| 22 |
{ |
| 23 |
const int concurrency = 100; |
| 24 |
|
| 25 |
var passed = true; |
| 26 |
|
| 27 |
var s = new Semaphore(0, int.MaxValue); |
| 28 |
var e = new ManualResetEvent(false); |
| 29 |
|
| 30 |
for (var i = 0; i < concurrency; ++i) |
| 31 |
NewThreadScheduler.Default.Schedule(() => |
| 32 |
{ |
| 33 |
e.WaitOne(); |
| 34 |
try |
| 35 |
{ |
| 36 |
if (Scheduler.CurrentThread.ScheduleRequired) |
| 37 |
Scheduler.CurrentThread.Schedule(() => { }); |
| 38 |
else |
| 39 |
new Action(() => { })(); |
| 40 |
} |
| 41 |
catch (NullReferenceException) |
| 42 |
{ |
| 43 |
passed = false; |
| 44 |
} |
| 45 |
finally |
| 46 |
{ |
| 47 |
s.Release(); |
| 48 |
} |
| 49 |
}); |
| 50 |
|
| 51 |
e.Set(); |
| 52 |
|
| 53 |
for (var i = 0; i < concurrency; ++i) |
| 54 |
s.WaitOne(); |
| 55 |
|
| 56 |
Assert.IsTrue(passed); |
| 57 |
} |
| 58 |
|
| 59 |
[TestMethod] |
| 60 |
public void CurrentScheduler_Schedule() |
| 61 |
{ |
| 62 |
const int concurrency = 100; |
| 63 |
|
| 64 |
var passed = true; |
| 65 |
|
| 66 |
var s = new Semaphore(0, int.MaxValue); |
| 67 |
var e = new ManualResetEvent(false); |
| 68 |
|
| 69 |
for (var i = 0; i < concurrency; ++i) |
| 70 |
NewThreadScheduler.Default.Schedule(() => |
| 71 |
{ |
| 72 |
e.WaitOne(); |
| 73 |
try |
| 74 |
{ |
| 75 |
if (Scheduler.CurrentThread.ScheduleRequired) |
| 76 |
Scheduler.CurrentThread.Schedule(() => { }); |
| 77 |
else |
| 78 |
new Action(() => { })(); |
| 79 |
} |
| 80 |
catch (NullReferenceException) |
| 81 |
{ |
| 82 |
passed = false; |
| 83 |
} |
| 84 |
finally |
| 85 |
{ |
| 86 |
s.Release(); |
| 87 |
} |
| 88 |
}); |
| 89 |
|
| 90 |
e.Set(); |
| 91 |
|
| 92 |
for (var i = 0; i < concurrency; ++i) |
| 93 |
s.WaitOne(); |
| 94 |
|
| 95 |
Assert.IsTrue(passed); |
| 96 |
} |
| 97 |
} |
| 98 |
} |