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

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

#if STRESS
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reactive.Disposables;
using System.Reflection;
using System.Threading;

namespace ReactiveTests.Stress.Disposables
{
    public class RefCount
    {
        /// <summary>
        /// Disposes the primary disposable first, allocates a number of dependents on different threads, and disposes them on different threads.
        /// Ref count should reach zero, and the inner disposable should be called.
        /// </summary>
        public static void PrimaryFirst_DependentsTrigger()
        {
            Console.Title = MethodInfo.GetCurrentMethod().Name + " - 0% complete";

            var rnd = new Random();
            
            for (int i = 1; i <= 100; i++)
            {
                Impl(true, false, new[] { 0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 });
                Impl(true, false, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
                Impl(true, false, Enumerable.Range(0, 10).Select(_ => rnd.Next(0, 1000)));

                Console.Title = MethodInfo.GetCurrentMethod().Name + " - " + i + "% complete";
            }
        }

        /// <summary>
        /// Allocates a number of dependents on different threads, disposes them on different threads, and disposes the primary disposable last.
        /// Ref count should reach zero, and the inner disposable should be called.
        /// </summary>
        public static void DependentsFirst_PrimaryTrigger()
        {
            Console.Title = MethodInfo.GetCurrentMethod().Name + " - 0% complete";

            var rnd = new Random();

            for (int i = 1; i <= 100; i++)
            {
                Impl(false, false, new[] { 0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 });
                Impl(false, false, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
                Impl(false, false, Enumerable.Range(0, 10).Select(_ => rnd.Next(0, 1000)));

                Console.Title = MethodInfo.GetCurrentMethod().Name + " - " + i + "% complete";
            }
        }

        /// <summary>
        /// Allocates a number of dependents on different threads, disposes them on different threads, and disposes the primary disposable at a random time.
        /// Ref count should reach zero, and the inner disposable should be called.
        /// </summary>
        public static void DependentsFirst_PrimaryRandom()
        {
            Console.Title = MethodInfo.GetCurrentMethod().Name + " - 0% complete";

            var rnd = new Random();

            for (int i = 1; i <= 100; i++)
            {
                Impl(false, true, new[] { 0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 });
                Impl(false, true, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
                Impl(false, true, Enumerable.Range(0, 10).Select(_ => rnd.Next(0, 1000)));

                Console.Title = MethodInfo.GetCurrentMethod().Name + " - " + i + "% complete";
            }
        }

        private static void Impl(bool primaryFirst, bool primaryRandom, IEnumerable<int> nDependents)
        {
            var rand = new Random();

            foreach (var n in nDependents)
            {
                var e = new ManualResetEvent(false);
                var hasDependent = new ManualResetEvent(false);
                var r = new RefCountDisposable(Disposable.Create(() => { e.Set(); }));

                var d = default(IDisposable);
                if (primaryFirst)
                {
                    d = r.GetDisposable();
                    r.Dispose();
                }
                else if (primaryRandom)
                {
                    var sleep = rand.Next(0, 10) == 0 /* 10% chance */ ? rand.Next(2, 100) : 0;

                    ThreadPool.QueueUserWorkItem(_ =>
                    {
                        hasDependent.WaitOne();
                        Helpers.SleepOrSpin(sleep);
                        r.Dispose();
                    });

                    if (n == 0)
                        hasDependent.Set();
                }

                Console.Write(n + " - ");

                var cd = new CountdownEvent(n * 2);
                for (int i = 0; i < n; i++)
                {
                    var j = i;

                    var sleep1 = rand.Next(0, 10) == 0 /* 10% chance */ ? rand.Next(2, 100) : 0;
                    var sleep2 = rand.Next(0, 10) == 0 /* 10% chance */ ? rand.Next(2, 100) : 0;
                    var sleep3 = rand.Next(0, 10) == 0 /* 10% chance */ ? rand.Next(2, 100) : 0;

                    ThreadPool.QueueUserWorkItem(_ =>
                    {
                        Helpers.SleepOrSpin(sleep1);

                        Console.Write("+");

                        var f = r.GetDisposable();

                        if (j == 0)
                            hasDependent.Set();

                        Helpers.SleepOrSpin(sleep2);

                        ThreadPool.QueueUserWorkItem(__ =>
                        {
                            Helpers.SleepOrSpin(sleep3);

                            f.Dispose();

                            Console.Write("-");

                            cd.Signal();
                        });

                        cd.Signal();
                    });
                }

                cd.Wait();

                if (primaryFirst)
                    d.Dispose();
                else if (!primaryRandom)
                    r.Dispose();

                e.WaitOne();

                Console.WriteLine(".");
            }
        }
    }
}
#endif