Skip to content

Commit

Permalink
fix: unit tests fulfillment
Browse files Browse the repository at this point in the history
  • Loading branch information
atierian committed Oct 13, 2023
1 parent 0295c08 commit df31bad
Show file tree
Hide file tree
Showing 82 changed files with 679 additions and 590 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class AWSAPICategoryPluginReachabilityTests: XCTestCase {
concurrentPerformCompleted.fulfill()

}
await fulfillment(of: [concurrentPerformCompleted], timeout: 1)
wait(for: [concurrentPerformCompleted], timeout: 1)
XCTAssertEqual(apiPlugin.reachabilityMap.count, 2)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class AWSGraphQLOperationTests: AWSAPICategoryPluginTestBase {
} receiveValue: { _ in }
defer { sink.cancel() }

await fulfillment(of: [receivedCompletion], timeout: 1)
wait(for: [receivedCompletion], timeout: 1)
let task = operation.mapper.task(for: operation)
XCTAssertNil(task)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class AWSGraphQLSubscriptionOperationCancelTests: XCTestCase {
completionListener: nil
)
await fulfillment(
of: [receivedValue],
of: [receivedValue, connectionCreation],
timeout: 5
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ class AWSGraphQLSubscriptionTaskRunnerCancelTests: XCTestCase {
receivedFailure.fulfill()
}
}
await fulfillment(of: [receivedValueConnecting])
await fulfillment(of: [receivedValueConnecting], timeout: 1)
subscriptionEvents.cancel()
await fulfillment(of: [receivedValueDisconnected, receivedCompletion, receivedFailure])
await fulfillment(of: [receivedValueDisconnected, receivedCompletion, receivedFailure], timeout: 1)
}

