Guillaume Nodet created CAMEL-23431:
---------------------------------------

             Summary: Migrate AS2 and other component tests from 
AvailablePortFinder to port-0 ServerSocket binding
                 Key: CAMEL-23431
                 URL: https://issues.apache.org/jira/browse/CAMEL-23431
             Project: Camel
          Issue Type: Improvement
          Components: tests
            Reporter: Guillaume Nodet


h3. Background

While fixing CAMEL-22525 (flaky AS2 tests with BindException), we identified 
that {{AvailablePortFinder}} has an inherent TOCTOU 
(time-of-check-to-time-of-use) race condition: it probes a port by 
opening/closing a {{ServerSocket}}, then the component creates its own 
{{ServerSocket}} later. Between the probe close and the actual bind, another 
process can steal the port.

The fix for CAMEL-22525 (PR #22981) demonstrated the proper pattern: bind to 
port 0 (OS assigns atomically), then read the actual port via 
{{getLocalPort()}}.

h3. Investigation Results

A survey of all 261 test files using {{AvailablePortFinder}} across the 
codebase found:

||Category||Files||Feasibility||Description||
|A: Port passed to server object|231 (88.5%)|High|Port assigned via 
{{setPort()}}, {{setListenPort()}}, constructor params. Server can support port 
0 + {{getLocalPort()}}|
|B: Port in route URI string|15 (5.7%)|Medium|Port concatenated into 
{{from("jetty:http://localhost:"; + port + "/...")}}. Requires route refactoring 
or property-based port injection|
|D: Other patterns|15 (5.7%)|Varies|Test utilities, property configurations|

h4. Top components by file count (Category A)

* camel-platform-http-vertx (27 files)
* camel-mllp (21 files)
* camel-grpc (14 files)
* camel-jetty (12 files)
* camel-cxf (11 files)
* camel-cometd (10 files)
* camel-syslog (9 files)
* camel-thrift (9 files)
* camel-netty (8 files)

h3. Proposed Approach

For each component with Category A tests:
# Add {{getLocalPort()}} to the server/connection class (if not already present)
# Change tests to bind to port 0 and read the actual port back
# Remove {{AvailablePortFinder}} usage for that port

Category B tests (port in URI strings) require more thought — either 
property-based port injection or restructuring the test lifecycle so the server 
starts before routes are defined.

{{AvailablePortFinder}} remains useful as a fallback when port 0 is not 
feasible, but port 0 should be the default choice whenever possible.

h3. Related

* CAMEL-22525 — Fix flaky AS2 tests (the specific fix that motivated this 
investigation)
* CAMEL-23214 — Migrate AvailablePortFinder in camel-as2 tests (prior migration 
that used AvailablePortFinder)
* CAMEL-19635 — AS2 tests identified as flaky risk



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to