-
Notifications
You must be signed in to change notification settings - Fork 630
Expose DelayHelper as a public timing API in the Iot.Device namespace #2585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,15 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Diagnostics; | ||
| using System.Threading; | ||
|
|
||
| namespace System.Device | ||
| namespace Iot.Device | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot changing the namespace without moving the helper in the other assembly raise a problem in terms of binary compatibility for the future.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed — once The clean fix is to give it its permanent home now, before it's public: move Which assembly should be the final home —
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @raffaeler As we don't have any plans in moving stuff from Iot.Device.Bindings to System.Device in the near future, I guess the Iot.Device namespace is the better one for this. |
||
| { | ||
| /// <summary> | ||
| /// Helpers for short waits. | ||
| /// Helpers for short, high precision waits. Prefer these over <see cref="Thread.Sleep(int)"/> when a device | ||
| /// binding needs to honor sub-millisecond timing requirements from a datasheet. | ||
| /// </summary> | ||
| static partial class DelayHelper | ||
| { | ||
|
|
@@ -79,7 +81,15 @@ public static void DelayMicroseconds(int microseconds, bool allowThreadYield) | |
| /// True to allow yielding the thread. If this is set to false, on single-proc systems | ||
| /// this will prevent all other code from running. | ||
| /// </param> | ||
| /// <remarks> | ||
| /// This overload is intended for internal use only. For millisecond scale waits prefer | ||
| /// <see cref="Thread.Sleep(int)"/>, which lets the operating system schedule other work. | ||
| /// </remarks> | ||
| #if BUILDING_IOT_DEVICE_BINDINGS | ||
| internal static void DelayMilliseconds(int milliseconds, bool allowThreadYield) | ||
| #else | ||
| public static void DelayMilliseconds(int milliseconds, bool allowThreadYield) | ||
| #endif | ||
| { | ||
| /* We have this as a separate method for now to make calling code clearer | ||
| * and to allow us to add additional logic to the millisecond wait in the | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Diagnostics; | ||
| using Xunit; | ||
|
|
||
| namespace Iot.Device.Common.Tests | ||
| { | ||
| public class DelayHelperTest | ||
| { | ||
| // Timing tests only verify the guaranteed lower bound (the helper waits for *at least* the | ||
| // requested time). A small, consistent tolerance absorbs the resolution of the underlying | ||
| // timer and the conversion between Stopwatch ticks and TimeSpan ticks. | ||
| private static readonly TimeSpan Tolerance = TimeSpan.FromMilliseconds(1); | ||
|
|
||
| [Theory] | ||
| [InlineData(true)] | ||
| [InlineData(false)] | ||
| public void DelayWaitsAtLeastTheRequestedTime(bool allowThreadYield) | ||
| { | ||
| TimeSpan requested = TimeSpan.FromMilliseconds(20); | ||
|
|
||
| Stopwatch stopwatch = Stopwatch.StartNew(); | ||
| DelayHelper.Delay(requested, allowThreadYield); | ||
| stopwatch.Stop(); | ||
|
|
||
| Assert.True( | ||
| stopwatch.Elapsed >= requested - Tolerance, | ||
| $"Expected to wait at least {requested.TotalMilliseconds}ms but only waited {stopwatch.Elapsed.TotalMilliseconds}ms."); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DelayMicrosecondsWaitsAtLeastTheRequestedTime() | ||
| { | ||
| const int microseconds = 5000; // 5ms | ||
| TimeSpan requested = TimeSpan.FromMilliseconds(5); | ||
|
|
||
| Stopwatch stopwatch = Stopwatch.StartNew(); | ||
| DelayHelper.DelayMicroseconds(microseconds, allowThreadYield: true); | ||
| stopwatch.Stop(); | ||
|
|
||
| Assert.True( | ||
| stopwatch.Elapsed >= requested - Tolerance, | ||
| $"Expected to wait at least {requested.TotalMilliseconds}ms but only waited {stopwatch.Elapsed.TotalMilliseconds}ms."); | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.