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

Yet Another Fix for RetryWhen #151

Closed
wants to merge 3 commits into from
Closed
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
12 changes: 11 additions & 1 deletion Sources/Common/DemandBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,17 @@ class DemandBuffer<S: Subscriber> {
init(subscriber: S) {
self.subscriber = subscriber
}


/// Signal to the buffer that the upstream has changed
/// - Returns: A demand that has already been requested from the previous upstream, but has not yet been processed
func attachToNewUpstream() -> Subscribers.Demand {
lock.lock()
defer { lock.unlock() }

demandState.sent = demandState.requested
return demandState.requested - demandState.processed
}

/// Buffer an upstream value to later be forwarded to
/// the downstream subscriber, once it demands it
///
Expand Down
8 changes: 7 additions & 1 deletion Sources/Common/Sink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ class Sink<Upstream: Publisher, Downstream: Subscriber>: Subscriber {
}

func receive(subscription: Subscription) {
upstreamSubscription = subscription
defer { upstreamSubscription = subscription }

if let upstreamSubscription {
upstreamSubscription.cancel()
let newDemand = buffer.attachToNewUpstream()
subscription.requestIfNeeded(newDemand)
}
}

func receive(_ input: Upstream.Output) -> Subscribers.Demand {
Expand Down
3 changes: 3 additions & 0 deletions Sources/Operators/RetryWhen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ extension Publishers.RetryWhen {
}

func cancel() {
cancellable?.cancel()
cancellable = nil
sink?.cancelUpstream()
sink = nil
}
}
Expand Down
86 changes: 86 additions & 0 deletions Tests/RetryWhenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,93 @@ class RetryWhenTests: XCTestCase {
XCTAssertEqual(completion, .finished)
XCTAssertEqual(times, 2)
}

func testSuccessfulRetryWithManyRetries() {
var times = 0
var retriesCount = 0
var subscriptionsCount = 0
var cancelCount = 0
var resultOutput: [Int] = []
var completion: Subscribers.Completion<RetryWhenTests.MyError>?

subscription = Deferred(createPublisher: { () -> AnyPublisher<Int, MyError> in
defer { times += 1 }
if times == 0 {
return Fail<Int, MyError>(error: MyError.someError).eraseToAnyPublisher()
} else {
return Just(times)
.setFailureType(to: MyError.self)
.handleEvents(
receiveSubscription: { _ in subscriptionsCount += 1 },
receiveCancel: { cancelCount += 1 }
)
.eraseToAnyPublisher()
}
})
.retryWhen { error in
return error
.handleEvents(receiveOutput: { _ in
retriesCount += 1
})
.flatMapLatest { _ in [1, 2].publisher }
}
.sink(
receiveCompletion: { completion = $0 },
receiveValue: { resultOutput.append($0) }
)

XCTAssertEqual(resultOutput, [2])
XCTAssertEqual(completion, .finished)
XCTAssertEqual(times, 3)
XCTAssertEqual(retriesCount, 1)
XCTAssertEqual(subscriptionsCount, 2)
XCTAssertEqual(cancelCount, 1)
}

func testSuccessfulRetryWithCustomDemand() {
var times = 0
var retriesCount = 0
var resultOutput: [Int] = []
var completion: Subscribers.Completion<RetryWhenTests.MyError>?

AnyPublisher<Int, MyError>.init { subscriber in
defer { times += 1 }
if times < 2 {
subscriber.send(times)
subscriber.send(completion: .failure(MyError.someError))
}
else {
subscriber.send(times)
subscriber.send(completion: .finished)
}

return AnyCancellable({})
}
.retryWhen { error in
error
.handleEvents(receiveOutput: { _ in retriesCount += 1})
.map { _ in }
}
.subscribe(
AnySubscriber(
receiveSubscription: { subscription in
self.subscription = AnyCancellable { subscription.cancel() }
subscription.request(.max(3))
},
receiveValue: {
resultOutput.append($0)
return .none
},
receiveCompletion: { completion = $0 }
)
)

XCTAssertEqual(resultOutput, [0, 1, 2])
XCTAssertEqual(completion, .finished)
XCTAssertEqual(times, 3)
XCTAssertEqual(retriesCount, 2)
}

func testRetryFailure() {
var expectedOutput: Int?

Expand Down