-
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
Open
cptkoolbeenz
wants to merge
5
commits into
main
Choose a base branch
from
fix/execute-text-command-lock
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+284
−14
Open
Changes from 4 commits
Commits
Show all changes
5 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 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
139 changes: 139 additions & 0 deletions
139
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,139 @@ | ||
| using System; | ||
| using System.Net; | ||
| using System.Reflection; | ||
| using System.Threading; | ||
| 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 re-entrant calls from the same | ||
| /// async flow (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. The disposed/disconnecting guards 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. Re-entrancy is tested by | ||
| /// flipping the AsyncLocal flag from inside the same logical flow. | ||
| /// </summary> | ||
| public class DaqifiDeviceTextCommandLockTests | ||
| { | ||
| [Fact] | ||
| public async Task ExecuteTextCommandAsync_WhenAlreadyInsideAsyncFlow_ThrowsInvalidOperation() | ||
| { | ||
| var device = new TextCommandTestableDevice("TestDevice"); | ||
|
|
||
| // Simulate "we're already inside ExecuteTextCommandAsync on this | ||
| // async flow" by setting the AsyncLocal flag. The re-entrancy | ||
| // guard runs before WaitAsync(), so this check fires immediately | ||
| // without touching any transport state. | ||
| GetIsInsideTextExchange(device).Value = true; | ||
|
|
||
| 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 xunit's per-test | ||
| // budget would time it out instead. | ||
| await Assert.ThrowsAsync<InvalidOperationException>( | ||
| () => device.CallExecuteTextCommandAsync(() => { })); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ExecuteTextCommandAsync_AsyncLocalClearedAfterReturn() | ||
| { | ||
| // Even when the call throws, the AsyncLocal re-entrancy flag | ||
| // is cleared in the finally block so a subsequent call from | ||
| // the same flow doesn't false-positive the re-entrancy check. | ||
| var device = new TextCommandTestableDevice("TestDevice"); | ||
|
|
||
| await Assert.ThrowsAsync<InvalidOperationException>( | ||
| () => device.CallExecuteTextCommandAsync(() => { })); | ||
|
|
||
| Assert.False(GetIsInsideTextExchange(device).Value); | ||
| } | ||
|
|
||
| // ── Reflection helpers — kept private to this test class so the | ||
| // production DaqifiDevice doesn't have to expose internals. ───── | ||
|
|
||
| private static AsyncLocal<bool> GetIsInsideTextExchange(DaqifiDevice device) | ||
| { | ||
| return (AsyncLocal<bool>)typeof(DaqifiDevice) | ||
| .GetField("_isInsideTextExchange", 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.