-
Notifications
You must be signed in to change notification settings - Fork 12
Adding async await to DataLoader #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
NeedleInAJayStack
merged 8 commits into
GraphQLSwift:master
from
d-exclaimation:async-await
Jul 25, 2022
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6979fdc
Added async await for DataLoader
d-exclaimation 1f16983
Added async await test that is not redudant
d-exclaimation 127ec68
Fixed delay to be more reliable
d-exclaimation b35b680
Opted for Task instead of async-let
d-exclaimation b79b0d7
Updated swift-actions and Moved actor from functions
d-exclaimation d637ec5
Updated actions on macOS to use setup-xcode
d-exclaimation 35238f1
Updated workflow to use latest Xcode
d-exclaimation 6a9ddee
Fixed test to compensate for non-ordered Task
d-exclaimation File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import XCTest | ||
import NIO | ||
|
||
@testable import DataLoader | ||
|
||
#if compiler(>=5.5) && canImport(_Concurrency) | ||
|
||
@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *) | ||
actor Concurrent<T> { | ||
var wrappedValue: T | ||
|
||
func nonmutating<Returned>(_ action: (T) throws -> Returned) async rethrows -> Returned { | ||
try action(wrappedValue) | ||
} | ||
|
||
func mutating<Returned>(_ action: (inout T) throws -> Returned) async rethrows -> Returned { | ||
try action(&wrappedValue) | ||
} | ||
|
||
init(_ value: T) { | ||
self.wrappedValue = value | ||
} | ||
} | ||
|
||
|
||
/// Primary API | ||
@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *) | ||
final class DataLoaderAsyncTests: XCTestCase { | ||
|
||
/// Builds a really really simple data loader with async await | ||
func testReallyReallySimpleDataLoader() async throws { | ||
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) | ||
defer { | ||
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) | ||
} | ||
|
||
let identityLoader = DataLoader<Int, Int>( | ||
on: eventLoopGroup.next(), | ||
options: DataLoaderOptions(batchingEnabled: false) | ||
) { keys async in | ||
let task = Task { | ||
keys.map { DataLoaderFutureValue.success($0) } | ||
} | ||
return await task.value | ||
} | ||
|
||
let value = try await identityLoader.load(key: 1, on: eventLoopGroup) | ||
|
||
XCTAssertEqual(value, 1) | ||
} | ||
|
||
/// Supports loading multiple keys in one call | ||
func testLoadingMultipleKeys() async throws { | ||
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) | ||
defer { | ||
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) | ||
} | ||
|
||
let identityLoader = DataLoader<Int, Int>(on: eventLoopGroup.next()) { keys in | ||
let task = Task { | ||
keys.map { DataLoaderFutureValue.success($0) } | ||
} | ||
return await task.value | ||
} | ||
|
||
let values = try await identityLoader.loadMany(keys: [1, 2], on: eventLoopGroup) | ||
|
||
XCTAssertEqual(values, [1,2]) | ||
|
||
let empty = try await identityLoader.loadMany(keys: [], on: eventLoopGroup) | ||
|
||
XCTAssertTrue(empty.isEmpty) | ||
} | ||
|
||
// Batches multiple requests | ||
func testMultipleRequests() async throws { | ||
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) | ||
defer { | ||
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) | ||
} | ||
|
||
let loadCalls = Concurrent<[[Int]]>([]) | ||
|
||
let identityLoader = DataLoader<Int, Int>( | ||
on: eventLoopGroup.next(), | ||
options: DataLoaderOptions( | ||
batchingEnabled: true, | ||
executionPeriod: nil | ||
) | ||
) { keys in | ||
await loadCalls.mutating { $0.append(keys) } | ||
let task = Task { | ||
keys.map { DataLoaderFutureValue.success($0) } | ||
} | ||
return await task.value | ||
} | ||
|
||
async let value1 = identityLoader.load(key: 1, on: eventLoopGroup) | ||
async let value2 = identityLoader.load(key: 2, on: eventLoopGroup) | ||
|
||
/// Have to wait for a split second because Tasks may not be executed before this statement | ||
try await Task.sleep(nanoseconds: 500_000_000) | ||
|
||
XCTAssertNoThrow(try identityLoader.execute()) | ||
|
||
let result1 = try await value1 | ||
XCTAssertEqual(result1, 1) | ||
let result2 = try await value2 | ||
XCTAssertEqual(result2, 2) | ||
|
||
let calls = await loadCalls.wrappedValue | ||
XCTAssertEqual(calls.count, 1) | ||
XCTAssertEqual(calls.map { $0.sorted() }, [[1, 2]]) | ||
} | ||
} | ||
|
||
#endif |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.