1 // OpenTween - Client of Twitter
2 // Copyright (c) 2024 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.
24 using System.Threading.Tasks;
29 public class AsyncExceptionBoundaryTest
32 public async Task Wrap_SynchronousTest()
34 static Task AsyncFunc()
35 => throw new OperationCanceledException();
38 await AsyncExceptionBoundary.Wrap(AsyncFunc);
42 public async Task Wrap_AsynchronousTest()
44 static async Task AsyncFunc()
47 throw new OperationCanceledException();
51 await AsyncExceptionBoundary.Wrap(AsyncFunc);
55 public async Task Wrap_IgnoredTest()
57 static Task AsyncFunc()
58 => throw new OperationCanceledException();
60 static bool IgnoreException(Exception ex)
61 => ex is OperationCanceledException;
64 await AsyncExceptionBoundary.Wrap(AsyncFunc, IgnoreException);
68 public async Task Wrap_NotIgnoredTest()
70 static Task AsyncFunc()
71 => throw new IOException();
73 static bool IgnoreException(Exception ex)
74 => ex is OperationCanceledException;
77 await Assert.ThrowsAsync<IOException>(
78 () => AsyncExceptionBoundary.Wrap(AsyncFunc, IgnoreException)
83 public async Task IgnoreException_Test()
85 var task = Task.FromException(new OperationCanceledException());
88 await AsyncExceptionBoundary.IgnoreException(task);
92 public async Task IgnoreExceptionAndDispose_Test()
94 var task = Task.FromException<int>(new OperationCanceledException());
97 await AsyncExceptionBoundary.IgnoreExceptionAndDispose(task);
101 public async Task IgnoreExceptionAndDispose_DisposeTest()
103 using var image = TestUtils.CreateDummyImage();
104 var task = Task.FromResult<object>(image); // IDisposable であることを静的に判定できない場合も想定
106 // 正常終了したとき Result が IDisposable だった場合は破棄する
107 await AsyncExceptionBoundary.IgnoreExceptionAndDispose(task);
108 Assert.True(image.IsDisposed);
112 public async Task IgnoreExceptionAndDispose_IEnumerableTest()
114 using var image1 = TestUtils.CreateDummyImage();
115 using var image2 = TestUtils.CreateDummyImage();
118 Task.FromResult<object>(image1), // IDisposable であることを静的に判定できない場合も想定
119 Task.FromResult<object>(image2),
122 // 正常終了したとき Result が IDisposable だった場合は破棄する
123 await AsyncExceptionBoundary.IgnoreExceptionAndDispose(tasks);
124 Assert.True(image1.IsDisposed);
125 Assert.True(image2.IsDisposed);