| 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.Reactive.Disposables; |
| 6 |
using System.Threading; |
| 7 |
|
| 8 |
namespace ReactiveTests |
| 9 |
{ |
| 10 |
class TestLongRunningScheduler : IScheduler, ISchedulerLongRunning, IServiceProvider |
| 11 |
{ |
| 12 |
private Action<ManualResetEvent> _setStart; |
| 13 |
private Action<ManualResetEvent> _setEnd; |
| 14 |
private Action<Exception> _setException; |
| 15 |
|
| 16 |
public TestLongRunningScheduler(Action<ManualResetEvent> setStart, Action<ManualResetEvent> setEnd) |
| 17 |
: this(setStart, setEnd, null) |
| 18 |
{ |
| 19 |
} |
| 20 |
|
| 21 |
public TestLongRunningScheduler(Action<ManualResetEvent> setStart, Action<ManualResetEvent> setEnd, Action<Exception> setException) |
| 22 |
{ |
| 23 |
_setStart = setStart; |
| 24 |
_setEnd = setEnd; |
| 25 |
_setException = setException; |
| 26 |
} |
| 27 |
|
| 28 |
public DateTimeOffset Now |
| 29 |
{ |
| 30 |
get { return DateTimeOffset.Now; } |
| 31 |
} |
| 32 |
|
| 33 |
public IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action) |
| 34 |
{ |
| 35 |
throw new NotImplementedException(); |
| 36 |
} |
| 37 |
|
| 38 |
public IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action) |
| 39 |
{ |
| 40 |
throw new NotImplementedException(); |
| 41 |
} |
| 42 |
|
| 43 |
public IDisposable Schedule<TState>(TState state, DateTimeOffset dueTime, Func<IScheduler, TState, IDisposable> action) |
| 44 |
{ |
| 45 |
throw new NotImplementedException(); |
| 46 |
} |
| 47 |
|
| 48 |
public IDisposable ScheduleLongRunning<TState>(TState state, Action<TState, ICancelable> action) |
| 49 |
{ |
| 50 |
var d = new BooleanDisposable(); |
| 51 |
|
| 52 |
var eb = new ManualResetEvent(false); |
| 53 |
_setStart(eb); |
| 54 |
|
| 55 |
var ee = new ManualResetEvent(false); |
| 56 |
_setEnd(ee); |
| 57 |
|
| 58 |
new Thread(() => |
| 59 |
{ |
| 60 |
eb.Set(); |
| 61 |
try |
| 62 |
{ |
| 63 |
action(state, d); |
| 64 |
} |
| 65 |
catch (Exception ex) |
| 66 |
{ |
| 67 |
if (_setException == null) |
| 68 |
throw; |
| 69 |
|
| 70 |
_setException(ex); |
| 71 |
} |
| 72 |
finally |
| 73 |
{ |
| 74 |
ee.Set(); |
| 75 |
} |
| 76 |
}).Start(); |
| 77 |
|
| 78 |
return d; |
| 79 |
} |
| 80 |
|
| 81 |
object IServiceProvider.GetService(Type serviceType) |
| 82 |
{ |
| 83 |
if (serviceType == typeof(ISchedulerLongRunning)) |
| 84 |
return this; |
| 85 |
|
| 86 |
return null; |
| 87 |
} |
| 88 |
} |
| 89 |
} |