Files
swift-nio/Tests
Si Beaumont 558f24a464 Fix thread pinning test, which crashed in CPU-restricted environments (#3562)
### Motivation:

`testCanPinThreadsToCores` crashes with a precondition failure
(`pthread_setaffinity_np failed: 22`) on CI environments where the
container's available CPUs don't start from 0.


```console
% docker run --rm -v $PWD:/pwd -w /pwd swift:6.2 nproc
8
```

Running the test in an environment where the CPUs didn't start from 0
crashed:

```console
% docker run --rm -v $PWD:/pwd -w /pwd --cpuset-cpus="2-3" swift:6.2 nproc
2
```

```console
% docker run --rm -v $PWD:/pwd -w /pwd --cpuset-cpus="2-3" swift:6.2 swift test --filter testCanPinThreadsToCores
...
◇ Suite "NIOThreadPoolTest" started.
◇ Test testCanPinThreadsToCores() started.
NIOPosix/LinuxCPUSet.swift:78: Precondition failed: pthread_setaffinity_np failed: 22

*** Signal 5: Backtracing from 0xf358a1ceb140... done ***

*** Program crashed: System trap at 0x0000f358a1ceb140 ***

Platform: arm64 Linux (Ubuntu 24.04.4 LTS)

Thread 0 "TP-#0" crashed:

  0              0x0000f358a1ceb140 _assertionFailure(_:_:file:line:flags:) + 164 in libswiftCore.so
  1 [ra]         0x0000ba727f86c0a0 static NIOThread.currentAffinity.setter + 939 in swift-nioPackageTests.xctest at /pwd/Sources/NIOPosix/LinuxCPUSet.swift:78:13
  2 [ra]         0x0000ba727f878b48 closure #2 in NIOThreadPool._start(threadNamePrefix:) + 295 in swift-nioPackageTests.xctest at /pwd/Sources/NIOPosix/NIOThreadPool.swift:365:47
  3 [ra] [thunk] 0x0000ba727f8fc5a0 thunk for @escaping @callee_guaranteed (@guaranteed NIOThread) -> () + 23 in swift-nioPackageTests.xctest at //<compiler-generated>
  4 [ra]         0x0000ba727f8ff5c8 closure #1 in closure #1 in static ThreadOpsPosix.run(handle:args:) + 767 in swift-nioPackageTests.xctest at /pwd/Sources/NIOPosix/ThreadPosix.swift:186:21
  5 [ra] [thunk] 0x0000ba727f8ff678 @objc closure #1 in closure #1 in static ThreadOpsPosix.run(handle:args:) + 11 in swift-nioPackageTests.xctest at //<compiler-generated>
  6 [ra]         0x0000f358a03d595c <unknown> in libc.so.6
...
```

Also, when there was just one CPU, this test would pass but wasn't
actually testing the pinning because it was using
`min(1, System.coreCount / 2)` instead of `max`, which clamped down to 0
for the loop.

### Modifications:

- Select from available CPU IDs using `NIOThread.currentAffinity.cpuIds`
- Use `max` instead of `min` to still run the test on single core
environments.

### Result:

The test no longer crashes on CPU-restricted environments:

```console
% docker run --rm -v $PWD:/pwd -w /pwd --cpuset-cpus="2-3" swift:6.2 swift test --filter testCanPinThreadsToCores
...
◇ Suite "NIOThreadPoolTest" started.
◇ Test testCanPinThreadsToCores() started.
✔ Test testCanPinThreadsToCores() passed after 0.001 seconds.
✔ Suite "NIOThreadPoolTest" passed after 0.001 seconds.
✔ Test run with 1 test in 1 suite passed after 0.001 seconds.
```
2026-03-26 15:46:49 +00:00
..
2024-07-19 11:48:17 +02:00
2025-10-06 10:58:33 +01:00
2025-05-07 12:24:44 +00:00