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

Generalize DispatchQueues to Dispatcher-protocol objects #888

Closed
wants to merge 27 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
132f608
First draft of Dispatcher conversion
GarthSnyder Jun 18, 2018
b4e39b5
Put back map(on: nil)
GarthSnyder Jun 18, 2018
8cd0121
Rename async() method args to forestall ambiguity; compiles
GarthSnyder Jun 18, 2018
18baef6
Working on Dispatcher tests
GarthSnyder Jun 20, 2018
1ac7e21
Make Dispatcher arguments nonoptional
GarthSnyder Jun 20, 2018
88b7937
Finish tests
GarthSnyder Jun 20, 2018
3516511
Generalize DispatchQueues to Dispatcher-protocol objects
GarthSnyder Jun 20, 2018
7c7835a
Port Thenable.tap and Guarantee.get to Dispatcher objects
GarthSnyder Aug 8, 2018
2cc021a
Travis-test Linux Swift 3 compatibility only with latest compiler
GarthSnyder Aug 9, 2018
70b0551
Use existing v7 Travis config file
GarthSnyder Dec 2, 2018
f8b0ead
First draft of Dispatcher conversion
GarthSnyder Jun 18, 2018
4ba57ed
Put back map(on: nil)
GarthSnyder Jun 18, 2018
262b4dd
Rename async() method args to forestall ambiguity; compiles
GarthSnyder Jun 18, 2018
044fe78
Working on Dispatcher tests
GarthSnyder Jun 20, 2018
0a027c8
Make Dispatcher arguments nonoptional
GarthSnyder Jun 20, 2018
855476a
Finish tests
GarthSnyder Jun 20, 2018
9e17e7d
Generalize DispatchQueues to Dispatcher-protocol objects
GarthSnyder Jun 20, 2018
686a1e6
Port Thenable.tap and Guarantee.get to Dispatcher objects
GarthSnyder Aug 8, 2018
d1a38a5
Fixed bug where pendingPromiseDeallocated event was incorrectly repor…
neallester Dec 14, 2018
0e90203
Update ObjectiveC.md
mxcl Dec 14, 2018
84f35e5
Add explicit import for Dispatch in tests (needed for Travis builds)
GarthSnyder Dec 15, 2018
1136163
Travis-build all branches in Travis TRAVIS_BRANCHES env variable
GarthSnyder Dec 16, 2018
8ae5bfa
v7 Travis: Swift 3 only on latest compiler, sync Linux test/build
GarthSnyder Dec 16, 2018
4689415
Save artifacts (env must define ARTIFACTS_* for KEY, SECRET, REGION, …
GarthSnyder Dec 16, 2018
f877a39
Disable artifact collection by default
GarthSnyder Dec 18, 2018
2c45b0b
Merge branch 'dispatch-v7' into dispatch
GarthSnyder Dec 18, 2018
68763cf
Revert commits 0e902 and d1a38; these were rebased in from master
GarthSnyder Dec 18, 2018
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
Prev Previous commit
Next Next commit
Working on Dispatcher tests
GarthSnyder committed Dec 2, 2018

Verified

This commit was signed with the committer’s verified signature.
commit 18baef69bd26679da04b9cac6592933f6d9c2bcc
4 changes: 4 additions & 0 deletions PromiseKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
@@ -79,6 +79,7 @@
63D9B2EF203385FD0075C00B /* race.m in Sources */ = {isa = PBXBuildFile; fileRef = 63D9B2EE203385FD0075C00B /* race.m */; };
63D9B2F120338D5D0075C00B /* Deprecations.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63D9B2F020338D5D0075C00B /* Deprecations.swift */; };
BB2524DE20D729A60010F7B0 /* Dispatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB2524DD20D729A60010F7B0 /* Dispatcher.swift */; };
BB4AF7C520D820700008333D /* DispatcherTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB4AF7C320D819360008333D /* DispatcherTests.swift */; };
C013F7382048E3B6006B57B1 /* MockNodeEnvironment.swift in Sources */ = {isa = PBXBuildFile; fileRef = C013F7372048E3B6006B57B1 /* MockNodeEnvironment.swift */; };
C013F73A2049076A006B57B1 /* JSPromise.swift in Sources */ = {isa = PBXBuildFile; fileRef = C013F7392049076A006B57B1 /* JSPromise.swift */; };
C013F73C20494291006B57B1 /* JSAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = C013F73B20494291006B57B1 /* JSAdapter.swift */; };
@@ -221,6 +222,7 @@
63D9B2EE203385FD0075C00B /* race.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = race.m; path = Sources/race.m; sourceTree = "<group>"; };
63D9B2F020338D5D0075C00B /* Deprecations.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Deprecations.swift; path = Sources/Deprecations.swift; sourceTree = "<group>"; };
BB2524DD20D729A60010F7B0 /* Dispatcher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Dispatcher.swift; path = Sources/Dispatcher.swift; sourceTree = "<group>"; };
BB4AF7C320D819360008333D /* DispatcherTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DispatcherTests.swift; sourceTree = "<group>"; };
C013F7372048E3B6006B57B1 /* MockNodeEnvironment.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MockNodeEnvironment.swift; path = "Tests/JS-A+/MockNodeEnvironment.swift"; sourceTree = "<group>"; };
C013F7392049076A006B57B1 /* JSPromise.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = JSPromise.swift; path = "Tests/JS-A+/JSPromise.swift"; sourceTree = "<group>"; };
C013F73B20494291006B57B1 /* JSAdapter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = JSAdapter.swift; path = "Tests/JS-A+/JSAdapter.swift"; sourceTree = "<group>"; };
@@ -349,6 +351,7 @@
635D64161D59635300BC0AF5 /* StressTests.swift */,
635D640D1D59635300BC0AF5 /* ZalgoTests.swift */,
639BF755203DF02C00FA577B /* Utilities.swift */,
BB4AF7C320D819360008333D /* DispatcherTests.swift */,
);
name = Core;
path = Tests/CorePromise;
@@ -674,6 +677,7 @@
635D64221D59635300BC0AF5 /* ZalgoTests.swift in Sources */,
635D64271D59635300BC0AF5 /* RaceTests.swift in Sources */,
632FBBE51F33B338008F8FBB /* CatchableTests.swift in Sources */,
BB4AF7C520D820700008333D /* DispatcherTests.swift in Sources */,
63CF6D80203CD19200EC8927 /* ThenableTests.swift in Sources */,
635D642B1D59635300BC0AF5 /* StressTests.swift in Sources */,
630A805A203CEF6800D25F23 /* WhenTests.m in Sources */,
2 changes: 1 addition & 1 deletion Sources/Dispatcher.swift
Original file line number Diff line number Diff line change
@@ -369,7 +369,7 @@ public extension CatchMixin {
- Parameter body: The handler to execute if this promise is rejected.
- SeeAlso: [Cancellation](http://promisekit.org/docs/)
*/
func recover<U: Thenable>(on: DispatchQueue? = conf.Q.map, flags: DispatchWorkItemFlags? = nil, policy: CatchPolicy = conf.catchPolicy, _ body: @escaping(Error) throws -> U) -> Promise<T> where U.T == T {
func recover<U: Thenable>(on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, policy: CatchPolicy = conf.catchPolicy, _ body: @escaping(Error) throws -> U) -> Promise<T> where U.T == T {
let dispatcher = selectDispatcher(given: on, configured: conf.D.map, flags: flags)
return recover(on: dispatcher, policy: policy, body)
}
2 changes: 1 addition & 1 deletion Sources/Thenable.swift
Original file line number Diff line number Diff line change
@@ -141,7 +141,7 @@ public extension Thenable {
print(response.data)
}
*/
func done(on: Dispatcher? = conf.Q.return, _ body: @escaping(T) throws -> Void) -> Promise<Void> {
func done(on: Dispatcher? = conf.D.return, _ body: @escaping(T) throws -> Void) -> Promise<Void> {
let rp = Promise<Void>(.pending)
pipe {
switch $0 {
105 changes: 105 additions & 0 deletions Tests/CorePromise/DispatcherTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import PromiseKit
import XCTest

class RecordingDispatcher: Dispatcher {

var dispatchCount = 0

func async(_ body: @escaping () -> Void) {
dispatchCount += 1
DispatchQueue.global(qos: .background).async(execute: body)
}

}

class DispatcherTests: XCTestCase {

var dispatcher = RecordingDispatcher()
var dispatcherB = RecordingDispatcher()

override func setUp() {
dispatcher = RecordingDispatcher()
dispatcherB = RecordingDispatcher()
}

func testConfD() {
let ex = expectation(description: "conf.D")
let oldConf = PromiseKit.conf.D
PromiseKit.conf.D.map = dispatcher
PromiseKit.conf.D.return = dispatcherB
XCTAssertNil(PromiseKit.conf.Q.map) // Not representable as DispatchQueues
XCTAssertNil(PromiseKit.conf.Q.return)
Promise { seal in
seal.fulfill(42)
}.map {
$0 + 10
}.done() {
XCTAssertEqual($0, 52)
XCTAssertEqual(self.dispatcher.dispatchCount, 1)
XCTAssertEqual(self.dispatcherB.dispatchCount, 1)
ex.fulfill()
}.cauterize()
waitForExpectations(timeout: 1)
PromiseKit.conf.D.map = DispatchQueue.main
PromiseKit.conf.Q.return = .main
XCTAssert(PromiseKit.conf.Q.map === DispatchQueue.main)
XCTAssert((PromiseKit.conf.D.return as? DispatchQueue)! === DispatchQueue.main)
PromiseKit.conf.D = oldConf
}

func testDispatcherWithThrow() {
let ex = expectation(description: "Dispatcher with throw")
Promise { seal in
seal.fulfill(42)
}.map(on: dispatcher) { _ in
throw PMKError.badInput
}.catch(on: dispatcher) { _ in
ex.fulfill()
}
waitForExpectations(timeout: 1)
XCTAssertEqual(self.dispatcher.dispatchCount, 2)
}

func testDispatchQueueBackwardCompatibility() {
let ex = expectation(description: "DispatchQueue compatibility")
let oldConf = PromiseKit.conf.D
PromiseKit.conf.D = (map: dispatcher, return: dispatcher)
Promise.value(42).map(on: .global(qos: .background), flags: .barrier) { (x: Int) -> Int in
return x + 10
}.then(on: .main, flags: []) {
XCTAssertEqual($0, 52)
return Promise.value(50)
}.done(on: .global(qos: .userInitiated)) {
XCTAssertEqual($0, 50)
ex.fulfill()
}.cauterize()
waitForExpectations(timeout: 1)
XCTAssertEqual(self.dispatcher.dispatchCount, 0)
PromiseKit.conf.D = oldConf
}

func testDispatcherPromiseExtension() {
let ex = expectation(description: "Dispatcher.promise")
dispatcher.promise {
return 42
}.done(on: dispatcher) {
XCTAssertEqual($0, 42)
XCTAssertEqual(self.dispatcher.dispatchCount, 2)
ex.fulfill()
}.cauterize()
waitForExpectations(timeout: 1)
}

func testDispatcherGuaranteeExtension() {
let ex = expectation(description: "Dispatcher.guarantee")
dispatcher.guarantee {
return 42
}.done(on: .main) {
XCTAssertEqual($0, 42)
XCTAssertEqual(self.dispatcher.dispatchCount, 1)
ex.fulfill()
}
waitForExpectations(timeout: 1)
}

}