Repository navigation
Throttle iOS download progress events by time #113
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
ios/CodePushCore/Sources/CodePushCore/Download/ProgressThrottle.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import Foundation | ||
|
|
||
| /// Limits how often download progress reaches JS. | ||
| /// | ||
| /// One instance tracks one download. It isn't thread-safe: call it from one serial queue. | ||
| @objc(CPProgressThrottle) | ||
| public final class ProgressThrottle: NSObject { | ||
| private let interval: TimeInterval | ||
| private let now: () -> TimeInterval | ||
| private var lastSendTime: TimeInterval? | ||
|
|
||
| init(interval: TimeInterval, now: @escaping () -> TimeInterval) { | ||
| self.interval = interval | ||
| self.now = now | ||
| } | ||
|
|
||
| @objc public override convenience init() { | ||
| // Not systemUptime: that's a required-reason API, and the throttle doesn't need a monotonic clock. | ||
| self.init(interval: 0.1, now: { Date().timeIntervalSinceReferenceDate }) | ||
| } | ||
|
|
||
| /// The downloader's last event always has `receivedBytes == expectedBytes`, so it's never held | ||
| /// back. The download's done callback only runs after unzipping and verifying the update. | ||
| @objc(shouldSendExpectedBytes:receivedBytes:) | ||
| public func shouldSend(expectedBytes: Int64, receivedBytes: Int64) -> Bool { | ||
| let time = now() | ||
| // The wall clock can jump backwards. Sending restarts the interval instead of stalling. | ||
| if receivedBytes != expectedBytes, let lastSendTime, (0..<interval).contains(time - lastSendTime) { | ||
| return false | ||
| } | ||
| lastSendTime = time | ||
| return true | ||
| } | ||
| } |
77 changes: 77 additions & 0 deletions
77
ios/CodePushCore/Tests/CodePushCoreTests/ProgressThrottleTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import XCTest | ||
| @testable import CodePushCore | ||
|
|
||
| final class ProgressThrottleTests: XCTestCase { | ||
| private var time: TimeInterval = 1000 | ||
| private var throttle: ProgressThrottle! | ||
|
|
||
| override func setUp() { | ||
| super.setUp() | ||
| throttle = ProgressThrottle(interval: 0.1, now: { [unowned self] in self.time }) | ||
| } | ||
|
|
||
| func testSendsFirstChunkImmediately() { | ||
| XCTAssertTrue(throttle.shouldSend(expectedBytes: 300, receivedBytes: 100)) | ||
| } | ||
|
|
||
| func testDoesNotSendWithinInterval() { | ||
| XCTAssertTrue(throttle.shouldSend(expectedBytes: 300, receivedBytes: 100)) | ||
| time += 0.05 | ||
| XCTAssertFalse(throttle.shouldSend(expectedBytes: 300, receivedBytes: 200)) | ||
| time += 0.049 | ||
| XCTAssertFalse(throttle.shouldSend(expectedBytes: 300, receivedBytes: 250)) | ||
| } | ||
|
|
||
| func testSendsAfterInterval() { | ||
| XCTAssertTrue(throttle.shouldSend(expectedBytes: 300, receivedBytes: 100)) | ||
| time += 0.1 | ||
| XCTAssertTrue(throttle.shouldSend(expectedBytes: 300, receivedBytes: 200)) | ||
| } | ||
|
|
||
| func testIntervalCountsFromLastSendNotLastChunk() { | ||
| XCTAssertTrue(throttle.shouldSend(expectedBytes: 300, receivedBytes: 100)) | ||
| time += 0.06 | ||
| XCTAssertFalse(throttle.shouldSend(expectedBytes: 300, receivedBytes: 150)) | ||
| time += 0.06 | ||
| XCTAssertTrue(throttle.shouldSend(expectedBytes: 300, receivedBytes: 200)) | ||
| } | ||
|
|
||
| func testSendsWhenClockJumpsBackwards() { | ||
| XCTAssertTrue(throttle.shouldSend(expectedBytes: 300, receivedBytes: 100)) | ||
| time -= 60 | ||
| XCTAssertTrue(throttle.shouldSend(expectedBytes: 300, receivedBytes: 150)) | ||
| time += 0.05 | ||
| XCTAssertFalse(throttle.shouldSend(expectedBytes: 300, receivedBytes: 200)) | ||
| } | ||
|
|
||
| func testSendsCompletedProgressWithinInterval() { | ||
| XCTAssertTrue(throttle.shouldSend(expectedBytes: 300, receivedBytes: 100)) | ||
| time += 0.01 | ||
| XCTAssertTrue(throttle.shouldSend(expectedBytes: 300, receivedBytes: 300)) | ||
| } | ||
|
|
||
| func testThrottlesUnknownSizeUntilCompleted() { | ||
| XCTAssertTrue(throttle.shouldSend(expectedBytes: -1, receivedBytes: 100)) | ||
| time += 0.01 | ||
| XCTAssertFalse(throttle.shouldSend(expectedBytes: -1, receivedBytes: 300)) | ||
| // The downloader reports the final size once the response ends. | ||
| XCTAssertTrue(throttle.shouldSend(expectedBytes: 300, receivedBytes: 300)) | ||
| } | ||
|
|
||
| func testBoundsEventRateForManySmallChunks() { | ||
| // 1 MB in 1 KB chunks, one every millisecond: one second of downloading. | ||
| let total: Int64 = 1024 * 1024 | ||
| var sent: [Int64] = [] | ||
| for chunk in 1...1024 { | ||
| time += 0.001 | ||
| let received = Int64(chunk) * 1024 | ||
| if throttle.shouldSend(expectedBytes: total, receivedBytes: received) { | ||
| sent.append(received) | ||
| } | ||
| } | ||
|
|
||
| XCTAssertLessThanOrEqual(sent.count, 12) | ||
| XCTAssertGreaterThanOrEqual(sent.count, 10) | ||
| XCTAssertEqual(sent.last, total) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Available since iOS 8, no need to use a runtime availability check: https://developer.apple.com/documentation/foundation/operationqueue/underlyingqueue