Files
Jake PetroulesandGeorge Barnett 15b77e6400 [Windows] Compile the remaining test targets (NIOPosixTests, NIOFoundationCompatTests) (#3665)
## 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.

---------

Co-authored-by: George Barnett <gbarnett@apple.com>
2026-07-24 13:59:45 +00:00

66 lines
2.2 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2024 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIOCore
import NIOFoundationCompat
import NIOPosix
import XCTest
final class WaitSpinningRunLoopTests: XCTestCase {
private let loop = MultiThreadedEventLoopGroup.singleton.any()
func testPreFailedWorks() {
struct Dummy: Error {}
let future: EventLoopFuture<Never> = self.loop.makeFailedFuture(Dummy())
XCTAssertThrowsError(try future.waitSpinningRunLoop()) { error in
XCTAssert(error is Dummy)
}
}
func testPreSucceededWorks() {
let future = self.loop.makeSucceededFuture("hello")
XCTAssertEqual("hello", try future.waitSpinningRunLoop())
}
func testFailingAfterALittleWhileWorks() {
struct Dummy: Error {}
let future: EventLoopFuture<Never> = self.loop.scheduleTask(in: .milliseconds(10)) {
throw Dummy()
}.futureResult
XCTAssertThrowsError(try future.waitSpinningRunLoop()) { error in
XCTAssert(error is Dummy)
}
}
func testSucceedingAfterALittleWhileWorks() {
let future = self.loop.scheduleTask(in: .milliseconds(10)) {
"hello"
}.futureResult
XCTAssertEqual("hello", try future.waitSpinningRunLoop())
}
func testWeCanStillUseOurRunLoopWhilstBlocking() {
let promise = self.loop.makePromise(of: String.self)
let myRunLoop = RunLoop.current
let timer = Timer(timeInterval: 0.1, repeats: false) { [loop = self.loop] _ in
loop.scheduleTask(in: .microseconds(10)) {
promise.succeed("hello")
}
}
myRunLoop.add(timer, forMode: .default)
XCTAssertEqual("hello", try promise.futureResult.waitSpinningRunLoop())
}
}