Repository navigation
[Windows] Compile the remaining test targets (NIOPosixTests, NIOFoundationCompatTests) - #3665
Conversation
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.
20be3c6 to
591c307
Compare
| } | ||
|
|
||
| func testServerClientBootstrap_withAsyncChannel_clientConnectedSocket() async throws { | ||
| #if !os(Windows) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Sure they can:
func testSomething() {
#if os(Windows)
throw XCTSkip("Skipped because ...")
#else
// ...
#endif
}There was a problem hiding this comment.
Ah, I see what you mean. I've updated as you requested.
| // Uses `ChannelOptions.socket(SOL_SOCKET, SO_REUSEADDR)`, whose integer | ||
| // form is unavailable on Windows. |
There was a problem hiding this comment.
Is there a form on Windows it could use?
There was a problem hiding this comment.
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.
| .testTarget( | ||
| name: "NIOFoundationCompatTests", | ||
| dependencies: [ | ||
| "NIO", |
There was a problem hiding this comment.
What do you actually need here? We've been careful to not add blanket NIO dependencies.
There was a problem hiding this comment.
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.
… 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>
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 —
NIOPosixTestsandNIOFoundationCompatTests— compiling on Windows, so all 16 test targets now build, and switches Windows CI toswift 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
Compile
NIOFoundationCompatTests— the target imports theNIOumbrella (inWaitSpinningRunLoopTests, forMultiThreadedEventLoopGroup) but only declaredNIOCore/NIOFoundationCompatas dependencies.NIOre-exportsNIOPosix, which imports theCNIOPosixC 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 theNIOdependency it actually uses.Compile
NIOPosixTests— the deeply-POSIX target. Gate-out-plus-minimal-shim:import WinSDKwhere onlysockaddr/timeval/in_addrfield or type access was missing (MemberImportVisibility) — this alone cleared the two largest files (SocketAddressTest,HappyEyeballsTest).pipe→_pipe(viaucrt), anSTDOUT_FILENOshim,Thread.sleepinstead ofsleep, and lettingIOResulttype inference pickssize_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/msghdrconstruction,NonBlockingFileIO.lstat/symlink/listDirectory/createDirectory, vsock CID/connect tests,/dev/nullsyscall-wrapper perf tests, and socket-handle (NIOBSDSocket.HandleUInt64vsCInt) mismatches.SyscallAbstractionLayer[Context],SALChannelTests,SALEventLoopTests) — it's built onCIntfds and can't compile against Windows'sUInt64socket handles without a larger rework;ControlMessageTests(POSIXmsghdr/control-message throughout); andMulticastTest(deprecatedenumerateInterfaces/NIONetworkInterface).Build the test targets in CI — change the Windows matrix command from
swift buildtoswift build --build-testsinmain.ymlandpull_request.yml.Notes for reviewers
#if os(Windows)/#if !os(Windows); the few shared-line changes (Thread.sleep,IOResultinference, an IUO force-unwrap ofmsg_name) are identical on POSIX.