Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit fa188d3

Browse files
committed
Fix calling finally handlers and add regression test for it
1 parent af2b677 commit fa188d3

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

src/GitHub.Api/Tasks/TaskBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ protected virtual void RaiseOnEnd(TResult data)
639639
protected override void CallFinallyHandler()
640640
{
641641
finallyHandler?.Invoke(!taskFailed, result);
642+
base.CallFinallyHandler();
642643
}
643644

644645
public new Task<TResult> Task

src/tests/TaskSystemIntegrationTests/TaskSystem.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
<HintPath>$(SolutionDir)\packages\AsyncBridge.Net35.0.2.3333.0\lib\net35-Client\AsyncBridge.Net35.dll</HintPath>
3939
<Private>True</Private>
4040
</Reference>
41+
<Reference Include="FluentAssertions, Version=2.2.0.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a, processorArchitecture=MSIL">
42+
<HintPath>..\..\..\packages\FluentAssertions.2.2.0.0\lib\net35\FluentAssertions.dll</HintPath>
43+
<Private>True</Private>
44+
</Reference>
4145
<Reference Include="NSubstitute, Version=1.10.0.0, Culture=neutral, PublicKeyToken=92dd2e9066daa5ca, processorArchitecture=MSIL">
4246
<HintPath>$(SolutionDir)\packages\NSubstitute.1.10.0.0\lib\net35\NSubstitute.dll</HintPath>
4347
<Private>True</Private>

src/tests/TaskSystemIntegrationTests/Tests.cs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@
1010
using NSubstitute;
1111
using GitHub.Logging;
1212
using System.Diagnostics;
13+
using System.Runtime.CompilerServices;
14+
using FluentAssertions;
1315

1416
namespace IntegrationTests
1517
{
1618
class BaseTest
1719
{
20+
protected const int Timeout = 30000;
21+
1822
public BaseTest()
1923
{
2024
Logger = LogHelper.GetLogger(GetType());
@@ -75,6 +79,27 @@ public void Setup()
7579
public void Teardown()
7680
{
7781
}
82+
83+
protected void StartTest(out Stopwatch watch, out ILogging logger, [CallerMemberName] string testName = "test")
84+
{
85+
watch = new Stopwatch();
86+
logger = LogHelper.GetLogger(testName);
87+
logger.Trace("Starting test");
88+
}
89+
90+
protected void StartTrackTime(Stopwatch watch, ILogging logger = null, string message = "")
91+
{
92+
if (!String.IsNullOrEmpty(message))
93+
logger.Trace(message);
94+
watch.Reset();
95+
watch.Start();
96+
}
97+
98+
protected void StopTrackTimeAndLog(Stopwatch watch, ILogging logger)
99+
{
100+
watch.Stop();
101+
logger.Trace($"Time: {watch.ElapsedMilliseconds}");
102+
}
78103
}
79104

80105
[TestFixture]
@@ -617,11 +642,46 @@ public async Task ExceptionPropagatesOutIfNoFinally()
617642
await task.StartAsAsync();
618643
}
619644

645+
[Test]
646+
public async Task AllFinallyHandlersAreCalledOnException()
647+
{
648+
Stopwatch watch;
649+
ILogging logger;
650+
StartTest(out watch, out logger);
651+
652+
var task = new FuncTask<string>(TaskManager.Token, () => { throw new InvalidOperationException(); });
653+
bool exceptionThrown1, exceptionThrown2;
654+
exceptionThrown1 = exceptionThrown2 = false;
655+
656+
task.Finally(success => exceptionThrown1 = !success);
657+
task.Finally((success, _) => exceptionThrown2 = !success);
658+
659+
StartTrackTime(watch);
660+
var waitTask = task.Start().Task;
661+
var ret = await TaskEx.WhenAny(waitTask, TaskEx.Delay(Timeout));
662+
StopTrackTimeAndLog(watch, logger);
663+
Assert.AreEqual(ret, waitTask);
664+
665+
exceptionThrown1.Should().BeTrue();
666+
exceptionThrown2.Should().BeTrue();
667+
}
668+
620669
[Test]
621670
public async Task StartAsyncWorks()
622671
{
672+
Stopwatch watch;
673+
ILogging logger;
674+
StartTest(out watch, out logger);
675+
623676
var task = new FuncTask<int>(Token, _ => 1);
624-
var ret = await task.StartAsAsync();
677+
678+
StartTrackTime(watch);
679+
var waitTask = task.StartAsAsync();
680+
var retTask = await TaskEx.WhenAny(waitTask, TaskEx.Delay(Timeout));
681+
StopTrackTimeAndLog(watch, logger);
682+
Assert.AreEqual(retTask, waitTask);
683+
var ret = await waitTask;
684+
625685
Assert.AreEqual(1, ret);
626686
}
627687

src/tests/TaskSystemIntegrationTests/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="AsyncBridge.Net35" version="0.2.3333.0" targetFramework="net35" />
4+
<package id="FluentAssertions" version="2.2.0.0" targetFramework="net35" />
45
<package id="NSubstitute" version="1.10.0.0" targetFramework="net35" />
56
<package id="NUnit" version="2.6.4" targetFramework="net35" />
67
<package id="NUnit.Runners" version="2.6.4" targetFramework="net35" />

0 commit comments

Comments
 (0)