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

when does not work as expected when using cancelOnFailure: true #20

Closed
julasamer opened this issue Jun 25, 2018 · 2 comments
Closed

when does not work as expected when using cancelOnFailure: true #20

julasamer opened this issue Jun 25, 2018 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@julasamer
Copy link

when(fulfilled: promises, cancelOnFailure: true) does not seem to propagate cancel calls correctly - when requestCancel is called on the resulting promises, the sub-promises never receive a cancel request. This only happens with cancelOnFailure=true.

Below is my unit test reproducing the issue: The promise completes successfully even though cancel was called. didCancel is never set to true.

func testCancelingWhen() {
    var didCancel = false
    var resolveHooks: [()->()] = []
    let expectation = XCTestExpectation(description: ".always is called")
    
    let createPromise = {
        return Promise<(), NoError>(on: .main) {
            resolver in
            
            resolver.onRequestCancel(on: .immediate) {
                resolver in
                didCancel = true
                resolver.cancel()
            }
            
            resolveHooks.append {
                resolver.fulfill(with: ())
            }
        }
    }
    
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
        resolveHooks.forEach { hook in hook() }
    }
    
    let whenPromise = when(fulfilled: [createPromise(), createPromise()], cancelOnFailure: true).then {
        _ in
        XCTFail("Should not complete.")
    }
    
    whenPromise.requestCancel()
    
    whenPromise.always {
        _ in
        expectation.fulfill()
        XCTAssert(didCancel, "No cancel was propagated.")
    }
    
    wait(for: [expectation], timeout: 1)
}
@lilyball
Copy link
Owner

Good catch! Setting cancelOnFailure to true captures all input promises directly, instead of capturing cancellables, meaning it breaks traditional cancellation propagation.

I'll try to put out a fix for this tonight.

@lilyball lilyball self-assigned this Jun 25, 2018
@lilyball lilyball added the bug Something isn't working label Jun 25, 2018
@lilyball
Copy link
Owner

I just published v0.3.3 which includes this fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants