Skip to content

[Windows] Compile the remaining test targets (NIOPosixTests, NIOFoundationCompatTests) - #3665

Merged
glbrntt merged 7 commits into
apple:mainfrom
jakepetroules:windows-final-tests-compile
Jul 24, 2026
Merged

glbrntt merged 7 commits into
apple:mainfrom
jakepetroules:windows-final-tests-compile

Conversation

@jakepetroules

Copy link
Copy Markdown
Member

Motivation

Follow-up to #3663 (which got the "low-hanging" test targets compiling on Windows). This finishes the job by getting the last two test targets — NIOPosixTests and NIOFoundationCompatTests — compiling on Windows, so all 16 test targets now build, and switches Windows CI to swift build --build-tests.

These are compile-only changes: the goal is compilation, not runtime pass. Verified by building on a Windows ARM64 VM with swift build --build-tests.

What's here

  1. Compile NIOFoundationCompatTests — the target imports the NIO umbrella (in WaitSpinningRunLoopTests, for MultiThreadedEventLoopGroup) but only declared NIOCore/NIOFoundationCompat as dependencies. NIO re-exports NIOPosix, which imports the CNIOPosix C module; Linux/macOS resolve that transitively, but on Windows the C-module map isn't in the target's search path unless declared, giving the opaque <unknown>:0: error: missing required module 'CNIOPosix'. Fix: declare the NIO dependency it actually uses.

  2. Compile NIOPosixTests — the deeply-POSIX target. Gate-out-plus-minimal-shim:

    • import WinSDK where only sockaddr/timeval/in_addr field or type access was missing (MemberImportVisibility) — this alone cleared the two largest files (SocketAddressTest, HappyEyeballsTest).
    • Real fixes where cheap: pipe → _pipe (via ucrt), an STDOUT_FILENO shim, Thread.sleep instead of sleep, and letting IOResult type inference pick ssize_t.
    • #if !os(Windows) gating (per-method where possible, to preserve Windows-compilable coverage) for genuinely-unavailable APIs: fcntl/O_NONBLOCK/O_ASYNC/F_GETFL/F_SETFL, Posix.fcntl, Posix.socketpair, the integer socket-option API (ChannelOptions.socket, SOL_SOCKET, SO_REUSEADDR, so_timestamp), poll/pollfd, cmsg/msghdr construction, NonBlockingFileIO.lstat/symlink/listDirectory/createDirectory, vsock CID/connect tests, /dev/null syscall-wrapper perf tests, and socket-handle (NIOBSDSocket.Handle UInt64 vs CInt) mismatches.
    • Whole-file gating only where a file is pervasively unsupported: the SAL mock harness and its dependents (SyscallAbstractionLayer[Context], SALChannelTests, SALEventLoopTests) — it's built on CInt fds and can't compile against Windows's UInt64 socket handles without a larger rework; ControlMessageTests (POSIX msghdr/control-message throughout); and MulticastTest (deprecated enumerateInterfaces/NIONetworkInterface).
  3. Build the test targets in CI — change the Windows matrix command from swift build to swift build --build-tests in main.yml and pull_request.yml.

Notes for reviewers

  • No behavior change off Windows: every edit is behind #if os(Windows) / #if !os(Windows); the few shared-line changes (Thread.sleep, IOResult inference, an IUO force-unwrap of msg_name) are identical on POSIX.
  • The tests are compiled, not run on Windows yet. Running them needs real Winsock work (socket handle types, etc.) and is the natural next step.

The target imports the `NIO` umbrella (in WaitSpinningRunLoopTests, for
`MultiThreadedEventLoopGroup`) but only declared `NIOCore` and
`NIOFoundationCompat` as dependencies. `NIO` re-exports `NIOPosix`, which imports
the `CNIOPosix` C module; on Linux/macOS SwiftPM resolves that transitively, but
on Windows the C module map isn't in the target's search path unless declared,
giving `<unknown>:0: error: missing required module 'CNIOPosix'`. Declare the
`NIO` dependency the target actually uses.
Gets the last test target compiling on Windows, so all test targets now build.
Compile-only: the goal is compilation, not runtime pass. Non-Windows behavior is
unchanged (every edit is behind `#if os(Windows)` / `#if !os(Windows)`, and the
handful of shared-line changes are identical on POSIX).

Following the established gate-out-plus-minimal-shim approach:

- Add `import WinSDK` where only `sockaddr`/`timeval`/`in_addr`/etc. field or
  type access is missing (MemberImportVisibility): SocketAddressTest,
  HappyEyeballsTest, SocketOptionProviderTest, ChannelPipelineTest,
  PendingDatagramWritesManagerTests, NIOTransportAccessibleChannelCoreTests.
- Implement `pipe` via `_pipe` (import ucrt) in NIOFileHandleTest and
  AsyncChannelBootstrapTests; shim `STDOUT_FILENO = 1`; use `Thread.sleep`
  instead of `sleep`; let `IOResult` type inference pick `ssize_t`.
- Gate genuinely-unavailable test logic behind `#if !os(Windows)`: fcntl
  (`O_NONBLOCK`/`O_ASYNC`/`F_GETFL`/`F_SETFL`/`Posix.fcntl`), `Posix.socketpair`,
  the integer socket-option API (`ChannelOptions.socket`, `SOL_SOCKET`,
  `SO_REUSEADDR`, `so_timestamp`), `poll`/`pollfd`, cmsg / `msghdr`
  construction, `NonBlockingFileIO.lstat`/`symlink`/`listDirectory`/
  `createDirectory`, vsock CID / connect tests, `/dev/null` syscall-wrapper perf
  tests, and socket-handle (`NIOBSDSocket.Handle` vs `CInt`) mismatches. Gating
  is per-method where possible to preserve Windows-compilable coverage.
- Whole-file gate the SAL mock harness and its dependents
  (SyscallAbstractionLayer[Context], SALChannelTests, SALEventLoopTests): it is
  built on `CInt` file descriptors and can't compile against Windows's `UInt64`
  socket handles without a larger rework. ControlMessageTests (POSIX `msghdr` /
  control-message throughout) and MulticastTest (deprecated
  `enumerateInterfaces`/`NIONetworkInterface`) are also whole-file gated.
Now that all test targets compile on Windows, build them in CI: change the
Windows matrix command from `swift build` to `swift build --build-tests`. The
tests are not yet run on Windows (many are gated out, or unverified at runtime);
this only guards their continued compilation.
@jakepetroules
jakepetroules force-pushed the windows-final-tests-compile branch from 20be3c6 to 591c307 Compare July 20, 2026 08:21
}

