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

Amb: forward cancel to the inner publishers #144

Open
wants to merge 3 commits into
base: main
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
4 changes: 4 additions & 0 deletions Sources/Operators/Amb.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ private extension Publishers.Amb {
guard let decision = decision else { return }
switch decision {
case .first:
secondSink?.cancelUpstream()
secondSink = nil
case .second:
firstSink?.cancelUpstream()
firstSink = nil
}

Expand Down Expand Up @@ -144,7 +146,9 @@ private extension Publishers.Amb {
}

func cancel() {
firstSink?.cancelUpstream()
firstSink = nil
secondSink?.cancelUpstream()
secondSink = nil
}
}
Expand Down
59 changes: 59 additions & 0 deletions Tests/AmbTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,65 @@ class AmbTests: XCTestCase {
XCTAssertEqual(completion, .finished)
}

func testAmbCancelPreSubscription() {
let ambPublisher: AnyCancellable?

let subject1Cancelled = expectation(description: "first publisher cancelled")
let subject1 = PassthroughSubject<Int, Error>()
let subject1Publisher = subject1
.handleEvents(receiveCancel: {
subject1Cancelled.fulfill()
})
.eraseToAnyPublisher()

let subject2Cancelled = expectation(description: "second publisher cancelled")
let subject2 = PassthroughSubject<Int, Error>()
let subject2Publisher = subject2
.handleEvents(receiveCancel: {
subject2Cancelled.fulfill()
})
.eraseToAnyPublisher()

ambPublisher = Publishers.Amb(first: subject1Publisher, second: subject2Publisher)
.sink(receiveCompletion: { _ in },
receiveValue: { _ in })

// cancelling amb should cancel the inner publishers
ambPublisher?.cancel()

waitForExpectations(timeout: 0.01)
}

func testAmbCancelPostSubscription() {
let subject1 = PassthroughSubject<Int, Error>()
var subject1cancelCounter = 0
let subject1Publisher = subject1
.handleEvents(receiveCancel: {
subject1cancelCounter += 1
})
.eraseToAnyPublisher()

let subject2 = PassthroughSubject<Int, Error>()
var subject2cancelCounter = 0
let subject2Publisher = subject2
.handleEvents(receiveCancel: {
subject2cancelCounter += 1
})
.eraseToAnyPublisher()

Publishers.Amb(first: subject1Publisher, second: subject2Publisher)
.sink(receiveCompletion: { _ in },
receiveValue: { _ in })
.store(in: &subscriptions)

// subject1 wins the race, so 2 has to be cancelled
subject1.send(1)

// At dealloc both publishes are cancelled, so we cannot use expectations here and count the cancel events instead
XCTAssertEqual(subject1cancelCounter, 0)
XCTAssertEqual(subject2cancelCounter, 1)
}

func testAmbLimitedPreDemand() {
let subject1 = PassthroughSubject<Int, Never>()
let subject2 = PassthroughSubject<Int, Never>()
Expand Down