Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TestLongRunningScheduler.cs « Tests.System.Reactive « Rx.NET - github.com/mono/rx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f9545a8b73f88d132d7f99caac79e6789e21b9ba (plain)
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
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

using System;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
using System.Threading;

namespace ReactiveTests
{
    class TestLongRunningScheduler : IScheduler, ISchedulerLongRunning, IServiceProvider
    {
        private Action<ManualResetEvent> _setStart;
        private Action<ManualResetEvent> _setEnd;
        private Action<Exception> _setException;

        public TestLongRunningScheduler(Action<ManualResetEvent> setStart, Action<ManualResetEvent> setEnd)
            : this(setStart, setEnd, null)
        {
        }

        public TestLongRunningScheduler(Action<ManualResetEvent> setStart, Action<ManualResetEvent> setEnd, Action<Exception> setException)
        {
            _setStart = setStart;
            _setEnd = setEnd;
            _setException = setException;
        }

        public DateTimeOffset Now
        {
            get { return DateTimeOffset.Now; }
        }

        public IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action)
        {
            throw new NotImplementedException();
        }

        public IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action)
        {
            throw new NotImplementedException();
        }

        public IDisposable Schedule<TState>(TState state, DateTimeOffset dueTime, Func<IScheduler, TState, IDisposable> action)
        {
            throw new NotImplementedException();
        }

        public IDisposable ScheduleLongRunning<TState>(TState state, Action<TState, ICancelable> action)
        {
            var d = new BooleanDisposable();

            var eb = new ManualResetEvent(false);
            _setStart(eb);

            var ee = new ManualResetEvent(false);
            _setEnd(ee);

            new Thread(() =>
            {
                eb.Set();
                try
                {
                    action(state, d);
                }
                catch (Exception ex)
                {
                    if (_setException == null)
                        throw;

                    _setException(ex);
                }
                finally
                {
                    ee.Set();
                }
            }).Start();

            return d;
        }

        object IServiceProvider.GetService(Type serviceType)
        {
            if (serviceType == typeof(ISchedulerLongRunning))
                return this;

            return null;
        }
    }
}