Skip to content

Commit db65d34

Browse files
committed
Adding a utility to round TimeSpan instances and using it in tests where rounding was performed
1 parent 413bd55 commit db65d34

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
8+
namespace Microsoft.WebJobs.Script.Tests
9+
{
10+
public static class TimeSpanExtensions
11+
{
12+
public static TimeSpan RoundSeconds(this TimeSpan timeSpan, int digits, MidpointRounding rounding = MidpointRounding.ToEven)
13+
{
14+
return TimeSpan.FromSeconds(Math.Round(timeSpan.TotalSeconds, digits, rounding));
15+
}
16+
}
17+
}

test/WebJobs.Script.Tests.Shared/WebJobs.Script.Tests.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@
2525
<Compile Include="$(MSBuildThisFileDirectory)TestScopedSettings.cs" />
2626
<Compile Include="$(MSBuildThisFileDirectory)TestSecretManager.cs" />
2727
<Compile Include="$(MSBuildThisFileDirectory)TestSecretManagerFactory.cs" />
28+
<Compile Include="$(MSBuildThisFileDirectory)TimeSpanExtensions.cs" />
2829
</ItemGroup>
2930
</Project>

test/WebJobs.Script.Tests/IO/AutoRecoveringFileSystemWatcherTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ await TestHelpers.Await(() =>
127127
long expectedInterval = Convert.ToInt64((Math.Pow(2, i + 1) - 1) / 2);
128128
LogMessage currentEvent = retryEvents[i];
129129

130-
var actualInterval = currentEvent.Timestamp - previoustTimeStamp;
130+
TimeSpan actualInterval = currentEvent.Timestamp - previoustTimeStamp;
131+
TimeSpan roundedInterval = actualInterval.RoundSeconds(digits: 0);
131132
previoustTimeStamp = currentEvent.Timestamp;
132133

133-
int roundedIntervalInSeconds = (int)Math.Round(actualInterval.TotalSeconds, 0, MidpointRounding.ToEven);
134-
Assert.True(expectedInterval == roundedIntervalInSeconds,
135-
$"Recovering interval did not meet the expected interval (expected '{expectedInterval}', rounded '{roundedIntervalInSeconds}', actual '{actualInterval.Seconds}')");
134+
Assert.True(expectedInterval == roundedInterval.TotalSeconds,
135+
$"Recovering interval did not meet the expected interval (expected '{expectedInterval}', rounded '{roundedInterval.TotalSeconds}', actual '{actualInterval.TotalSeconds}')");
136136
}
137137

138138
Assert.True(loggerProvider.GetAllLogMessages().All(t => t.FormattedMessage.EndsWith(fileWatcherLogSuffix)));

test/WebJobs.Script.Tests/UtilityTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Threading;
1212
using System.Threading.Tasks;
1313
using Microsoft.Azure.WebJobs.Script.Config;
14+
using Microsoft.WebJobs.Script.Tests;
1415
using Moq;
1516
using Newtonsoft.Json.Linq;
1617
using Xunit;
@@ -72,7 +73,8 @@ public async Task DelayWithBackoffAsync_DelaysAsExpected()
7273

7374
// Workaround annoying test failures such as "Expected sw.Elapsed >= TimeSpan.FromSeconds(2); Actual: 1999.4092" by waiting slightly less than 2 seconds
7475
// Not sure what causes it, but either Task.Delay sometimes doesn't wait quite long enough or there is some clock skew.
75-
Assert.True(sw.Elapsed >= TimeSpan.FromMilliseconds(1990), $"Expected sw.Elapsed >= TimeSpan.FromMilliseconds(1990); Actual: {sw.Elapsed.TotalMilliseconds}");
76+
TimeSpan roundedElapsedSpan = sw.Elapsed.RoundSeconds(digits: 1);
77+
Assert.True(roundedElapsedSpan >= TimeSpan.FromSeconds(2), $"Expected roundedElapsedSpan >= TimeSpan.FromSeconds(2); Actual: {roundedElapsedSpan.TotalSeconds}");
7678
}
7779

7880
[Theory]

0 commit comments

Comments
 (0)