-
Notifications
You must be signed in to change notification settings - Fork 0
fix(device): serialize ExecuteTextCommandAsync (closes #186) #196
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
Merged
+360
−95
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f373f1a
fix(device): serialize ExecuteTextCommandAsync (closes #186)
cptkoolbeenz 328e212
fix: Apply Qodo /agentic_review pass 1: harden ExecuteTextCommandAsyn…
cptkoolbeenz f7efc6c
fix: Apply Qodo /improve pass 2: handle WaitAsync ObjectDisposedExcep…
cptkoolbeenz 7dc53d3
fix: Apply Qodo /agentic_review pass 3 on PR #196: shorter Disconnect…
cptkoolbeenz 2f23625
Apply Qodo /agentic_review pass 2: bump Disconnect lock wait to 10s
cptkoolbeenz cee5f17
style: fix indentation of inner try/finally in ExecuteTextCommandAsync
tylerkron bc43f96
Merge branch 'main' into fix/execute-text-command-lock
tylerkron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
145 changes: 145 additions & 0 deletions
145
src/Daqifi.Core.Tests/Device/DaqifiDeviceTextCommandLockTests.cs
This file contains hidden or 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,145 @@ | ||
| using System; | ||
| using System.Net; | ||
| using System.Reflection; | ||
| using System.Threading.Tasks; | ||
| using Daqifi.Core.Device; | ||
| using Xunit; | ||
|
|
||
| namespace Daqifi.Core.Tests.Device | ||
| { | ||
| /// <summary> | ||
| /// Tests for #186 — ExecuteTextCommandAsync must serialize concurrent | ||
| /// callers (SemaphoreSlim), reject same-thread re-entrant calls | ||
| /// (InvalidOperationException, not deadlock), and reject calls when | ||
| /// the device is disposed or disconnecting. | ||
| /// | ||
| /// The protected method is exercised via a thin subclass that exposes | ||
| /// it. Because spinning up a real transport here would be fragile, the | ||
| /// pre-lock re-entrancy guard and the disposed/disconnecting guard are | ||
| /// tested by setting the relevant private fields via reflection — those | ||
| /// guards run before any transport / consumer interaction, so this gives | ||
| /// faithful coverage without a transport stack. | ||
| /// </summary> | ||
| public class DaqifiDeviceTextCommandLockTests | ||
| { | ||
| [Fact] | ||
| public async Task ExecuteTextCommandAsync_WhenSameThreadAlreadyOwnsLock_ThrowsInvalidOperation() | ||
| { | ||
| var device = new TextCommandTestableDevice("TestDevice"); | ||
|
|
||
| // Simulate "we're already inside ExecuteTextCommandAsync on this | ||
| // thread" by directly setting the owner-thread tracker. The | ||
| // re-entrancy guard runs before WaitAsync(), so this check | ||
| // fires immediately without touching any transport state. | ||
| SetOwnerThreadId(device, Environment.CurrentManagedThreadId); | ||
|
|
||
| var ex = await Assert.ThrowsAsync<InvalidOperationException>( | ||
| () => device.CallExecuteTextCommandAsync(() => { })); | ||
| Assert.Contains("not re-entrant", ex.Message); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ExecuteTextCommandAsync_WhenDisposing_ThrowsInvalidOperation() | ||
| { | ||
| var device = new TextCommandTestableDevice("TestDevice"); | ||
| SetIsDisconnecting(device, true); | ||
|
|
||
| var ex = await Assert.ThrowsAsync<InvalidOperationException>( | ||
| () => device.CallExecuteTextCommandAsync(() => { })); | ||
| Assert.Contains("disposing or disconnecting", ex.Message); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ExecuteTextCommandAsync_WhenDisposed_ThrowsInvalidOperation() | ||
| { | ||
| var device = new TextCommandTestableDevice("TestDevice"); | ||
| SetDisposed(device, true); | ||
|
|
||
| var ex = await Assert.ThrowsAsync<InvalidOperationException>( | ||
| () => device.CallExecuteTextCommandAsync(() => { })); | ||
| Assert.Contains("disposing or disconnecting", ex.Message); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ExecuteTextCommandAsync_ReleasesLockAfterValidationFailure() | ||
| { | ||
| // After a validation failure (e.g. not connected), the lock | ||
| // must be released so subsequent calls don't hang. Verified | ||
| // by calling twice — second call must reach validation too, | ||
| // not block on WaitAsync. | ||
| var device = new TextCommandTestableDevice("TestDevice"); | ||
|
|
||
| await Assert.ThrowsAsync<InvalidOperationException>( | ||
| () => device.CallExecuteTextCommandAsync(() => { })); | ||
| // Second call: also throws, but ONLY if the lock was released. | ||
| // If the lock leaked, this would deadlock and pytest's per-test | ||
| // budget would time it out instead. | ||
| await Assert.ThrowsAsync<InvalidOperationException>( | ||
| () => device.CallExecuteTextCommandAsync(() => { })); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ExecuteTextCommandAsync_OwnerThreadIdClearedAfterReturn() | ||
| { | ||
| // Even when the call throws, the owner-thread tracker is | ||
| // cleared in the finally block so a subsequent call from the | ||
| // same thread doesn't false-positive the re-entrancy check. | ||
| var device = new TextCommandTestableDevice("TestDevice"); | ||
|
|
||
| await Assert.ThrowsAsync<InvalidOperationException>( | ||
| () => device.CallExecuteTextCommandAsync(() => { })); | ||
|
|
||
| Assert.Null(GetOwnerThreadId(device)); | ||
| } | ||
|
|
||
| // ── Reflection helpers — kept private to this test class so the | ||
| // production DaqifiDevice doesn't have to expose internals. ───── | ||
|
|
||
| private static void SetOwnerThreadId(DaqifiDevice device, int? value) | ||
| { | ||
| typeof(DaqifiDevice) | ||
| .GetField("_textExchangeOwnerThreadId", BindingFlags.Instance | BindingFlags.NonPublic)! | ||
| .SetValue(device, value); | ||
| } | ||
|
|
||
| private static int? GetOwnerThreadId(DaqifiDevice device) | ||
| { | ||
| return (int?)typeof(DaqifiDevice) | ||
| .GetField("_textExchangeOwnerThreadId", BindingFlags.Instance | BindingFlags.NonPublic)! | ||
| .GetValue(device); | ||
| } | ||
|
|
||
| private static void SetIsDisconnecting(DaqifiDevice device, bool value) | ||
| { | ||
| typeof(DaqifiDevice) | ||
| .GetField("_isDisconnecting", BindingFlags.Instance | BindingFlags.NonPublic)! | ||
| .SetValue(device, value); | ||
| } | ||
|
|
||
| private static void SetDisposed(DaqifiDevice device, bool value) | ||
| { | ||
| typeof(DaqifiDevice) | ||
| .GetField("_disposed", BindingFlags.Instance | BindingFlags.NonPublic)! | ||
| .SetValue(device, value); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Subclass that exposes the protected ExecuteTextCommandAsync via | ||
| /// a public wrapper so tests can call it directly. Does NOT override | ||
| /// it — the real method runs, including the lock + guards. | ||
| /// </summary> | ||
| private class TextCommandTestableDevice : DaqifiDevice | ||
| { | ||
| public TextCommandTestableDevice(string name, IPAddress? ipAddress = null) | ||
| : base(name, ipAddress) | ||
| { | ||
| } | ||
|
|
||
| public Task<System.Collections.Generic.IReadOnlyList<string>> CallExecuteTextCommandAsync( | ||
| Action setupAction) | ||
| { | ||
| return ExecuteTextCommandAsync(setupAction, responseTimeoutMs: 100, completionTimeoutMs: 50); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.