From 5119bf188353abc11fcf2dbe972bdc22e0df5185 Mon Sep 17 00:00:00 2001 From: Tobias Due Munk Date: Sat, 11 Jul 2015 20:43:56 +0200 Subject: [PATCH 1/4] Update example project for iOS --- .../AsyncExample_iOSTests.swift | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/AsyncExample/AsyncExample iOSTests/AsyncExample_iOSTests.swift b/AsyncExample/AsyncExample iOSTests/AsyncExample_iOSTests.swift index 6c4a313..e6fd48e 100644 --- a/AsyncExample/AsyncExample iOSTests/AsyncExample_iOSTests.swift +++ b/AsyncExample/AsyncExample iOSTests/AsyncExample_iOSTests.swift @@ -30,10 +30,10 @@ class AsyncExample_iOSTests: XCTestCase { let expectation = expectationWithDescription("Expected after time") let qos = QOS_CLASS_BACKGROUND - let queue = dispatch_get_global_queue(qos.id, 0) + let queue = dispatch_get_global_queue(qos, 0) dispatch_async(queue) { let currentQos = qos_class_self() - XCTAssertEqual(currentQos.id, qos.id, "On \(currentQos.description) (expected \(qos.description))") + XCTAssertEqual(currentQos, qos, "On \(currentQos.description) (expected \(qos.description))") expectation.fulfill() } waitForExpectationsWithTimeout(1, handler: nil) @@ -46,7 +46,7 @@ class AsyncExample_iOSTests: XCTestCase { let expectation = expectationWithDescription("Expected on main queue") var calledStuffAfterSinceAsync = false Async.main { - XCTAssertEqual(qos_class_self().id, qos_class_main().id, "On \(qos_class_self().description) (expexted \(qos_class_main().description))") + XCTAssertEqual(qos_class_self(), qos_class_main(), "On \(qos_class_self().description) (expexted \(qos_class_main().description))") XCTAssert(calledStuffAfterSinceAsync, "Should be async") expectation.fulfill() } @@ -57,7 +57,7 @@ class AsyncExample_iOSTests: XCTestCase { func testAsyncUserInteractive() { let expectation = expectationWithDescription("Expected On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") Async.userInteractive { - XCTAssertEqual(qos_class_self().id, QOS_CLASS_USER_INTERACTIVE.id, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") expectation.fulfill() } waitForExpectationsWithTimeout(1, handler: nil) @@ -66,7 +66,7 @@ class AsyncExample_iOSTests: XCTestCase { func testAsyncUserInitiared() { let expectation = expectationWithDescription("Expected On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") Async.userInitiated { - XCTAssertEqual(qos_class_self().id, QOS_CLASS_USER_INITIATED.id, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INITIATED, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") expectation.fulfill() } waitForExpectationsWithTimeout(1, handler: nil) @@ -75,7 +75,7 @@ class AsyncExample_iOSTests: XCTestCase { func testAsyncUtility() { let expectation = expectationWithDescription("Expected On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") Async.utility { - XCTAssertEqual(qos_class_self().id, QOS_CLASS_UTILITY.id, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") + XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") expectation.fulfill() } waitForExpectationsWithTimeout(1, handler: nil) @@ -84,7 +84,7 @@ class AsyncExample_iOSTests: XCTestCase { func testAsyncBackground() { let expectation = expectationWithDescription("Expected On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") Async.background { - XCTAssertEqual(qos_class_self().id, QOS_CLASS_BACKGROUND.id, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") + XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") expectation.fulfill() } waitForExpectationsWithTimeout(1, handler: nil) @@ -94,10 +94,10 @@ class AsyncExample_iOSTests: XCTestCase { let expectation = expectationWithDescription("Expected on background to main queue") var wasInBackground = false Async.background { - XCTAssertEqual(qos_class_self().id, QOS_CLASS_BACKGROUND.id, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") + XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") wasInBackground = true }.main { - XCTAssertEqual(qos_class_self().id, qos_class_main().id, "On \(qos_class_self().description) (expected \(qos_class_main().description))") + XCTAssertEqual(qos_class_self(), qos_class_main(), "On \(qos_class_self().description) (expected \(qos_class_main().description))") XCTAssert(wasInBackground, "Was in background first") expectation.fulfill() } @@ -108,19 +108,19 @@ class AsyncExample_iOSTests: XCTestCase { let expectation = expectationWithDescription("Expected On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") var id = 0 Async.main { - XCTAssertEqual(qos_class_self().id, qos_class_main().id, "On \(qos_class_self().description) (expexted \(qos_class_main().description))") + XCTAssertEqual(qos_class_self(), qos_class_main(), "On \(qos_class_self().description) (expexted \(qos_class_main().description))") XCTAssertEqual(++id, 1, "Count main queue") }.userInteractive { - XCTAssertEqual(qos_class_self().id, QOS_CLASS_USER_INTERACTIVE.id, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") XCTAssertEqual(++id, 2, "Count user interactive queue") }.userInitiated { - XCTAssertEqual(qos_class_self().id, QOS_CLASS_USER_INITIATED.id, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INITIATED, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") XCTAssertEqual(++id, 3, "Count user initiated queue") }.utility { - XCTAssertEqual(qos_class_self().id, QOS_CLASS_UTILITY.id, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") + XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") XCTAssertEqual(++id, 4, "Count utility queue") }.background { - XCTAssertEqual(qos_class_self().id, QOS_CLASS_BACKGROUND.id, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") + XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") XCTAssertEqual(++id, 5, "Count background queue") expectation.fulfill() } @@ -151,13 +151,13 @@ class AsyncExample_iOSTests: XCTestCase { let timeDelay = 1.0 let upperTimeDelay = timeDelay + 0.2 let time = dispatch_time(DISPATCH_TIME_NOW, Int64(timeDelay * Double(NSEC_PER_SEC))) - let queue = dispatch_get_global_queue(QOS_CLASS_BACKGROUND.id, 0) + let queue = dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0) dispatch_after(time, queue, { let timePassed = NSDate().timeIntervalSinceDate(date) println("\(timePassed)") XCTAssert(timePassed >= timeDelay, "Should wait \(timeDelay) seconds before firing") XCTAssert(timePassed < upperTimeDelay, "Shouldn't wait \(upperTimeDelay) seconds before firing") - XCTAssertEqual(qos_class_self().id, QOS_CLASS_BACKGROUND.id, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") + XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") expectation.fulfill() }) waitForExpectationsWithTimeout(timeDelay*2, handler: nil) @@ -172,7 +172,7 @@ class AsyncExample_iOSTests: XCTestCase { let timePassed = NSDate().timeIntervalSinceDate(date) XCTAssert(timePassed >= timeDelay, "Should wait \(timeDelay) seconds before firing") XCTAssert(timePassed < upperTimeDelay, "Shouldn't wait \(upperTimeDelay) seconds before firing") - XCTAssertEqual(qos_class_self().id, qos_class_main().id, "On main queue") + XCTAssertEqual(qos_class_self(), qos_class_main(), "On main queue") expectation.fulfill() } waitForExpectationsWithTimeout(timeDelay*2, handler: nil) @@ -193,7 +193,7 @@ class AsyncExample_iOSTests: XCTestCase { let timePassed = NSDate().timeIntervalSinceDate(date1) XCTAssert(timePassed >= timeDelay1, "Should wait \(timeDelay1) seconds before firing") XCTAssert(timePassed < upperTimeDelay1, "Shouldn't wait \(upperTimeDelay1) seconds before firing") - XCTAssertEqual(qos_class_self().id, QOS_CLASS_USER_INTERACTIVE.id, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") date2 = NSDate() // Update }.utility(after: timeDelay2) { @@ -202,7 +202,7 @@ class AsyncExample_iOSTests: XCTestCase { let timePassed = NSDate().timeIntervalSinceDate(date2) XCTAssert(timePassed >= timeDelay2, "Should wait \(timeDelay2) seconds before firing") XCTAssert(timePassed < upperTimeDelay2, "Shouldn't wait \(upperTimeDelay2) seconds before firing") - XCTAssertEqual(qos_class_self().id, QOS_CLASS_UTILITY.id, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") + XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") expectation.fulfill() } waitForExpectationsWithTimeout((timeDelay1 + timeDelay2) * 2, handler: nil) @@ -223,7 +223,7 @@ class AsyncExample_iOSTests: XCTestCase { let timePassed = NSDate().timeIntervalSinceDate(date1) XCTAssert(timePassed >= timeDelay1, "Should wait \(timeDelay1) seconds before firing") XCTAssert(timePassed < upperTimeDelay1, "Shouldn't wait \(upperTimeDelay1) seconds before firing") - XCTAssertEqual(qos_class_self().id, QOS_CLASS_USER_INTERACTIVE.id, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") date2 = NSDate() // Update }.userInteractive(after: timeDelay2) { @@ -232,7 +232,7 @@ class AsyncExample_iOSTests: XCTestCase { let timePassed = NSDate().timeIntervalSinceDate(date2) XCTAssert(timePassed >= timeDelay2, "Should wait \(timeDelay2) seconds before firing") XCTAssert(timePassed < upperTimeDelay2, "Shouldn't wait \(upperTimeDelay2) seconds before firing") - XCTAssertEqual(qos_class_self().id, QOS_CLASS_USER_INTERACTIVE.id, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") expectation.fulfill() } waitForExpectationsWithTimeout((timeDelay1 + timeDelay2) * 2, handler: nil) @@ -253,7 +253,7 @@ class AsyncExample_iOSTests: XCTestCase { let timePassed = NSDate().timeIntervalSinceDate(date1) XCTAssert(timePassed >= timeDelay1, "Should wait \(timeDelay1) seconds before firing") XCTAssert(timePassed < upperTimeDelay1, "Shouldn't wait \(upperTimeDelay1) seconds before firing") - XCTAssertEqual(qos_class_self().id, QOS_CLASS_USER_INITIATED.id, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INITIATED, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") date2 = NSDate() // Update }.userInitiated(after: timeDelay2) { @@ -262,7 +262,7 @@ class AsyncExample_iOSTests: XCTestCase { let timePassed = NSDate().timeIntervalSinceDate(date2) XCTAssert(timePassed >= timeDelay2, "Should wait \(timeDelay2) seconds before firing") XCTAssert(timePassed < upperTimeDelay2, "Shouldn't wait \(upperTimeDelay2) seconds before firing") - XCTAssertEqual(qos_class_self().id, QOS_CLASS_USER_INITIATED.id, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INITIATED, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") expectation.fulfill() } waitForExpectationsWithTimeout((timeDelay1 + timeDelay2) * 2, handler: nil) @@ -283,7 +283,7 @@ class AsyncExample_iOSTests: XCTestCase { let timePassed = NSDate().timeIntervalSinceDate(date1) XCTAssert(timePassed >= timeDelay1, "Should wait \(timeDelay1) seconds before firing") XCTAssert(timePassed < upperTimeDelay1, "Shouldn't wait \(upperTimeDelay1) seconds before firing") - XCTAssertEqual(qos_class_self().id, QOS_CLASS_UTILITY.id, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") + XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") date2 = NSDate() // Update }.utility(after: timeDelay2) { @@ -292,7 +292,7 @@ class AsyncExample_iOSTests: XCTestCase { let timePassed = NSDate().timeIntervalSinceDate(date2) XCTAssert(timePassed >= timeDelay2, "Should wait \(timeDelay2) seconds before firing") XCTAssert(timePassed < upperTimeDelay2, "Shouldn't wait \(upperTimeDelay2) seconds before firing") - XCTAssertEqual(qos_class_self().id, QOS_CLASS_UTILITY.id, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") + XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") expectation.fulfill() } waitForExpectationsWithTimeout((timeDelay1 + timeDelay2) * 2, handler: nil) @@ -313,7 +313,7 @@ class AsyncExample_iOSTests: XCTestCase { let timePassed = NSDate().timeIntervalSinceDate(date1) XCTAssert(timePassed >= timeDelay1, "Should wait \(timeDelay1) seconds before firing") XCTAssert(timePassed < upperTimeDelay1, "Shouldn't wait \(upperTimeDelay1) seconds before firing") - XCTAssertEqual(qos_class_self().id, QOS_CLASS_BACKGROUND.id, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") + XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") date2 = NSDate() // Update }.background(after: timeDelay2) { @@ -322,7 +322,7 @@ class AsyncExample_iOSTests: XCTestCase { let timePassed = NSDate().timeIntervalSinceDate(date2) XCTAssert(timePassed >= timeDelay2, "Should wait \(timeDelay2) seconds before firing") XCTAssert(timePassed < upperTimeDelay2, "Shouldn't wait \(upperTimeDelay2) seconds before firing") - XCTAssertEqual(qos_class_self().id, QOS_CLASS_BACKGROUND.id, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") + XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") expectation.fulfill() } waitForExpectationsWithTimeout((timeDelay1 + timeDelay2) * 2, handler: nil) From 1b13b98bc4bd62174370b06e87088cf96c72deef Mon Sep 17 00:00:00 2001 From: Tobias Due Munk Date: Sat, 11 Jul 2015 21:52:42 +0200 Subject: [PATCH 2/4] Extend code coverage --- .../AsyncExample_OS_XTests.swift | 840 ++++++++++-------- .../AsyncExample_iOSTests.swift | 840 ++++++++++-------- 2 files changed, 938 insertions(+), 742 deletions(-) diff --git a/AsyncExample/AsyncExample OS XTests/AsyncExample_OS_XTests.swift b/AsyncExample/AsyncExample OS XTests/AsyncExample_OS_XTests.swift index 56a7e48..c91920e 100644 --- a/AsyncExample/AsyncExample OS XTests/AsyncExample_OS_XTests.swift +++ b/AsyncExample/AsyncExample OS XTests/AsyncExample_OS_XTests.swift @@ -21,379 +21,477 @@ class AsyncExample_OS_XTests: XCTestCase { // Put teardown code here. This method is called after the invocation of each test method in the class. super.tearDown() } - - - /* GCD */ - - func testGCD() { - - let expectation = expectationWithDescription("Expected after time") - - let qos = QOS_CLASS_BACKGROUND - let queue = dispatch_get_global_queue(qos, 0) - dispatch_async(queue) { - let currentQos = qos_class_self() - XCTAssertEqual(currentQos, qos, "On \(currentQos.description) (expected \(qos.description))") - expectation.fulfill() - } - waitForExpectationsWithTimeout(1, handler: nil) - } - - - /* dispatch_async() */ + + + /* GCD */ + + func testGCD() { + + let expectation = expectationWithDescription("Expected after time") + + let qos = QOS_CLASS_BACKGROUND + let queue = dispatch_get_global_queue(qos, 0) + dispatch_async(queue) { + let currentQos = qos_class_self() + XCTAssertEqual(currentQos, qos, "On \(currentQos.description) (expected \(qos.description))") + expectation.fulfill() + } + waitForExpectationsWithTimeout(1, handler: nil) + } + + + /* dispatch_async() */ func testAsyncMain() { - let expectation = expectationWithDescription("Expected on main queue") - var calledStuffAfterSinceAsync = false - Async.main { - XCTAssertEqual(qos_class_self(), qos_class_main(), "On \(qos_class_self().description) (expexted \(qos_class_main().description))") - XCTAssert(calledStuffAfterSinceAsync, "Should be async") - expectation.fulfill() - } - calledStuffAfterSinceAsync = true - waitForExpectationsWithTimeout(1, handler: nil) - } - - func testAsyncUserInteractive() { - let expectation = expectationWithDescription("Expected On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") - Async.userInteractive { - XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") - expectation.fulfill() - } - waitForExpectationsWithTimeout(1, handler: nil) - } - - func testAsyncUserInitiared() { - let expectation = expectationWithDescription("Expected On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") - Async.userInitiated { - XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INITIATED, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") - expectation.fulfill() - } - waitForExpectationsWithTimeout(1, handler: nil) - } - - func testAsyncUtility() { - let expectation = expectationWithDescription("Expected On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") - Async.utility { - XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") - expectation.fulfill() - } - waitForExpectationsWithTimeout(1, handler: nil) - } - - func testAsyncBackground() { - let expectation = expectationWithDescription("Expected On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") - Async.background { - XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") - expectation.fulfill() - } - waitForExpectationsWithTimeout(1, handler: nil) - } - - func testAsyncBackgroundToMain() { - let expectation = expectationWithDescription("Expected on background to main queue") - var wasInBackground = false - Async.background { - XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") - wasInBackground = true - }.main { - XCTAssertEqual(qos_class_self(), qos_class_main(), "On \(qos_class_self().description) (expected \(qos_class_main().description))") - XCTAssert(wasInBackground, "Was in background first") - expectation.fulfill() - } - waitForExpectationsWithTimeout(1, handler: nil) - } - - func testChaining() { - let expectation = expectationWithDescription("Expected On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") - var id = 0 - Async.main { - XCTAssertEqual(qos_class_self(), qos_class_main(), "On \(qos_class_self().description) (expexted \(qos_class_main().description))") - XCTAssertEqual(++id, 1, "Count main queue") - }.userInteractive { - XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") - XCTAssertEqual(++id, 2, "Count user interactive queue") - }.userInitiated { - XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INITIATED, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") - XCTAssertEqual(++id, 3, "Count user initiated queue") - }.utility { - XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") - XCTAssertEqual(++id, 4, "Count utility queue") - }.background { - XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") - XCTAssertEqual(++id, 5, "Count background queue") - expectation.fulfill() - } - waitForExpectationsWithTimeout(1, handler: nil) - } - - func testCustomQueue() { - let expectation = expectationWithDescription("Expected custom queues") - var id = 0 - let customQueue = dispatch_queue_create("CustomQueueLabel", DISPATCH_QUEUE_CONCURRENT) - let otherCustomQueue = dispatch_queue_create("OtherCustomQueueLabel", DISPATCH_QUEUE_SERIAL) - Async.customQueue(customQueue) { - XCTAssertEqual(++id, 1, "Count custom queue") - }.customQueue(otherCustomQueue) { - XCTAssertEqual(++id, 2, "Count other custom queue") - expectation.fulfill() - } - waitForExpectationsWithTimeout(1, handler: nil) - } - - - /* dispatch_after() */ - - func testAfterGCD() { - - let expectation = expectationWithDescription("Expected after time") - let date = NSDate() - let timeDelay = 1.0 - let upperTimeDelay = timeDelay + 0.2 - let time = dispatch_time(DISPATCH_TIME_NOW, Int64(timeDelay * Double(NSEC_PER_SEC))) - let queue = dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0) - dispatch_after(time, queue, { - let timePassed = NSDate().timeIntervalSinceDate(date) - println("\(timePassed)") - XCTAssert(timePassed >= timeDelay, "Should wait \(timeDelay) seconds before firing") - XCTAssert(timePassed < upperTimeDelay, "Shouldn't wait \(upperTimeDelay) seconds before firing") - XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") - expectation.fulfill() - }) - waitForExpectationsWithTimeout(timeDelay*2, handler: nil) - } - - func testAfterMain() { - let expectation = expectationWithDescription("Expected after time") - let date = NSDate() - let timeDelay = 1.0 - let upperTimeDelay = timeDelay + 0.2 - Async.main(after: timeDelay) { - let timePassed = NSDate().timeIntervalSinceDate(date) - XCTAssert(timePassed >= timeDelay, "Should wait \(timeDelay) seconds before firing") - XCTAssert(timePassed < upperTimeDelay, "Shouldn't wait \(upperTimeDelay) seconds before firing") - XCTAssertEqual(qos_class_self(), qos_class_main(), "On main queue") - expectation.fulfill() - } - waitForExpectationsWithTimeout(timeDelay*2, handler: nil) - } - - func testChainedAfter() { - let expectation = expectationWithDescription("Expected after time") - let date1 = NSDate() - var date2 = NSDate() - let timeDelay1 = 1.1 - let upperTimeDelay1 = timeDelay1 + 0.2 - let timeDelay2 = 1.2 - let upperTimeDelay2 = timeDelay2 + 0.2 - var id = 0 - Async.userInteractive(after: timeDelay1) { - XCTAssertEqual(++id, 1, "First after") - - let timePassed = NSDate().timeIntervalSinceDate(date1) - XCTAssert(timePassed >= timeDelay1, "Should wait \(timeDelay1) seconds before firing") - XCTAssert(timePassed < upperTimeDelay1, "Shouldn't wait \(upperTimeDelay1) seconds before firing") - XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") - - date2 = NSDate() // Update - }.utility(after: timeDelay2) { - XCTAssertEqual(++id, 2, "Second after") - - let timePassed = NSDate().timeIntervalSinceDate(date2) - XCTAssert(timePassed >= timeDelay2, "Should wait \(timeDelay2) seconds before firing") - XCTAssert(timePassed < upperTimeDelay2, "Shouldn't wait \(upperTimeDelay2) seconds before firing") - XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") - expectation.fulfill() - } - waitForExpectationsWithTimeout((timeDelay1 + timeDelay2) * 2, handler: nil) - } - - func testAfterUserInteractive() { - let expectation = expectationWithDescription("Expected after time") - let date1 = NSDate() - var date2 = NSDate() - let timeDelay1 = 1.1 - let upperTimeDelay1 = timeDelay1 + 0.2 - let timeDelay2 = 1.2 - let upperTimeDelay2 = timeDelay2 + 0.2 - var id = 0 - Async.userInteractive(after: timeDelay1) { - XCTAssertEqual(++id, 1, "First after") - - let timePassed = NSDate().timeIntervalSinceDate(date1) - XCTAssert(timePassed >= timeDelay1, "Should wait \(timeDelay1) seconds before firing") - XCTAssert(timePassed < upperTimeDelay1, "Shouldn't wait \(upperTimeDelay1) seconds before firing") - XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") - - date2 = NSDate() // Update - }.userInteractive(after: timeDelay2) { - XCTAssertEqual(++id, 2, "Second after") - - let timePassed = NSDate().timeIntervalSinceDate(date2) - XCTAssert(timePassed >= timeDelay2, "Should wait \(timeDelay2) seconds before firing") - XCTAssert(timePassed < upperTimeDelay2, "Shouldn't wait \(upperTimeDelay2) seconds before firing") - XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") - expectation.fulfill() - } - waitForExpectationsWithTimeout((timeDelay1 + timeDelay2) * 2, handler: nil) - } - - func testAfterUserInitiated() { - let expectation = expectationWithDescription("Expected after time") - let date1 = NSDate() - var date2 = NSDate() - let timeDelay1 = 1.1 - let upperTimeDelay1 = timeDelay1 + 0.2 - let timeDelay2 = 1.2 - let upperTimeDelay2 = timeDelay2 + 0.2 - var id = 0 - Async.userInitiated(after: timeDelay1) { - XCTAssertEqual(++id, 1, "First after") - - let timePassed = NSDate().timeIntervalSinceDate(date1) - XCTAssert(timePassed >= timeDelay1, "Should wait \(timeDelay1) seconds before firing") - XCTAssert(timePassed < upperTimeDelay1, "Shouldn't wait \(upperTimeDelay1) seconds before firing") - XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INITIATED, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") - - date2 = NSDate() // Update - }.userInitiated(after: timeDelay2) { - XCTAssertEqual(++id, 2, "Second after") - - let timePassed = NSDate().timeIntervalSinceDate(date2) - XCTAssert(timePassed >= timeDelay2, "Should wait \(timeDelay2) seconds before firing") - XCTAssert(timePassed < upperTimeDelay2, "Shouldn't wait \(upperTimeDelay2) seconds before firing") - XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INITIATED, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") - expectation.fulfill() - } - waitForExpectationsWithTimeout((timeDelay1 + timeDelay2) * 2, handler: nil) - } - - func testAfterUtility() { - let expectation = expectationWithDescription("Expected after time") - let date1 = NSDate() - var date2 = NSDate() - let timeDelay1 = 1.1 - let upperTimeDelay1 = timeDelay1 + 0.2 - let timeDelay2 = 1.2 - let upperTimeDelay2 = timeDelay2 + 0.2 - var id = 0 - Async.utility(after: timeDelay1) { - XCTAssertEqual(++id, 1, "First after") - - let timePassed = NSDate().timeIntervalSinceDate(date1) - XCTAssert(timePassed >= timeDelay1, "Should wait \(timeDelay1) seconds before firing") - XCTAssert(timePassed < upperTimeDelay1, "Shouldn't wait \(upperTimeDelay1) seconds before firing") - XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") - - date2 = NSDate() // Update - }.utility(after: timeDelay2) { - XCTAssertEqual(++id, 2, "Second after") - - let timePassed = NSDate().timeIntervalSinceDate(date2) - XCTAssert(timePassed >= timeDelay2, "Should wait \(timeDelay2) seconds before firing") - XCTAssert(timePassed < upperTimeDelay2, "Shouldn't wait \(upperTimeDelay2) seconds before firing") - XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") - expectation.fulfill() - } - waitForExpectationsWithTimeout((timeDelay1 + timeDelay2) * 2, handler: nil) - } - - func testAfterBackground() { - let expectation = expectationWithDescription("Expected after time") - let date1 = NSDate() - var date2 = NSDate() - let timeDelay1 = 1.1 - let upperTimeDelay1 = timeDelay1 + 0.2 - let timeDelay2 = 1.2 - let upperTimeDelay2 = timeDelay2 + 0.2 - var id = 0 - Async.background(after: timeDelay1) { - XCTAssertEqual(++id, 1, "First after") - - let timePassed = NSDate().timeIntervalSinceDate(date1) - XCTAssert(timePassed >= timeDelay1, "Should wait \(timeDelay1) seconds before firing") - XCTAssert(timePassed < upperTimeDelay1, "Shouldn't wait \(upperTimeDelay1) seconds before firing") - XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") - - date2 = NSDate() // Update - }.background(after: timeDelay2) { - XCTAssertEqual(++id, 2, "Second after") - - let timePassed = NSDate().timeIntervalSinceDate(date2) - XCTAssert(timePassed >= timeDelay2, "Should wait \(timeDelay2) seconds before firing") - XCTAssert(timePassed < upperTimeDelay2, "Shouldn't wait \(upperTimeDelay2) seconds before firing") - XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") - expectation.fulfill() - } - waitForExpectationsWithTimeout((timeDelay1 + timeDelay2) * 2, handler: nil) - } - - - /* dispatch_block_cancel() */ - - func testCancel() { - let expectation = expectationWithDescription("Block1 should run") - - let block1 = Async.background { - // Heavy work - for i in 0...1000 { - println("A \(i)") - } - expectation.fulfill() - } - let block2 = block1.background { - println("B – shouldn't be reached, since cancelled") - XCTFail("Shouldn't be reached, since cancelled") - } - - Async.main(after: 0.01) { - block1.cancel() // First block is _not_ cancelled - block2.cancel() // Second block _is_ cancelled - } - - waitForExpectationsWithTimeout(20, handler: nil) - } - - - /* dispatch_wait() */ - - func testWait() { - var id = 0 - let block = Async.background { - // Heavy work - for i in 0...100 { - println("A \(i)") - } - XCTAssertEqual(++id, 1, "") - } - XCTAssertEqual(id, 0, "") - - block.wait() - XCTAssertEqual(++id, 2, "") - } - - func testWaitMax() { - var id = 0 - let block = Async.background { - XCTAssertEqual(++id, 1, "") // A - // Heavy work - for i in 0...10000 { - println("A \(i)") - } - XCTAssertEqual(++id, 3, "") // C - } - XCTAssertEqual(id, 0, "") - - let date = NSDate() - let timeDelay = 0.3 - let upperTimeDelay = timeDelay + 0.2 - - block.wait(seconds: timeDelay) - - XCTAssertEqual(++id, 2, "") // B - let timePassed = NSDate().timeIntervalSinceDate(date) - XCTAssert(timePassed < upperTimeDelay, "Shouldn't wait \(upperTimeDelay) seconds before firing") - } + let expectation = expectationWithDescription("Expected on main queue") + var calledStuffAfterSinceAsync = false + Async.main { + XCTAssertEqual(qos_class_self(), qos_class_main(), "On \(qos_class_self().description) (expexted \(qos_class_main().description))") + XCTAssert(calledStuffAfterSinceAsync, "Should be async") + expectation.fulfill() + } + calledStuffAfterSinceAsync = true + waitForExpectationsWithTimeout(1, handler: nil) + } + + func testAsyncUserInteractive() { + let expectation = expectationWithDescription("Expected on user interactive queue") + Async.userInteractive { + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") + expectation.fulfill() + } + waitForExpectationsWithTimeout(1, handler: nil) + } + + func testAsyncUserInitiated() { + let expectation = expectationWithDescription("Expected on user initiated queue") + Async.userInitiated { + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INITIATED, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") + expectation.fulfill() + } + waitForExpectationsWithTimeout(1, handler: nil) + } + + func testAsyncUtility() { + let expectation = expectationWithDescription("Expected on utility queue") + Async.utility { + XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") + expectation.fulfill() + } + waitForExpectationsWithTimeout(1, handler: nil) + } + + func testAsyncBackground() { + let expectation = expectationWithDescription("Expected on background queue") + Async.background { + XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") + expectation.fulfill() + } + waitForExpectationsWithTimeout(1, handler: nil) + } + + func testAsyncCustomQueueConcurrent() { + let expectation = expectationWithDescription("Expected custom queue") + let customQueue = dispatch_queue_create("CustomQueueLabel", DISPATCH_QUEUE_CONCURRENT) + Async.customQueue(customQueue) { + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INITIATED, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") + expectation.fulfill() + } + waitForExpectationsWithTimeout(1, handler: nil) + } + + func testAsyncCustomQueueSerial() { + let expectation = expectationWithDescription("Expected custom queue") + let customQueue = dispatch_queue_create("CustomQueueLabel", DISPATCH_QUEUE_SERIAL) + Async.customQueue(customQueue) { + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INITIATED, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") + expectation.fulfill() + } + waitForExpectationsWithTimeout(1, handler: nil) + } + + + /* Chaining */ + + func testAsyncBackgroundToMain() { + let expectation = expectationWithDescription("Expected on background to main queue") + var wasInBackground = false + Async.background { + XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") + wasInBackground = true + }.main { + XCTAssertEqual(qos_class_self(), qos_class_main(), "On \(qos_class_self().description) (expected \(qos_class_main().description))") + XCTAssert(wasInBackground, "Was in background first") + expectation.fulfill() + } + waitForExpectationsWithTimeout(1, handler: nil) + } + + func testChaining() { + let expectation = expectationWithDescription("Expected On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") + var id = 0 + Async.main { + XCTAssertEqual(qos_class_self(), qos_class_main(), "On \(qos_class_self().description) (expexted \(qos_class_main().description))") + XCTAssertEqual(++id, 1, "Count main queue") + }.userInteractive { + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") + XCTAssertEqual(++id, 2, "Count user interactive queue") + }.userInitiated { + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INITIATED, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") + XCTAssertEqual(++id, 3, "Count user initiated queue") + }.utility { + XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") + XCTAssertEqual(++id, 4, "Count utility queue") + }.background { + XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") + XCTAssertEqual(++id, 5, "Count background queue") + expectation.fulfill() + } + waitForExpectationsWithTimeout(1, handler: nil) + } + + func testAsyncCustomQueueChaining() { + let expectation = expectationWithDescription("Expected custom queues") + var id = 0 + let customQueue = dispatch_queue_create("CustomQueueLabel", DISPATCH_QUEUE_CONCURRENT) + let otherCustomQueue = dispatch_queue_create("OtherCustomQueueLabel", DISPATCH_QUEUE_SERIAL) + Async.customQueue(customQueue) { + XCTAssertEqual(++id, 1, "Count custom queue") + }.customQueue(otherCustomQueue) { + XCTAssertEqual(++id, 2, "Count other custom queue") + expectation.fulfill() + } + waitForExpectationsWithTimeout(1, handler: nil) + } + + + /* dispatch_after() */ + + func testAfterGCD() { + + let expectation = expectationWithDescription("Expected after time") + let date = NSDate() + let timeDelay = 1.0 + let upperTimeDelay = timeDelay + 0.2 + let time = dispatch_time(DISPATCH_TIME_NOW, Int64(timeDelay * Double(NSEC_PER_SEC))) + let queue = dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0) + dispatch_after(time, queue, { + let timePassed = NSDate().timeIntervalSinceDate(date) + print("\(timePassed)") + XCTAssert(timePassed >= timeDelay, "Should wait \(timeDelay) seconds before firing") + XCTAssert(timePassed < upperTimeDelay, "Shouldn't wait \(timePassed), but <\(upperTimeDelay) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") + expectation.fulfill() + }) + waitForExpectationsWithTimeout(timeDelay*2, handler: nil) + } + + func testAfterMain() { + let expectation = expectationWithDescription("Expected after time") + let date = NSDate() + let timeDelay = 1.0 + let upperTimeDelay = timeDelay + 0.2 + Async.main(after: timeDelay) { + let timePassed = NSDate().timeIntervalSinceDate(date) + XCTAssert(timePassed >= timeDelay, "Should wait \(timeDelay) seconds before firing") + XCTAssert(timePassed < upperTimeDelay, "Shouldn't wait \(timePassed), but <\(upperTimeDelay) seconds before firing") + XCTAssertEqual(qos_class_self(), qos_class_main(), "On main queue") + expectation.fulfill() + } + waitForExpectationsWithTimeout(timeDelay*2, handler: nil) + } + + func testAfterUserInteractive() { + let expectation = expectationWithDescription("Expected after time") + let date = NSDate() + let timeDelay = 1.0 + let upperTimeDelay = timeDelay + 0.2 + Async.userInteractive(after: timeDelay) { + let timePassed = NSDate().timeIntervalSinceDate(date) + XCTAssert(timePassed >= timeDelay, "Should wait \(timeDelay) seconds before firing") + XCTAssert(timePassed < upperTimeDelay, "Shouldn't wait \(timePassed), but <\(upperTimeDelay) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On user interactive queue") + expectation.fulfill() + } + waitForExpectationsWithTimeout(timeDelay*2, handler: nil) + } + + func testAfterUserInitated() { + let expectation = expectationWithDescription("Expected after time") + let date = NSDate() + let timeDelay = 1.0 + let upperTimeDelay = timeDelay + 0.2 + Async.userInitiated(after: timeDelay) { + let timePassed = NSDate().timeIntervalSinceDate(date) + XCTAssert(timePassed >= timeDelay, "Should wait \(timeDelay) seconds before firing") + XCTAssert(timePassed < upperTimeDelay, "Shouldn't wait \(timePassed), but <\(upperTimeDelay) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INITIATED, "On user initiated queue") + expectation.fulfill() + } + waitForExpectationsWithTimeout(timeDelay*2, handler: nil) + } + + func testAfterUtility() { + let expectation = expectationWithDescription("Expected after time") + let date = NSDate() + let timeDelay = 1.0 + let upperTimeDelay = timeDelay + 0.2 + Async.utility(after: timeDelay) { + let timePassed = NSDate().timeIntervalSinceDate(date) + XCTAssert(timePassed >= timeDelay, "Should wait \(timeDelay) seconds before firing") + XCTAssert(timePassed < upperTimeDelay, "Shouldn't wait \(timePassed), but <\(upperTimeDelay) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On utility queue") + expectation.fulfill() + } + waitForExpectationsWithTimeout(timeDelay*2, handler: nil) + } + + func testAfterBackground() { + let expectation = expectationWithDescription("Expected after time") + let date = NSDate() + let timeDelay = 1.0 + let upperTimeDelay = timeDelay + 0.2 + Async.background(after: timeDelay) { + let timePassed = NSDate().timeIntervalSinceDate(date) + XCTAssert(timePassed >= timeDelay, "Should wait \(timeDelay) seconds before firing") + XCTAssert(timePassed < upperTimeDelay, "Shouldn't wait \(timePassed), but <\(upperTimeDelay) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On background queue") + expectation.fulfill() + } + waitForExpectationsWithTimeout(timeDelay*2, handler: nil) + } + + func testAfterCustomQueue() { + let expectation = expectationWithDescription("Expected after time") + let date = NSDate() + let timeDelay = 1.0 + let upperTimeDelay = timeDelay + 0.2 + let customQueue = dispatch_queue_create("CustomQueueLabel", DISPATCH_QUEUE_CONCURRENT) + Async.customQueue(after: timeDelay, queue: customQueue) { + let timePassed = NSDate().timeIntervalSinceDate(date) + XCTAssert(timePassed >= timeDelay, "Should wait \(timeDelay) seconds before firing") + XCTAssert(timePassed < upperTimeDelay, "Shouldn't wait \(timePassed), but <\(upperTimeDelay) seconds before firing") + expectation.fulfill() + } + waitForExpectationsWithTimeout(timeDelay*2, handler: nil) + } + + func testAfterChainedMix() { + let expectation = expectationWithDescription("Expected after time") + let date1 = NSDate() + var date2 = NSDate() + let timeDelay1 = 1.1 + let upperTimeDelay1 = timeDelay1 + 0.2 + let timeDelay2 = 1.2 + let upperTimeDelay2 = timeDelay2 + 0.2 + var id = 0 + Async.userInteractive(after: timeDelay1) { + XCTAssertEqual(++id, 1, "First after") + + let timePassed = NSDate().timeIntervalSinceDate(date1) + XCTAssert(timePassed >= timeDelay1, "Should wait \(timeDelay1) seconds before firing") + XCTAssert(timePassed < upperTimeDelay1, "Shouldn't wait \(timePassed), but <\(upperTimeDelay1) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") + + date2 = NSDate() // Update + }.utility(after: timeDelay2) { + XCTAssertEqual(++id, 2, "Second after") + + let timePassed = NSDate().timeIntervalSinceDate(date2) + XCTAssert(timePassed >= timeDelay2, "Should wait \(timeDelay2) seconds before firing") + XCTAssert(timePassed < upperTimeDelay2, "Shouldn't wait \(timePassed), but <\(upperTimeDelay2) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") + expectation.fulfill() + } + waitForExpectationsWithTimeout((timeDelay1 + timeDelay2) * 2, handler: nil) + } + + func testAfterChainedUserInteractive() { + let expectation = expectationWithDescription("Expected after time") + let date1 = NSDate() + var date2 = NSDate() + let timeDelay1 = 1.1 + let upperTimeDelay1 = timeDelay1 + 0.2 + let timeDelay2 = 1.2 + let upperTimeDelay2 = timeDelay2 + 0.2 + var id = 0 + Async.userInteractive(after: timeDelay1) { + XCTAssertEqual(++id, 1, "First after") + + let timePassed = NSDate().timeIntervalSinceDate(date1) + XCTAssert(timePassed >= timeDelay1, "Should wait \(timeDelay1) seconds before firing") + XCTAssert(timePassed < upperTimeDelay1, "Shouldn't wait \(timePassed), but <\(upperTimeDelay1) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") + + date2 = NSDate() // Update + }.userInteractive(after: timeDelay2) { + XCTAssertEqual(++id, 2, "Second after") + + let timePassed = NSDate().timeIntervalSinceDate(date2) + XCTAssert(timePassed >= timeDelay2, "Should wait \(timeDelay2) seconds before firing") + XCTAssert(timePassed < upperTimeDelay2, "Shouldn't wait \(timePassed), but <\(upperTimeDelay2) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") + expectation.fulfill() + } + waitForExpectationsWithTimeout((timeDelay1 + timeDelay2) * 2, handler: nil) + } + + func testAfterChainedUserInitiated() { + let expectation = expectationWithDescription("Expected after time") + let date1 = NSDate() + var date2 = NSDate() + let timeDelay1 = 1.1 + let upperTimeDelay1 = timeDelay1 + 0.2 + let timeDelay2 = 1.2 + let upperTimeDelay2 = timeDelay2 + 0.2 + var id = 0 + Async.userInitiated(after: timeDelay1) { + XCTAssertEqual(++id, 1, "First after") + + let timePassed = NSDate().timeIntervalSinceDate(date1) + XCTAssert(timePassed >= timeDelay1, "Should wait \(timeDelay1) seconds before firing") + XCTAssert(timePassed < upperTimeDelay1, "Shouldn't wait \(timePassed), but <\(upperTimeDelay1) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INITIATED, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") + + date2 = NSDate() // Update + }.userInitiated(after: timeDelay2) { + XCTAssertEqual(++id, 2, "Second after") + + let timePassed = NSDate().timeIntervalSinceDate(date2) + XCTAssert(timePassed >= timeDelay2, "Should wait \(timeDelay2) seconds before firing") + XCTAssert(timePassed < upperTimeDelay2, "Shouldn't wait \(timePassed), but <\(upperTimeDelay2) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INITIATED, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") + expectation.fulfill() + } + waitForExpectationsWithTimeout((timeDelay1 + timeDelay2) * 2, handler: nil) + } + + func testAfterChainedUtility() { + let expectation = expectationWithDescription("Expected after time") + let date1 = NSDate() + var date2 = NSDate() + let timeDelay1 = 1.1 + let upperTimeDelay1 = timeDelay1 + 0.2 + let timeDelay2 = 1.2 + let upperTimeDelay2 = timeDelay2 + 0.2 + var id = 0 + Async.utility(after: timeDelay1) { + XCTAssertEqual(++id, 1, "First after") + + let timePassed = NSDate().timeIntervalSinceDate(date1) + XCTAssert(timePassed >= timeDelay1, "Should wait \(timeDelay1) seconds before firing") + XCTAssert(timePassed < upperTimeDelay1, "Shouldn't wait \(timePassed), but <\(upperTimeDelay1) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") + + date2 = NSDate() // Update + }.utility(after: timeDelay2) { + XCTAssertEqual(++id, 2, "Second after") + + let timePassed = NSDate().timeIntervalSinceDate(date2) + XCTAssert(timePassed >= timeDelay2, "Should wait \(timeDelay2) seconds before firing") + XCTAssert(timePassed < upperTimeDelay2, "Shouldn't wait \(timePassed), but <\(upperTimeDelay2) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") + expectation.fulfill() + } + waitForExpectationsWithTimeout((timeDelay1 + timeDelay2) * 2, handler: nil) + } + + func testAfterChainedBackground() { + let expectation = expectationWithDescription("Expected after time") + let date1 = NSDate() + var date2 = NSDate() + let timeDelay1 = 1.1 + let upperTimeDelay1 = timeDelay1 + 0.2 + let timeDelay2 = 1.2 + let upperTimeDelay2 = timeDelay2 + 0.2 + var id = 0 + Async.background(after: timeDelay1) { + XCTAssertEqual(++id, 1, "First after") + + let timePassed = NSDate().timeIntervalSinceDate(date1) + XCTAssert(timePassed >= timeDelay1, "Should wait \(timeDelay1) seconds before firing") + XCTAssert(timePassed < upperTimeDelay1, "Shouldn't wait \(timePassed), but <\(upperTimeDelay1) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") + + date2 = NSDate() // Update + }.background(after: timeDelay2) { + XCTAssertEqual(++id, 2, "Second after") + + let timePassed = NSDate().timeIntervalSinceDate(date2) + XCTAssert(timePassed >= timeDelay2, "Should wait \(timeDelay2) seconds before firing") + XCTAssert(timePassed < upperTimeDelay2, "Shouldn't wait \(timePassed), but <\(upperTimeDelay2) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") + expectation.fulfill() + } + waitForExpectationsWithTimeout((timeDelay1 + timeDelay2) * 2, handler: nil) + } + + + /* dispatch_block_cancel() */ + + func testCancel() { + let expectation = expectationWithDescription("Block1 should run") + + let block1 = Async.background { + // Heavy work + for i in 0...1000 { + print("A \(i)") + } + expectation.fulfill() + } + let block2 = block1.background { + print("B – shouldn't be reached, since cancelled") + XCTFail("Shouldn't be reached, since cancelled") + } + + Async.main(after: 0.01) { + block1.cancel() // First block is _not_ cancelled + block2.cancel() // Second block _is_ cancelled + } + + waitForExpectationsWithTimeout(20, handler: nil) + } + + + /* dispatch_wait() */ + + func testWait() { + var id = 0 + let block = Async.background { + // Heavy work + for i in 0...100 { + print("A \(i)") + } + XCTAssertEqual(++id, 1, "") + } + XCTAssertEqual(id, 0, "") + + block.wait() + XCTAssertEqual(++id, 2, "") + } + + func testWaitMax() { + var id = 0 + let block = Async.background { + XCTAssertEqual(++id, 1, "") // A + // Heavy work + for i in 0...100000 { + print("A \(i)") + } + XCTAssertEqual(++id, 3, "") // C + } + XCTAssertEqual(id, 0, "") + + let date = NSDate() + let timeDelay = 0.3 + let upperTimeDelay = timeDelay + 0.2 + + block.wait(seconds: timeDelay) + + XCTAssertEqual(++id, 2, "") // B + let timePassed = NSDate().timeIntervalSinceDate(date) + XCTAssert(timePassed < upperTimeDelay, "Shouldn't wait \(upperTimeDelay) seconds before firing") + } /* dispatch_apply() */ diff --git a/AsyncExample/AsyncExample iOSTests/AsyncExample_iOSTests.swift b/AsyncExample/AsyncExample iOSTests/AsyncExample_iOSTests.swift index e6fd48e..8a6a60f 100644 --- a/AsyncExample/AsyncExample iOSTests/AsyncExample_iOSTests.swift +++ b/AsyncExample/AsyncExample iOSTests/AsyncExample_iOSTests.swift @@ -21,379 +21,477 @@ class AsyncExample_iOSTests: XCTestCase { // Put teardown code here. This method is called after the invocation of each test method in the class. super.tearDown() } - - - /* GCD */ - - func testGCD() { - - let expectation = expectationWithDescription("Expected after time") - - let qos = QOS_CLASS_BACKGROUND - let queue = dispatch_get_global_queue(qos, 0) - dispatch_async(queue) { - let currentQos = qos_class_self() - XCTAssertEqual(currentQos, qos, "On \(currentQos.description) (expected \(qos.description))") - expectation.fulfill() - } - waitForExpectationsWithTimeout(1, handler: nil) - } - - - /* dispatch_async() */ + + + /* GCD */ + + func testGCD() { + + let expectation = expectationWithDescription("Expected after time") + + let qos = QOS_CLASS_BACKGROUND + let queue = dispatch_get_global_queue(qos, 0) + dispatch_async(queue) { + let currentQos = qos_class_self() + XCTAssertEqual(currentQos, qos, "On \(currentQos.description) (expected \(qos.description))") + expectation.fulfill() + } + waitForExpectationsWithTimeout(1, handler: nil) + } + + + /* dispatch_async() */ func testAsyncMain() { - let expectation = expectationWithDescription("Expected on main queue") - var calledStuffAfterSinceAsync = false - Async.main { - XCTAssertEqual(qos_class_self(), qos_class_main(), "On \(qos_class_self().description) (expexted \(qos_class_main().description))") - XCTAssert(calledStuffAfterSinceAsync, "Should be async") - expectation.fulfill() - } - calledStuffAfterSinceAsync = true - waitForExpectationsWithTimeout(1, handler: nil) - } - - func testAsyncUserInteractive() { - let expectation = expectationWithDescription("Expected On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") - Async.userInteractive { - XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") - expectation.fulfill() - } - waitForExpectationsWithTimeout(1, handler: nil) - } - - func testAsyncUserInitiared() { - let expectation = expectationWithDescription("Expected On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") - Async.userInitiated { - XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INITIATED, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") - expectation.fulfill() - } - waitForExpectationsWithTimeout(1, handler: nil) - } - - func testAsyncUtility() { - let expectation = expectationWithDescription("Expected On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") - Async.utility { - XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") - expectation.fulfill() - } - waitForExpectationsWithTimeout(1, handler: nil) - } - - func testAsyncBackground() { - let expectation = expectationWithDescription("Expected On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") - Async.background { - XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") - expectation.fulfill() - } - waitForExpectationsWithTimeout(1, handler: nil) - } - - func testAsyncBackgroundToMain() { - let expectation = expectationWithDescription("Expected on background to main queue") - var wasInBackground = false - Async.background { - XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") - wasInBackground = true - }.main { - XCTAssertEqual(qos_class_self(), qos_class_main(), "On \(qos_class_self().description) (expected \(qos_class_main().description))") - XCTAssert(wasInBackground, "Was in background first") - expectation.fulfill() - } - waitForExpectationsWithTimeout(1, handler: nil) - } - - func testChaining() { - let expectation = expectationWithDescription("Expected On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") - var id = 0 - Async.main { - XCTAssertEqual(qos_class_self(), qos_class_main(), "On \(qos_class_self().description) (expexted \(qos_class_main().description))") - XCTAssertEqual(++id, 1, "Count main queue") - }.userInteractive { - XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") - XCTAssertEqual(++id, 2, "Count user interactive queue") - }.userInitiated { - XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INITIATED, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") - XCTAssertEqual(++id, 3, "Count user initiated queue") - }.utility { - XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") - XCTAssertEqual(++id, 4, "Count utility queue") - }.background { - XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") - XCTAssertEqual(++id, 5, "Count background queue") - expectation.fulfill() - } - waitForExpectationsWithTimeout(1, handler: nil) - } - - func testCustomQueue() { - let expectation = expectationWithDescription("Expected custom queues") - var id = 0 - let customQueue = dispatch_queue_create("CustomQueueLabel", DISPATCH_QUEUE_CONCURRENT) - let otherCustomQueue = dispatch_queue_create("OtherCustomQueueLabel", DISPATCH_QUEUE_SERIAL) - Async.customQueue(customQueue) { - XCTAssertEqual(++id, 1, "Count custom queue") - }.customQueue(otherCustomQueue) { - XCTAssertEqual(++id, 2, "Count other custom queue") - expectation.fulfill() - } - waitForExpectationsWithTimeout(1, handler: nil) - } - - - /* dispatch_after() */ - - func testAfterGCD() { - - let expectation = expectationWithDescription("Expected after time") - let date = NSDate() - let timeDelay = 1.0 - let upperTimeDelay = timeDelay + 0.2 - let time = dispatch_time(DISPATCH_TIME_NOW, Int64(timeDelay * Double(NSEC_PER_SEC))) - let queue = dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0) - dispatch_after(time, queue, { - let timePassed = NSDate().timeIntervalSinceDate(date) - println("\(timePassed)") - XCTAssert(timePassed >= timeDelay, "Should wait \(timeDelay) seconds before firing") - XCTAssert(timePassed < upperTimeDelay, "Shouldn't wait \(upperTimeDelay) seconds before firing") - XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") - expectation.fulfill() - }) - waitForExpectationsWithTimeout(timeDelay*2, handler: nil) - } - - func testAfterMain() { - let expectation = expectationWithDescription("Expected after time") - let date = NSDate() - let timeDelay = 1.0 - let upperTimeDelay = timeDelay + 0.2 - Async.main(after: timeDelay) { - let timePassed = NSDate().timeIntervalSinceDate(date) - XCTAssert(timePassed >= timeDelay, "Should wait \(timeDelay) seconds before firing") - XCTAssert(timePassed < upperTimeDelay, "Shouldn't wait \(upperTimeDelay) seconds before firing") - XCTAssertEqual(qos_class_self(), qos_class_main(), "On main queue") - expectation.fulfill() - } - waitForExpectationsWithTimeout(timeDelay*2, handler: nil) - } - - func testChainedAfter() { - let expectation = expectationWithDescription("Expected after time") - let date1 = NSDate() - var date2 = NSDate() - let timeDelay1 = 1.1 - let upperTimeDelay1 = timeDelay1 + 0.2 - let timeDelay2 = 1.2 - let upperTimeDelay2 = timeDelay2 + 0.2 - var id = 0 - Async.userInteractive(after: timeDelay1) { - XCTAssertEqual(++id, 1, "First after") - - let timePassed = NSDate().timeIntervalSinceDate(date1) - XCTAssert(timePassed >= timeDelay1, "Should wait \(timeDelay1) seconds before firing") - XCTAssert(timePassed < upperTimeDelay1, "Shouldn't wait \(upperTimeDelay1) seconds before firing") - XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") - - date2 = NSDate() // Update - }.utility(after: timeDelay2) { - XCTAssertEqual(++id, 2, "Second after") - - let timePassed = NSDate().timeIntervalSinceDate(date2) - XCTAssert(timePassed >= timeDelay2, "Should wait \(timeDelay2) seconds before firing") - XCTAssert(timePassed < upperTimeDelay2, "Shouldn't wait \(upperTimeDelay2) seconds before firing") - XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") - expectation.fulfill() - } - waitForExpectationsWithTimeout((timeDelay1 + timeDelay2) * 2, handler: nil) - } - - func testAfterUserInteractive() { - let expectation = expectationWithDescription("Expected after time") - let date1 = NSDate() - var date2 = NSDate() - let timeDelay1 = 1.1 - let upperTimeDelay1 = timeDelay1 + 0.2 - let timeDelay2 = 1.2 - let upperTimeDelay2 = timeDelay2 + 0.2 - var id = 0 - Async.userInteractive(after: timeDelay1) { - XCTAssertEqual(++id, 1, "First after") - - let timePassed = NSDate().timeIntervalSinceDate(date1) - XCTAssert(timePassed >= timeDelay1, "Should wait \(timeDelay1) seconds before firing") - XCTAssert(timePassed < upperTimeDelay1, "Shouldn't wait \(upperTimeDelay1) seconds before firing") - XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") - - date2 = NSDate() // Update - }.userInteractive(after: timeDelay2) { - XCTAssertEqual(++id, 2, "Second after") - - let timePassed = NSDate().timeIntervalSinceDate(date2) - XCTAssert(timePassed >= timeDelay2, "Should wait \(timeDelay2) seconds before firing") - XCTAssert(timePassed < upperTimeDelay2, "Shouldn't wait \(upperTimeDelay2) seconds before firing") - XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") - expectation.fulfill() - } - waitForExpectationsWithTimeout((timeDelay1 + timeDelay2) * 2, handler: nil) - } - - func testAfterUserInitiated() { - let expectation = expectationWithDescription("Expected after time") - let date1 = NSDate() - var date2 = NSDate() - let timeDelay1 = 1.1 - let upperTimeDelay1 = timeDelay1 + 0.2 - let timeDelay2 = 1.2 - let upperTimeDelay2 = timeDelay2 + 0.2 - var id = 0 - Async.userInitiated(after: timeDelay1) { - XCTAssertEqual(++id, 1, "First after") - - let timePassed = NSDate().timeIntervalSinceDate(date1) - XCTAssert(timePassed >= timeDelay1, "Should wait \(timeDelay1) seconds before firing") - XCTAssert(timePassed < upperTimeDelay1, "Shouldn't wait \(upperTimeDelay1) seconds before firing") - XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INITIATED, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") - - date2 = NSDate() // Update - }.userInitiated(after: timeDelay2) { - XCTAssertEqual(++id, 2, "Second after") - - let timePassed = NSDate().timeIntervalSinceDate(date2) - XCTAssert(timePassed >= timeDelay2, "Should wait \(timeDelay2) seconds before firing") - XCTAssert(timePassed < upperTimeDelay2, "Shouldn't wait \(upperTimeDelay2) seconds before firing") - XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INITIATED, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") - expectation.fulfill() - } - waitForExpectationsWithTimeout((timeDelay1 + timeDelay2) * 2, handler: nil) - } - - func testAfterUtility() { - let expectation = expectationWithDescription("Expected after time") - let date1 = NSDate() - var date2 = NSDate() - let timeDelay1 = 1.1 - let upperTimeDelay1 = timeDelay1 + 0.2 - let timeDelay2 = 1.2 - let upperTimeDelay2 = timeDelay2 + 0.2 - var id = 0 - Async.utility(after: timeDelay1) { - XCTAssertEqual(++id, 1, "First after") - - let timePassed = NSDate().timeIntervalSinceDate(date1) - XCTAssert(timePassed >= timeDelay1, "Should wait \(timeDelay1) seconds before firing") - XCTAssert(timePassed < upperTimeDelay1, "Shouldn't wait \(upperTimeDelay1) seconds before firing") - XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") - - date2 = NSDate() // Update - }.utility(after: timeDelay2) { - XCTAssertEqual(++id, 2, "Second after") - - let timePassed = NSDate().timeIntervalSinceDate(date2) - XCTAssert(timePassed >= timeDelay2, "Should wait \(timeDelay2) seconds before firing") - XCTAssert(timePassed < upperTimeDelay2, "Shouldn't wait \(upperTimeDelay2) seconds before firing") - XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") - expectation.fulfill() - } - waitForExpectationsWithTimeout((timeDelay1 + timeDelay2) * 2, handler: nil) - } - - func testAfterBackground() { - let expectation = expectationWithDescription("Expected after time") - let date1 = NSDate() - var date2 = NSDate() - let timeDelay1 = 1.1 - let upperTimeDelay1 = timeDelay1 + 0.2 - let timeDelay2 = 1.2 - let upperTimeDelay2 = timeDelay2 + 0.2 - var id = 0 - Async.background(after: timeDelay1) { - XCTAssertEqual(++id, 1, "First after") - - let timePassed = NSDate().timeIntervalSinceDate(date1) - XCTAssert(timePassed >= timeDelay1, "Should wait \(timeDelay1) seconds before firing") - XCTAssert(timePassed < upperTimeDelay1, "Shouldn't wait \(upperTimeDelay1) seconds before firing") - XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") - - date2 = NSDate() // Update - }.background(after: timeDelay2) { - XCTAssertEqual(++id, 2, "Second after") - - let timePassed = NSDate().timeIntervalSinceDate(date2) - XCTAssert(timePassed >= timeDelay2, "Should wait \(timeDelay2) seconds before firing") - XCTAssert(timePassed < upperTimeDelay2, "Shouldn't wait \(upperTimeDelay2) seconds before firing") - XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") - expectation.fulfill() - } - waitForExpectationsWithTimeout((timeDelay1 + timeDelay2) * 2, handler: nil) - } - - - /* dispatch_block_cancel() */ - - func testCancel() { - let expectation = expectationWithDescription("Block1 should run") - - let block1 = Async.background { - // Heavy work - for i in 0...1000 { - println("A \(i)") - } - expectation.fulfill() - } - let block2 = block1.background { - println("B – shouldn't be reached, since cancelled") - XCTFail("Shouldn't be reached, since cancelled") - } - - Async.main(after: 0.01) { - block1.cancel() // First block is _not_ cancelled - block2.cancel() // Second block _is_ cancelled - } - - waitForExpectationsWithTimeout(20, handler: nil) - } - - - /* dispatch_wait() */ - - func testWait() { - var id = 0 - let block = Async.background { - // Heavy work - for i in 0...100 { - println("A \(i)") - } - XCTAssertEqual(++id, 1, "") - } - XCTAssertEqual(id, 0, "") - - block.wait() - XCTAssertEqual(++id, 2, "") - } - - func testWaitMax() { - var id = 0 - let block = Async.background { - XCTAssertEqual(++id, 1, "") // A - // Heavy work - for i in 0...10000 { - println("A \(i)") - } - XCTAssertEqual(++id, 3, "") // C - } - XCTAssertEqual(id, 0, "") - - let date = NSDate() - let timeDelay = 0.3 - let upperTimeDelay = timeDelay + 0.2 - - block.wait(seconds: timeDelay) - - XCTAssertEqual(++id, 2, "") // B - let timePassed = NSDate().timeIntervalSinceDate(date) - XCTAssert(timePassed < upperTimeDelay, "Shouldn't wait \(upperTimeDelay) seconds before firing") - } + let expectation = expectationWithDescription("Expected on main queue") + var calledStuffAfterSinceAsync = false + Async.main { + XCTAssertEqual(qos_class_self(), qos_class_main(), "On \(qos_class_self().description) (expexted \(qos_class_main().description))") + XCTAssert(calledStuffAfterSinceAsync, "Should be async") + expectation.fulfill() + } + calledStuffAfterSinceAsync = true + waitForExpectationsWithTimeout(1, handler: nil) + } + + func testAsyncUserInteractive() { + let expectation = expectationWithDescription("Expected on user interactive queue") + Async.userInteractive { + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") + expectation.fulfill() + } + waitForExpectationsWithTimeout(1, handler: nil) + } + + func testAsyncUserInitiated() { + let expectation = expectationWithDescription("Expected on user initiated queue") + Async.userInitiated { + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INITIATED, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") + expectation.fulfill() + } + waitForExpectationsWithTimeout(1, handler: nil) + } + + func testAsyncUtility() { + let expectation = expectationWithDescription("Expected on utility queue") + Async.utility { + XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") + expectation.fulfill() + } + waitForExpectationsWithTimeout(1, handler: nil) + } + + func testAsyncBackground() { + let expectation = expectationWithDescription("Expected on background queue") + Async.background { + XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") + expectation.fulfill() + } + waitForExpectationsWithTimeout(1, handler: nil) + } + + func testAsyncCustomQueueConcurrent() { + let expectation = expectationWithDescription("Expected custom queue") + let customQueue = dispatch_queue_create("CustomQueueLabel", DISPATCH_QUEUE_CONCURRENT) + Async.customQueue(customQueue) { + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INITIATED, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") + expectation.fulfill() + } + waitForExpectationsWithTimeout(1, handler: nil) + } + + func testAsyncCustomQueueSerial() { + let expectation = expectationWithDescription("Expected custom queue") + let customQueue = dispatch_queue_create("CustomQueueLabel", DISPATCH_QUEUE_SERIAL) + Async.customQueue(customQueue) { + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INITIATED, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") + expectation.fulfill() + } + waitForExpectationsWithTimeout(1, handler: nil) + } + + + /* Chaining */ + + func testAsyncBackgroundToMain() { + let expectation = expectationWithDescription("Expected on background to main queue") + var wasInBackground = false + Async.background { + XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") + wasInBackground = true + }.main { + XCTAssertEqual(qos_class_self(), qos_class_main(), "On \(qos_class_self().description) (expected \(qos_class_main().description))") + XCTAssert(wasInBackground, "Was in background first") + expectation.fulfill() + } + waitForExpectationsWithTimeout(1, handler: nil) + } + + func testChaining() { + let expectation = expectationWithDescription("Expected On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") + var id = 0 + Async.main { + XCTAssertEqual(qos_class_self(), qos_class_main(), "On \(qos_class_self().description) (expexted \(qos_class_main().description))") + XCTAssertEqual(++id, 1, "Count main queue") + }.userInteractive { + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") + XCTAssertEqual(++id, 2, "Count user interactive queue") + }.userInitiated { + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INITIATED, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") + XCTAssertEqual(++id, 3, "Count user initiated queue") + }.utility { + XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") + XCTAssertEqual(++id, 4, "Count utility queue") + }.background { + XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") + XCTAssertEqual(++id, 5, "Count background queue") + expectation.fulfill() + } + waitForExpectationsWithTimeout(1, handler: nil) + } + + func testAsyncCustomQueueChaining() { + let expectation = expectationWithDescription("Expected custom queues") + var id = 0 + let customQueue = dispatch_queue_create("CustomQueueLabel", DISPATCH_QUEUE_CONCURRENT) + let otherCustomQueue = dispatch_queue_create("OtherCustomQueueLabel", DISPATCH_QUEUE_SERIAL) + Async.customQueue(customQueue) { + XCTAssertEqual(++id, 1, "Count custom queue") + }.customQueue(otherCustomQueue) { + XCTAssertEqual(++id, 2, "Count other custom queue") + expectation.fulfill() + } + waitForExpectationsWithTimeout(1, handler: nil) + } + + + /* dispatch_after() */ + + func testAfterGCD() { + + let expectation = expectationWithDescription("Expected after time") + let date = NSDate() + let timeDelay = 1.0 + let upperTimeDelay = timeDelay + 0.2 + let time = dispatch_time(DISPATCH_TIME_NOW, Int64(timeDelay * Double(NSEC_PER_SEC))) + let queue = dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0) + dispatch_after(time, queue, { + let timePassed = NSDate().timeIntervalSinceDate(date) + print("\(timePassed)") + XCTAssert(timePassed >= timeDelay, "Should wait \(timeDelay) seconds before firing") + XCTAssert(timePassed < upperTimeDelay, "Shouldn't wait \(timePassed), but <\(upperTimeDelay) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") + expectation.fulfill() + }) + waitForExpectationsWithTimeout(timeDelay*2, handler: nil) + } + + func testAfterMain() { + let expectation = expectationWithDescription("Expected after time") + let date = NSDate() + let timeDelay = 1.0 + let upperTimeDelay = timeDelay + 0.2 + Async.main(after: timeDelay) { + let timePassed = NSDate().timeIntervalSinceDate(date) + XCTAssert(timePassed >= timeDelay, "Should wait \(timeDelay) seconds before firing") + XCTAssert(timePassed < upperTimeDelay, "Shouldn't wait \(timePassed), but <\(upperTimeDelay) seconds before firing") + XCTAssertEqual(qos_class_self(), qos_class_main(), "On main queue") + expectation.fulfill() + } + waitForExpectationsWithTimeout(timeDelay*2, handler: nil) + } + + func testAfterUserInteractive() { + let expectation = expectationWithDescription("Expected after time") + let date = NSDate() + let timeDelay = 1.0 + let upperTimeDelay = timeDelay + 0.2 + Async.userInteractive(after: timeDelay) { + let timePassed = NSDate().timeIntervalSinceDate(date) + XCTAssert(timePassed >= timeDelay, "Should wait \(timeDelay) seconds before firing") + XCTAssert(timePassed < upperTimeDelay, "Shouldn't wait \(timePassed), but <\(upperTimeDelay) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On user interactive queue") + expectation.fulfill() + } + waitForExpectationsWithTimeout(timeDelay*2, handler: nil) + } + + func testAfterUserInitated() { + let expectation = expectationWithDescription("Expected after time") + let date = NSDate() + let timeDelay = 1.0 + let upperTimeDelay = timeDelay + 0.2 + Async.userInitiated(after: timeDelay) { + let timePassed = NSDate().timeIntervalSinceDate(date) + XCTAssert(timePassed >= timeDelay, "Should wait \(timeDelay) seconds before firing") + XCTAssert(timePassed < upperTimeDelay, "Shouldn't wait \(timePassed), but <\(upperTimeDelay) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INITIATED, "On user initiated queue") + expectation.fulfill() + } + waitForExpectationsWithTimeout(timeDelay*2, handler: nil) + } + + func testAfterUtility() { + let expectation = expectationWithDescription("Expected after time") + let date = NSDate() + let timeDelay = 1.0 + let upperTimeDelay = timeDelay + 0.2 + Async.utility(after: timeDelay) { + let timePassed = NSDate().timeIntervalSinceDate(date) + XCTAssert(timePassed >= timeDelay, "Should wait \(timeDelay) seconds before firing") + XCTAssert(timePassed < upperTimeDelay, "Shouldn't wait \(timePassed), but <\(upperTimeDelay) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On utility queue") + expectation.fulfill() + } + waitForExpectationsWithTimeout(timeDelay*2, handler: nil) + } + + func testAfterBackground() { + let expectation = expectationWithDescription("Expected after time") + let date = NSDate() + let timeDelay = 1.0 + let upperTimeDelay = timeDelay + 0.2 + Async.background(after: timeDelay) { + let timePassed = NSDate().timeIntervalSinceDate(date) + XCTAssert(timePassed >= timeDelay, "Should wait \(timeDelay) seconds before firing") + XCTAssert(timePassed < upperTimeDelay, "Shouldn't wait \(timePassed), but <\(upperTimeDelay) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On background queue") + expectation.fulfill() + } + waitForExpectationsWithTimeout(timeDelay*2, handler: nil) + } + + func testAfterCustomQueue() { + let expectation = expectationWithDescription("Expected after time") + let date = NSDate() + let timeDelay = 1.0 + let upperTimeDelay = timeDelay + 0.2 + let customQueue = dispatch_queue_create("CustomQueueLabel", DISPATCH_QUEUE_CONCURRENT) + Async.customQueue(after: timeDelay, queue: customQueue) { + let timePassed = NSDate().timeIntervalSinceDate(date) + XCTAssert(timePassed >= timeDelay, "Should wait \(timeDelay) seconds before firing") + XCTAssert(timePassed < upperTimeDelay, "Shouldn't wait \(timePassed), but <\(upperTimeDelay) seconds before firing") + expectation.fulfill() + } + waitForExpectationsWithTimeout(timeDelay*2, handler: nil) + } + + func testAfterChainedMix() { + let expectation = expectationWithDescription("Expected after time") + let date1 = NSDate() + var date2 = NSDate() + let timeDelay1 = 1.1 + let upperTimeDelay1 = timeDelay1 + 0.2 + let timeDelay2 = 1.2 + let upperTimeDelay2 = timeDelay2 + 0.2 + var id = 0 + Async.userInteractive(after: timeDelay1) { + XCTAssertEqual(++id, 1, "First after") + + let timePassed = NSDate().timeIntervalSinceDate(date1) + XCTAssert(timePassed >= timeDelay1, "Should wait \(timeDelay1) seconds before firing") + XCTAssert(timePassed < upperTimeDelay1, "Shouldn't wait \(timePassed), but <\(upperTimeDelay1) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") + + date2 = NSDate() // Update + }.utility(after: timeDelay2) { + XCTAssertEqual(++id, 2, "Second after") + + let timePassed = NSDate().timeIntervalSinceDate(date2) + XCTAssert(timePassed >= timeDelay2, "Should wait \(timeDelay2) seconds before firing") + XCTAssert(timePassed < upperTimeDelay2, "Shouldn't wait \(timePassed), but <\(upperTimeDelay2) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") + expectation.fulfill() + } + waitForExpectationsWithTimeout((timeDelay1 + timeDelay2) * 2, handler: nil) + } + + func testAfterChainedUserInteractive() { + let expectation = expectationWithDescription("Expected after time") + let date1 = NSDate() + var date2 = NSDate() + let timeDelay1 = 1.1 + let upperTimeDelay1 = timeDelay1 + 0.2 + let timeDelay2 = 1.2 + let upperTimeDelay2 = timeDelay2 + 0.2 + var id = 0 + Async.userInteractive(after: timeDelay1) { + XCTAssertEqual(++id, 1, "First after") + + let timePassed = NSDate().timeIntervalSinceDate(date1) + XCTAssert(timePassed >= timeDelay1, "Should wait \(timeDelay1) seconds before firing") + XCTAssert(timePassed < upperTimeDelay1, "Shouldn't wait \(timePassed), but <\(upperTimeDelay1) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") + + date2 = NSDate() // Update + }.userInteractive(after: timeDelay2) { + XCTAssertEqual(++id, 2, "Second after") + + let timePassed = NSDate().timeIntervalSinceDate(date2) + XCTAssert(timePassed >= timeDelay2, "Should wait \(timeDelay2) seconds before firing") + XCTAssert(timePassed < upperTimeDelay2, "Shouldn't wait \(timePassed), but <\(upperTimeDelay2) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INTERACTIVE, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))") + expectation.fulfill() + } + waitForExpectationsWithTimeout((timeDelay1 + timeDelay2) * 2, handler: nil) + } + + func testAfterChainedUserInitiated() { + let expectation = expectationWithDescription("Expected after time") + let date1 = NSDate() + var date2 = NSDate() + let timeDelay1 = 1.1 + let upperTimeDelay1 = timeDelay1 + 0.2 + let timeDelay2 = 1.2 + let upperTimeDelay2 = timeDelay2 + 0.2 + var id = 0 + Async.userInitiated(after: timeDelay1) { + XCTAssertEqual(++id, 1, "First after") + + let timePassed = NSDate().timeIntervalSinceDate(date1) + XCTAssert(timePassed >= timeDelay1, "Should wait \(timeDelay1) seconds before firing") + XCTAssert(timePassed < upperTimeDelay1, "Shouldn't wait \(timePassed), but <\(upperTimeDelay1) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INITIATED, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") + + date2 = NSDate() // Update + }.userInitiated(after: timeDelay2) { + XCTAssertEqual(++id, 2, "Second after") + + let timePassed = NSDate().timeIntervalSinceDate(date2) + XCTAssert(timePassed >= timeDelay2, "Should wait \(timeDelay2) seconds before firing") + XCTAssert(timePassed < upperTimeDelay2, "Shouldn't wait \(timePassed), but <\(upperTimeDelay2) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_USER_INITIATED, "On \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description))") + expectation.fulfill() + } + waitForExpectationsWithTimeout((timeDelay1 + timeDelay2) * 2, handler: nil) + } + + func testAfterChainedUtility() { + let expectation = expectationWithDescription("Expected after time") + let date1 = NSDate() + var date2 = NSDate() + let timeDelay1 = 1.1 + let upperTimeDelay1 = timeDelay1 + 0.2 + let timeDelay2 = 1.2 + let upperTimeDelay2 = timeDelay2 + 0.2 + var id = 0 + Async.utility(after: timeDelay1) { + XCTAssertEqual(++id, 1, "First after") + + let timePassed = NSDate().timeIntervalSinceDate(date1) + XCTAssert(timePassed >= timeDelay1, "Should wait \(timeDelay1) seconds before firing") + XCTAssert(timePassed < upperTimeDelay1, "Shouldn't wait \(timePassed), but <\(upperTimeDelay1) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") + + date2 = NSDate() // Update + }.utility(after: timeDelay2) { + XCTAssertEqual(++id, 2, "Second after") + + let timePassed = NSDate().timeIntervalSinceDate(date2) + XCTAssert(timePassed >= timeDelay2, "Should wait \(timeDelay2) seconds before firing") + XCTAssert(timePassed < upperTimeDelay2, "Shouldn't wait \(timePassed), but <\(upperTimeDelay2) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_UTILITY, "On \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description))") + expectation.fulfill() + } + waitForExpectationsWithTimeout((timeDelay1 + timeDelay2) * 2, handler: nil) + } + + func testAfterChainedBackground() { + let expectation = expectationWithDescription("Expected after time") + let date1 = NSDate() + var date2 = NSDate() + let timeDelay1 = 1.1 + let upperTimeDelay1 = timeDelay1 + 0.2 + let timeDelay2 = 1.2 + let upperTimeDelay2 = timeDelay2 + 0.2 + var id = 0 + Async.background(after: timeDelay1) { + XCTAssertEqual(++id, 1, "First after") + + let timePassed = NSDate().timeIntervalSinceDate(date1) + XCTAssert(timePassed >= timeDelay1, "Should wait \(timeDelay1) seconds before firing") + XCTAssert(timePassed < upperTimeDelay1, "Shouldn't wait \(timePassed), but <\(upperTimeDelay1) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") + + date2 = NSDate() // Update + }.background(after: timeDelay2) { + XCTAssertEqual(++id, 2, "Second after") + + let timePassed = NSDate().timeIntervalSinceDate(date2) + XCTAssert(timePassed >= timeDelay2, "Should wait \(timeDelay2) seconds before firing") + XCTAssert(timePassed < upperTimeDelay2, "Shouldn't wait \(timePassed), but <\(upperTimeDelay2) seconds before firing") + XCTAssertEqual(qos_class_self(), QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))") + expectation.fulfill() + } + waitForExpectationsWithTimeout((timeDelay1 + timeDelay2) * 2, handler: nil) + } + + + /* dispatch_block_cancel() */ + + func testCancel() { + let expectation = expectationWithDescription("Block1 should run") + + let block1 = Async.background { + // Heavy work + for i in 0...1000 { + print("A \(i)") + } + expectation.fulfill() + } + let block2 = block1.background { + print("B – shouldn't be reached, since cancelled") + XCTFail("Shouldn't be reached, since cancelled") + } + + Async.main(after: 0.01) { + block1.cancel() // First block is _not_ cancelled + block2.cancel() // Second block _is_ cancelled + } + + waitForExpectationsWithTimeout(20, handler: nil) + } + + + /* dispatch_wait() */ + + func testWait() { + var id = 0 + let block = Async.background { + // Heavy work + for i in 0...100 { + print("A \(i)") + } + XCTAssertEqual(++id, 1, "") + } + XCTAssertEqual(id, 0, "") + + block.wait() + XCTAssertEqual(++id, 2, "") + } + + func testWaitMax() { + var id = 0 + let block = Async.background { + XCTAssertEqual(++id, 1, "") // A + // Heavy work + for i in 0...100000 { + print("A \(i)") + } + XCTAssertEqual(++id, 3, "") // C + } + XCTAssertEqual(id, 0, "") + + let date = NSDate() + let timeDelay = 0.3 + let upperTimeDelay = timeDelay + 0.2 + + block.wait(seconds: timeDelay) + + XCTAssertEqual(++id, 2, "") // B + let timePassed = NSDate().timeIntervalSinceDate(date) + XCTAssert(timePassed < upperTimeDelay, "Shouldn't wait \(upperTimeDelay) seconds before firing") + } /* dispatch_apply() */ From 7346e7bda6aa0dc91ac89299560b7cde416c6376 Mon Sep 17 00:00:00 2001 From: Tobias Due Munk Date: Sat, 11 Jul 2015 22:07:07 +0200 Subject: [PATCH 3/4] Improved customQueue syntax Changed from: `Async.customQueue(after: 5, queue: someQueue) {}` to: `Async.customQueue(someQueue, after: 5) {}` --- Async.swift | 4 ++-- .../AsyncExample OS XTests/AsyncExample_OS_XTests.swift | 2 +- .../AsyncExample iOSTests/AsyncExample_iOSTests.swift | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Async.swift b/Async.swift index 1a4976c..b46cacf 100644 --- a/Async.swift +++ b/Async.swift @@ -130,7 +130,7 @@ public extension Async { // Static methods static func background(#after: Double, block: dispatch_block_t) -> Async { return Async.after(after, block: block, inQueue: GCD.backgroundQueue()) } - static func customQueue(#after: Double, queue: dispatch_queue_t, block: dispatch_block_t) -> Async { + static func customQueue(queue: dispatch_queue_t, after: Double, block: dispatch_block_t) -> Async { return Async.after(after, block: block, inQueue: queue) } } @@ -208,7 +208,7 @@ public extension Async { func background(#after: Double, block: dispatch_block_t) -> Async { return self.after(after, block: block, runInQueue: GCD.backgroundQueue()) } - func customQueue(#after: Double, queue: dispatch_queue_t, block: dispatch_block_t) -> Async { + func customQueue(queue: dispatch_queue_t, after: Double, block: dispatch_block_t) -> Async { return self.after(after, block: block, runInQueue: queue) } diff --git a/AsyncExample/AsyncExample OS XTests/AsyncExample_OS_XTests.swift b/AsyncExample/AsyncExample OS XTests/AsyncExample_OS_XTests.swift index c91920e..172aaca 100644 --- a/AsyncExample/AsyncExample OS XTests/AsyncExample_OS_XTests.swift +++ b/AsyncExample/AsyncExample OS XTests/AsyncExample_OS_XTests.swift @@ -267,7 +267,7 @@ class AsyncExample_OS_XTests: XCTestCase { let timeDelay = 1.0 let upperTimeDelay = timeDelay + 0.2 let customQueue = dispatch_queue_create("CustomQueueLabel", DISPATCH_QUEUE_CONCURRENT) - Async.customQueue(after: timeDelay, queue: customQueue) { + Async.customQueue(customQueue, after: timeDelay) { let timePassed = NSDate().timeIntervalSinceDate(date) XCTAssert(timePassed >= timeDelay, "Should wait \(timeDelay) seconds before firing") XCTAssert(timePassed < upperTimeDelay, "Shouldn't wait \(timePassed), but <\(upperTimeDelay) seconds before firing") diff --git a/AsyncExample/AsyncExample iOSTests/AsyncExample_iOSTests.swift b/AsyncExample/AsyncExample iOSTests/AsyncExample_iOSTests.swift index 8a6a60f..30351e7 100644 --- a/AsyncExample/AsyncExample iOSTests/AsyncExample_iOSTests.swift +++ b/AsyncExample/AsyncExample iOSTests/AsyncExample_iOSTests.swift @@ -267,7 +267,7 @@ class AsyncExample_iOSTests: XCTestCase { let timeDelay = 1.0 let upperTimeDelay = timeDelay + 0.2 let customQueue = dispatch_queue_create("CustomQueueLabel", DISPATCH_QUEUE_CONCURRENT) - Async.customQueue(after: timeDelay, queue: customQueue) { + Async.customQueue(customQueue, after: timeDelay) { let timePassed = NSDate().timeIntervalSinceDate(date) XCTAssert(timePassed >= timeDelay, "Should wait \(timeDelay) seconds before firing") XCTAssert(timePassed < upperTimeDelay, "Shouldn't wait \(timePassed), but <\(upperTimeDelay) seconds before firing") From 99d540ba2e042074b95bf8ccc2e1f31eba226d8e Mon Sep 17 00:00:00 2001 From: Tobias Due Munk Date: Sat, 11 Jul 2015 23:09:28 +0200 Subject: [PATCH 4/4] Bump pod spec version to 1.3.0 --- Async.podspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Async.podspec b/Async.podspec index f736136..a1bc2ac 100644 --- a/Async.podspec +++ b/Async.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "Async" - s.version = "1.2.1" + s.version = "1.3.0" s.summary = "Syntactic sugar in Swift for asynchronous dispatches in Grand Central Dispatch" s.homepage = "https://github.com/duemunk/Async" s.license = { :type => "MIT" } @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.osx.deployment_target = "10.10" s.ios.deployment_target = "8.0" - s.source = { :git => "https://github.com/duemunk/Async.git", :tag => "1.2.1"} + s.source = { :git => "https://github.com/duemunk/Async.git", :tag => "1.3.0"} s.source_files = "Async.{h,swift}" s.requires_arc = true end \ No newline at end of file