OSDN Git Service

GitHub Actionsで使用するactionをアップデート
[opentween/open-tween.git] / OpenTween.Tests / TaskCollectionTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2023 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
10 // any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 // for more details.
16 //
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
21
22 using System;
23 using System.Collections.Generic;
24 using System.IO;
25 using System.Linq;
26 using System.Text;
27 using System.Threading;
28 using System.Threading.Tasks;
29 using Xunit;
30
31 namespace OpenTween
32 {
33     public class TaskCollectionTest
34     {
35         [Fact]
36         public async Task RunAll_Test()
37         {
38             var tasks = new TaskCollection();
39
40             var tcs1 = new TaskCompletionSource<int>();
41             tasks.Add(() => tcs1.Task);
42
43             var tcs2 = new TaskCompletionSource<int>();
44             tasks.Add(() => tcs2.Task);
45
46             var runAllTask = tasks.RunAll();
47
48             // すべての Task が完了するまで待機する
49             Assert.NotEqual(runAllTask, await Task.WhenAny(runAllTask, Task.Delay(100)));
50
51             tcs1.SetResult(0);
52             Assert.NotEqual(runAllTask, await Task.WhenAny(runAllTask, Task.Delay(100)));
53
54             tcs2.SetResult(0);
55             Assert.Equal(runAllTask, await Task.WhenAny(runAllTask, Task.Delay(100)));
56         }
57
58         [Fact]
59         public async Task RunAll_EmptyTest()
60         {
61             var tasks = new TaskCollection();
62             await tasks.RunAll();
63         }
64
65         [Fact]
66         public async Task RunAll_RunOnThreadPoolEnabledTest()
67         {
68             var parentThreadId = Thread.CurrentThread.ManagedThreadId;
69             var tasks = new TaskCollection();
70
71             tasks.Add(() =>
72             {
73                 var childThreadId = Thread.CurrentThread.ManagedThreadId;
74                 Assert.NotEqual(parentThreadId, childThreadId);
75                 return Task.CompletedTask;
76             });
77
78             await tasks.RunAll(runOnThreadPool: true);
79         }
80
81         [Fact]
82         public async Task RunAll_RunOnThreadPoolDisabledTest()
83         {
84             var parentThreadId = Thread.CurrentThread.ManagedThreadId;
85             var tasks = new TaskCollection();
86
87             tasks.Add(() =>
88             {
89                 var childThreadId = Thread.CurrentThread.ManagedThreadId;
90                 Assert.Equal(parentThreadId, childThreadId);
91                 return Task.CompletedTask;
92             });
93
94             await tasks.RunAll(runOnThreadPool: false);
95         }
96
97         [Fact]
98         public async Task IgnoreException_IgnoredTest()
99         {
100             var tasks = new TaskCollection();
101             tasks.Add(() => Task.FromException(new OperationCanceledException()));
102             tasks.IgnoreException(ex => ex is OperationCanceledException);
103
104             await tasks.RunAll();
105         }
106
107         [Fact]
108         public async Task IgnoreException_NotIgnoredTest()
109         {
110             var tasks = new TaskCollection();
111             tasks.Add(() => Task.FromException(new OperationCanceledException()));
112             tasks.IgnoreException(ex => ex is IOException);
113
114             await Assert.ThrowsAsync<OperationCanceledException>(
115                 () => tasks.RunAll()
116             );
117         }
118     }
119 }