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

Fix .throttle with .seconds() rounding issue. #2600

Closed
wants to merge 2 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
2 changes: 1 addition & 1 deletion RxSwift/Date+Dispatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extension DispatchTimeInterval {
let interval = laterDate.timeIntervalSince(earlierDate)
let remainder = Double(value) - interval * factor
guard remainder > 0 else { return 0 }
return Int(remainder.rounded(.toNearestOrAwayFromZero))
return Int(remainder.rounded(.up))
}
}
}
Expand Down
28 changes: 27 additions & 1 deletion Tests/RxSwiftTests/Observable+ThrottleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,33 @@ extension ObservableThrottleTest {
XCTAssertEqual(2, end.timeIntervalSince(start), accuracy: 0.5)
XCTAssertEqual(a, [0, 1])
}


func test_ThrottleTimeSpan_WithRealScheduler_secondsRounding() {
let scheduler = ConcurrentDispatchQueueScheduler(qos: .default)

let throttlingSubject = PublishSubject<Int>()
for i in 1...5 {
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(i * 250)) {
throttlingSubject.on(.next(i))
}
}
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(1500)) {
throttlingSubject.on(.completed)
}

let start = Date()

let a = try! throttlingSubject
.throttle(.seconds(1), latest: true, scheduler: scheduler)
.toBlocking()
.toArray()

let end = Date()

XCTAssertEqual(2, end.timeIntervalSince(start), accuracy: 0.5)
XCTAssertEqual(a, [1, 5])
}

func test_ThrottleTimeSpan_WithRealScheduler_milliseconds() {
let scheduler = ConcurrentDispatchQueueScheduler(qos: .default)

Expand Down