|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// Copyright © 2026 Apple Inc. and the Containerization project authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +import Foundation |
| 18 | +import Testing |
| 19 | + |
| 20 | +@testable import ContainerizationExtras |
| 21 | + |
| 22 | +private final class Unprotected: @unchecked Sendable { |
| 23 | + var value: Int |
| 24 | + init(_ value: Int) { self.value = value } |
| 25 | +} |
| 26 | + |
| 27 | +final class AsyncLockTests { |
| 28 | + @Test |
| 29 | + func testBasicModification() async throws { |
| 30 | + let lock = AsyncLock() |
| 31 | + let counter = Unprotected(0) |
| 32 | + |
| 33 | + let result = await lock.withLock { _ in |
| 34 | + counter.value += 1 |
| 35 | + return counter.value |
| 36 | + } |
| 37 | + |
| 38 | + #expect(result == 1) |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + func testSequentialReturnValues() async throws { |
| 43 | + let lock = AsyncLock() |
| 44 | + |
| 45 | + let first = await lock.withLock { _ in 1 } |
| 46 | + let second = await lock.withLock { _ in first + 1 } |
| 47 | + let third = await lock.withLock { _ in second + 1 } |
| 48 | + |
| 49 | + #expect(third == 3) |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + func testMultipleModifications() async throws { |
| 54 | + let lock = AsyncLock() |
| 55 | + let counter = Unprotected(0) |
| 56 | + |
| 57 | + await lock.withLock { _ in |
| 58 | + counter.value += 5 |
| 59 | + } |
| 60 | + |
| 61 | + let result = await lock.withLock { value in |
| 62 | + counter.value += 10 |
| 63 | + return counter.value |
| 64 | + } |
| 65 | + |
| 66 | + #expect(result == 15) |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + func testMutualExclusion() async throws { |
| 71 | + let lock = AsyncLock() |
| 72 | + let counter = Unprotected(0) |
| 73 | + let iterations = 100 |
| 74 | + |
| 75 | + await withTaskGroup(of: Void.self) { group in |
| 76 | + for _ in 0..<iterations { |
| 77 | + group.addTask { |
| 78 | + await lock.withLock { _ in |
| 79 | + let current = counter.value |
| 80 | + try? await Task.sleep(for: .milliseconds(10)) |
| 81 | + counter.value = current + 1 |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + #expect(counter.value == iterations) |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + func testThrowingClosure() async throws { |
| 92 | + let lock = AsyncLock() |
| 93 | + let counter = Unprotected(0) |
| 94 | + |
| 95 | + await #expect(throws: POSIXError.self) { |
| 96 | + try await lock.withLock { _ in |
| 97 | + counter.value = 1 |
| 98 | + throw POSIXError(.ENOENT) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // Value should still be modified even though closure threw |
| 103 | + #expect(counter.value == 1) |
| 104 | + |
| 105 | + // Lock should still be usable even though the previous closure threw |
| 106 | + await lock.withLock { _ in |
| 107 | + counter.value = 2 |
| 108 | + } |
| 109 | + |
| 110 | + #expect(counter.value == 2) |
| 111 | + } |
| 112 | +} |
0 commit comments