Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
Add test for completion block in async write
Browse files Browse the repository at this point in the history
  • Loading branch information
Luigi Parpinel committed Feb 6, 2019
1 parent 65fa6d2 commit 966b8f2
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion Example/Tests/DataStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ class DataStoreTests: XCTestCase {
testWriteAsyncCalledFromBkgThreadIsAsyncOnBackgroundThread(skopelosChildDataStore)
}

// MARK: Writings Async - completion block
func testWriteAsyncInMagicalRecordCallsCompletion() {
testWriteAsyncCalledFromMainThreadIsAsyncOnBackgroundThread(magicalRecordDataStore)
}

func testWriteAsyncInSkopelosCallsCompletion() {
testWriteAsyncCallsCompletion(skopelosDataStore)
}

// MARK: Private

fileprivate func testReadCalledFromMainThreadIsSyncOnMainThread(_ dataStore: DataStore) {
Expand Down Expand Up @@ -215,7 +224,36 @@ class DataStoreTests: XCTestCase {
//XCTAssertEqual(stepSequence[1], 0)

}


fileprivate func testWriteAsyncCallsCompletion(_ dataStore: DataStore) {

let asyncExpectation = expectation(description: "thread safety expectation")
let completionExpectation = expectation(description: "completion expectation")

/**
* stepSuquence logic is commented-out as the execution of the block (ultimately a 'performBlock'/'perform' in CoreData
* is not guaranteed to happen on a dispatched block and might be executed before the line following the block
*/
//var stepSequence: [Int] = []
let writeBlock: (DataStoreReadWriteAccessor) -> Void = { accessor in

XCTAssertFalse(Thread.current.isMainThread)
//stepSequence.append(0)
asyncExpectation.fulfill()
}

dataStore.writeAsync(writeBlock) {
completionExpectation.fulfill()
}

//stepSequence.append(1)
wait(for: [asyncExpectation, completionExpectation], timeout: DataStoreTestsConsts.UnitTestTimeout)

//XCTAssertEqual(stepSequence[0], 1)
//XCTAssertEqual(stepSequence[1], 0)

}

fileprivate func testReadCalledFromBkgThreadIsSyncOnMainThread(_ dataStore: DataStore) {

let asyncExpectation = expectation(description: "thread safety expectation")
Expand Down

0 comments on commit 966b8f2

Please sign in to comment.