├── .github
├── FUNDING.yml
└── workflows
│ └── build.yml
├── .gitignore
├── LICENSE
├── Nito.AsyncEx.Testing.sln
├── README.md
├── src
├── Directory.Build.props
├── Directory.Build.targets
├── Nito.AsyncEx.Testing
│ ├── AsyncAssert.cs
│ └── Nito.AsyncEx.Testing.csproj
├── icon.png
└── project.props
└── test
├── Directory.Build.props
└── UnitTests
├── Cancels.cs
├── CancelsAsync_Delegate.cs
├── CancelsAsync_Delegate_Synchronous.cs
├── CancelsAsync_Task.cs
├── CancelsAsync_Task_Synchronous.cs
├── NeverCompletesAsync.cs
├── Throws.cs
├── ThrowsAsync_Delegate.cs
├── ThrowsAsync_Delegate_Synchronous.cs
├── ThrowsAsync_Task.cs
├── ThrowsAsync_Task_Synchronous.cs
└── UnitTests.csproj
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | github: [StephenCleary]
2 |
--------------------------------------------------------------------------------
/.github/workflows/build.yml:
--------------------------------------------------------------------------------
1 | # build.yml v1.4
2 | # 1.4 - Avoid set-env.
3 | # 1.3 - Include tag workflow in this file.
4 | # 1.2 - Define DOTNET_SKIP_FIRST_TIME_EXPERIENCE/NUGET_XMLDOC_MODE.
5 | # 1.1 - Use actions/cache@v2.
6 | # 1.0 - Initial release.
7 |
8 | name: Build
9 |
10 | on:
11 | - push
12 |
13 | env:
14 | CI: 'true'
15 | DOTNET_CLI_TELEMETRY_OPTOUT: 'true'
16 | DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 'true'
17 | NUGET_XMLDOC_MODE: 'skip'
18 |
19 | jobs:
20 | build:
21 | runs-on: ubuntu-latest
22 | steps:
23 |
24 | - name: Dump context
25 | env:
26 | CONTEXT: ${{ toJson(github) }}
27 | run: |
28 | echo "Context: $Env:CONTEXT"
29 |
30 | - name: Determine cache fallbacks
31 | if: github.event_name != 'push'
32 | id: cache_fallbacks
33 | run: |
34 | echo "::set-output name=nuget::nuget-"
35 |
36 | - name: Checkout
37 | uses: actions/checkout@v2
38 | with:
39 | fetch-depth: 0
40 |
41 | - name: Cache nuget
42 | uses: actions/cache@v2
43 | with:
44 | path: ~/.nuget/packages
45 | key: nuget-${{ hashFiles('**/*.csproj') }}-${{ hashFiles('**/*.props') }}
46 | restore-keys: ${{ steps.cache_fallbacks.outputs.nuget }}
47 |
48 | - name: Get existing tag
49 | id: existingtag
50 | uses: WyriHaximus/github-action-get-previous-tag@0.2.0
51 | continue-on-error: true
52 |
53 | - name: Get current version
54 | run: |
55 | dotnet tool install --global Nito.ProjProps
56 | echo "NEWTAG=v$(projprops --name version --output-format SingleValueOnly --project src --project-search)" >> $GITHUB_ENV
57 |
58 | - name: Build
59 | run: |
60 | dotnet build --configuration Release
61 | dotnet test --configuration Release --no-build --collect:"XPlat Code Coverage"
62 | dotnet pack --configuration Release --no-build
63 |
64 | - name: Upload package artifacts
65 | uses: actions/upload-artifact@v2
66 | with:
67 | name: nuget-packages
68 | path: |
69 | **/*.nupkg
70 | **/*.snupkg
71 |
72 | - name: Publish code coverage
73 | uses: codecov/codecov-action@v1
74 |
75 | - name: Publish packages
76 | if: env.NEWTAG != steps.existingtag.outputs.tag
77 | run: |
78 | dotnet nuget push **/*.nupkg --source https://api.nuget.org/v3/index.json --skip-duplicate --api-key ${{ secrets.NUGET_API_KEY }}
79 |
80 | - name: Create tag
81 | if: env.NEWTAG != steps.existingtag.outputs.tag
82 | run: |
83 | git tag ${{ env.NEWTAG }}
84 | git push --tags
85 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # User-specific files
5 | *.suo
6 | *.user
7 | *.userosscache
8 | *.sln.docstates
9 |
10 | # User-specific files (MonoDevelop/Xamarin Studio)
11 | *.userprefs
12 |
13 | # Build results
14 | [Dd]ebug/
15 | [Dd]ebugPublic/
16 | [Rr]elease/
17 | [Rr]eleases/
18 | x64/
19 | x86/
20 | build/
21 | bld/
22 | [Bb]in/
23 | [Oo]bj/
24 |
25 | # Visual Studo 2015 cache/options directory
26 | .vs/
27 |
28 | # MSTest test Results
29 | [Tt]est[Rr]esult*/
30 | [Bb]uild[Ll]og.*
31 |
32 | # NUNIT
33 | *.VisualState.xml
34 | TestResult.xml
35 |
36 | # Build Results of an ATL Project
37 | [Dd]ebugPS/
38 | [Rr]eleasePS/
39 | dlldata.c
40 |
41 | *_i.c
42 | *_p.c
43 | *_i.h
44 | *.ilk
45 | *.meta
46 | *.obj
47 | *.pch
48 | *.pdb
49 | *.pgc
50 | *.pgd
51 | *.rsp
52 | *.sbr
53 | *.tlb
54 | *.tli
55 | *.tlh
56 | *.tmp
57 | *.tmp_proj
58 | *.log
59 | *.vspscc
60 | *.vssscc
61 | .builds
62 | *.pidb
63 | *.svclog
64 | *.scc
65 |
66 | # Chutzpah Test files
67 | _Chutzpah*
68 |
69 | # Visual C++ cache files
70 | ipch/
71 | *.aps
72 | *.ncb
73 | *.opensdf
74 | *.sdf
75 | *.cachefile
76 |
77 | # Visual Studio profiler
78 | *.psess
79 | *.vsp
80 | *.vspx
81 |
82 | # TFS 2012 Local Workspace
83 | $tf/
84 |
85 | # Guidance Automation Toolkit
86 | *.gpState
87 |
88 | # ReSharper is a .NET coding add-in
89 | _ReSharper*/
90 | *.[Rr]e[Ss]harper
91 | *.DotSettings.user
92 |
93 | # JustCode is a .NET coding addin-in
94 | .JustCode
95 |
96 | # TeamCity is a build add-in
97 | _TeamCity*
98 |
99 | # DotCover is a Code Coverage Tool
100 | *.dotCover
101 |
102 | # NCrunch
103 | _NCrunch_*
104 | .*crunch*.local.xml
105 |
106 | # MightyMoose
107 | *.mm.*
108 | AutoTest.Net/
109 |
110 | # Web workbench (sass)
111 | .sass-cache/
112 |
113 | # Installshield output folder
114 | [Ee]xpress/
115 |
116 | # DocProject is a documentation generator add-in
117 | DocProject/buildhelp/
118 | DocProject/Help/*.HxT
119 | DocProject/Help/*.HxC
120 | DocProject/Help/*.hhc
121 | DocProject/Help/*.hhk
122 | DocProject/Help/*.hhp
123 | DocProject/Help/Html2
124 | DocProject/Help/html
125 |
126 | # Click-Once directory
127 | publish/
128 |
129 | # Publish Web Output
130 | *.[Pp]ublish.xml
131 | *.azurePubxml
132 | # TODO: Comment the next line if you want to checkin your web deploy settings
133 | # but database connection strings (with potential passwords) will be unencrypted
134 | *.pubxml
135 | *.publishproj
136 |
137 | # NuGet Packages
138 | *.nupkg
139 | # The packages folder can be ignored because of Package Restore
140 | **/packages/*
141 | # except build/, which is used as an MSBuild target.
142 | !**/packages/build/
143 | # Uncomment if necessary however generally it will be regenerated when needed
144 | #!**/packages/repositories.config
145 |
146 | # Windows Azure Build Output
147 | csx/
148 | *.build.csdef
149 |
150 | # Windows Store app package directory
151 | AppPackages/
152 |
153 | # Others
154 | *.[Cc]ache
155 | ClientBin/
156 | [Ss]tyle[Cc]op.*
157 | ~$*
158 | *~
159 | *.dbmdl
160 | *.dbproj.schemaview
161 | *.pfx
162 | *.publishsettings
163 | node_modules/
164 | bower_components/
165 |
166 | # RIA/Silverlight projects
167 | Generated_Code/
168 |
169 | # Backup & report files from converting an old project file
170 | # to a newer Visual Studio version. Backup files are not needed,
171 | # because we have git ;-)
172 | _UpgradeReport_Files/
173 | Backup*/
174 | UpgradeLog*.XML
175 | UpgradeLog*.htm
176 |
177 | # SQL Server files
178 | *.mdf
179 | *.ldf
180 |
181 | # Business Intelligence projects
182 | *.rdl.data
183 | *.bim.layout
184 | *.bim_*.settings
185 |
186 | # Microsoft Fakes
187 | FakesAssemblies/
188 |
189 | # Node.js Tools for Visual Studio
190 | .ntvs_analysis.dat
191 |
192 | # Visual Studio 6 build log
193 | *.plg
194 |
195 | # Visual Studio 6 workspace options file
196 | *.opt
197 |
198 | *.lock.json
199 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 Stephen Cleary
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/Nito.AsyncEx.Testing.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.30517.126
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{8FBC0C94-6B41-4290-BA5E-595D2C4D1D76}"
7 | EndProject
8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{D83376BF-DB75-423F-9DFF-C8C369CE4BD7}"
9 | EndProject
10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nito.AsyncEx.Testing", "src\Nito.AsyncEx.Testing\Nito.AsyncEx.Testing.csproj", "{C6C3EB33-71DD-4616-B47F-1FC35BC5B903}"
11 | EndProject
12 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTests", "test\UnitTests\UnitTests.csproj", "{4A5CD78D-E1D8-47A8-96A6-5B58291BA5CB}"
13 | EndProject
14 | Global
15 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
16 | Debug|Any CPU = Debug|Any CPU
17 | Release|Any CPU = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
20 | {C6C3EB33-71DD-4616-B47F-1FC35BC5B903}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21 | {C6C3EB33-71DD-4616-B47F-1FC35BC5B903}.Debug|Any CPU.Build.0 = Debug|Any CPU
22 | {C6C3EB33-71DD-4616-B47F-1FC35BC5B903}.Release|Any CPU.ActiveCfg = Release|Any CPU
23 | {C6C3EB33-71DD-4616-B47F-1FC35BC5B903}.Release|Any CPU.Build.0 = Release|Any CPU
24 | {4A5CD78D-E1D8-47A8-96A6-5B58291BA5CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25 | {4A5CD78D-E1D8-47A8-96A6-5B58291BA5CB}.Debug|Any CPU.Build.0 = Debug|Any CPU
26 | {4A5CD78D-E1D8-47A8-96A6-5B58291BA5CB}.Release|Any CPU.ActiveCfg = Release|Any CPU
27 | {4A5CD78D-E1D8-47A8-96A6-5B58291BA5CB}.Release|Any CPU.Build.0 = Release|Any CPU
28 | EndGlobalSection
29 | GlobalSection(SolutionProperties) = preSolution
30 | HideSolutionNode = FALSE
31 | EndGlobalSection
32 | GlobalSection(NestedProjects) = preSolution
33 | {C6C3EB33-71DD-4616-B47F-1FC35BC5B903} = {8FBC0C94-6B41-4290-BA5E-595D2C4D1D76}
34 | {4A5CD78D-E1D8-47A8-96A6-5B58291BA5CB} = {D83376BF-DB75-423F-9DFF-C8C369CE4BD7}
35 | EndGlobalSection
36 | GlobalSection(ExtensibilityGlobals) = postSolution
37 | SolutionGuid = {919AC759-203D-4A85-93E3-47CA5E0862D0}
38 | EndGlobalSection
39 | EndGlobal
40 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 
2 |
3 | # AsyncEx.Testing [](https://github.com/StephenCleary/AsyncEx.Testing/actions?query=workflow%3ABuild) [](https://codecov.io/gh/StephenCleary/AsyncEx.Testing) [](https://www.nuget.org/packages/Nito.AsyncEx.Testing) [](http://dotnetapis.com/pkg/Nito.AsyncEx.Testing)
4 |
5 | Helper types for writing unit tests for asynchronous code.
6 |
--------------------------------------------------------------------------------
/src/Directory.Build.props:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
11 |
12 |
13 |
14 | true
15 | latest
16 | enable
17 | true
18 |
19 |
20 | true
21 |
22 |
23 |
24 | all
25 | runtime; build; native; contentfiles; analyzers; buildtransitive
26 |
27 |
28 |
29 |
30 |
31 | https://github.com/$(GITHUB_REPOSITORY)
32 | MIT
33 |
34 |
35 |
36 |
37 | icon.png
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 | true
46 | true
47 | true
48 | snupkg
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 | dev
62 |
63 |
64 |
65 |
66 |
67 |
68 |
72 |
73 | <_LocalTopLevelSourceRoot Include="@(SourceRoot)" Condition="'%(SourceRoot.NestedRoot)' == ''"/>
74 |
75 |
76 |
--------------------------------------------------------------------------------
/src/Directory.Build.targets:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
9 |
10 | false
11 | false
12 | NU5128
13 |
14 |
15 |
16 | true
17 |
18 |
--------------------------------------------------------------------------------
/src/Nito.AsyncEx.Testing/AsyncAssert.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.ExceptionServices;
3 | using System.Threading.Tasks;
4 |
5 | namespace Nito.AsyncEx.Testing
6 | {
7 | ///
8 | /// Provides static methods useful for testing asynchronous methods and tasks.
9 | ///
10 | public static class AsyncAssert
11 | {
12 | ///
13 | /// Ensures that a synchronous delegate throws an exception of an expected type.
14 | ///
15 | /// The type of exception to expect.
16 | /// The synchronous delegate to test.
17 | /// Whether derived types should be accepted.
18 | public static TException Throws(Action action, bool allowDerivedTypes = true)
19 | where TException : Exception
20 | {
21 | _ = action ?? throw new ArgumentNullException(nameof(action));
22 | try
23 | {
24 | action();
25 | }
26 | catch (Exception ex)
27 | {
28 | if (allowDerivedTypes && !(ex is TException))
29 | throw new Exception("Delegate threw exception of type " + ex.GetType().Name + ", but " + typeof(TException).Name + " or a derived type was expected.", ex);
30 | if (!allowDerivedTypes && ex.GetType() != typeof(TException))
31 | throw new Exception("Delegate threw exception of type " + ex.GetType().Name + ", but " + typeof(TException).Name + " was expected.", ex);
32 | return (TException)ex;
33 | }
34 | throw new Exception("Delegate did not throw expected exception " + typeof(TException).Name + ".");
35 | }
36 |
37 | ///
38 | /// Ensures that a synchronous delegate throws an exception.
39 | ///
40 | /// The synchronous delegate to test.
41 | public static Exception Throws(Action action)
42 | {
43 | return Throws(action, true);
44 | }
45 |
46 | ///
47 | /// Ensures that an asynchronous delegate throws an exception of an expected type.
48 | ///
49 | /// The type of exception to expect.
50 | /// The asynchronous delegate to test.
51 | /// Whether derived types should be accepted.
52 | public static async Task ThrowsAsync(Func action, bool allowDerivedTypes = true)
53 | where TException : Exception
54 | {
55 | _ = action ?? throw new ArgumentNullException(nameof(action));
56 | try
57 | {
58 | await action().ConfigureAwait(false);
59 | }
60 | catch (Exception ex)
61 | {
62 | if (allowDerivedTypes && !(ex is TException))
63 | throw new Exception("Delegate threw exception of type " + ex.GetType().Name + ", but " + typeof(TException).Name + " or a derived type was expected.", ex);
64 | if (!allowDerivedTypes && ex.GetType() != typeof(TException))
65 | throw new Exception("Delegate threw exception of type " + ex.GetType().Name + ", but " + typeof(TException).Name + " was expected.", ex);
66 | return (TException)ex;
67 | }
68 | throw new Exception("Delegate did not throw expected exception " + typeof(TException).Name + ".");
69 | }
70 |
71 | ///
72 | /// Ensures that an asynchronous delegate throws an exception.
73 | ///
74 | /// The asynchronous delegate to test.
75 | public static Task ThrowsAsync(Func action)
76 | {
77 | return ThrowsAsync(action, true);
78 | }
79 |
80 | ///
81 | /// Ensures that a task throws an exception of an expected type.
82 | ///
83 | /// The type of exception to expect.
84 | /// The task to observe.
85 | /// Whether derived types should be accepted.
86 | public static Task ThrowsAsync(Task task, bool allowDerivedTypes = true)
87 | where TException : Exception
88 | {
89 | return ThrowsAsync(() => task, allowDerivedTypes);
90 | }
91 |
92 | ///
93 | /// Ensures that a task throws an exception.
94 | ///
95 | /// The task to observe.
96 | public static Task ThrowsAsync(Task task)
97 | {
98 | return ThrowsAsync(task, true);
99 | }
100 |
101 | ///
102 | /// Ensures that a synchronous delegate is cancelled.
103 | ///
104 | /// The synchronous delegate to test.
105 | public static OperationCanceledException Cancels(Action action)
106 | {
107 | return Throws(action, true);
108 | }
109 |
110 |
111 | ///
112 | /// Ensures that an asynchronous delegate is cancelled.
113 | ///
114 | /// The asynchronous delegate to test.
115 | public static Task CancelsAsync(Func action)
116 | {
117 | return ThrowsAsync(action, true);
118 | }
119 |
120 | ///
121 | /// Ensures that a task is cancelled.
122 | ///
123 | /// The task to observe.
124 | public static Task CancelsAsync(Task task)
125 | {
126 | return ThrowsAsync(task, true);
127 | }
128 |
129 | ///
130 | /// Attempts to ensure that a task never completes. If the task takes a long time to complete, this method may not detect that it (incorrectly) completes.
131 | ///
132 | /// The task to observe.
133 | /// The amount of time to (asynchronously) wait for the task to complete.
134 | public static async Task NeverCompletesAsync(Task task, int timeout = 500)
135 | {
136 | _ = task ?? throw new ArgumentNullException(nameof(task));
137 |
138 | // Wait for the task to complete, or the timeout to fire.
139 | var completedTask = await Task.WhenAny(task, Task.Delay(timeout)).ConfigureAwait(false);
140 | if (completedTask == task)
141 | throw new Exception("Task completed unexpectedly.");
142 |
143 | // If the task didn't complete, attach a continuation that will raise an exception on a random thread pool thread if it ever does complete.
144 | try
145 | {
146 | throw new Exception("Task completed unexpectedly.");
147 | }
148 | #pragma warning disable CA1031 // Do not catch general exception types
149 | catch (Exception ex)
150 | #pragma warning restore CA1031 // Do not catch general exception types
151 | {
152 | var info = ExceptionDispatchInfo.Capture(ex);
153 | var __ = task.ContinueWith(_ => info.Throw(), TaskScheduler.Default);
154 | }
155 | }
156 | }
157 | }
158 |
--------------------------------------------------------------------------------
/src/Nito.AsyncEx.Testing/Nito.AsyncEx.Testing.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Helpful types for writing asynchronous unit tests.
5 | netstandard1.0;netstandard2.0;net461
6 | async;unit;test
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/src/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/StephenCleary/AsyncEx.Testing/fcbaeacc4d6c00461d40d50f282b40bb72740a38/src/icon.png
--------------------------------------------------------------------------------
/src/project.props:
--------------------------------------------------------------------------------
1 |
2 |
3 | 1.1.1
4 | Stephen Cleary
5 |
6 |
--------------------------------------------------------------------------------
/test/Directory.Build.props:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 | latest
9 |
10 |
11 |
12 |
13 |
14 |
15 | all
16 | runtime; build; native; contentfiles; analyzers; buildtransitive
17 |
18 |
19 | all
20 | runtime; build; native; contentfiles; analyzers; buildtransitive
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/test/UnitTests/Cancels.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading.Tasks;
3 | using Nito.AsyncEx.Testing;
4 | using Xunit;
5 |
6 | namespace UnitTests
7 | {
8 | public partial class UnitTests
9 | {
10 | [Fact]
11 | public void Cancels_DelegateDoesNotCancel_Fails()
12 | {
13 | Assert.Throws(() =>
14 | {
15 | AsyncAssert.Cancels(() => { });
16 | });
17 | }
18 |
19 | [Fact]
20 | public void Cancels_DelegateThrowsWrongException_Fails()
21 | {
22 | Assert.Throws(() =>
23 | {
24 | AsyncAssert.Cancels(() => { throw new InvalidOperationException(); });
25 | });
26 | }
27 |
28 | [Fact]
29 | public void Cancels_DelegateCancels_ReturnsException()
30 | {
31 | var expectedException = new OperationCanceledException();
32 | var result = AsyncAssert.Cancels(() => { throw expectedException; });
33 | Assert.Same(expectedException, result);
34 | }
35 |
36 | [Fact]
37 | public void Cancels_DelegateCancelsWithDerivedException_ReturnsException()
38 | {
39 | var expectedException = new TaskCanceledException();
40 | var result = AsyncAssert.Cancels(() => { throw expectedException; });
41 | Assert.Same(expectedException, result);
42 | }
43 |
44 | [Fact]
45 | public void Cancels_DelegateThrowsBaseException_Fails()
46 | {
47 | Assert.Throws(() =>
48 | {
49 | AsyncAssert.Cancels(() => { throw new Exception(); });
50 | });
51 | }
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/test/UnitTests/CancelsAsync_Delegate.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading.Tasks;
3 | using Nito.AsyncEx.Testing;
4 | using Xunit;
5 |
6 | namespace UnitTests
7 | {
8 | public partial class UnitTests
9 | {
10 | [Fact]
11 | public async Task CancelsAsync_DelegateDoesNotCancel_Fails()
12 | {
13 | await Assert.ThrowsAsync(async () =>
14 | {
15 | await AsyncAssert.CancelsAsync(async () => { await Task.Yield(); });
16 | });
17 | }
18 |
19 | [Fact]
20 | public async Task CancelsAsync_DelegateThrowsWrongException_Fails()
21 | {
22 | await Assert.ThrowsAsync(async () =>
23 | {
24 | await AsyncAssert.CancelsAsync(async () => { await Task.Yield(); throw new InvalidOperationException(); });
25 | });
26 | }
27 |
28 | [Fact]
29 | public async Task CancelsAsync_DelegateCancels_ReturnsException()
30 | {
31 | var expectedException = new OperationCanceledException();
32 | var result = await AsyncAssert.CancelsAsync(async () => { await Task.Yield(); throw expectedException; });
33 | Assert.Same(expectedException, result);
34 | }
35 |
36 | [Fact]
37 | public async Task CancelsAsync_DelegateCancelsWithDerivedException_ReturnsException()
38 | {
39 | var expectedException = new TaskCanceledException();
40 | var result = await AsyncAssert.CancelsAsync(async () => { await Task.Yield(); throw expectedException; });
41 | Assert.Same(expectedException, result);
42 | }
43 |
44 | [Fact]
45 | public async Task CancelsAsync_DelegateThrowsBaseException_Fails()
46 | {
47 | await Assert.ThrowsAsync(async () =>
48 | {
49 | await AsyncAssert.CancelsAsync(async () => { await Task.Yield(); throw new Exception(); });
50 | });
51 | }
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/test/UnitTests/CancelsAsync_Delegate_Synchronous.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading.Tasks;
3 | using Nito.AsyncEx.Testing;
4 | using Xunit;
5 |
6 | namespace UnitTests
7 | {
8 | public partial class UnitTests
9 | {
10 | [Fact]
11 | public async Task CancelsAsync_SynchronousDelegateDoesNotCancel_Fails()
12 | {
13 | await Assert.ThrowsAsync(async () =>
14 | {
15 | await AsyncAssert.CancelsAsync(() => Task.FromResult(0));
16 | });
17 | }
18 |
19 | [Fact]
20 | public async Task CancelsAsync_SynchronousDelegateThrowsWrongException_Fails()
21 | {
22 | await Assert.ThrowsAsync(async () =>
23 | {
24 | await AsyncAssert.CancelsAsync(() => { throw new InvalidOperationException(); });
25 | });
26 | }
27 |
28 | [Fact]
29 | public async Task CancelsAsync_SynchronousDelegateCancels_ReturnsException()
30 | {
31 | var expectedException = new OperationCanceledException();
32 | var result = await AsyncAssert.CancelsAsync(() => { throw expectedException; });
33 | Assert.Same(expectedException, result);
34 | }
35 |
36 | [Fact]
37 | public async Task CancelsAsync_SynchronousDelegateCancelsWithDerivedException_ReturnsException()
38 | {
39 | var expectedException = new TaskCanceledException();
40 | var result = await AsyncAssert.CancelsAsync(() => { throw expectedException; });
41 | Assert.Same(expectedException, result);
42 | }
43 |
44 | [Fact]
45 | public async Task CancelsAsync_SynchronousDelegateThrowsBaseException_Fails()
46 | {
47 | await Assert.ThrowsAsync(async () =>
48 | {
49 | await AsyncAssert.CancelsAsync(() => { throw new Exception(); });
50 | });
51 | }
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/test/UnitTests/CancelsAsync_Task.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading.Tasks;
3 | using Nito.AsyncEx.Testing;
4 | using Xunit;
5 |
6 | namespace UnitTests
7 | {
8 | public partial class UnitTests
9 | {
10 | [Fact]
11 | public async Task CancelsAsync_TaskDoesNotCancel_Fails()
12 | {
13 | await Assert.ThrowsAsync(async () =>
14 | {
15 | var tcs = new TaskCompletionSource