Skip to content

Conversation

martin308
Copy link
Contributor

@martin308 martin308 commented Aug 29, 2025

This pull request refactors test dependencies and utilities to simplify the test infrastructure and remove direct dependencies on swift-nio test utilities. The main change is the introduction of a shared HttpTestServer utility, which is now used across multiple test targets. This reduces reliance on low-level NIO test servers and streamlines test setup and teardown.

Test Infrastructure Refactoring:

  • Added a new SharedTestUtils target in Package.swift to provide shared testing utilities for multiple test targets.
  • Updated test targets (OpenTelemetryProtocolExporterTests, URLSessionInstrumentationTests) to depend on SharedTestUtils instead of directly depending on swift-nio test products. [1] [2]
  • Removed the old HttpTestServer implementation from Tests/InstrumentationTests/URLSessionTests/Utils/HttpTestServer.swift, consolidating server utilities into SharedTestUtils.

Test Code Simplification and Modernization:

  • Replaced usage of NIOHTTP1TestServer and related NIO constructs in test files with the new HttpTestServer from SharedTestUtils, simplifying server setup and teardown logic. [1] [2] [3] [4]
  • Updated test assertions to work with the new server utility and modernized body handling (e.g., using String(decoding:body,as:UTF8.self) instead of NIO ByteBuffer). [1] [2] [3] [4]
  • Adjusted HTTP client test expectations to align with the new test server's behavior (e.g., status code and method checks). [1] [2] [3]

These changes collectively improve the maintainability and clarity of the test suite by centralizing test utilities and reducing external dependencies.

…erver

Replaces NIO-based test servers with a new HttpTestServer implemented using Foundation and POSIX sockets, eliminating the need for swift-nio and related packages in test targets. Updates all affected test files to use the new server and refactors test logic for compatibility. This simplifies test dependencies and improves portability.
Consolidates two duplicate HTTP test server implementations into a single
shared utility that can be used by both OTLP exporter tests and URLSession
instrumentation tests.

Changes:
- Create unified HttpTestServer in Tests/Shared/TestUtils that combines
  functionality from both previous implementations
- Uses POSIX sockets directly, maintaining independence from external
  dependencies (no NIO, Swifter, etc.)
- Supports OTLP test requirements: request recording, protobuf binary
  payloads, header verification
- Supports URLSession test requirements: path-based responses, success/error
  callbacks, configuration options
- Add SharedTestUtils target to Package.swift for shared test utilities
- Update both test targets to depend on SharedTestUtils
- Remove duplicate implementations from ExportersTests and
  InstrumentationTests
- Update test files to import SharedTestUtils and use consistent API

Benefits:
- Eliminates code duplication between test suites
- Provides consistent HTTP mocking functionality
- Makes it easier to maintain and extend test infrastructure
- All OTLP HTTP exporter tests (16) passing successfully

Note: URLSession tests have 4 pre-existing failures in error handling
that are unrelated to this refactoring.
Added logic to HttpTestServer to properly handle requests to paths starting with /error when no config is provided, simulating a network error by closing the connection without a response.
The Darwin import was not used in HttpTestServer.swift
- Add conditional imports for Glibc/Musl to support Linux builds
- Replace macOS-specific CFSwapInt16BigToHost with portable ntohs for byte order conversion
- Fix shutdown() calls to use platform-specific namespaces (Darwin/Glibc/Musl)
- Fix minor code warnings (unused variable, boolean test)

The HTTP test server now compiles and runs correctly on both macOS and Linux platforms,
maintaining full backward compatibility while enabling cross-platform support.

This change is required for the test suite to pass in Linux CI environments.
@martin308 martin308 force-pushed the martin308/remove-nio-from-tests branch from 2d8e446 to 114edc2 Compare September 9, 2025 20:57
Copy link

codecov bot commented Sep 9, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 61.67%. Comparing base (7bad8ae) to head (114edc2).
⚠️ Report is 184 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #903      +/-   ##
==========================================
- Coverage   67.89%   61.67%   -6.23%     
==========================================
  Files         344       82     -262     
  Lines       15169     5724    -9445     
==========================================
- Hits        10299     3530    -6769     
+ Misses       4870     2194    -2676     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

- Fix SOCK_STREAM type conversion on Linux (cast __socket_type to Int32)
- Fix SHUT_RDWR type conversion on Linux (cast Int to Int32)

These changes ensure proper type compatibility between macOS and Linux socket APIs.
- Add conditional compilation for Darwin vs Linux socket() call
- macOS uses SOCK_STREAM directly, Linux needs Int32(SOCK_STREAM.rawValue)
- Silence unused result warnings for fcntl and withCString calls
- Maintain full compatibility with both OTLP and URLSession tests
@martin308 martin308 marked this pull request as ready for review September 9, 2025 22:17
Copy link
Member

@ArielDemarco ArielDemarco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! LGTM!
Added a simple suggestion :)

Copy link
Member

@nachoBonafonte nachoBonafonte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really great work, thanks a lot for the effort.

- Ensures clean state when restarting server
- Prevents potential memory leaks from accumulated requests
- Maintains consistency with initialization state
@martin308
Copy link
Contributor Author

@ArielDemarco @nachoBonafonte I still see some NIO looking bits (imports and a usage of NIOCore.EventLoopFuture) in these two files

  • Tests/ExportersTests/OpenTelemetryProtocol/OtlpLogRecordExporterTests.swift
  • Tests/ExportersTests/OpenTelemetryProtocol/OtlpTraceExporterTests.swift

Shouldn't they fail to build now the dependencies have been removed from the Package.swift file?

Copy link
Contributor

@williazz williazz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't they fail to build now the dependencies have been removed from the Package.swift file?

I think transitive dependencies are included in test suites as well.

@bryce-b bryce-b enabled auto-merge (squash) October 2, 2025 16:10
@bryce-b bryce-b merged commit 591ed5f into open-telemetry:main Oct 2, 2025
10 checks passed
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.

5 participants