-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Bug where subscription connections happen at the same time (#389)
- Loading branch information
Showing
7 changed files
with
168 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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 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
75 changes: 75 additions & 0 deletions
75
...eCategoryPluginTests/Sync/SubscriptionSync/AWSIncomingEventReconciliationQueueTests.swift
This file contains 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,75 @@ | ||
// | ||
// Copyright 2018-2020 Amazon.com, | ||
// Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
import XCTest | ||
|
||
@testable import Amplify | ||
@testable import AmplifyTestCommon | ||
@testable import AWSPluginsCore | ||
@testable import AWSDataStoreCategoryPlugin | ||
|
||
|
||
class AWSIncomingEventReconciliationQueueTests: XCTestCase { | ||
var storageAdapter: MockSQLiteStorageEngineAdapter! | ||
var apiPlugin: MockAPICategoryPlugin! | ||
|
||
override func setUp() { | ||
MockModelReconciliationQueue.reset() | ||
storageAdapter = MockSQLiteStorageEngineAdapter() | ||
storageAdapter.returnOnQuery(dataStoreResult: .none) | ||
storageAdapter.returnOnSave(dataStoreResult: .none) | ||
|
||
apiPlugin = MockAPICategoryPlugin() | ||
|
||
} | ||
var operationQueue: OperationQueue! | ||
|
||
//This test case attempts to hit a race condition, and may be required to execute multiple times | ||
// in order to demonstrate the bug | ||
func testTwoConnectionStatusUpdatesAtSameTime() { | ||
let expectInitialized = expectation(description: "eventQueue expected to send out initialized state") | ||
|
||
let modelReconciliationQueueFactory | ||
= MockModelReconciliationQueue.init(modelType:storageAdapter:api:incomingSubscriptionEvents:) | ||
let eventQueue = AWSIncomingEventReconciliationQueue( | ||
modelTypes: [Post.self, Comment.self], | ||
api: apiPlugin, | ||
storageAdapter: storageAdapter, | ||
modelReconciliationQueueFactory: modelReconciliationQueueFactory) | ||
eventQueue.start() | ||
|
||
let eventSync = eventQueue.publisher.sink(receiveCompletion: { _ in | ||
XCTFail("Not expecting this to call") | ||
}, receiveValue: { event in | ||
switch event { | ||
case .initialized: | ||
expectInitialized.fulfill() | ||
default: | ||
XCTFail("Should not expect any other state") | ||
} | ||
}) | ||
|
||
operationQueue = OperationQueue() | ||
operationQueue.name = "com.amazonaws.DataStore.UnitTestQueue" | ||
operationQueue.maxConcurrentOperationCount = 2 | ||
operationQueue.underlyingQueue = DispatchQueue.global() | ||
operationQueue.isSuspended = true | ||
|
||
let reconciliationQueues = MockModelReconciliationQueue.mockModelReconciliationQueues | ||
for (queueName, queue) in reconciliationQueues { | ||
let cancellableOperation = CancelAwareBlockOperation { | ||
queue.modelReconciliationQueueSubject.send(.connected(queueName)) | ||
} | ||
operationQueue.addOperation(cancellableOperation) | ||
} | ||
operationQueue.isSuspended = false | ||
waitForExpectations(timeout: 2) | ||
|
||
} | ||
} | ||
|
52 changes: 52 additions & 0 deletions
52
...aStoreCategoryPluginTests/Sync/SubscriptionSync/Support/MockModelReconciliatinQueue.swift
This file contains 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,52 @@ | ||
// | ||
// Copyright 2018-2020 Amazon.com, | ||
// Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
|
||
@testable import Amplify | ||
@testable import AmplifyTestCommon | ||
@testable import AWSPluginsCore | ||
@testable import AWSDataStoreCategoryPlugin | ||
class MockModelReconciliationQueue: ModelReconciliationQueue { | ||
|
||
public static var mockModelReconciliationQueues: [String: MockModelReconciliationQueue] = [:] | ||
|
||
private let modelType: Model.Type | ||
let modelReconciliationQueueSubject: PassthroughSubject<ModelReconciliationQueueEvent, DataStoreError> | ||
var publisher: AnyPublisher<ModelReconciliationQueueEvent, DataStoreError> { | ||
return modelReconciliationQueueSubject.eraseToAnyPublisher() | ||
} | ||
|
||
init(modelType: Model.Type, | ||
storageAdapter: StorageEngineAdapter?, | ||
api: APICategoryGraphQLBehavior, | ||
incomingSubscriptionEvents: IncomingSubscriptionEventPublisher? = nil) { | ||
self.modelReconciliationQueueSubject = PassthroughSubject<ModelReconciliationQueueEvent, DataStoreError>() | ||
self.modelType = modelType | ||
MockModelReconciliationQueue.mockModelReconciliationQueues[modelType.modelName] = self | ||
} | ||
|
||
func start() { | ||
//no-op | ||
} | ||
func pause() { | ||
//no-op | ||
} | ||
|
||
func cancel() { | ||
//no-op | ||
} | ||
|
||
func enqueue(_ remoteModel: MutationSync<AnyModel>) { | ||
//no-op | ||
} | ||
|
||
static func reset() { | ||
MockModelReconciliationQueue.mockModelReconciliationQueues = [:] | ||
} | ||
} |
This file contains 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 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