Skip to content

Fix for b/74749605: Cancel pending backoff operations when closing streams. #958

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 1 commit into from
Mar 21, 2018
Merged
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
5 changes: 5 additions & 0 deletions Firestore/Example/Tests/SpecTests/FSTSpecTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,11 @@ - (void)doWatchReset:(NSArray<NSNumber *> *)watchReset snapshot:(NSNumber *_Null
- (void)doWatchStreamClose:(NSDictionary *)closeSpec {
NSDictionary *errorSpec = closeSpec[@"error"];
int code = ((NSNumber *)(errorSpec[@"code"])).intValue;

NSNumber *runBackoffTimer = closeSpec[@"runBackoffTimer"];
// TODO(b/72313632): Incorporate backoff in iOS Spec Tests.
FSTAssert(runBackoffTimer.boolValue, @"iOS Spec Tests don't support backoff.");

[self.driver receiveWatchStreamError:code userInfo:errorSpec];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,8 @@
"error": {
"code": 14,
"message": "Simulated Backend Error"
}
},
"runBackoffTimer": true
},
"stateExpect": {
"activeTargets": {
Expand Down
69 changes: 46 additions & 23 deletions Firestore/Example/Tests/SpecTests/json/offline_spec_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,17 @@
"error": {
"code": 14,
"message": "Simulated Backend Error"
}
},
"runBackoffTimer": true
}
},
{
"watchStreamClose": {
"error": {
"code": 14,
"message": "Simulated Backend Error"
}
},
"runBackoffTimer": true
},
"expect": [
{
Expand All @@ -62,15 +64,17 @@
"error": {
"code": 14,
"message": "Simulated Backend Error"
}
},
"runBackoffTimer": true
}
},
{
"watchStreamClose": {
"error": {
"code": 14,
"message": "Simulated Backend Error"
}
},
"runBackoffTimer": true
}
}
]
Expand Down Expand Up @@ -115,23 +119,26 @@
"error": {
"code": 14,
"message": "Simulated Backend Error"
}
},
"runBackoffTimer": true
}
},
{
"watchStreamClose": {
"error": {
"code": 14,
"message": "Simulated Backend Error"
}
},
"runBackoffTimer": true
}
},
{
"watchStreamClose": {
"error": {
"code": 14,
"message": "Simulated Backend Error"
}
},
"runBackoffTimer": true
},
"expect": [
{
Expand All @@ -151,15 +158,17 @@
"error": {
"code": 14,
"message": "Simulated Backend Error"
}
},
"runBackoffTimer": true
}
},
{
"watchStreamClose": {
"error": {
"code": 14,
"message": "Simulated Backend Error"
}
},
"runBackoffTimer": true
}
}
]
Expand Down Expand Up @@ -199,15 +208,17 @@
"error": {
"code": 14,
"message": "Simulated Backend Error"
}
},
"runBackoffTimer": true
}
},
{
"watchStreamClose": {
"error": {
"code": 14,
"message": "Simulated Backend Error"
}
},
"runBackoffTimer": true
},
"expect": [
{
Expand Down Expand Up @@ -240,7 +251,8 @@
"error": {
"code": 14,
"message": "Simulated Backend Error"
}
},
"runBackoffTimer": true
}
},
{
Expand Down Expand Up @@ -270,15 +282,17 @@
"error": {
"code": 14,
"message": "Simulated Backend Error"
}
},
"runBackoffTimer": true
}
},
{
"watchStreamClose": {
"error": {
"code": 14,
"message": "Simulated Backend Error"
}
},
"runBackoffTimer": true
},
"expect": [
{
Expand Down Expand Up @@ -381,7 +395,8 @@
"error": {
"code": 14,
"message": "Simulated Backend Error"
}
},
"runBackoffTimer": true
},
"stateExpect": {
"activeTargets": {
Expand All @@ -401,15 +416,17 @@
"error": {
"code": 14,
"message": "Simulated Backend Error"
}
},
"runBackoffTimer": true
}
},
{
"watchStreamClose": {
"error": {
"code": 14,
"message": "Simulated Backend Error"
}
},
"runBackoffTimer": true
},
"expect": [
{
Expand Down Expand Up @@ -595,7 +612,8 @@
"error": {
"code": 14,
"message": "Simulated Backend Error"
}
},
"runBackoffTimer": true
},
"stateExpect": {
"activeTargets": {
Expand Down Expand Up @@ -623,15 +641,17 @@
"error": {
"code": 14,
"message": "Simulated Backend Error"
}
},
"runBackoffTimer": true
}
},
{
"watchStreamClose": {
"error": {
"code": 14,
"message": "Simulated Backend Error"
}
},
"runBackoffTimer": true
}
},
{
Expand Down Expand Up @@ -764,15 +784,17 @@
"error": {
"code": 14,
"message": "Simulated Backend Error"
}
},
"runBackoffTimer": true
}
},
{
"watchStreamClose": {
"error": {
"code": 14,
"message": "Simulated Backend Error"
}
},
"runBackoffTimer": true
}
},
{
Expand Down Expand Up @@ -834,7 +856,8 @@
"error": {
"code": 14,
"message": "Simulated Backend Error"
}
},
"runBackoffTimer": true
},
"stateExpect": {
"activeTargets": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,8 @@
"error": {
"code": 14,
"message": "Simulated Backend Error"
}
},
"runBackoffTimer": true
}
},
{
Expand Down Expand Up @@ -543,5 +544,76 @@
]
}
]
},
"Handles user changes while offline (b/74749605).": {
"describeName": "Remote store:",
"itName": "Handles user changes while offline (b/74749605).",
"tags": [
"no-android",
"no-ios"
],
"config": {
"useGarbageCollection": true
},
"steps": [
{
"userListen": [
2,
{
"path": "collection",
"filters": [],
"orderBys": []
}
],
"stateExpect": {
"activeTargets": {
"2": {
"query": {
"path": "collection",
"filters": [],
"orderBys": []
},
"resumeToken": ""
}
}
}
},
{
"watchStreamClose": {
"error": {
"code": 14,
"message": "Simulated Backend Error"
},
"runBackoffTimer": false
},
"stateExpect": {
"activeTargets": {}
}
},
{
"changeUser": "abc",
"stateExpect": {
"activeTargets": {
"2": {
"query": {
"path": "collection",
"filters": [],
"orderBys": []
},
"resumeToken": ""
}
}
}
},
{
"watchStreamClose": {
"error": {
"code": 14,
"message": "Simulated Backend Error"
},
"runBackoffTimer": true
}
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@
"error": {
"code": 14,
"message": "Simulated Backend Error"
}
},
"runBackoffTimer": true
},
"stateExpect": {
"activeTargets": {
Expand Down
3 changes: 3 additions & 0 deletions Firestore/Source/Remote/FSTExponentialBackoff.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (void)backoffAndRunBlock:(void (^)(void))block;

/** Cancels any pending backoff block scheduled via backoffAndRunBlock:. */
- (void)cancel;

@end

NS_ASSUME_NONNULL_END
12 changes: 9 additions & 3 deletions Firestore/Source/Remote/FSTExponentialBackoff.mm
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ - (void)resetToMax {
}

- (void)backoffAndRunBlock:(void (^)(void))block {
if (self.timerCallback) {
[self.timerCallback cancel];
}
[self cancel];

// First schedule the block using the current base (which may be 0 and should be honored as such).
NSTimeInterval delayWithJitter = _currentBase + [self jitterDelay];
if (_currentBase > 0) {
Expand All @@ -89,6 +88,13 @@ - (void)backoffAndRunBlock:(void (^)(void))block {
}
}

- (void)cancel {
if (self.timerCallback) {
[self.timerCallback cancel];
self.timerCallback = nil;
}
}

/** Returns a random value in the range [-currentBase/2, currentBase/2] */
- (NSTimeInterval)jitterDelay {
std::uniform_real_distribution<double> dist;
Expand Down
Loading