func testServerClientBootstrap_withAsyncChannel_clientConnectedSocket() async throws {
#if !os(Windows)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Rather than compiling out the bodies of tests on Windows, can we XCTSkip them instead and include the reason for skipping them?

For whole test suites, I think compiling out the whole suite is fine so long as it includes a comment.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

These can't be XCTSkiped — the bodies use APIs that don't exist on Windows, so they wouldn't compile (this one uses the non-owning fd→handle conversion for a pre-connected socket; others use socketpair, fcntl/O_NONBLOCK, SO_TIMESTAMP, in_addr.s_addr, or vsock). I've kept them compiled out and added a comment on each #if !os(Windows) naming the specific reason. The one exception was the single-threaded-networking test, which was gated only for the integer socket-option form — switched it to the typed .socketOption(.so_reuseaddr) so it compiles, and .disabled it on Windows.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sure they can:

func testSomething() {
    #if os(Windows)
    throw XCTSkip("Skipped because ...")
    #else
    // ...
    #endif
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ah, I see what you mean. I've updated as you requested.

Comment thread Tests/NIOPosixTests/EventLoopTest.swift Outdated
Comment on lines +1618 to +1619
// Uses `ChannelOptions.socket(SOL_SOCKET, SO_REUSEADDR)`, whose integer
// form is unavailable on Windows.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there a form on Windows it could use?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes. The integer ChannelOptions.socket(_:_:) form is unavailable on Windows because WinSDK imports the level/name constants as enum cases rather than plain ints, but the typed .socketOption(.so_reuseaddr) works everywhere. Switched to that so the test compiles on Windows, and .disabled it there since it relies on withCurrentThreadAsEventLoop (thread takeover), which isn't supported on Windows.

Comment thread Package.swift Outdated
.testTarget(
name: "NIOFoundationCompatTests",
dependencies: [
"NIO",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What do you actually need here? We've been careful to not add blanket NIO dependencies.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

WaitSpinningRunLoopTests only uses EventLoopFuture (NIOCore) and MultiThreadedEventLoopGroup (NIOPosix), so the umbrella NIO isn't needed. Switched the target to depend on NIOCore/NIOFoundationCompat/NIOPosix directly, and the test imports those. The NIO dep was only masking a missing NIOPosix dependency — that's what surfaced as the CNIOPosix error on Windows.

- NIOFoundationCompatTests: depend on NIOCore/NIOFoundationCompat/NIOPosix
  directly instead of the umbrella `NIO`; `WaitSpinningRunLoopTests` imports the
  specific modules it uses. The umbrella dep was only masking a missing NIOPosix
  dependency (the source of the `CNIOPosix` error on Windows).
- EventLoopTest.testWeCanDoTrulySingleThreadedNetworking: use the typed
  `.socketOption(.so_reuseaddr)` (available on Windows) instead of the integer
  `ChannelOptions.socket(SOL_SOCKET, SO_REUSEADDR)` form, and `.disabled` it on
  Windows (it relies on thread takeover) rather than compiling out the body.
- Add a comment to each remaining `#if !os(Windows)` test gate naming the
  Windows-unavailable API (socketpair, pre-connected socket fd conversion,
  fcntl/O_NONBLOCK, SO_TIMESTAMP, in_addr.s_addr, vsock). These bodies can't be
  `XCTSkip`ed because they don't compile on Windows.
Following review feedback: instead of compiling the bodies out (which makes the
tests silently pass as empty no-ops on Windows), keep the body in the `#else`
branch and `throw XCTSkip(reason)` on Windows so they report as skipped. The
body is still only compiled on non-Windows, so tests that use Windows-unavailable
APIs (socketpair, pre-connected socket fd conversion, fcntl/O_NONBLOCK,
SO_TIMESTAMP, in_addr.s_addr, vsock) still don't need to compile there.
@glbrntt
glbrntt enabled auto-merge (squash) July 24, 2026 12:35
@glbrntt glbrntt added the semver/none No version bump required. label Jul 24, 2026
@glbrntt
glbrntt merged commit 15b77e6 into apple:main Jul 24, 2026
53 of 58 checks passed
@jakepetroules
jakepetroules deleted the windows-final-tests-compile branch July 24, 2026 16:22
glbrntt added a commit that referenced this pull request Jul 29, 2026
… in CI (#3680)

### Motivation:

All of the test targets now compile on Windows (#3663, #3665), but
`swift test` doesn't yet pass: a number of tests exercise functionality
that isn't supported or isn't yet functional on Windows, some crash or
hang, and a few are flaky on the WSAPoll-based selector. Before the
Windows unit tests can run in CI, those tests need to be skipped (with
reasons) so a full `swift test` exits cleanly.

### Modifications:

Skip the tests that don't pass on Windows, using `XCTSkip` / Swift
Testing's `.disabled` / a gated `setUp` where a fixture itself traps,
each with a reason. Broadly:

- **Unsupported transports/APIs:** Unix domain sockets, datagram/UDP
channels, pipe channels, raw sockets, vsock, pre-connected sockets, and
`FileRegion` sends.
- **Whole suites** for subsystems that aren't functional on Windows yet,
including the **NIOFileSystem family** (`NIOFSTests`,
`NIOFSIntegrationTests`, `NIOFSFoundationCompatTests`) whose Windows
syscall shims are still stubbed, plus `EchoServerClientTest`,
`ChannelTests`, `DatagramChannelTests`, `StreamChannelTest`,
`FileRegionTest`, `PipeChannelTest`, `RawSocketBootstrapTests`,
`ThreadTest`, and `SocketOptionProviderTest`.
- **Real crashes surfaced by a full run:** `System.enumerateDevices()`
(mis-sized `GetAdaptersAddresses` buffer), the WSAPoll selector trapping
on an invalid fd and on schedule-after-shutdown, and a connection-reset
promise leak.
- **Flaky, timing-sensitive tests:** the event-loop tick-count metrics
tests (the WSAPoll selector wakes far more often than epoll/kqueue).

Then enable the Windows 6.3 + nightly jobs in the reusable unit-tests
workflow (which runs `swift test`) in both the PR and push workflows,
and drop the separate build-only Windows job (which only ran `swift
build --build-tests`).

Windows uses the default `swift test` arguments rather than the Linux
`-Xswiftc -warnings-as-errors` override, because a `#warning` in the
(skipped) stream-channel tests would otherwise fail the build.

All skips are Windows-only; there is no behaviour change on other
platforms.

### Result:

`swift test` passes on Windows, and the Windows unit tests run in CI.

---------

Co-authored-by: George Barnett <gbarnett@apple.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

semver/none No version bump required.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants