-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bug in MeasureDurationResult (#5035)
* Fix millisecond measurement * Add some unit tests --------- Co-authored-by: Bogdan Gavril <[email protected]>
- Loading branch information
Showing
3 changed files
with
64 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
tests/Microsoft.Identity.Test.Unit/UtilTests/StopWatchServiceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Identity.Client.Utils; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace Microsoft.Identity.Test.Unit.UtilTests | ||
{ | ||
[TestClass] | ||
public class StopWatchServiceTests | ||
{ | ||
[TestMethod] | ||
public void MeasureCodeBlock() | ||
{ | ||
MeasureDurationResult result = StopwatchService.MeasureCodeBlock(() => Thread.Sleep(50)); | ||
|
||
Assert.IsTrue(result.Milliseconds >= 50, "Measured time is less than expected."); | ||
Assert.IsTrue(result.Milliseconds < 100, "Measured time is too high."); | ||
|
||
long diff = result.Microseconds - (result.Milliseconds * 1000); | ||
Assert.IsTrue(diff >= -1000, "Microseconds is less than expected."); | ||
Assert.IsTrue(diff < 1000, "Microseconds is too high."); | ||
} | ||
|
||
[TestMethod] | ||
public async Task MeasureCodeBlockAsync() | ||
{ | ||
MeasureDurationResult result = await StopwatchService.MeasureCodeBlockAsync(async () => | ||
{ | ||
await Task.Delay(50).ConfigureAwait(true); | ||
}).ConfigureAwait(true); | ||
|
||
Assert.IsTrue(result.Milliseconds >= 50, "Measured time is less than expected."); | ||
Assert.IsTrue(result.Milliseconds < 100, "Measured time is too high."); | ||
|
||
long diff = result.Microseconds - (result.Milliseconds * 1000); | ||
Assert.IsTrue(diff >= -1000, "Microseconds is less than expected."); | ||
Assert.IsTrue(diff < 1000, "Microseconds is too high."); | ||
} | ||
|
||
[TestMethod] | ||
public async Task MeasureCodeBlockAsyncWithResult() | ||
{ | ||
MeasureDurationResult<int> result = await StopwatchService.MeasureCodeBlockAsync(async () => | ||
{ | ||
await Task.Delay(50).ConfigureAwait(true); | ||
return 42; | ||
}).ConfigureAwait(true); | ||
|
||
Assert.AreEqual(42, result.Result, "Result is not as expected."); | ||
Assert.IsTrue(result.Milliseconds >= 50, "Measured time is less than expected."); | ||
Assert.IsTrue(result.Milliseconds < 100, "Measured time is too high."); | ||
|
||
long diff = result.Microseconds - (result.Milliseconds * 1000); | ||
Assert.IsTrue(diff >= -1000, "Microseconds is less than expected."); | ||
Assert.IsTrue(diff < 1000, "Microseconds is too high."); | ||
} | ||
} | ||
} |