Lazy-initialize SerialPort in transport classes#145
Merged
Conversation
Add tests asserting that Windows-style descriptors (COM999) also fail on Linux/Mac, and that Unix-style descriptors (/dev/...) succeed at getCommPort on Windows but fail at openPort. Fix test expectations based on CI results: Windows getCommPort does not throw for bogus COM ports, deferring failure to openPort.
There was a problem hiding this comment.
Pull request overview
This PR defers creation of jSerialComm SerialPort instances until connect/bind time to normalize cross-platform behavior (Linux/macOS throwing on invalid descriptors vs Windows deferring failure to openPort()), and adds tests + CI coverage to lock that behavior in.
Changes:
- Lazily initialize
SerialPortin client/server transports (double-checked locking) and wrap failures inModbusException/ModbusConnectException. - Add OS-gated tests documenting
SerialPort.getCommPort()behavior and ensuring bogus ports fail consistently viaModbusConnectException. - Expand GitHub Actions CI to run Maven build/tests on Ubuntu, macOS, and Windows.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
modbus-serial/src/main/java/com/digitalpetri/modbus/serial/client/SerialPortClientTransport.java |
Lazy SerialPort creation + improved exception wrapping and connection checks. |
modbus-serial/src/main/java/com/digitalpetri/modbus/serial/server/SerialPortServerTransport.java |
Lazy SerialPort creation + improved exception wrapping during bind/unbind. |
modbus-serial/src/test/java/com/digitalpetri/modbus/serial/SerialPortGetCommPortBehaviorTest.java |
New cross-platform behavior tests for getCommPort() and transport failure semantics. |
.github/workflows/maven.yml |
CI now runs the Maven build on Linux/macOS/Windows to exercise platform-specific tests. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
SerialPort.getCommPort()from constructor to connect/bind time using double-checked locking. Previously, theSerialPortwas eagerly created in the constructor, which on Linux/macOS throws immediately for non-existent port descriptors (since jSerialComm validates the filesystem path). This made it impossible to catch and handle the error as aModbusConnectException. On Windows,getCommPort()never validates — failure is deferred toopenPort()— so the eager call silently succeeded but the behavior was inconsistent across platforms.getCommPortfailures are now caught and surfaced asModbusException, and connect/bind failures (both fromgetCommPortandopenPort) are wrapped asModbusConnectException. Previously these were genericExceptioninstances.getCommPortbehavior and verifying that both client and server transports handle bogus port names correctly on all OSes.Motivation
SerialPort.getCommPort(String)behaves differently by platform:SerialPortInvalidPortExceptionimmediately if the file does not exist.\\.\COMxformat without validation. TheSerialPortobject is created successfully; failure is deferred toopenPort(), which returnsfalse.By deferring
getCommPortto connect/bind time, the transport classes can catch the exception on Linux/macOS and wrap it as aModbusConnectException, giving callers a consistent error-handling experience across all platforms.Test plan
SerialPortGetCommPortBehaviorTest— OS-gated tests document rawgetCommPortbehavior on each platformClientTransport/ServerTransportnested tests verify constructor never callsgetCommPort, and that connect/bind with bogus ports fails withModbusConnectException