Skip to content

Conversation

@c3rebro
Copy link
Owner

@c3rebro c3rebro commented Dec 28, 2025

Motivation

  • Make serial transport testable by removing direct dependency on System.IO.Ports.SerialPort and enable unit testing of open/close behavior.
  • Harden serial port handling to avoid races and improper use after disposal when opening/closing concurrently.
  • Ensure buffers are discarded and event handlers are cleaned up on disconnect/dispose to avoid resource leaks.

Description

  • Introduce ISerialPortAdapter and SerialPortAdapter in src/Drivers/Transport/SerialPortAdapter.cs to wrap SerialPort for testability.
  • Refactor SerialPortTransport to depend on ISerialPortAdapter, add a SemaphoreSlim _connectionLock, _disposed checks, ThrowIfDisposed() and CreateConfiguredPort() factory method, and ensure event unsubscription and buffer discard on dispose.
  • Replace ad-hoc Task.Run wrappers around open/close with proper async locking and disposal guards in ConnectAsync/DisconnectAsync and add safe Dispose logic.
  • Add unit tests tests/Elatec.NET.Tests/SerialPortTransportTests.cs with a FakeSerialPortAdapter to validate open-once behavior, disconnect buffer discard, dispose closing, and thrown exceptions when using after dispose.

Testing

  • Ran dotnet test tests/Elatec.NET.Tests/Elatec.NET.Tests.csproj to execute the test suite.
  • All tests passed: Passed! - Failed: 0, Passed: 21, Skipped: 0, Total: 21.
  • Test run produced some non-fatal compiler warnings about unused events, but no test failures occurred.
  • Unit tests cover ConnectAsync, DisconnectAsync, Dispose, and behavior when calling ConnectAsync after disposal.

Codex Task

@c3rebro c3rebro merged commit 1eee8b6 into master Dec 28, 2025
1 of 2 checks passed
@c3rebro c3rebro deleted the investigate-serial-port-handling-issues branch December 28, 2025 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants