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
using System;
using System.Threading;
using NUnit.Framework;
using OpenTap.Engine.UnitTests;
namespace OpenTap.UnitTests
{
    public class ThreadManagerTest
    {
        [Test]
        public void TestAsyncTest()
        {
            bool completed = false;
            TapThread.StartAwaitable(() => completed = true, "test").Wait();
            Assert.IsTrue(completed);
        }
 
        [Test]
        public void TestWorkQueueContext()
        {
            var blockBaseThreadWait = new SemaphoreSlim(0);
            var trd1AbortWait = new SemaphoreSlim(0);
            var jobEndWait = new SemaphoreSlim(0);
 
            var baseThread = TapThread.Start(() => blockBaseThreadWait.Wait());
 
            var workQueue = new WorkQueue(WorkQueue.Options.None, "testthread", baseThread);
            bool workEnded1 = false;
            var trd1 = TapThread.Start(() =>
            {
                trd1AbortWait.Wait();
                workQueue.EnqueueWork(() =>
                {
                    // verify that since baseThread is not aborted, this does not throw.
                    // at this point only trd1 is aborted.
                    TapThread.ThrowIfAborted();
                    workEnded1 = true;
                    jobEndWait.Release();
                });
            });
            
            // Abort the thread that enqueued the work (trd1). 
            // This should not abort the work itself (i.e. jobEndWait should be released) 
            // since the WorkQueue uses another thread (baseThread) as the parent thread for all work
            trd1.Abort();
            trd1AbortWait.Release();
 
            Assert.IsTrue(jobEndWait.Wait(10000));
            Assert.IsTrue(workEnded1);
            blockBaseThreadWait.Release();
            baseThread.Abort();
 
            bool workEnded2 = false;
            workQueue.EnqueueWork(() =>
            {
                // Now that the baseThread is aborted, test that the thread running the work is also set to abort.
                Assert.IsTrue(TapThread.Current.AbortToken.IsCancellationRequested);
                workEnded2 = true;
            });
            workQueue.Wait();
            Assert.IsTrue(workEnded2);
        }
 
        [Test]
        public void AwaitableTest()
        {
            var task = TapThread.StartAwaitable(() => throw new ExpectedException("TEST"));
            try
            {
                task.Wait();
            }
            catch (AggregateException e)
            {
                Assert.IsTrue(e.InnerException is ExpectedException);
 
 
            }
        }
 
        class ExpectedException : Exception
        {
            public ExpectedException(string message): base(message)
            {
 
            }
        }
    }
}