From 34ebf10b0acc65f1924d723e82085d4104bc281d Mon Sep 17 00:00:00 2001 From: Michael Lehenbauer Date: Mon, 5 Mar 2018 09:49:41 -0800 Subject: Add 10 second timeout waiting for connection before client behaves as-if offline. (#872) [Port of https://github.com/firebase/firebase-js-sdk/commit/0fa319e5e019dd0d40ab441d2ff9f8f6d4724e43] * Refactored FSTOnlineState tracking out of FSTRemoteStore and into new FSTOnlineStateTracker component. * Added a 10 second timeout to transition from OnlineState.Unknown to OnlineState.Offline rather than waiting indefinitely for the stream to succeed or fail. * Removed hack to run SpecTests using an FSTDispatchQueue that wrapped dispatch_get_main_queue(). This was incompatible with [FSTDispatchQueue runDelayedCallbacksUntil:] since it queues work and blocks waiting for it to complete. Now spec tests create / use a proper FSTDispatchQueue. * Added a SpecTest to verify OnlineState timeout behavior. * Misc cleanup: * Renamed FSTOnlineState states: Failed => Offline, Healthy => Online * Renamed FSTTimerIds (ListenStreamConnection => ListenStreamConnectionBackoff) * Added ability to run timers from spec tests. --- Firestore/Source/Util/FSTDispatchQueue.h | 26 +++++++++++++++++++++++--- Firestore/Source/Util/FSTDispatchQueue.mm | 4 ++++ 2 files changed, 27 insertions(+), 3 deletions(-) (limited to 'Firestore/Source/Util') diff --git a/Firestore/Source/Util/FSTDispatchQueue.h b/Firestore/Source/Util/FSTDispatchQueue.h index 9b28c9c..7922600 100644 --- a/Firestore/Source/Util/FSTDispatchQueue.h +++ b/Firestore/Source/Util/FSTDispatchQueue.h @@ -23,11 +23,24 @@ NS_ASSUME_NONNULL_BEGIN * can then be used from tests to check for the presence of callbacks or to run them early. */ typedef NS_ENUM(NSInteger, FSTTimerID) { - FSTTimerIDAll, // Sentinel value to be used with runDelayedCallbacksUntil: to run all blocks. + /** All can be used with runDelayedCallbacksUntil: to run all timers. */ + FSTTimerIDAll, + + /** + * The following 4 timers are used in FSTStream for the listen and write streams. The "Idle" timer + * is used to close the stream due to inactivity. The "ConnectionBackoff" timer is used to + * restart a stream once the appropriate backoff delay has elapsed. + */ FSTTimerIDListenStreamIdle, - FSTTimerIDListenStreamConnection, + FSTTimerIDListenStreamConnectionBackoff, FSTTimerIDWriteStreamIdle, - FSTTimerIDWriteStreamConnection + FSTTimerIDWriteStreamConnectionBackoff, + + /** + * A timer used in FSTOnlineStateTracker to transition from FSTOnlineState Unknown to Offline + * after a set timeout, rather than waiting indefinitely for success or failure. + */ + FSTTimerIDOnlineStateTimeout }; /** @@ -80,6 +93,13 @@ typedef NS_ENUM(NSInteger, FSTTimerID) { */ - (void)dispatchAsyncAllowingSameQueue:(void (^)(void))block; +/** + * Wrapper for dispatch_sync(). Mostly meant for use in tests. + * + * @param block The block to run. + */ +- (void)dispatchSync:(void (^)(void))block; + /** * Schedules a callback after the specified delay. * diff --git a/Firestore/Source/Util/FSTDispatchQueue.mm b/Firestore/Source/Util/FSTDispatchQueue.mm index 5bd7f27..3184d29 100644 --- a/Firestore/Source/Util/FSTDispatchQueue.mm +++ b/Firestore/Source/Util/FSTDispatchQueue.mm @@ -187,6 +187,10 @@ NS_ASSUME_NONNULL_BEGIN dispatch_async(self.queue, block); } +- (void)dispatchSync:(void (^)(void))block { + dispatch_sync(self.queue, block); +} + - (FSTDelayedCallback *)dispatchAfterDelay:(NSTimeInterval)delay timerID:(FSTTimerID)timerID block:(void (^)(void))block { -- cgit v1.2.3