Skip to content

Commit 401d48c

Browse files
committed
Implemented async with delay
1 parent 14c2670 commit 401d48c

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

ExecutionContext/DispatchExecutionContext.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@
4141
}
4242
}
4343

44+
public func async(after:Double, task:SafeTask) {
45+
if after > 0 {
46+
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(after * NSTimeInterval(NSEC_PER_SEC)))
47+
dispatch_after(time, queue) {
48+
task()
49+
}
50+
} else {
51+
async(task)
52+
}
53+
}
54+
4455
public func sync<ReturnType>(task:() throws -> ReturnType) throws -> ReturnType {
4556
if dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL) == dispatch_queue_get_label(queue) {
4657
return try task()

ExecutionContext/ExecutionContext.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public protocol TaskSchedulerType {
3838
func async(task:Task)
3939
func async(task:SafeTask)
4040

41+
//after is in seconds
42+
func async(after:Double, task:Task)
43+
func async(after:Double, task:SafeTask)
44+
4145
func sync<ReturnType>(task:() throws -> ReturnType) throws -> ReturnType
4246
func sync<ReturnType>(task:() -> ReturnType) -> ReturnType
4347
}
@@ -82,6 +86,18 @@ public extension ErrorHandlerRegistryType where Self : TaskSchedulerType {
8286
}
8387
}
8488
}
89+
90+
//after is in seconds
91+
func async(after:Double, task:Task) {
92+
//specify explicitely, that it's safe task
93+
async(after) { () -> Void in
94+
do {
95+
try task()
96+
} catch let e {
97+
self.handleError(e)
98+
}
99+
}
100+
}
85101
}
86102

87103
public extension TaskSchedulerType {

ExecutionContext/ImmediateExecutionContext.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ public class ImmediateExecutionContext : ExecutionContextBase, TaskSchedulerType
2121
task()
2222
}
2323

24+
public func async(after:Double, task:SafeTask) {
25+
let sec = time_t(after)
26+
let nsec = Int((after - Double(sec)) * 1000 * 1000 * 1000)//nano seconds
27+
var time = timespec(tv_sec:sec, tv_nsec: nsec)
28+
29+
nanosleep(&time, nil)
30+
async(task)
31+
}
32+
2433
public func sync<ReturnType>(task:() throws -> ReturnType) throws -> ReturnType {
2534
return try task()
2635
}

Tests/ExecutionContext/ExecutionContextTests.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,32 +44,67 @@ class ExecutionContextTests: XCTestCase {
4444
self.waitForExpectationsWithTimeout(2, handler: nil)
4545
}
4646

47+
func afterTest(context:ExecutionContextType) {
48+
let expectation = self.expectationWithDescription("OK AFTER")
49+
50+
context.async(0.5) {
51+
sleep(1)
52+
expectation.fulfill()
53+
}
54+
55+
self.waitForExpectationsWithTimeout(2, handler: nil)
56+
}
57+
58+
func afterTestAdvanced(context:ExecutionContextType) {
59+
var ok = true
60+
61+
context.async(3) {
62+
ok = false
63+
}
64+
65+
sleep(2)
66+
67+
XCTAssert(ok)
68+
69+
sleep(2)
70+
71+
XCTAssertFalse(ok)
72+
}
73+
4774
func testSerial() {
4875
let context:ExecutionContextType = DefaultExecutionContext(kind: .Serial)
4976

5077
syncTest(context)
5178
asyncTest(context)
79+
afterTest(context)
80+
afterTestAdvanced(context)
5281
}
5382

5483
func testParallel() {
5584
let context:ExecutionContextType = DefaultExecutionContext(kind: .Parallel)
5685

5786
syncTest(context)
5887
asyncTest(context)
88+
afterTest(context)
89+
afterTestAdvanced(context)
5990
}
6091

6192
func testGlobal() {
6293
let context:ExecutionContextType = DefaultExecutionContext.global
6394

6495
syncTest(context)
6596
asyncTest(context)
97+
afterTest(context)
98+
afterTestAdvanced(context)
6699
}
67100

68101
func testMain() {
69102
let context:ExecutionContextType = DefaultExecutionContext.main
70103

71104
syncTest(context)
72105
asyncTest(context)
106+
afterTest(context)
107+
//afterTestAdvanced - no it will not work here
73108
}
74109
}
75110

0 commit comments

Comments
 (0)