-
Notifications
You must be signed in to change notification settings - Fork 2
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: evaluation scheduler not being reset when the request succeeds #37
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import Foundation | ||
|
||
final class EvaluationForegroundTask: ScheduledTask { | ||
private weak var component: Component? | ||
private let component: Component | ||
private let queue: DispatchQueue | ||
private var poller: Poller? | ||
private var retryPollingInterval: Int64 | ||
|
@@ -20,11 +20,10 @@ final class EvaluationForegroundTask: ScheduledTask { | |
self.maxRetryCount = maxRetryCount | ||
} | ||
|
||
private func reschedule(isRetrying: Bool) { | ||
private func reschedule(interval: Int64) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactored this function to keep the consistency with Android SDK. |
||
self.stop() | ||
guard let component = component else { return } | ||
self.poller = .init( | ||
intervalMillis: isRetrying ? retryPollingInterval : component.config.pollingInterval, | ||
intervalMillis: interval, | ||
queue: queue, | ||
logger: component.config.logger, | ||
handler: { [weak self] _ in | ||
|
@@ -37,7 +36,7 @@ final class EvaluationForegroundTask: ScheduledTask { | |
} | ||
|
||
func start() { | ||
reschedule(isRetrying: false) | ||
reschedule(interval: self.component.config.pollingInterval) | ||
} | ||
|
||
func stop() { | ||
|
@@ -46,7 +45,6 @@ final class EvaluationForegroundTask: ScheduledTask { | |
} | ||
|
||
private func fetchEvaluations() { | ||
guard let component = component else { return } | ||
let eventInteractor = component.eventInteractor | ||
let retryCount = self.retryCount | ||
let maxRetryCount = self.maxRetryCount | ||
|
@@ -61,8 +59,11 @@ final class EvaluationForegroundTask: ScheduledTask { | |
seconds: response.seconds, | ||
sizeByte: response.sizeByte | ||
) | ||
// reset retry count | ||
self?.retryCount = 0 | ||
// reset the scheduler to use the default polling interval configured in the BKTConfig | ||
if (retryCount > 0) { | ||
self?.retryCount = 0 | ||
self?.reschedule(interval: pollingInterval) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixes the polling interval setting not being reset when the request succeeds after retrying. |
||
} | ||
|
||
case .failure(let error, let featureTag): | ||
try eventInteractor.trackFetchEvaluationsFailure( | ||
|
@@ -79,16 +80,19 @@ final class EvaluationForegroundTask: ScheduledTask { | |
// we can retry more | ||
self?.retryCount += 1 | ||
if !retried { | ||
self?.reschedule(isRetrying: true) | ||
// we reschedule just once and wait until it reaches | ||
// the max retrying count or succeeds to reschedule it again | ||
// to use the default polling interval configured in the BKTConfig | ||
self?.reschedule(interval: retryPollingInterval) | ||
} | ||
} else { | ||
// we already retried enough, let's get back to daily job | ||
self?.retryCount = 0 | ||
self?.reschedule(isRetrying: false) | ||
self?.reschedule(interval: pollingInterval) | ||
} | ||
} | ||
} catch let error { | ||
self?.component?.config.logger?.error(error) | ||
self?.component.config.logger?.error(error) | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ import XCTest | |
final class EvaluationForegroundTaskTests: XCTestCase { | ||
func testStartAndReceiveSuccess() { | ||
let expectation = self.expectation(description: "") | ||
expectation.expectedFulfillmentCount = 3 | ||
expectation.expectedFulfillmentCount = 2 | ||
expectation.assertForOverFulfill = true | ||
let dispatchQueue = DispatchQueue(label: "default", qos: .default) | ||
|
||
|
@@ -93,7 +93,7 @@ final class EvaluationForegroundTaskTests: XCTestCase { | |
let task = EvaluationForegroundTask( | ||
component: component, | ||
queue: dispatchQueue, | ||
retryPollingInterval: 1, | ||
retryPollingInterval: 1000, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1 millisecond is too low and the test could fail sometimes, so I'm increasing it by 1 second. |
||
maxRetryCount: 5 | ||
) | ||
task.start() | ||
|
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.
The component at this point can't be nil.