Skip to content

Commit 4db97c3

Browse files
committedOct 18, 2024
fix: testMultipleRequestsWithMaxBatchSize
1 parent 3ac1c75 commit 4db97c3

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed
 

‎Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.10
1+
// swift-tools-version:5.9
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription

‎Sources/AsyncDataLoader/Channel/Channel.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
internal actor Channel<Success: Sendable, Failure: Error>: Sendable {
1+
actor Channel<Success: Sendable, Failure: Error>: Sendable {
22
private var state = State<Success, Failure>()
33
}
44

5-
internal extension Channel {
5+
extension Channel {
66
@discardableResult
77
func fulfill(_ value: Success) async -> Bool {
88
if await state.result == nil {
@@ -16,7 +16,7 @@ internal extension Channel {
1616

1717
return false
1818
}
19-
19+
2020
return true
2121
}
2222

‎Sources/AsyncDataLoader/DataLoader.swift

+15-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ public enum DataLoaderValue<T: Sendable>: Sendable {
66
case failure(Error)
77
}
88

9-
public typealias BatchLoadFunction<Key: Hashable & Sendable, Value: Sendable> = @Sendable (_ keys: [Key]) async throws -> [DataLoaderValue<Value>]
10-
private typealias LoaderQueue<Key: Hashable & Sendable, Value: Sendable> = [(key: Key, channel: Channel<Value, Error>)]
9+
public typealias BatchLoadFunction<Key: Hashable & Sendable, Value: Sendable> =
10+
@Sendable (_ keys: [Key]) async throws -> [DataLoaderValue<Value>]
11+
private typealias LoaderQueue<Key: Hashable & Sendable, Value: Sendable> = [(
12+
key: Key,
13+
channel: Channel<Value, Error>
14+
)]
1115

1216
/// DataLoader creates a public API for loading data from a particular
1317
/// data back-end with unique keys such as the id column of a SQL table
@@ -61,7 +65,11 @@ public actor DataLoader<Key: Hashable & Sendable, Value: Sendable> {
6165
let results = try await self.batchLoadFunction([key])
6266

6367
if results.isEmpty {
64-
await channel.fail(DataLoaderError.noValueForKey("Did not return value for key: \(key)"))
68+
await channel
69+
.fail(
70+
DataLoaderError
71+
.noValueForKey("Did not return value for key: \(key)")
72+
)
6573
} else {
6674
let result = results[0]
6775

@@ -190,7 +198,10 @@ public actor DataLoader<Key: Hashable & Sendable, Value: Sendable> {
190198
let values = try await batchLoadFunction(keys)
191199

192200
if values.count != keys.count {
193-
throw DataLoaderError.typeError("The function did not return an array of the same length as the array of keys. \nKeys count: \(keys.count)\nValues count: \(values.count)")
201+
throw DataLoaderError
202+
.typeError(
203+
"The function did not return an array of the same length as the array of keys. \nKeys count: \(keys.count)\nValues count: \(values.count)"
204+
)
194205
}
195206

196207
for entry in batch.enumerated() {

‎Tests/AsyncDataLoaderTests/DataLoaderTests.swift

+8-6
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ actor Concurrent<T> {
2121
}
2222

2323
/// Primary API
24-
///The `try await Task.sleep(nanoseconds: 2_000_000)` introduces a small delay to simulate asynchronous
25-
///behavior and ensure that concurrent requests (`value1`, `value2`...) are grouped into a single batch
26-
///for processing, as intended by the batching settings.
24+
/// The `try await Task.sleep(nanoseconds: 2_000_000)` introduces a small delay to simulate
25+
/// asynchronous behavior and ensure that concurrent requests (`value1`, `value2`...)
26+
/// are grouped into a single batch for processing, as intended by the batching settings.
2727
final class DataLoaderTests: XCTestCase {
2828
/// Builds a really really simple data loader'
2929
func testReallyReallySimpleDataLoader() async throws {
@@ -126,16 +126,18 @@ final class DataLoaderTests: XCTestCase {
126126

127127
XCTAssertNil(didFailWithError)
128128

129-
let (result1, result2) = try await (value1, value2)
129+
let result1 = try await value1
130+
let result2 = try await value2
130131
let result3 = try await value3
131132

132133
XCTAssertEqual(result1, 1)
133134
XCTAssertEqual(result2, 2)
134135
XCTAssertEqual(result3, 3)
135136

136137
let calls = await loadCalls.wrappedValue
137-
138-
XCTAssertEqual(calls.map { $0.sorted() }, [[1, 2], [3]])
138+
139+
XCTAssertEqual(calls.first?.count, 2)
140+
XCTAssertEqual(calls.last?.count, 1)
139141
}
140142

141143
/// Coalesces identical requests

0 commit comments

Comments
 (0)
Please sign in to comment.