Skip to content

Commit

Permalink
feat: Refactor network transport auth handling; provide new member-wi…
Browse files Browse the repository at this point in the history
…se initializer (#422)

Refs: #415
  • Loading branch information
palpatim authored Nov 5, 2020
1 parent 8904642 commit e62fab2
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 190 deletions.
307 changes: 171 additions & 136 deletions AWSAppSyncClient/AWSAppSyncHTTPNetworkTransport.swift

Large diffs are not rendered by default.

48 changes: 24 additions & 24 deletions AWSAppSyncIntegrationTests/AWSAppSyncAPIKeyAuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ class AWSAppSyncAPIKeyAuthTests: XCTestCase {
let postCreated = expectation(description: "Post created successfully.")
let addPost = DefaultTestPostData.defaultCreatePostWithoutFileUsingParametersMutation

deinitNotifiableAppSyncClient?.perform(mutation: addPost, queue: AWSAppSyncAPIKeyAuthTests.mutationQueue) { result, error in
deinitNotifiableAppSyncClient?.perform(mutation: addPost, queue: AWSAppSyncAPIKeyAuthTests.mutationQueue, resultHandler: { result, error in
postCreated.fulfill()
}
})

// Wait for mutation to return before releasing client
wait(for: [postCreated], timeout: AWSAppSyncAPIKeyAuthTests.networkOperationTimeout)
Expand All @@ -74,7 +74,7 @@ class AWSAppSyncAPIKeyAuthTests: XCTestCase {
let postCreated = expectation(description: "Post created successfully.")
let addPost = DefaultTestPostData.defaultCreatePostWithoutFileUsingParametersMutation

appSyncClient?.perform(mutation: addPost, queue: AWSAppSyncAPIKeyAuthTests.mutationQueue) { result, error in
appSyncClient?.perform(mutation: addPost, queue: AWSAppSyncAPIKeyAuthTests.mutationQueue, resultHandler: { result, error in
XCTAssertNil(error)
XCTAssertNotNil(result?.data?.createPostWithoutFileUsingParameters?.id)
XCTAssertEqual(
Expand All @@ -83,7 +83,7 @@ class AWSAppSyncAPIKeyAuthTests: XCTestCase {
)
print("Created post \(result?.data?.createPostWithoutFileUsingParameters?.id ?? "(ID unexpectedly nil)")")
postCreated.fulfill()
}
})

wait(for: [postCreated], timeout: AWSAppSyncAPIKeyAuthTests.networkOperationTimeout)
}
Expand All @@ -92,15 +92,15 @@ class AWSAppSyncAPIKeyAuthTests: XCTestCase {
let postCreated = expectation(description: "Post created successfully.")
let addPost = DefaultTestPostData.defaultCreatePostWithoutFileUsingParametersMutation

appSyncClient?.perform(mutation: addPost, queue: AWSAppSyncAPIKeyAuthTests.mutationQueue) { result, error in
appSyncClient?.perform(mutation: addPost, queue: AWSAppSyncAPIKeyAuthTests.mutationQueue, resultHandler: { result, error in
XCTAssertNil(error)
XCTAssertNotNil(result?.data?.createPostWithoutFileUsingParameters?.id)
XCTAssertEqual(
result!.data!.createPostWithoutFileUsingParameters?.author,
DefaultTestPostData.author
)
postCreated.fulfill()
}
})

wait(for: [postCreated], timeout: AWSAppSyncAPIKeyAuthTests.networkOperationTimeout)

Expand All @@ -122,15 +122,15 @@ class AWSAppSyncAPIKeyAuthTests: XCTestCase {
let postCreated = expectation(description: "Post created successfully.")
let addPost = DefaultTestPostData.defaultCreatePostWithoutFileUsingParametersMutation

appSyncClient?.perform(mutation: addPost, queue: AWSAppSyncAPIKeyAuthTests.mutationQueue) { result, error in
appSyncClient?.perform(mutation: addPost, queue: AWSAppSyncAPIKeyAuthTests.mutationQueue, resultHandler: { result, error in
XCTAssertNil(error)
XCTAssertNotNil(result?.data?.createPostWithoutFileUsingParameters?.id)
XCTAssertEqual(
result!.data!.createPostWithoutFileUsingParameters?.author,
DefaultTestPostData.author
)
postCreated.fulfill()
}
})

wait(for: [postCreated], timeout: AWSAppSyncAPIKeyAuthTests.networkOperationTimeout)

Expand Down Expand Up @@ -179,13 +179,13 @@ class AWSAppSyncAPIKeyAuthTests: XCTestCase {

let addPost = DefaultTestPostData.defaultCreatePostWithoutFileUsingParametersMutation

appSyncClient?.perform(mutation: addPost, queue: AWSAppSyncAPIKeyAuthTests.mutationQueue) { result, error in
appSyncClient?.perform(mutation: addPost, queue: AWSAppSyncAPIKeyAuthTests.mutationQueue, resultHandler: { result, error in
print("CreatePost result handler invoked")
XCTAssertNil(error)
XCTAssertNotNil(result?.data?.createPostWithoutFileUsingParameters?.id)
XCTAssertEqual(result!.data!.createPostWithoutFileUsingParameters?.author, DefaultTestPostData.author)
postCreated.fulfill()
}
})
wait(for: [postCreated], timeout: AWSAppSyncAPIKeyAuthTests.networkOperationTimeout)

let fetchQuery = ListPostsQuery()
Expand Down Expand Up @@ -277,14 +277,14 @@ class AWSAppSyncAPIKeyAuthTests: XCTestCase {
let addPost = DefaultTestPostData.defaultCreatePostWithoutFileUsingParametersMutation

var idHolder: GraphQLID?
appSyncClient.perform(mutation: addPost, queue: AWSAppSyncAPIKeyAuthTests.mutationQueue) { result, error in
appSyncClient.perform(mutation: addPost, queue: AWSAppSyncAPIKeyAuthTests.mutationQueue, resultHandler: { result, error in
print("CreatePost result handler invoked")
XCTAssertNil(error)
XCTAssertNotNil(result?.data?.createPostWithoutFileUsingParameters?.id)
XCTAssertEqual(result!.data!.createPostWithoutFileUsingParameters?.author, DefaultTestPostData.author)
idHolder = result?.data?.createPostWithoutFileUsingParameters?.id
postCreated.fulfill()
}
})
wait(for: [postCreated], timeout: AWSAppSyncAPIKeyAuthTests.networkOperationTimeout)

guard let id = idHolder else {
Expand Down Expand Up @@ -438,12 +438,12 @@ class AWSAppSyncAPIKeyAuthTests: XCTestCase {
DispatchQueue.global().async {
sleep(3)
self.appSyncClient?.perform(mutation: firstUpvoteMutation,
queue: AWSAppSyncAPIKeyAuthTests.mutationQueue) { result, error in
print("Received first upvote mutation response")
XCTAssertNil(error)
XCTAssertNotNil(result?.data?.upvotePost?.id)
firstUpvoteComplete.fulfill()
}
queue: AWSAppSyncAPIKeyAuthTests.mutationQueue, resultHandler: { result, error in
print("Received first upvote mutation response")
XCTAssertNil(error)
XCTAssertNotNil(result?.data?.upvotePost?.id)
firstUpvoteComplete.fulfill()
})
}

wait(
Expand Down Expand Up @@ -584,12 +584,12 @@ class AWSAppSyncAPIKeyAuthTests: XCTestCase {
DispatchQueue.global().async {
sleep(3)
self.appSyncClient?.perform(mutation: secondUpvoteMutation,
queue: AWSAppSyncAPIKeyAuthTests.mutationQueue) { result, error in
print("Received second upvote mutation response")
XCTAssertNil(error)
XCTAssertNotNil(result?.data?.upvotePost?.id)
secondUpvoteComplete.fulfill()
}
queue: AWSAppSyncAPIKeyAuthTests.mutationQueue, resultHandler: { result, error in
print("Received second upvote mutation response")
XCTAssertNil(error)
XCTAssertNotNil(result?.data?.upvotePost?.id)
secondUpvoteComplete.fulfill()
})
}

wait(
Expand Down
15 changes: 8 additions & 7 deletions AWSAppSyncIntegrationTests/AWSAppSyncCognitoAuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ class AWSAppSyncCognitoAuthTests: XCTestCase {
let postCreated = expectation(description: "Post created successfully.")
let addPost = DefaultTestPostData.defaultCreatePostWithoutFileUsingParametersMutation

appSyncClient.perform(mutation: addPost, queue: AWSAppSyncCognitoAuthTests.mutationQueue) { result, error in
appSyncClient.perform(mutation: addPost, queue: AWSAppSyncCognitoAuthTests.mutationQueue, resultHandler: { result, error in
XCTAssertNil(error)
XCTAssertNotNil(result?.data?.createPostWithoutFileUsingParameters?.id)
XCTAssertEqual(
result!.data!.createPostWithoutFileUsingParameters?.author,
DefaultTestPostData.author
)
postCreated.fulfill()
}
})

wait(for: [postCreated], timeout: AWSAppSyncCognitoAuthTests.networkOperationTimeout)
}
Expand Down Expand Up @@ -84,11 +84,12 @@ class AWSAppSyncCognitoAuthTests: XCTestCase {

var mutationResult: GraphQLResult<CreatePostWithFileUsingParametersMutation.Data>? = nil
appSyncClient.perform(mutation: createPostWithFile,
queue: AWSAppSyncCognitoAuthTests.mutationQueue) { result, error in
XCTAssertNil(error)
mutationResult = result
postCreated.fulfill()
}
queue: AWSAppSyncCognitoAuthTests.mutationQueue,
resultHandler: { result, error in
XCTAssertNil(error)
mutationResult = result
postCreated.fulfill()
})

wait(for: [postCreated], timeout: AWSAppSyncCognitoAuthTests.networkOperationTimeout)

Expand Down
28 changes: 14 additions & 14 deletions AWSAppSyncIntegrationTests/AWSAppSyncMultiAuthClientsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ class AWSAppSyncMultiAuthClientsTests: XCTestCase {
let addPost = DefaultTestPostData.defaultCreatePostWithoutFileUsingParametersMutation

userPoolsAppSyncClient.perform(mutation: addPost,
queue: AWSAppSyncMultiAuthClientsTests.mutationQueue) { result, error in
// The result is disregarded.
userpoolPostCreated.fulfill()
}
queue: AWSAppSyncMultiAuthClientsTests.mutationQueue, resultHandler: { result, error in
// The result is disregarded.
userpoolPostCreated.fulfill()
})

iamAppSyncClient.perform(mutation: addPost,
queue: AWSAppSyncMultiAuthClientsTests.mutationQueue) { result, error in
// The result is disregarded.
iamPostCreated.fulfill()
}
queue: AWSAppSyncMultiAuthClientsTests.mutationQueue, resultHandler: { result, error in
// The result is disregarded.
iamPostCreated.fulfill()
})

wait(for: [iamPostCreated, userpoolPostCreated],
timeout: AWSAppSyncMultiAuthClientsTests.networkOperationTimeout,
Expand Down Expand Up @@ -77,7 +77,7 @@ class AWSAppSyncMultiAuthClientsTests: XCTestCase {
let addPost = DefaultTestPostData.defaultCreatePostWithoutFileUsingParametersMutation

var createdId: GraphQLID?
iamAppSyncClient.perform(mutation: addPost, queue: AWSAppSyncMultiAuthClientsTests.mutationQueue) { result, error in
iamAppSyncClient.perform(mutation: addPost, queue: AWSAppSyncMultiAuthClientsTests.mutationQueue, resultHandler: { result, error in
XCTAssertNil(error)
XCTAssertNotNil(result?.data?.createPostWithoutFileUsingParameters?.id)
createdId = result!.data!.createPostWithoutFileUsingParameters!.id
Expand All @@ -86,7 +86,7 @@ class AWSAppSyncMultiAuthClientsTests: XCTestCase {
DefaultTestPostData.author
)
postCreated.fulfill()
}
})

wait(for: [postCreated], timeout: AWSAppSyncMultiAuthClientsTests.networkOperationTimeout)

Expand Down Expand Up @@ -118,10 +118,10 @@ class AWSAppSyncMultiAuthClientsTests: XCTestCase {
let upVote = UpvotePostMutation(id: createdId!)
let upVoted = expectation(description: "Post upvoted")

iamAppSyncClient.perform(mutation: upVote, queue: AWSAppSyncMultiAuthClientsTests.mutationQueue) { result, error in
iamAppSyncClient.perform(mutation: upVote, queue: AWSAppSyncMultiAuthClientsTests.mutationQueue, resultHandler: { result, error in
XCTAssertNil(error)
upVoted.fulfill()
}
})

wait(for: [upVoted], timeout: AWSAppSyncMultiAuthClientsTests.networkOperationTimeout)
wait(for: [upVoteEventTriggered], timeout: AWSAppSyncMultiAuthClientsTests.networkOperationTimeout)
Expand All @@ -148,7 +148,7 @@ class AWSAppSyncMultiAuthClientsTests: XCTestCase {

let addPost = DefaultTestPostData.defaultCreatePostWithoutFileUsingParametersMutation
var createdId: GraphQLID?
iamAppSyncClient.perform(mutation: addPost, queue: AWSAppSyncMultiAuthClientsTests.mutationQueue) { result, error in
iamAppSyncClient.perform(mutation: addPost, queue: AWSAppSyncMultiAuthClientsTests.mutationQueue, resultHandler: { result, error in
XCTAssertNil(error)
XCTAssertNotNil(result?.data?.createPostWithoutFileUsingParameters?.id)
createdId = result!.data!.createPostWithoutFileUsingParameters!.id
Expand All @@ -157,7 +157,7 @@ class AWSAppSyncMultiAuthClientsTests: XCTestCase {
DefaultTestPostData.author
)
postCreated.fulfill()
}
})

wait(for: [postCreated], timeout: AWSAppSyncMultiAuthClientsTests.networkOperationTimeout)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,24 @@ class SubscriptionStressTestHelper: XCTestCase {
allPostsAreCreatedExpectations.append(testData.postCreated)

appSyncClient.perform(mutation: DefaultTestPostData.defaultCreatePostWithoutFileUsingParametersMutation,
queue: SubscriptionStressTestHelper.mutationQueue) { result, error in
queue: SubscriptionStressTestHelper.mutationQueue, resultHandler: { result, error in
XCTAssertNil(error, "Error should be nil")

guard
let result = result,
let payload = result.data?.createPostWithoutFileUsingParameters
else {
XCTFail("Result & payload should not be nil")
return
else {
XCTFail("Result & payload should not be nil")
return
}

XCTAssertEqual(payload.author, DefaultTestPostData.author, "Authors should match.")
let id = payload.id
self.subscriptionTestStateHolders[id] = testData
testData.postId = id
testData.postCreated.fulfill()
print("Post created \(i) (\(id))")
}
})
}

wait(for: allPostsAreCreatedExpectations, timeout: SubscriptionStressTestHelper.networkOperationTimeout)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ class MutationQueuePerformanceTests: XCTestCase {

operationQueue.addOperation {
let semaphore = DispatchSemaphore(value: numberOfMutations)
appSyncClient.perform(mutation: mutation, queue: self.mutationHandlerQueue) { _, _ in
appSyncClient.perform(mutation: mutation, queue: self.mutationHandlerQueue, resultHandler: { _, _ in
semaphore.signal()
}
})
let _ = semaphore.wait(timeout: DispatchTime.now() + .seconds(60))
}
}
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ also includes support for offline operations.

### Misc. Updates

- Refactored `AWSAppSyncHTTPNetworkTransport` to simplify internal auth handling, and to support providing your own `URLSession` in the new initializer. ([PR #422](https://github.com/awslabs/aws-mobile-appsync-sdk-ios/pull/422)). Thanks for the initial PR, @lesmuc! 🎉
- Update dependencies. ([PR #436](https://github.com/awslabs/aws-mobile-appsync-sdk-ios/pull/436))

## 3.1.10
Expand Down

0 comments on commit e62fab2

Please sign in to comment.