func testFailureOnConnection() async {
Expand Down Expand Up @@ -171,8 +171,12 @@ class AWSGraphQLSubscriptionTaskRunnerCancelTests: XCTestCase {
responseType: JSONValue.self)

let receivedValue = expectation(description: "Received value for connecting")
receivedValue.expectedFulfillmentCount = 1
receivedValue.assertForOverFulfill = false

let receivedFailure = expectation(description: "Received failure")
receivedFailure.isInverted = true

let receivedCompletion = expectation(description: "Received completion")

let subscriptionEvents = apiPlugin.subscribe(request: request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class AWSRESTOperationTests: OperationTestBase {
callbackInvoked.fulfill()
}

await fulfillment(of: [callbackInvoked], timeout: 1.0)
wait(for: [callbackInvoked], timeout: 1.0)
}

func testRESTOperation_withCustomHeader_shouldOverrideDefaultAmplifyHeaders() throws {
Expand All @@ -118,7 +118,7 @@ class AWSRESTOperationTests: OperationTestBase {
}
callbackInvoked.fulfill()
}
await fulfillment(of: [callbackInvoked, validated], timeout: 1.0)
wait(for: [callbackInvoked, validated], timeout: 1.0)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ class GraphQLSubscribeCombineTests: OperationTestBase {
var sink: AnyCancellable?

// Setup expectations
var onSubscribeInvoked: AsyncExpectation!
var receivedCompletionSuccess: AsyncExpectation!
var receivedCompletionFailure: AsyncExpectation!
var onSubscribeInvoked: XCTestExpectation!
var receivedCompletionSuccess: XCTestExpectation!
var receivedCompletionFailure: XCTestExpectation!

// Subscription state expectations
var receivedStateValueConnecting: AsyncExpectation!
var receivedStateValueConnected: AsyncExpectation!
var receivedStateValueDisconnected: AsyncExpectation!
var receivedStateValueConnecting: XCTestExpectation!
var receivedStateValueConnected: XCTestExpectation!
var receivedStateValueDisconnected: XCTestExpectation!

// Subscription item expectations
var receivedDataValueSuccess: AsyncExpectation!
var receivedDataValueError: AsyncExpectation!
var receivedDataValueSuccess: XCTestExpectation!
var receivedDataValueError: XCTestExpectation!

// Handles to the subscription item and event handler used to make mock calls into the
// subscription system
Expand Down Expand Up @@ -68,14 +68,8 @@ class GraphQLSubscribeCombineTests: OperationTestBase {
}

func testHappyPath() async throws {
await receivedCompletionSuccess.setShouldTrigger(true)
await receivedCompletionFailure.setShouldTrigger(false)
await receivedStateValueConnecting.setShouldTrigger(true)
await receivedStateValueConnected.setShouldTrigger(true)
await receivedStateValueDisconnected.setShouldTrigger(true)

await receivedDataValueSuccess.setShouldTrigger(true)
await receivedDataValueError.setShouldTrigger(false)
receivedCompletionFailure.isInverted = true
receivedDataValueError.isInverted = true

let testJSON: JSONValue = ["foo": true]
let testData = #"{"data": {"foo": true}}"# .data(using: .utf8)!
Expand All @@ -92,14 +86,9 @@ class GraphQLSubscribeCombineTests: OperationTestBase {
}

func testConnectionWithNoData() async throws {
await receivedCompletionSuccess.setShouldTrigger(true)
await receivedCompletionFailure.setShouldTrigger(false)
await receivedStateValueConnecting.setShouldTrigger(true)
await receivedStateValueConnected.setShouldTrigger(true)
await receivedStateValueDisconnected.setShouldTrigger(true)

await receivedDataValueSuccess.setShouldTrigger(false)
await receivedDataValueError.setShouldTrigger(false)
receivedCompletionFailure.isInverted = true
receivedDataValueSuccess.isInverted = true
receivedDataValueError.isInverted = true

try await subscribe()
await fulfillment(of: [onSubscribeInvoked], timeout: 0.05)
Expand All @@ -112,14 +101,11 @@ class GraphQLSubscribeCombineTests: OperationTestBase {
}

func testConnectionError() async throws {
await receivedCompletionSuccess.setShouldTrigger(false)
await receivedCompletionFailure.setShouldTrigger(true)
await receivedStateValueConnecting.setShouldTrigger(true)
await receivedStateValueConnected.setShouldTrigger(false)
await receivedStateValueDisconnected.setShouldTrigger(false)

await receivedDataValueSuccess.setShouldTrigger(false)
await receivedDataValueError.setShouldTrigger(false)
receivedCompletionSuccess.isInverted = true
receivedStateValueConnected.isInverted = true
receivedStateValueDisconnected.isInverted = true
receivedDataValueSuccess.isInverted = true
receivedDataValueError.isInverted = true

try await subscribe()
await fulfillment(of: [onSubscribeInvoked], timeout: 0.05)
Expand All @@ -132,14 +118,8 @@ class GraphQLSubscribeCombineTests: OperationTestBase {

func testDecodingError() async throws {
let testData = #"{"data": {"foo": true}, "errors": []}"# .data(using: .utf8)!
await receivedCompletionSuccess.setShouldTrigger(true)
await receivedCompletionFailure.setShouldTrigger(false)
await receivedStateValueConnecting.setShouldTrigger(true)
await receivedStateValueConnected.setShouldTrigger(true)
await receivedStateValueDisconnected.setShouldTrigger(true)

await receivedDataValueSuccess.setShouldTrigger(false)
await receivedDataValueError.setShouldTrigger(true)
receivedCompletionFailure.isInverted = true
receivedDataValueSuccess.isInverted = true

try await subscribe()
await fulfillment(of: [onSubscribeInvoked], timeout: 0.05)
Expand All @@ -155,15 +135,9 @@ class GraphQLSubscribeCombineTests: OperationTestBase {
func testMultipleSuccessValues() async throws {
let testJSON: JSONValue = ["foo": true]
let testData = #"{"data": {"foo": true}}"# .data(using: .utf8)!
await receivedCompletionSuccess.setShouldTrigger(true)
await receivedCompletionFailure.setShouldTrigger(false)
await receivedStateValueConnecting.setShouldTrigger(true)
await receivedStateValueConnected.setShouldTrigger(true)
await receivedStateValueDisconnected.setShouldTrigger(true)

await receivedDataValueSuccess.setShouldTrigger(true)
await receivedDataValueSuccess.setExpectedFulfillmentCount(2)
await receivedDataValueError.setShouldTrigger(false)
receivedCompletionFailure.isInverted = true
receivedDataValueError.isInverted = true
receivedDataValueSuccess.expectedFulfillmentCount = 2

try await subscribe(expecting: testJSON)
await fulfillment(of: [onSubscribeInvoked], timeout: 0.05)
Expand All @@ -180,15 +154,8 @@ class GraphQLSubscribeCombineTests: OperationTestBase {
func testMixedSuccessAndErrorValues() async throws {
let successfulTestData = #"{"data": {"foo": true}}"# .data(using: .utf8)!
let invalidTestData = #"{"data": {"foo": true}, "errors": []}"# .data(using: .utf8)!
await receivedCompletionSuccess.setShouldTrigger(true)
await receivedCompletionFailure.setShouldTrigger(false)
await receivedStateValueConnecting.setShouldTrigger(true)
await receivedStateValueConnected.setShouldTrigger(true)
await receivedStateValueDisconnected.setShouldTrigger(true)

await receivedDataValueSuccess.setShouldTrigger(true)
await receivedDataValueSuccess.setExpectedFulfillmentCount(2)
await receivedDataValueError.setShouldTrigger(true)
receivedCompletionFailure.isInverted = true
receivedDataValueSuccess.expectedFulfillmentCount = 2

try await subscribe()
await fulfillment(of: [onSubscribeInvoked], timeout: 0.05)
Expand Down Expand Up @@ -219,7 +186,7 @@ class GraphQLSubscribeCombineTests: OperationTestBase {

self.subscriptionItem = item
self.subscriptionEventHandler = eventHandler
Task { await self.onSubscribeInvoked.fulfill() }
self.onSubscribeInvoked.fulfill()
return item
}

Expand Down Expand Up @@ -247,30 +214,30 @@ class GraphQLSubscribeCombineTests: OperationTestBase {
sink = Amplify.Publisher.create(subscription).sink { completion in
switch completion {
case .failure:
Task { await self.receivedCompletionFailure.fulfill() }
self.receivedCompletionFailure.fulfill()
case .finished:
Task { await self.receivedCompletionSuccess.fulfill() }
self.receivedCompletionSuccess.fulfill()
}
} receiveValue: { subscriptionEvent in
switch subscriptionEvent {
case .connection(let connectionState):
switch connectionState {
case .connecting:
Task { await self.receivedStateValueConnecting.fulfill() }
self.receivedStateValueConnecting.fulfill()
case .connected:
Task { await self.receivedStateValueConnected.fulfill() }
self.receivedStateValueConnected.fulfill()
case .disconnected:
Task { await self.receivedStateValueDisconnected.fulfill() }
self.receivedStateValueDisconnected.fulfill()
}
case .data(let result):
switch result {
case .success(let actualValue):
if let expectedValue = expectedValue {
XCTAssertEqual(actualValue, expectedValue)
}
Task { await self.receivedDataValueSuccess.fulfill() }
self.receivedDataValueSuccess.fulfill()
case .failure:
Task { await self.receivedDataValueError.fulfill() }
self.receivedDataValueError.fulfill()
}
}
}
Expand Down
Loading

0 comments on commit df31bad

Please sign in to comment.