Files
swift-mirror/test/Concurrency/Runtime/continuation_validation.swift
Max Desiatov ab7cd238fd [6.2] test/CMakeLists.txt: run Embedded Swift for Wasm tests (#83287)
Cherry-pick of #83128, #82399, and #82878, merged as ea6ca2b5db, 0c4e56174b, and e34eb3331f respectively.

**Explanation**: Currently `test/CMakeLists.txt` can only set `SWIFT_LIT_ARGS` for all tests uniformly. This means that we can't have tests for Embedded Swift with a different set of `lit.py` arguments.

Also, create new `check-swift-embedded-wasi` target from `test/CMakeLists.txt`, tweak `lit.cfg` to support WASI Clang resource dir, exclude unsupported tests based on `CPU=wasm32` instead of `OS=wasi`.

**Scope**: Limited to Embedded Swift test suite.
**Risk**: Low, due to limited scope.
**Testing**: #82878 was incubated on `main` for 2 weeks, #82399 for 3 weeks with no disruption, #83128 merged this week, but enables all these tests on CI, which are consistently passing.
**Issue**: rdar://156585717
**Reviewer**: @bnbarham
2025-07-26 09:48:47 +01:00

98 lines
3.3 KiB
Swift

// RUN: %empty-directory(%t)
// RUN: %target-build-swift -target %target-swift-5.1-abi-triple -parse-as-library %s -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: env %env-SWIFT_DEBUG_VALIDATE_UNCHECKED_CONTINUATIONS=1 %target-run %t/a.out
// REQUIRES: executable_test
// REQUIRES: concurrency
// REQUIRES: concurrency_runtime
// UNSUPPORTED: back_deployment_runtime
// Commit afc5116ef0de added support for the error check
// UNSUPPORTED: back_deploy_concurrency
// UNSUPPORTED: use_os_stdlib
// UNSUPPORTED: freestanding
// UNSUPPORTED: OS=wasip1
import StdlibUnittest
@MainActor
@available(SwiftStdlib 5.1, *)
func test_isolation_withUnsafeContinuation() async {
// This test specifically should have only one suspension point,
// as it would trigger a problem with the previous @_unsafeInheritExecutor
// implementation, where we optimize away a switch accidentally, causing
// wrong isolation.
await withUnsafeContinuation { continuation in
MainActor.shared.assertIsolated() // OK
continuation.resume(returning: ())
}
}
@MainActor
@available(SwiftStdlib 5.1, *)
func test_isolation_withUnsafeThrowingContinuation() async {
// See comment in `test_isolation_withUnsafeContinuation` about exact test case shape
try! await withUnsafeThrowingContinuation { continuation in
MainActor.shared.assertIsolated() // OK
continuation.resume(returning: ())
}
}
@MainActor
@available(SwiftStdlib 5.1, *)
func test_isolation_withCheckedContinuation() async {
// See comment in `test_isolation_withUnsafeContinuation` about exact test case shape
await withCheckedContinuation { continuation in
MainActor.shared.assertIsolated() // OK
continuation.resume(returning: ())
}
}
@MainActor
@available(SwiftStdlib 5.1, *)
func test_isolation_withCheckedThrowingContinuation() async {
// See comment in `test_isolation_withUnsafeContinuation` about exact test case shape
try! await withCheckedThrowingContinuation { continuation in
MainActor.shared.assertIsolated() // OK
continuation.resume(returning: ())
}
}
@main struct Main {
static func main() async {
let tests = TestSuite("ContinuationValidation")
if #available(SwiftStdlib 5.1, *) {
tests.test("withUnsafeThrowingContinuation: continuation should be on calling isolation") {
await Task.detached {
await test_isolation_withUnsafeThrowingContinuation()
}.value
}
tests.test("withUnsafeContinuation: continuation should be on calling isolation") {
await Task.detached {
await test_isolation_withUnsafeContinuation()
}.value
}
tests.test("withCheckedContinuation: continuation should be on calling isolation") {
await Task.detached {
await test_isolation_withCheckedContinuation()
}.value
}
tests.test("withCheckedThrowingContinuation: continuation should be on calling isolation") {
await Task.detached {
await test_isolation_withCheckedThrowingContinuation()
}.value
}
tests.test("trap on double resume of unchecked continuation") {
expectCrashLater(withMessage: "may have already been resumed")
await withUnsafeContinuation { c in
c.resume(returning: ())
c.resume(returning: ())
}
}
}
await runAllTestsAsync()
}
}