Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GCD chained arguments and returns take2 #6

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .distiller.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
xcode:
version: '6.0'
5 changes: 3 additions & 2 deletions AsyncExample/AsyncExample iOS/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6206.8" systemVersion="13E28" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6246" systemVersion="13E28" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7026.1"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6239"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
"size" : "60x60",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "3x"
},
{
"idiom" : "ipad",
"size" : "29x29",
Expand Down
142 changes: 139 additions & 3 deletions AsyncExample/AsyncExample iOSTests/AsyncTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func heavyWork() {
}
let allowEarlyDispatchBy = 0.001

let stringArray = ["an", "earthquake", "birds", "and", "snakes"]

class AsyncTests: XCTestCase {


Expand Down Expand Up @@ -141,12 +143,15 @@ class AsyncTests: XCTestCase {
}

func testAsyncBackgroundToMain() {
let expectation0 = expectationWithDescription("Expected on background queue")
let expectation = expectationWithDescription("Expected on background to main queue")
var wasInBackground = false
Async.background {
Async.background {
// XCTAssertEqual(+qos_class_self(), +QOS_CLASS_BACKGROUND, "On \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))")
wasInBackground = true
}.main {
expectation0.fulfill()

}.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()
Expand Down Expand Up @@ -196,7 +201,7 @@ class AsyncTests: XCTestCase {
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")
XCTAssertEqual(++id, Int(1), "Count custom queue")
}.customQueue(otherCustomQueue) {
XCTAssertEqual(++id, 2, "Count other custom queue")
expectation.fulfill()
Expand Down Expand Up @@ -500,4 +505,135 @@ class AsyncTests: XCTestCase {
let timePassed = CFAbsoluteTimeGetCurrent() - date
XCTAssertLessThan(timePassed, upperTimeDelay, "Shouldn't wait \(upperTimeDelay) seconds before firing")
}

func testReturnClosure() {
var id = 0
let block = AsyncPlus.background((), block: { ()->Int in
// Medium light work
println("Fib 12 = \(dumbFibonachi(12))")
XCTAssertEqual(++id, 1, "")
return 1
})
XCTAssertEqual(id, 0, "")

let returned_value = block.waitResult()
XCTAssertEqual(returned_value, 1)
XCTAssertEqual(++id, 2, "")
}

func testArgumentClosure() {
var id = 0
let expectation = expectationWithDescription("Block ran")
let block = AsyncPlus.background(5, block: { (i:Int)->() in
// Medium light work
println("Argument received = \(i) - DumbFib(12) = \(dumbFibonachi(12))")
XCTAssertEqual(++id, 1, "")
XCTAssertEqual(i, 5)
expectation.fulfill()
})
XCTAssertEqual(id, 0, "")

block.wait()
XCTAssertEqual(++id, 2, "")
waitForExpectationsWithTimeout(0.00001, handler: nil)
}

func testArgumentReturnClosure() {
var id = 0
let expectation = expectationWithDescription("block did run")
let block = AsyncPlus.background(2, block: { (i:Int)->String in
// Medium light work
println("Fib 12 = \(dumbFibonachi(12))")
XCTAssertEqual(++id, 1, "")
XCTAssertEqual(i, 2)
expectation.fulfill()
return stringArray[i]
})
XCTAssertEqual(id, 0, "")

let returned_value = block.waitResult()
XCTAssertEqual(returned_value, "birds")
XCTAssertEqual(++id, 2, "")
waitForExpectationsWithTimeout(0.00001, handler: nil)
}

func testArgumentReturnChainedClosures() {
var id = 0
let expectation0 = expectationWithDescription("first block did run")
let expectation1 = expectationWithDescription("second block did run")
let block0 = AsyncPlus.background((), block: { ()->Int in
// Medium light work
println("Fib 12 = \(dumbFibonachi(12))")
XCTAssertEqual(++id, 1, "")
expectation0.fulfill()
return 4
})
let block1 = block0.background({ (i:Int)->String in
// Medium light work
println("Fib 12 = \(dumbFibonachi(12))")
XCTAssertEqual(++id, 2, "")
XCTAssertEqual(i, 4)
expectation1.fulfill()
return stringArray[i]
})
XCTAssertEqual(id, 0, "")

let returned_value = block1.waitResult()
XCTAssertEqual(returned_value, "snakes")
XCTAssertEqual(++id, 3, "")
waitForExpectationsWithTimeout(0.00001, handler: nil)
}

func testArgumentReturnChainedClosuresToVoid() {
var id = 0
let expectation0 = expectationWithDescription("first block did run")
let expectation1 = expectationWithDescription("second block did run")
let block0 = AsyncPlus.background((), block: { ()->Int in
// Medium light work
println("Fib 12 = \(dumbFibonachi(12))")
XCTAssertEqual(++id, 1, "")
expectation0.fulfill()
return 4
})
let block1 = block0.background({ (i:Int) in
// Medium light work
println("Fib 12 = \(dumbFibonachi(12))")
XCTAssertEqual(++id, 2, "")
XCTAssertEqual(i, 4)
expectation1.fulfill()
})
XCTAssertEqual(id, 0, "")

block1.wait()
XCTAssertEqual(++id, 3, "")
waitForExpectationsWithTimeout(0000.1, handler: nil)
}

func testTupleArgumentReturnChainedClosures() {
var id = 0
let expectation0 = expectationWithDescription("first block did run")
let expectation1 = expectationWithDescription("second block did run")
let block0 = AsyncPlus.background((), block: { ()->(Int,String) in
// Medium light work
println("Fib 12 = \(dumbFibonachi(12))")
XCTAssertEqual(++id, 1, "")
expectation0.fulfill()
return (4,"You")
})
let block1 = block0.background({ (i:Int, s:String)->String in
// Medium light work
println("Fib 12 = \(dumbFibonachi(12))")
XCTAssertEqual(++id, 2, "")
XCTAssertEqual(i, 4)
XCTAssertEqual(s, "You")
expectation1.fulfill()
return stringArray[i]
})
XCTAssertEqual(id, 0, "")

let returned_value = block1.waitResult()
XCTAssertEqual(returned_value, "snakes")
XCTAssertEqual(++id, 3, "")
waitForExpectationsWithTimeout(0.00001, handler: nil)
}
}
2 changes: 0 additions & 2 deletions AsyncExample/AsyncExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
4EF830A01975952B00C5F0EE /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EF8309F1975952B00C5F0EE /* main.swift */; };
4EF830A21975952B00C5F0EE /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EF830A11975952B00C5F0EE /* ViewController.swift */; };
4EF830A41975952B00C5F0EE /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EF830A31975952B00C5F0EE /* AppDelegate.swift */; };
4EF830A61975952B00C5F0EE /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4EF830A51975952B00C5F0EE /* Images.xcassets */; };
4EF830CB1975954B00C5F0EE /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EF830CA1975954B00C5F0EE /* AppDelegate.swift */; };
4EF830CD1975954B00C5F0EE /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EF830CC1975954B00C5F0EE /* ViewController.swift */; };
4EF830D01975954B00C5F0EE /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4EF830CE1975954B00C5F0EE /* Main.storyboard */; };
Expand Down Expand Up @@ -370,7 +369,6 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
4EF830A61975952B00C5F0EE /* Images.xcassets in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0600"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "4EF8309A1975952B00C5F0EE"
BuildableName = "AsyncExample OS X.app"
BlueprintName = "AsyncExample OS X"
ReferencedContainer = "container:AsyncExample.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "4EF830AD1975952B00C5F0EE"
BuildableName = "AsyncExample OS XTests.xctest"
BlueprintName = "AsyncExample OS XTests"
ReferencedContainer = "container:AsyncExample.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "4EF830AD1975952B00C5F0EE"
BuildableName = "AsyncExample OS XTests.xctest"
BlueprintName = "AsyncExample OS XTests"
ReferencedContainer = "container:AsyncExample.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "4EF8309A1975952B00C5F0EE"
BuildableName = "AsyncExample OS X.app"
BlueprintName = "AsyncExample OS X"
ReferencedContainer = "container:AsyncExample.xcodeproj">
</BuildableReference>
</MacroExpansion>
</TestAction>
<LaunchAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
allowLocationSimulation = "YES">
<BuildableProductRunnable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "4EF8309A1975952B00C5F0EE"
BuildableName = "AsyncExample OS X.app"
BlueprintName = "AsyncExample OS X"
ReferencedContainer = "container:AsyncExample.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<BuildableProductRunnable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "4EF8309A1975952B00C5F0EE"
BuildableName = "AsyncExample OS X.app"
BlueprintName = "AsyncExample OS X"
ReferencedContainer = "container:AsyncExample.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Loading