From 02d93e623fffacf5cdb340a322956272c263fc95 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 11:52:22 +0000 Subject: [PATCH] Update coverage counters atomically and time out test jobs Coverage counters are plain loads and stores, and Swift Testing runs tests in parallel, so a counter that several tests increment can lose an increment. llvm-cov does not count every region directly: it subtracts one counter from another for an `else` branch, or for the code after a `guard` that can return or a call that can throw. A lost increment can then make a line that ran report as never executed, which breaks the 100% coverage target at random. The Linux coverage job now passes `-instrprof-atomic-counter-update-all` to LLVM, which makes every counter update atomic, so every count is exact. This adds about a second to the build. The Xcode jobs still upload coverage built with plain counters. Codecov merges the uploads and counts a line as covered when any upload reports it as run, so the exact Linux report keeps a line that ran from reading as missed. A build system can drop the flag: SwiftPM's swiftbuild build system dropped it from SafeDI's macOS build. A new step after the tests disassembles the test binary with llvm-objdump and fails with an `::error::` annotation if any coverage counter update lacks a `lock` prefix, or if none has one, as when the binary has no counters. It writes the disassembly to a file before reading it, so that a missing llvm-objdump or test binary fails the step with llvm-objdump's own error rather than with a misleading annotation. The check reads x86_64 code, which the ubuntu-24.04 runner runs; on another architecture it finds no `lock` and fails. Every job that runs tests now times out: the simulator jobs after 50 minutes and the others after 15. On recent runs of main, the simulator jobs took 4 to 11 minutes and the others about a minute. A test that deadlocks keeps the test run from ending, and would otherwise hold a runner for GitHub's six-hour default, so for a regression that makes a test hang, the timeout is the failure signal. The unused `Expectation` helper and its tests are removed. They are a copy of the swift-testing-expectation package that no other test refers to, and its `fulfillment(withinSeconds:)` fails a test by wall-clock timeout, so a test that used it would depend on timing. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HLuVQLsTNvRnyh155N25UM --- .github/workflows/ci.yml | 41 +++++- Tests/AsyncQueueTests/ExpectationTests.swift | 106 ---------------- .../Utilities/Expectation.swift | 119 ------------------ 3 files changed, 40 insertions(+), 226 deletions(-) delete mode 100644 Tests/AsyncQueueTests/ExpectationTests.swift delete mode 100644 Tests/AsyncQueueTests/Utilities/Expectation.swift diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 37792f5..6492921 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,6 +17,9 @@ jobs: spm-16: name: Build Xcode 26 runs-on: macos-15 + # A test that deadlocks keeps the test run from ending, so stop the + # job after 50 minutes rather than at GitHub's six-hour default. + timeout-minutes: 50 strategy: matrix: platforms: [ @@ -69,6 +72,9 @@ jobs: spm-16-swift: name: Swift Build Xcode 26 runs-on: macos-15 + # A test that deadlocks keeps the test run from ending, so stop the + # job after 15 minutes rather than at GitHub's six-hour default. + timeout-minutes: 15 permissions: contents: read steps: @@ -82,13 +88,43 @@ jobs: name: "Build and Test on Linux Swift 6.2" runs-on: ubuntu-24.04 container: swift:6.2 + # A test that deadlocks keeps the test run from ending, so stop the + # job after 15 minutes rather than at GitHub's six-hour default. + timeout-minutes: 15 permissions: contents: read steps: - name: Checkout Repo uses: actions/checkout@v7 + # Coverage counters are plain loads and stores, and Swift Testing + # runs tests in parallel, so a counter that several tests update + # can lose an increment. llvm-cov counts some regions, such as an + # `else` branch or the code after a `guard`, by subtracting one + # counter from another, so a region that ran can read zero. + # `-instrprof-atomic-counter-update-all` makes every update + # atomic, so the counts are exact. A build system can drop the + # flag, which the next step catches. - name: Build and Test Framework - run: swift test -c release --enable-code-coverage -Xswiftc -enable-testing + run: swift test -c release --enable-code-coverage -Xswiftc -enable-testing -Xswiftc -Xllvm -Xswiftc -instrprof-atomic-counter-update-all + # Fails unless the test binary updates coverage counters and + # every update is atomic. It reads x86_64 code: an atomic update + # follows a `lock` prefix, which llvm-objdump prints on its own + # line, and `leaq` only takes a counter's address. On another + # architecture it finds no `lock` and fails. The disassembly goes + # to a file rather than a pipe, so that a missing llvm-objdump or + # test binary fails the step with llvm-objdump's own error. + - name: Check Atomic Coverage Counters + run: | + llvm-objdump -d --no-show-raw-insn .build/x86_64-unknown-linux-gnu/release/swift-async-queuePackageTests.xctest > .build/disassembly.txt + awk ' + /<__profc_/ && $2 != "leaq" { if (previous == "lock") atomic++; else plain++ } + { previous = $2 } + END { + if (plain > 0) print "::error::" plain " coverage counter updates in the test binary are not atomic." + if (atomic == 0) print "::error::The test binary has no atomic coverage counter updates." + print atomic + 0 " atomic and " plain + 0 " non-atomic coverage counter updates." + exit (plain > 0 || atomic == 0) + }' .build/disassembly.txt - name: Prepare Coverage Reports run: | llvm-cov export -format="lcov" .build/x86_64-unknown-linux-gnu/release/swift-async-queuePackageTests.xctest -instr-profile .build/x86_64-unknown-linux-gnu/release/codecov/default.profdata > coverage.lcov @@ -110,6 +146,9 @@ jobs: # collected by the pinned jobs only, so a different toolchain can't # skew the report. container: swift:latest + # A test that deadlocks keeps the test run from ending, so stop the + # job after 15 minutes rather than at GitHub's six-hour default. + timeout-minutes: 15 permissions: contents: read steps: diff --git a/Tests/AsyncQueueTests/ExpectationTests.swift b/Tests/AsyncQueueTests/ExpectationTests.swift deleted file mode 100644 index eb3cbc1..0000000 --- a/Tests/AsyncQueueTests/ExpectationTests.swift +++ /dev/null @@ -1,106 +0,0 @@ -// MIT License -// -// Copyright (c) 2024 Dan Federman -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. - -import Testing - -struct ExpectationTests { - // MARK: Behavior Tests - - @Test - func fulfill_triggersExpectation() async { - await confirmation { confirmation in - let systemUnderTest = Expectation( - expectedCount: 1, - expect: { expectation, _, _ in - #expect(expectation) - confirmation() - }, - ) - await systemUnderTest.fulfill().value - } - } - - @Test - func fulfill_triggersExpectationOnceWhenCalledTwiceAndExpectedCountIsTwo() async { - await confirmation { confirmation in - let systemUnderTest = Expectation( - expectedCount: 2, - expect: { expectation, _, _ in - #expect(expectation) - confirmation() - }, - ) - await systemUnderTest.fulfill().value - await systemUnderTest.fulfill().value - } - } - - @Test - func fulfill_triggersExpectationWhenExpectedCountIsZero() async { - await confirmation { confirmation in - let systemUnderTest = Expectation( - expectedCount: 0, - expect: { expectation, _, _ in - #expect(!expectation) - confirmation() - }, - ) - await systemUnderTest.fulfill().value - } - } - - @Test - func fulfillment_doesNotWaitIfAlreadyFulfilled() async { - let systemUnderTest = Expectation(expectedCount: 0) - await systemUnderTest.fulfillment(withinSeconds: 30) - } - - @MainActor // Global actor ensures Task ordering. - @Test - func fulfillment_waitsForFulfillment() async { - let systemUnderTest = Expectation(expectedCount: 1) - var hasFulfilled = false - let wait = Task { - await systemUnderTest.fulfillment(withinSeconds: 30) - #expect(hasFulfilled) - } - Task { - systemUnderTest.fulfill() - hasFulfilled = true - } - await wait.value - } - - @Test - func fulfillment_triggersFalseExpectationWhenItTimesOut() async { - await confirmation { confirmation in - let systemUnderTest = Expectation( - expectedCount: 1, - expect: { expectation, _, _ in - #expect(!expectation) - confirmation() - }, - ) - await systemUnderTest.fulfillment(withinSeconds: 0) - } - } -} diff --git a/Tests/AsyncQueueTests/Utilities/Expectation.swift b/Tests/AsyncQueueTests/Utilities/Expectation.swift deleted file mode 100644 index 4fc245e..0000000 --- a/Tests/AsyncQueueTests/Utilities/Expectation.swift +++ /dev/null @@ -1,119 +0,0 @@ -// MIT License -// -// Copyright (c) 2024 Dan Federman -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. - -import Testing - -public actor Expectation { - // MARK: Initialization - - public init( - expectedCount: UInt = 1, - ) { - self.init( - expectedCount: expectedCount, - expect: { #expect($0, $1, sourceLocation: $2) }, - ) - } - - init( - expectedCount: UInt, - expect: @escaping (Bool, Comment?, SourceLocation) -> Void, - ) { - self.expectedCount = expectedCount - self.expect = expect - } - - // MARK: Public - - public func fulfillment( - withinSeconds seconds: UInt64, - filePath: String = #filePath, - fileID: String = #fileID, - line: Int = #line, - column: Int = #column, - ) async { - guard !isComplete else { return } - let wait = Task { - try await Task.sleep(nanoseconds: seconds * 1_000_000_000) - expect(isComplete, "Expectation not fulfilled within \(seconds) seconds", .init( - fileID: fileID, - filePath: filePath, - line: line, - column: column, - )) - } - waits.append(wait) - try? await wait.value - } - - @discardableResult - nonisolated public func fulfill( - filePath: String = #filePath, - fileID: String = #fileID, - line: Int = #line, - column: Int = #column, - ) -> Task { - Task { - await self._fulfill( - filePath: filePath, - fileID: fileID, - line: line, - column: column, - ) - } - } - - // MARK: Private - - private var waits = [Task]() - private var fulfillCount: UInt = 0 - private var isComplete: Bool { - expectedCount <= fulfillCount - } - - private let expectedCount: UInt - private let expect: (Bool, Comment?, SourceLocation) -> Void - - private func _fulfill( - filePath: String, - fileID: String, - line: Int, - column: Int, - ) { - fulfillCount += 1 - guard isComplete else { return } - expect( - expectedCount == fulfillCount, - "Expected \(expectedCount) calls to `fulfill()`. Received \(fulfillCount).", - .init( - fileID: fileID, - filePath: filePath, - line: line, - column: column, - ), - ) - for wait in waits { - wait.cancel() - } - waits = [] - } -}