Skip to content

Commit

Permalink
chore: migrate unit test to use await fulfillment (#3703)
Browse files Browse the repository at this point in the history
  • Loading branch information
phantumcode authored May 23, 2024
1 parent 1c97ee5 commit 63b4c1b
Show file tree
Hide file tree
Showing 56 changed files with 687 additions and 585 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AWSAPICategoryPluginGraphQLBehaviorTests: AWSAPICategoryPluginTestBase {

// MARK: Query API Tests

func testQuery() {
func testQuery() async {
let operationFinished = expectation(description: "Operation should finish")
let request = GraphQLRequest<JSONValue>(apiName: apiName,
document: testDocument,
Expand All @@ -39,12 +39,12 @@ class AWSAPICategoryPluginGraphQLBehaviorTests: AWSAPICategoryPluginTestBase {
XCTAssertEqual(operationRequest.operationType, GraphQLOperationType.query)
XCTAssertNotNil(operationRequest.options)
XCTAssertNil(operationRequest.variables)
waitForExpectations(timeout: 1)
await fulfillment(of: [operationFinished], timeout: 1)
}

// MARK: Mutate API Tests

func testMutate() {
func testMutate() async {
let operationFinished = expectation(description: "Operation should finish")
let request = GraphQLRequest(apiName: apiName,
document: testDocument,
Expand All @@ -68,12 +68,12 @@ class AWSAPICategoryPluginGraphQLBehaviorTests: AWSAPICategoryPluginTestBase {
XCTAssertEqual(operationRequest.operationType, GraphQLOperationType.mutation)
XCTAssertNotNil(operationRequest.options)
XCTAssertNil(operationRequest.variables)
waitForExpectations(timeout: 1)
await fulfillment(of: [operationFinished], timeout: 1)
}

// MARK: Subscribe API Tests

func testSubscribe() {
func testSubscribe() async {
let operationFinished = expectation(description: "Operation should finish")
let request = GraphQLRequest(apiName: apiName,
document: testDocument,
Expand All @@ -97,6 +97,6 @@ class AWSAPICategoryPluginGraphQLBehaviorTests: AWSAPICategoryPluginTestBase {
XCTAssertEqual(operationRequest.operationType, GraphQLOperationType.subscription)
XCTAssertNotNil(operationRequest.options)
XCTAssertNil(operationRequest.variables)
waitForExpectations(timeout: 1)
await fulfillment(of: [operationFinished], timeout: 1)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class AWSAPICategoryPluginRESTClientBehaviorTests: AWSAPICategoryPluginTestBase

// MARK: Get API tests

func testGet() {
func testGet() async {
let operationFinished = expectation(description: "Operation should finish")
let request = RESTRequest(apiName: apiName, path: testPath)
let operation = apiPlugin.get(request: request) { _ in
Expand All @@ -38,12 +38,12 @@ class AWSAPICategoryPluginRESTClientBehaviorTests: AWSAPICategoryPluginTestBase
XCTAssertEqual(operationRequest.operationType, RESTOperationType.get)
XCTAssertNotNil(operationRequest.options)
XCTAssertNotNil(operationRequest.path)
waitForExpectations(timeout: 1)
await fulfillment(of: [operationFinished], timeout: 1)
}

// MARK: Post API tests

func testPost() {
func testPost() async {
let operationFinished = expectation(description: "Operation should finish")
let request = RESTRequest(apiName: apiName, path: testPath, body: testBody)
let operation = apiPlugin.post(request: request) { _ in
Expand All @@ -65,7 +65,7 @@ class AWSAPICategoryPluginRESTClientBehaviorTests: AWSAPICategoryPluginTestBase
XCTAssertEqual(operationRequest.operationType, RESTOperationType.post)
XCTAssertNotNil(operationRequest.options)
XCTAssertNotNil(operationRequest.path)
waitForExpectations(timeout: 1)
await fulfillment(of: [operationFinished], timeout: 1)
}

// MARK: Put API tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AWSRESTOperationTests: OperationTestBase {
}

// TODO: Fix this test
func testGetReturnsOperation() throws {
func testGetReturnsOperation() async throws {
try setUpPlugin(endpointType: .rest)

// Use this as a semaphore to ensure the task is cleaned up before proceeding to the next test
Expand All @@ -49,11 +49,10 @@ class AWSRESTOperationTests: OperationTestBase {
}

XCTAssertNotNil(operation.request)

waitForExpectations(timeout: 1.00)
await fulfillment(of: [listenerWasInvoked], timeout: 1)
}

func testGetFailsWithBadAPIName() throws {
func testGetFailsWithBadAPIName() async throws {
let sentData = Data([0x00, 0x01, 0x02, 0x03])
try setUpPluginForSingleResponse(sending: sentData, for: .rest)

Expand All @@ -71,7 +70,7 @@ class AWSRESTOperationTests: OperationTestBase {
}
}

waitForExpectations(timeout: 1.00)
await fulfillment(of: [receivedFailure, receivedSuccess], timeout: 1)
}

/// - Given: A configured plugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import XCTest
class GraphQLMutateCombineTests: OperationTestBase {
let testDocument = "mutate { updateTodo { id name description }}"

func testMutateSucceeds() throws {
func testMutateSucceeds() async throws {
let testJSONData: JSONValue = ["foo": true]
let sentData = Data(#"{"data": {"foo": true}}"#.utf8)
try setUpPluginForSingleResponse(sending: sentData, for: .graphQL)
Expand Down Expand Up @@ -46,12 +46,11 @@ class GraphQLMutateCombineTests: OperationTestBase {
receivedResponseError.fulfill()
}
})

waitForExpectations(timeout: 0.05)
await fulfillment(of: [receivedValue, receivedFinish, receivedFailure, receivedResponseError], timeout: 0.05)
sink.cancel()
}

func testMutateHandlesResponseError() throws {
func testMutateHandlesResponseError() async throws {
let sentData = Data(#"{"data": {"foo": true}, "errors": []}"#.utf8)
try setUpPluginForSingleResponse(sending: sentData, for: .graphQL)

Expand Down Expand Up @@ -82,12 +81,12 @@ class GraphQLMutateCombineTests: OperationTestBase {
}
})

waitForExpectations(timeout: 0.05)
await fulfillment(of: [receivedValue, receivedFinish, receivedFailure, receivedResponseError], timeout: 0.05)
sink.cancel()

}

func testMutateFails() throws {
func testMutateFails() async throws {
try setUpPluginForSingleError(for: .graphQL)

let request = GraphQLRequest(document: testDocument, variables: nil, responseType: JSONValue.self)
Expand Down Expand Up @@ -118,7 +117,7 @@ class GraphQLMutateCombineTests: OperationTestBase {
}
})

waitForExpectations(timeout: 1.0)
await fulfillment(of: [receivedValue, receivedFinish, receivedFailure, receivedResponseError], timeout: 1)
sink.cancel()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import XCTest
class GraphQLQueryCombineTests: OperationTestBase {
let testDocument = "query { getTodo { id name description }}"

func testQuerySucceeds() throws {
func testQuerySucceeds() async throws {
let testJSONData: JSONValue = ["foo": true]
let sentData = Data(#"{"data": {"foo": true}}"#.utf8)
try setUpPluginForSingleResponse(sending: sentData, for: .graphQL)
Expand Down Expand Up @@ -47,11 +47,16 @@ class GraphQLQueryCombineTests: OperationTestBase {
}
})

waitForExpectations(timeout: 0.05)
await fulfillment(of: [receivedValue,
receivedFinish,
receivedFailure,
receivedResponseError
],
timeout: 0.05)
sink.cancel()
}

func testQueryHandlesResponseError() throws {
func testQueryHandlesResponseError() async throws {
let sentData = Data(#"{"data": {"foo": true}, "errors": []}"#.utf8)
try setUpPluginForSingleResponse(sending: sentData, for: .graphQL)

Expand Down Expand Up @@ -82,12 +87,17 @@ class GraphQLQueryCombineTests: OperationTestBase {
}
})

waitForExpectations(timeout: 0.05)
await fulfillment(of: [receivedValue,
receivedFinish,
receivedFailure,
receivedResponseError
],
timeout: 0.05)
sink.cancel()

}

func testQueryFails() throws {
func testQueryFails() async throws {
try setUpPluginForSingleError(for: .graphQL)

let request = GraphQLRequest(document: testDocument, variables: nil, responseType: JSONValue.self)
Expand Down Expand Up @@ -118,7 +128,12 @@ class GraphQLQueryCombineTests: OperationTestBase {
}
})

waitForExpectations(timeout: 0.05)
await fulfillment(of: [receivedValue,
receivedFinish,
receivedFailure,
receivedResponseError
],
timeout: 0.05)
sink.cancel()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,14 @@ class GraphQLSubscribeTests: OperationTestBase {
mockAppSyncRealTimeClient.triggerEvent(.data(testData))
mockAppSyncRealTimeClient.triggerEvent(.unsubscribed)

await waitForExpectations(timeout: 0.05)
await fulfillment(of: [receivedCompletionFinish,
receivedCompletionFailure,
receivedConnected,
receivedDisconnected,
receivedSubscriptionEventData,
receivedSubscriptionEventError
],
timeout: 0.05)
}

/// Lifecycle test
Expand Down Expand Up @@ -131,7 +138,14 @@ class GraphQLSubscribeTests: OperationTestBase {
try await MockAppSyncRealTimeClient.waitForSubscirbed()
mockAppSyncRealTimeClient.triggerEvent(.unsubscribed)

await waitForExpectations(timeout: 0.05)
await fulfillment(of: [receivedCompletionFinish,
receivedCompletionFailure,
receivedConnected,
receivedDisconnected,
receivedSubscriptionEventData,
receivedSubscriptionEventError
],
timeout: 0.05)
}

/// Lifecycle test
Expand All @@ -158,7 +172,14 @@ class GraphQLSubscribeTests: OperationTestBase {
try await MockAppSyncRealTimeClient.waitForSubscirbing()
mockAppSyncRealTimeClient.triggerEvent(.error(["Error"]))

await waitForExpectations(timeout: 0.05)
await fulfillment(of: [receivedCompletionFinish,
receivedCompletionFailure,
receivedConnected,
receivedDisconnected,
receivedSubscriptionEventData,
receivedSubscriptionEventError
],
timeout: 0.05)
}

/// Lifecycle test
Expand Down Expand Up @@ -193,7 +214,14 @@ class GraphQLSubscribeTests: OperationTestBase {
mockAppSyncRealTimeClient.triggerEvent(.data(testData))
mockAppSyncRealTimeClient.triggerEvent(.unsubscribed)

await waitForExpectations(timeout: 0.05)
await fulfillment(of: [receivedCompletionFinish,
receivedCompletionFailure,
receivedConnected,
receivedDisconnected,
receivedSubscriptionEventData,
receivedSubscriptionEventError
],
timeout: 0.05)
}

func testMultipleSuccessValues() async throws {
Expand All @@ -220,7 +248,14 @@ class GraphQLSubscribeTests: OperationTestBase {
mockAppSyncRealTimeClient.triggerEvent(.data(testData))
mockAppSyncRealTimeClient.triggerEvent(.unsubscribed)

await waitForExpectations(timeout: 0.05)
await fulfillment(of: [receivedCompletionFinish,
receivedCompletionFailure,
receivedConnected,
receivedDisconnected,
receivedSubscriptionEventData,
receivedSubscriptionEventError
],
timeout: 0.05)
}

func testMixedSuccessAndErrorValues() async throws {
Expand Down Expand Up @@ -253,7 +288,14 @@ class GraphQLSubscribeTests: OperationTestBase {
mockAppSyncRealTimeClient.triggerEvent(.data(successfulTestData))
mockAppSyncRealTimeClient.triggerEvent(.unsubscribed)

await waitForExpectations(timeout: 0.05)
await fulfillment(of: [receivedCompletionFinish,
receivedCompletionFailure,
receivedConnected,
receivedDisconnected,
receivedSubscriptionEventData,
receivedSubscriptionEventError
],
timeout: 0.05)
}

// MARK: - Utilities
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import XCTest

class RESTCombineTests: OperationTestBase {

func testGetSucceeds() throws {
func testGetSucceeds() async throws {
let sentData = Data([0x00, 0x01, 0x02, 0x03])
try setUpPluginForSingleResponse(sending: sentData, for: .graphQL)

Expand All @@ -38,11 +38,11 @@ class RESTCombineTests: OperationTestBase {
receivedValue.fulfill()
})

waitForExpectations(timeout: 0.05)
await fulfillment(of: [receivedValue, receivedFinish, receivedFailure], timeout: 0.05)
sink.cancel()
}

func testGetFails() throws {
func testGetFails() async throws {
let sentData = Data([0x00, 0x01, 0x02, 0x03])
try setUpPluginForSingleError(for: .graphQL)

Expand All @@ -68,7 +68,7 @@ class RESTCombineTests: OperationTestBase {
receivedValue.fulfill()
})

waitForExpectations(timeout: 0.05)
await fulfillment(of: [receivedValue, receivedFinish, receivedFailure], timeout: 0.05)
sink.cancel()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class NetworkReachabilityNotifierTests: XCTestCase {
MockReachability.iConnection = .wifi
}

func testWifiConnectivity() {
func testWifiConnectivity() async {
MockReachability.iConnection = .wifi
let expect = expectation(description: ".sink receives values")
var values = [Bool]()
Expand All @@ -45,11 +45,11 @@ class NetworkReachabilityNotifierTests: XCTestCase {
notification = Notification.init(name: .reachabilityChanged)
NotificationCenter.default.post(notification)

waitForExpectations(timeout: 1.0)
await fulfillment(of: [expect], timeout: 1)
cancellable.cancel()
}

func testCellularConnectivity() {
func testCellularConnectivity() async {
MockReachability.iConnection = .wifi
let expect = expectation(description: ".sink receives values")
var values = [Bool]()
Expand All @@ -67,11 +67,11 @@ class NetworkReachabilityNotifierTests: XCTestCase {
notification = Notification.init(name: .reachabilityChanged)
NotificationCenter.default.post(notification)

waitForExpectations(timeout: 1.0)
await fulfillment(of: [expect], timeout: 1)
cancellable.cancel()
}

func testNoConnectivity() {
func testNoConnectivity() async {
MockReachability.iConnection = .unavailable
let expect = expectation(description: ".sink receives values")
var values = [Bool]()
Expand All @@ -89,7 +89,7 @@ class NetworkReachabilityNotifierTests: XCTestCase {
notification = Notification.init(name: .reachabilityChanged)
NotificationCenter.default.post(notification)

waitForExpectations(timeout: 1.0)
await fulfillment(of: [expect], timeout: 1)
cancellable.cancel()
}

Expand Down
Loading

0 comments on commit 63b4c1b

Please sign in to comment.