|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Microsoft.Identity.Client.Utils; |
| 6 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 7 | + |
| 8 | +namespace Microsoft.Identity.Test.Unit.UtilTests |
| 9 | +{ |
| 10 | + [TestClass] |
| 11 | + public class StopWatchServiceTests |
| 12 | + { |
| 13 | + [TestMethod] |
| 14 | + public void MeasureCodeBlock() |
| 15 | + { |
| 16 | + MeasureDurationResult result = StopwatchService.MeasureCodeBlock(() => Thread.Sleep(50)); |
| 17 | + |
| 18 | + Assert.IsTrue(result.Milliseconds >= 50, "Measured time is less than expected."); |
| 19 | + Assert.IsTrue(result.Milliseconds < 100, "Measured time is too high."); |
| 20 | + |
| 21 | + long diff = result.Microseconds - (result.Milliseconds * 1000); |
| 22 | + Assert.IsTrue(diff >= -1000, "Microseconds is less than expected."); |
| 23 | + Assert.IsTrue(diff < 1000, "Microseconds is too high."); |
| 24 | + } |
| 25 | + |
| 26 | + [TestMethod] |
| 27 | + public async Task MeasureCodeBlockAsync() |
| 28 | + { |
| 29 | + MeasureDurationResult result = await StopwatchService.MeasureCodeBlockAsync(async () => |
| 30 | + { |
| 31 | + await Task.Delay(50).ConfigureAwait(true); |
| 32 | + }).ConfigureAwait(true); |
| 33 | + |
| 34 | + Assert.IsTrue(result.Milliseconds >= 50, "Measured time is less than expected."); |
| 35 | + Assert.IsTrue(result.Milliseconds < 100, "Measured time is too high."); |
| 36 | + |
| 37 | + long diff = result.Microseconds - (result.Milliseconds * 1000); |
| 38 | + Assert.IsTrue(diff >= -1000, "Microseconds is less than expected."); |
| 39 | + Assert.IsTrue(diff < 1000, "Microseconds is too high."); |
| 40 | + } |
| 41 | + |
| 42 | + [TestMethod] |
| 43 | + public async Task MeasureCodeBlockAsyncWithResult() |
| 44 | + { |
| 45 | + MeasureDurationResult<int> result = await StopwatchService.MeasureCodeBlockAsync(async () => |
| 46 | + { |
| 47 | + await Task.Delay(50).ConfigureAwait(true); |
| 48 | + return 42; |
| 49 | + }).ConfigureAwait(true); |
| 50 | + |
| 51 | + Assert.AreEqual(42, result.Result, "Result is not as expected."); |
| 52 | + Assert.IsTrue(result.Milliseconds >= 50, "Measured time is less than expected."); |
| 53 | + Assert.IsTrue(result.Milliseconds < 100, "Measured time is too high."); |
| 54 | + |
| 55 | + long diff = result.Microseconds - (result.Milliseconds * 1000); |
| 56 | + Assert.IsTrue(diff >= -1000, "Microseconds is less than expected."); |
| 57 | + Assert.IsTrue(diff < 1000, "Microseconds is too high."); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments