Skip to content

Commit b79b0d7

Browse files
Updated swift-actions and Moved actor from functions
1 parent b35b680 commit b79b0d7

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
os: [macos-latest, ubuntu-latest]
1515
steps:
1616
- uses: actions/checkout@v2
17-
- uses: fwal/setup-swift@v1
17+
- uses: swift-actions/setup-swift@v1
1818
- name: Build
1919
run: swift build
2020
- name: Run tests

Tests/DataLoaderTests/DataLoaderAsyncTests.swift

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@ import NIO
55

66
#if compiler(>=5.5) && canImport(_Concurrency)
77

8+
@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
9+
actor Concurrent<T> {
10+
var wrappedValue: T
11+
12+
func nonmutating<Returned>(_ action: (T) throws -> Returned) async rethrows -> Returned {
13+
try action(wrappedValue)
14+
}
15+
16+
func mutating<Returned>(_ action: (inout T) throws -> Returned) async rethrows -> Returned {
17+
try action(&wrappedValue)
18+
}
19+
20+
init(_ value: T) {
21+
self.wrappedValue = value
22+
}
23+
}
24+
25+
826
/// Primary API
927
@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
1028
final class DataLoaderAsyncTests: XCTestCase {
@@ -61,15 +79,7 @@ final class DataLoaderAsyncTests: XCTestCase {
6179
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
6280
}
6381

64-
actor LoadCalls {
65-
var loadCalls = [[Int]]()
66-
67-
func append(_ calls: [Int]) {
68-
loadCalls.append(calls)
69-
}
70-
71-
static let shared: LoadCalls = .init()
72-
}
82+
let loadCalls = Concurrent<[[Int]]>([])
7383

7484
let identityLoader = DataLoader<Int, Int>(
7585
on: eventLoopGroup.next(),
@@ -78,35 +88,28 @@ final class DataLoaderAsyncTests: XCTestCase {
7888
executionPeriod: nil
7989
)
8090
) { keys in
81-
await LoadCalls.shared.append(keys)
91+
await loadCalls.mutating { $0.append(keys) }
8292
let task = Task {
8393
keys.map { DataLoaderFutureValue.success($0) }
8494
}
8595
return await task.value
8696
}
8797

88-
// Normally async-let is a better option that using explicit Task, but the test machine fails to use async let multiple times already
89-
// async let value1 = identityLoader.load(key: 1, on: eventLoopGroup)
90-
// async let value2 = identityLoader.load(key: 2, on: eventLoopGroup)
91-
let value1 = Task {
92-
try await identityLoader.load(key: 1, on: eventLoopGroup)
93-
}
94-
let value2 = Task {
95-
try await identityLoader.load(key: 2, on: eventLoopGroup)
96-
}
98+
async let value1 = identityLoader.load(key: 1, on: eventLoopGroup)
99+
async let value2 = identityLoader.load(key: 2, on: eventLoopGroup)
97100

98101
/// Have to wait for a split second because Tasks may not be executed before this statement
99102
try await Task.sleep(nanoseconds: 500_000_000)
100103

101104
XCTAssertNoThrow(try identityLoader.execute())
102105

103-
let result1 = try await value1.value
106+
let result1 = try await value1
104107
XCTAssertEqual(result1, 1)
105-
let result2 = try await value2.value
108+
let result2 = try await value2
106109
XCTAssertEqual(result2, 2)
107110

108-
let loadCalls = await LoadCalls.shared.loadCalls
109-
XCTAssertEqual(loadCalls, [[1,2]])
111+
let calls = await loadCalls.wrappedValue
112+
XCTAssertEqual(calls, [[1,2]])
110113
}
111114
}
112115

0 commit comments

Comments
 (0)