chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
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
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
 
namespace OpenTap.UnitTests
{
    [TestFixture]
    public class TapThreadTest
    {
 
        static ThreadField<object> tf = new ThreadField<object>();
        static ThreadField<object> tf2 = new ThreadField<object>();
        static ThreadField<object> tflat = new ThreadField<object>(ThreadFieldMode.Flat);
        [Test]
        public void TestThreadField()
        {
            tf.Value = 100;
            tf2.Value = 200;
            object value = null;
            Semaphore sem = new Semaphore(0,1);
            TapThread.Start(() =>
            {
                value = tf.Value;
                sem.Release();
            });
            sem.WaitOne();
            Assert.AreEqual(100, value);
 
            for (int i = 2000; i < 2010; i += 1)
            {
                TapThread.WithNewContext(() =>
                {
                    Assert.AreEqual(100, tf.Value);
                    tf.Value = i;
                    Assert.AreEqual(i, tf.Value);
                    TapThread.WithNewContext(() =>
                    {
                        // verify inherit values from parent thread.
                        Assert.AreEqual(i, tf.Value);
                        tf.Value = i * 2;
                        Assert.AreEqual(i * 2, tf.Value);
                    });
                });
                Assert.AreEqual(100, tf.Value);
            }
 
            // verify tflat.
            tflat.Value = 100;
 
            TapThread.WithNewContext(() =>
            {
                Assert.AreEqual(null, tflat.Value);
                tflat.Value = 200;
                TapThread.WithNewContext(() =>
                {
                    // verify it does NOT inherit values from parent thread.
                    Assert.AreEqual(null, tflat.Value);
                    tflat.Value = 300;
                    Assert.AreEqual(300, tflat.Value);
 
                });
                Assert.AreEqual(200, tflat.Value);
            });
            Assert.AreEqual(100, tflat.Value);
 
        }
 
        /// <summary>
        /// Test two levels of children where the last level finishes last
        /// </summary>
        [Test]
        public void TestHierarchyCompleted1()
        {
            ManualResetEventSlim level1Completed = new ManualResetEventSlim();
            ManualResetEventSlim level2Completed = new ManualResetEventSlim();
            ManualResetEventSlim hierarchyCompletedCallbackCalled = new ManualResetEventSlim();
            bool level1CompletedBeforeHierarchyCompletedCallback = false;
            bool level2CompletedBeforeHierarchyCompletedCallback = false;
            TapThread.Start(() =>
            {
                TapThread.Start(() =>
                {
                    Thread.Sleep(100);
                    TapThread.Start(() =>
                    {
                        Thread.Sleep(100);
                        level2Completed.Set();
                    }, "Level2");
                    level1Completed.Set();
                }, "Level1");
            }, () =>
            {
                level1CompletedBeforeHierarchyCompletedCallback = level1Completed.IsSet;
                level2CompletedBeforeHierarchyCompletedCallback = level2Completed.IsSet;
                hierarchyCompletedCallbackCalled.Set();
            }, "Root");
            Thread.Sleep(200);
            Assert.IsTrue(hierarchyCompletedCallbackCalled.Wait(3000), "onHierarchyCompleted callback not called.");
            Assert.IsTrue(level1CompletedBeforeHierarchyCompletedCallback, "Child thread did not complete before onHierarchyCompleted callback.");
            Assert.IsTrue(level2CompletedBeforeHierarchyCompletedCallback, "Second level child thread did not complete before onHierarchyCompleted callback.");
        }
 
        /// <summary>
        /// Test two levels of children where the last level finishes first
        /// </summary>
        [Test]
        public void TestHierarchyCompleted2()
        {
            ManualResetEventSlim level1Completed = new ManualResetEventSlim();
            ManualResetEventSlim level2Completed = new ManualResetEventSlim();
            ManualResetEventSlim hierarchyCompletedCallbackCalled = new ManualResetEventSlim();
            bool level1CompletedBeforeHierarchyCompletedCallback = false;
            bool level2CompletedBeforeHierarchyCompletedCallback = false;
            TapThread.Start(() =>
            {
                TapThread.Start(() =>
                {
                    TapThread.Start(() =>
                    {
                        level2Completed.Set();
                    }, "Level2");
                    Thread.Sleep(100);
                    level1Completed.Set();
                }, "Level1");
            }, () =>
            {
                level1CompletedBeforeHierarchyCompletedCallback = level1Completed.IsSet;
                level2CompletedBeforeHierarchyCompletedCallback = level2Completed.IsSet;
                hierarchyCompletedCallbackCalled.Set();
            }, "Root");
            Thread.Sleep(200);
            Assert.IsTrue(hierarchyCompletedCallbackCalled.Wait(30 * 1000), "onHierarchyCompleted callback not called.");
            Assert.IsTrue(level1CompletedBeforeHierarchyCompletedCallback, "Child thread did not complete before onHierarchyCompleted callback.");
            Assert.IsTrue(level2CompletedBeforeHierarchyCompletedCallback, "Second level child thread did not complete before onHierarchyCompleted callback.");
        }
 
        [Test]
        public async Task TestAwaitedThreadThrows([Values(true, false)] bool throws)
        {
            void callback()
            {
                if (throws)
                    throw new Exception("Throws");
            }
 
            var trd = TapThread.StartAwaitable(callback);
 
            if (throws)
            {
                try
                {
                    await trd;
                    Assert.Fail("This should have thrown.");
                }
                catch (Exception ex)
                {
                    StringAssert.Contains("Throws", ex.Message);
                }
            }
            else
            {
                await trd;
                Assert.Pass();
            }
        }
 
        /// <summary>
        /// Test what happens when sibling threads gets aborted - verify that only the right ones are aborted.
        /// </summary>
        [Test]
        public void MultipleThreadAbort()
        {
            TapThread mainThread = null;
            (TapThread, Semaphore)[] internalThreads = null;
 
            TapThread.WithNewContext(() =>
            {
 
                (TapThread, Semaphore) createThread()
                {
                    // Semaphores are released when the threads are aborted.
                    var sem = new Semaphore(0, 1);
                    var trd = TapThread.Start(() =>
                    {
                        try
                        {
                            TapThread.Sleep(200000);
                            Assert.Fail("The thread should have been aborted");
                        }
                        catch (OperationCanceledException)
                        {
                            sem.Release(1);
                        }
                    });
                    return (trd, sem);
                }
 
                var threadSems = Enumerable.Range(0, 20).Select(x => createThread()).ToArray();
                for (int i = 0; i < threadSems.Length; i++)
                {
                    threadSems[i].Item1.Abort();
                    Assert.IsTrue(threadSems[i].Item2.WaitOne(20000));
                    for (int j = i + 1; j < threadSems.Length; j++)
                    {
                        Assert.IsFalse(threadSems[j].Item2.WaitOne(0));
                    }
                }
 
                // now lets try aborting the parent thread.
                internalThreads = Enumerable.Range(0, 20).Select(x => createThread()).ToArray();
                mainThread = TapThread.Current;
            });
 
            mainThread.Abort();
            for (int i = 0; i < internalThreads.Length; i++)
            {
                Assert.IsTrue(internalThreads[i].Item2.WaitOne(20000));
            }
        }
    }
}