Skip to content

Tomas/connection raii initial#413

Draft
ProfessorTom wants to merge 275 commits into
dannagle:developmentfrom
ProfessorTom:tomas/connection-raii-initial
Draft

Tomas/connection raii initial#413
ProfessorTom wants to merge 275 commits into
dannagle:developmentfrom
ProfessorTom:tomas/connection-raii-initial

Conversation

@ProfessorTom

@ProfessorTom ProfessorTom commented Mar 6, 2026

Copy link
Copy Markdown
Contributor

Before submitting a pull request:

  • Did you fork from the development branch? yes
  • Are you submitting the pull request to the development branch? (not master) yes

This will likely be a Cthulhu PR that will need to be broken up into a few smaller PRs once the work is done. Unfortunately, the way through is a long running branch in the short term.

Some benefits and thoughts as work continues on this branch:

TcpThread class will be replaced with BaseTcpThread, OutgoingTcpThread and IncomingTcpThread.

The idea here is to combine common code in the base class but separate the different directions so that they have room to breath and you can follow the main flows more easily. All three of those classes will automagically clean up both the socket and the threads via RAII and Qt's object lifecycle and parenting system.

There will be a root Connection class, a BaseTcpConnection class, an IncomingTCPConnection and an OutgoingTcpConnection class.

Here's the basic idea:

Connection (abstract)

├── BaseTcpConnection
│ ├── OutgoingTcpConnection
│ └── IncomingTcpConnection

├── BaseDtlsConnection
│ ├── OutgoingDtlsConnection
│ └── IncomingDtlsConnection

├── BaseUdpConnection
│ └── UdpConnection (usually no separate in/out)

└── ... (future protocols: RS-232 [aka Serial] QUIC, WebSocket, SCTP, etc.)

Unit tests have been added for code I have modified or added and will continue to be added for code I modify or add.

qMake is likely going away.

I'm not sure how Dan builds for each platform.

I was under the impression that most if not all platforms were using CMake now. I know snapcraft and macOS does. Regardless, the unit test only work with CMake, in part, because I am developing on a Mac. There may be some way to get the unit tests to build and run with qMake, but as I understand it, Dan wanted to move away from qMake anyway, so now's the chance.

This means I should be able to delete the .pro files in this PR. At the time of this writing I am not doing so because I don't know what the consequences are for taking that action. e.g. there may be one build that is still using qMake. I talked with @dannagle and he said that Windows stills needs to be converted to CMake. I know that the snapcraft build uses CMake. Not sure about the Debian build in the pipeline.

This likely means modifying the build process to build and run the unit tests and perhaps have such failures block PR merges. While the latter can be done once here on GitHub, the former will have to be done on each platform that PacketSender is built for. Given we are dealing with some low-level functionality (e.g. IPv4 vs IPv6, os default network stacks, etc.), it's possible that some unit tests may fail on platforms other than macOS. I haven't tested on any other platform. I fully intend for this to be a problem for Dan to solve, not because I'm being lazy or want to give Dan more work, but because at the time of this writing, this PR is not complete and I don't have access to each build system to test them.

Fixes #151

@ProfessorTom
ProfessorTom force-pushed the tomas/connection-raii-initial branch 2 times, most recently from 81ffa3c to c4d8576 Compare March 7, 2026 01:44
@ProfessorTom
ProfessorTom force-pushed the tomas/connection-raii-initial branch from 1c8340b to 99d2b79 Compare May 25, 2026 22:17
This constructor will be used by Connection Manager for incoming/server connections
- Moves initialization of closeRequest from run() to member declaration
- Eliminates redundant assignment and prevents uninitialized-read warnings
- Improves code clarity and maintainability (C++11+ in-class initializer)
if all the tests pass, tell us that
if there are failures, tell us that

Qtest doesn't do this automatically. It prints successes and failures per file. It needs a test reporter.
- Use state_ instead of thread_ checks in isConnected()
- Update terminateConnection()
- Add proper destructor
- Remove immediate thread_.reset()
- Add hasConnection()
- Add create*Object() factory methods
- Use mutex in close()
- Safer disconnected handler
- Add testDefaultStateIsCreated()
- Add testTerminate_TransitionsStateToClosing_whenThreadIsNullptr()
- Add testTerminate_TransitionsStateToClosed_whenThreadIsNOTNullptr()
- Update ConnectionTests class

These tests verify basic state transitions in the new Connection state machine.
- testHandleOutgoingPlainTCP_success_setsConnectionStateToActive()
- testHandleOutgoingPlainTCP_unsuccessful_setsConnectionStateToInactive()

These tests verify that the Connection state machine correctly updates
on successful and unsuccessful send() operations using the test double.
- Add testSocketSuccessfullySetsSocketDescriptor_TransitionsStateToActive()
- Add testSocketDisconnectedBeforeIncomingDataRead_TransitionsStateToInactive()
- Extend IncomingTcpConnectionTestDouble with socket configuration options
- Add more message mappings in BaseTcpConnection::stateFromMessage()

These changes improve test coverage for the Connection state machine, especially for incoming connections.
…uration

- Added `testIncomingSSL_failure_setsConnectionStateToInactive()`
- Extended `IncomingTcpConnectionTestDouble` with `HANDLE_INCOMING_SSL_FAILURE` case
- Added `createSslFailure()` helper that simulates SSL handshake failure
  (self-signed certificate error)
- Updated state mapping in `BaseTcpConnection` for `SSL_HANDSHAKE_FAILED`
- Replace `createMockSocketForTest()` with `createMockSocketWithSocketDescriptorOtherThan0()` in tests
- This avoids socket descriptor 0 errors in IncomingTcpThread constructor
- Update MockSslSocket and TestUtils with new helper
- Minor test cleanups and debug improvements

This resolves several test failures related to socket descriptor validation.
- testHandleOutgoingSSL_success_setsConnectionStateToActive()
- testHandleOutgoingSSL_failure_setsConnectionStateToInactive()
- Extend OutgoingTcpConnectionTestDouble with SSL success/failure configurations
- Update test runner and supporting test utilities

This improves test coverage for SSL handshake paths in the Connection state machine.
- Add signalSpyCountMessage*() helpers
- Add TestUtils::toString(Connection::State)
…e in the debug output.

(saves 1.5 days trying to figure out where the app is crashing.)
- Add setState() method
- Update stateFromMessage() with more mappings
- Skip redundant "Sending data" state changes
- Better default state on unknown message
- Emit proper ConnectionStatusMessages on SSL success/failure
- Minor debug logging improvements
- Add states vector and printStates() in test doubles
- Override setState() to record state transitions
- Improve SSL configuration options in test doubles
- Replace direct state checks with vector-based assertions
- Use new signalSpyCountMessage helpers where appropriate
- Update test setup for socket descriptor validation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant