1 // OpenTween - Client of Twitter
2 // Copyright (c) 2023 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
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)
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
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.
23 using System.Collections.Generic;
27 using System.Threading;
28 using System.Threading.Tasks;
33 public class TaskCollectionTest
36 public async Task RunAll_Test()
38 var tasks = new TaskCollection();
40 var tcs1 = new TaskCompletionSource<int>();
41 tasks.Add(() => tcs1.Task);
43 var tcs2 = new TaskCompletionSource<int>();
44 tasks.Add(() => tcs2.Task);
46 var runAllTask = tasks.RunAll();
48 // すべての Task が完了するまで待機する
49 Assert.NotEqual(runAllTask, await Task.WhenAny(runAllTask, Task.Delay(100)));
52 Assert.NotEqual(runAllTask, await Task.WhenAny(runAllTask, Task.Delay(100)));
55 Assert.Equal(runAllTask, await Task.WhenAny(runAllTask, Task.Delay(100)));
59 public async Task RunAll_EmptyTest()
61 var tasks = new TaskCollection();
66 public async Task RunAll_RunOnThreadPoolEnabledTest()
68 var parentThreadId = Thread.CurrentThread.ManagedThreadId;
69 var tasks = new TaskCollection();
73 var childThreadId = Thread.CurrentThread.ManagedThreadId;
74 Assert.NotEqual(parentThreadId, childThreadId);
75 return Task.CompletedTask;
78 await tasks.RunAll(runOnThreadPool: true);
82 public async Task RunAll_RunOnThreadPoolDisabledTest()
84 var parentThreadId = Thread.CurrentThread.ManagedThreadId;
85 var tasks = new TaskCollection();
89 var childThreadId = Thread.CurrentThread.ManagedThreadId;
90 Assert.Equal(parentThreadId, childThreadId);
91 return Task.CompletedTask;
94 await tasks.RunAll(runOnThreadPool: false);
98 public async Task IgnoreException_IgnoredTest()
100 var tasks = new TaskCollection();
101 tasks.Add(() => Task.FromException(new OperationCanceledException()));
102 tasks.IgnoreException(ex => ex is OperationCanceledException);
104 await tasks.RunAll();
108 public async Task IgnoreException_NotIgnoredTest()
110 var tasks = new TaskCollection();
111 tasks.Add(() => Task.FromException(new OperationCanceledException()));
112 tasks.IgnoreException(ex => ex is IOException);
114 await Assert.ThrowsAsync<OperationCanceledException>(