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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
// 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 SynchronizationContextSchedulerTest
{
[TestMethod]
public void SynchronizationContext_ArgumentChecking()
{
var ms = new MySync();
var s = new SynchronizationContextScheduler(ms);
ReactiveAssert.Throws<ArgumentNullException>(() => new SynchronizationContextScheduler(null));
ReactiveAssert.Throws<ArgumentNullException>(() => new SynchronizationContextScheduler(null, true));
ReactiveAssert.Throws<ArgumentNullException>(() => s.Schedule<int>(42, default(Func<IScheduler, int, IDisposable>)));
ReactiveAssert.Throws<ArgumentNullException>(() => s.Schedule<int>(42, DateTimeOffset.Now, default(Func<IScheduler, int, IDisposable>)));
ReactiveAssert.Throws<ArgumentNullException>(() => s.Schedule<int>(42, TimeSpan.Zero, default(Func<IScheduler, int, IDisposable>)));
}
[TestMethod]
public void SynchronizationContext_Now()
{
var ms = new MySync();
var s = new SynchronizationContextScheduler(ms);
var res = s.Now - DateTime.Now;
Assert.IsTrue(res.Seconds < 1);
}
[TestMethod]
public void SynchronizationContext_ScheduleAction()
{
var ms = new MySync();
var s = new SynchronizationContextScheduler(ms);
var ran = false;
s.Schedule(() => { ran = true; });
Assert.IsTrue(ms.Count == 1);
Assert.IsTrue(ran);
}
[TestMethod]
public void SynchronizationContext_ScheduleAction_TimeSpan()
{
var ms = new MySync();
var s = new SynchronizationContextScheduler(ms);
var e = new ManualResetEvent(false);
s.Schedule(TimeSpan.FromMilliseconds(1), () => { e.Set(); });
e.WaitOne();
Assert.IsTrue(ms.Count == 1);
}
[TestMethod]
public void SynchronizationContext_ScheduleAction_DateTimeOffset()
{
var ms = new MySync();
var s = new SynchronizationContextScheduler(ms);
var e = new ManualResetEvent(false);
s.Schedule(DateTimeOffset.Now.AddMilliseconds(100), () => { e.Set(); });
e.WaitOne();
Assert.IsTrue(ms.Count >= 1); // Can be > 1 in case of timer queue retry operations.
}
[TestMethod]
public void SynchronizationContext_ScheduleActionError()
{
var ms = new MySync();
var s = new SynchronizationContextScheduler(ms);
var ex = new Exception();
try
{
s.Schedule(() => { throw ex; });
Assert.Fail();
}
catch (Exception e)
{
Assert.AreSame(e, ex);
}
Assert.IsTrue(ms.Count == 1);
}
#if !SILVERLIGHT
[TestMethod]
[Ignore]
public void SynchronizationContext_ScheduleActionDue()
{
var ms = new MySync();
var s = new SynchronizationContextScheduler(ms);
var evt = new ManualResetEvent(false);
var sw = new Stopwatch();
sw.Start();
s.Schedule(TimeSpan.FromSeconds(0.2), () => { sw.Stop(); evt.Set(); });
evt.WaitOne();
Assert.IsTrue(sw.ElapsedMilliseconds > 180, "due " + sw.ElapsedMilliseconds);
Assert.IsTrue(ms.Count == 1);
}
#endif
class MySync : SynchronizationContext
{
public int Count { get; private set; }
public override void Post(SendOrPostCallback d, object state)
{
Count++;
d(state);
}
public override void Send(SendOrPostCallback d, object state)
{
throw new NotImplementedException();
}
public int Started { get; private set; }
public override void OperationStarted()
{
base.OperationStarted();
Started++;
}
public int Completed { get; private set; }
public override void OperationCompleted()
{
base.OperationCompleted();
Completed++;
}
}
[TestMethod]
public void SynchronizationContext_StartedCompleted()
{
var ms = new MySync();
var s = new SynchronizationContextScheduler(ms);
var started = 0;
s.Schedule<int>(42, TimeSpan.Zero, (self, x) => { started = ms.Started; return Disposable.Empty; });
Assert.IsTrue(started == 1);
Assert.IsTrue(ms.Count == 1);
Assert.IsTrue(ms.Completed == 1);
}
[TestMethod]
public void SynchronizationContext_DontPost_Different()
{
var ms = new MySync();
var s = new SynchronizationContextScheduler(ms, false);
var ran = false;
s.Schedule(() => { ran = true; });
Assert.IsTrue(ms.Count == 1);
Assert.IsTrue(ran);
}
[TestMethod]
public void SynchronizationContext_DontPost_Same()
{
var count = 0;
var ran = false;
var t = new Thread(() =>
{
var ms = new MySync();
SynchronizationContext.SetSynchronizationContext(ms);
var s = new SynchronizationContextScheduler(ms, false);
s.Schedule(() => { ran = true; });
count = ms.Count;
});
t.Start();
t.Join();
Assert.IsTrue(count == 0 /* no post */);
Assert.IsTrue(ran);
}
[TestMethod]
public void SynchronizationContext_AlwaysPost_Different()
{
var ms = new MySync();
var s = new SynchronizationContextScheduler(ms, true);
var ran = false;
s.Schedule(() => { ran = true; });
Assert.IsTrue(ms.Count == 1);
Assert.IsTrue(ran);
}
[TestMethod]
public void SynchronizationContext_AlwaysPost_Same()
{
var count = 0;
var ran = false;
var t = new Thread(() =>
{
var ms = new MySync();
SynchronizationContext.SetSynchronizationContext(ms);
var s = new SynchronizationContextScheduler(ms, true);
s.Schedule(() => { ran = true; });
count = ms.Count;
});
t.Start();
t.Join();
Assert.IsTrue(count == 1 /* post */);
Assert.IsTrue(ran);
}
}
}
|