Skip to content

Commit

Permalink
修改单测
Browse files Browse the repository at this point in the history
  • Loading branch information
NianJi committed Mar 8, 2019
1 parent 63bfffe commit 37e230a
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
80 changes: 78 additions & 2 deletions Examples/coSwiftDemo/coSwiftDemoTests/coSwiftPromiseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,35 @@ func testPromise3() -> Promise<String> {


let timer = Timer.scheduledTimer(withTimeInterval: 3, repeats: false) { (timer) in
print("[\(NSDate())]promise on fulfill 1234")
promise.fulfill(value: "1234")
}

promise.onCancel { (promise) in
print("[\(NSDate())]promise on cancel")
timer.invalidate()
}
return promise
}

func testPromise21() -> Promise<String> {
return Promise<String> { (fulfill, reject) in

DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(5000), execute: {
fulfill("1")
})
}
}

func testPromise22() -> Promise<String> {
return Promise<String> { (fulfill, reject) in

DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(4000), execute: {
fulfill("2")
})
}
}

class PromiseSpec: QuickSpec {


Expand Down Expand Up @@ -158,16 +178,26 @@ class PromiseSpec: QuickSpec {
it("cancel a coroutine when await a promise") {

let co = co_launch {
print("[\(NSDate())]test begin")

let result = try await{ testPromise3() }

_ = try await{ testPromise3() }
switch result {
case .fulfilled(let str):
print("[\(NSDate())]test error with fulfilled: \(str)")
break
case .rejected(let error):
print("[\(NSDate())]test error with error: \(error)")
break
}

// should be cancelled
expect(false).to(beTrue())
}

waitUntil(timeout: 5, action: { (done) in
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(1000), execute: {

print("[\(NSDate())]test error begin co cancel")
co.cancel()
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(1000), execute: {
done()
Expand Down Expand Up @@ -202,6 +232,52 @@ class PromiseSpec: QuickSpec {
})
})
}

it("test concurrent await promise") {
var step = 0;
let co = co_launch {

let begin = CACurrentMediaTime()

let p1 = testPromise21()
let p2 = testPromise22()

let r1 = try await { p1 }
switch r1 {
case .fulfilled(let str):
expect(str).to(equal("1"))
break
case .rejected(_):
expect(false).to(beTrue())
break
}


let r2 = try await { p2 }
switch r2 {
case .fulfilled(let str):
expect(str).to(equal("2"))
break
case .rejected(_):
expect(false).to(beTrue())
break
}

let duration = CACurrentMediaTime() - begin
expect(duration < 5.5).to(beTrue())
step = 1;

}

waitUntil(timeout: 6, action: { (done) in

co_launch {
co.join()
expect(step).to(equal(1))
done()
}
})
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions coswift/Promise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ public class Promise<T> {
public init() {}

public convenience init(constructor: @escaping PromiseConstructor) {
self.init(constructor: constructor, on: co_get_current_queue())
self.init(on: co_get_current_queue(), constructor: constructor)
}

public convenience init(constructor: @escaping PromiseConstructor, on queue: DispatchQueue?) {
public convenience init(on queue: DispatchQueue?, constructor: @escaping PromiseConstructor) {
self.init()

let fulfill: PromiseFulfill = { (val: T) in
Expand Down

0 comments on commit 37e230a

Please sign in to comment.