Skip to content

Commit 1c078fe

Browse files
Adding missing files
1 parent 9d745d2 commit 1c078fe

File tree

10 files changed

+209
-3
lines changed

10 files changed

+209
-3
lines changed

Firestore/Example/Firestore.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
AFE6114F0D4DAECBA7B7C089 /* Pods_Firestore_IntegrationTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B2FA635DF5D116A67A7441CD /* Pods_Firestore_IntegrationTests.framework */; };
6060
C4E749275AD0FBDF9F4716A8 /* Pods_SwiftBuildTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 32AD40BF6B0E849B07FFD05E /* Pods_SwiftBuildTest.framework */; };
6161
D5B2532E4676014F57A7EAB9 /* FSTStreamTests.m in Sources */ = {isa = PBXBuildFile; fileRef = D5B25C0D4AADFCA3ADB883E4 /* FSTStreamTests.m */; };
62+
D5B25474286C9800CE42B8C2 /* FSTTestDispatchQueue.m in Sources */ = {isa = PBXBuildFile; fileRef = D5B25292CED31B81FDED0411 /* FSTTestDispatchQueue.m */; };
63+
D5B259FDEE8094E8D710C5BF /* FSTTestDispatchQueue.m in Sources */ = {isa = PBXBuildFile; fileRef = D5B25292CED31B81FDED0411 /* FSTTestDispatchQueue.m */; };
6264
DE03B2C91F2149D600A30B9C /* FSTTransactionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DE51B1C61F0D48AC0013853F /* FSTTransactionTests.m */; };
6365
DE03B2D41F2149D600A30B9C /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6003F5AF195388D20070C39A /* XCTest.framework */; };
6466
DE03B2D51F2149D600A30B9C /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6003F591195388D20070C39A /* UIKit.framework */; };
@@ -222,6 +224,8 @@
222224
B2FA635DF5D116A67A7441CD /* Pods_Firestore_IntegrationTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Firestore_IntegrationTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
223225
CE00BABB5A3AAB44A4C209E2 /* Pods-Firestore_Tests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Firestore_Tests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Firestore_Tests/Pods-Firestore_Tests.debug.xcconfig"; sourceTree = "<group>"; };
224226
D3CC3DC5338DCAF43A211155 /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
227+
D5B25292CED31B81FDED0411 /* FSTTestDispatchQueue.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FSTTestDispatchQueue.m; sourceTree = "<group>"; };
228+
D5B259DAA9149B80D6245B57 /* FSTTestDispatchQueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FSTTestDispatchQueue.h; sourceTree = "<group>"; };
225229
D5B25C0D4AADFCA3ADB883E4 /* FSTStreamTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FSTStreamTests.m; sourceTree = "<group>"; };
226230
DB17FEDFB80770611A935A60 /* Pods-Firestore_IntegrationTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Firestore_IntegrationTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-Firestore_IntegrationTests/Pods-Firestore_IntegrationTests.release.xcconfig"; sourceTree = "<group>"; };
227231
DE03B2E91F2149D600A30B9C /* Firestore_IntegrationTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Firestore_IntegrationTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -561,6 +565,8 @@
561565
DE51B18A1F0D48AC0013853F /* FSTUtilTests.m */,
562566
54E9282A1F339CAD00C1953E /* XCTestCase+Await.h */,
563567
54E9282B1F339CAD00C1953E /* XCTestCase+Await.m */,
568+
D5B259DAA9149B80D6245B57 /* FSTTestDispatchQueue.h */,
569+
D5B25292CED31B81FDED0411 /* FSTTestDispatchQueue.m */,
564570
);
565571
path = Util;
566572
sourceTree = "<group>";
@@ -1175,6 +1181,7 @@
11751181
DE51B1CD1F0D48CD0013853F /* FSTDatabaseInfoTests.m in Sources */,
11761182
DE51B1F21F0D49140013853F /* FSTPathTests.m in Sources */,
11771183
DE51B1DD1F0D490D0013853F /* FSTLocalStoreTests.m in Sources */,
1184+
D5B25474286C9800CE42B8C2 /* FSTTestDispatchQueue.m in Sources */,
11781185
);
11791186
runOnlyForDeploymentPostprocessing = 0;
11801187
};
@@ -1199,6 +1206,7 @@
11991206
DE03B2C91F2149D600A30B9C /* FSTTransactionTests.m in Sources */,
12001207
54DA12B11F315F3800DD57A1 /* FIRValidationTests.m in Sources */,
12011208
D5B2532E4676014F57A7EAB9 /* FSTStreamTests.m in Sources */,
1209+
D5B259FDEE8094E8D710C5BF /* FSTTestDispatchQueue.m in Sources */,
12021210
);
12031211
runOnlyForDeploymentPostprocessing = 0;
12041212
};

Firestore/Example/Tests/Integration/API/FIRDatabaseTests.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,4 +845,22 @@ - (void)testCantGetDocumentsWhileOffline {
845845
[self awaitExpectations];
846846
}
847847

848+
- (void)testWriteStreamReconnectsAfterIdle {
849+
FIRDocumentReference *doc = [self documentRef];
850+
FIRFirestore *firestore = doc.firestore;
851+
852+
[self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
853+
[self waitForIdle:firestore];
854+
[self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
855+
}
856+
857+
- (void)testWatchStreamReconnectsAfterIdle {
858+
FIRDocumentReference *doc = [self documentRef];
859+
FIRFirestore *firestore = doc.firestore;
860+
861+
[self readSnapshotForRef:[self documentRef] online:YES];
862+
[self waitForIdle:firestore];
863+
[self readSnapshotForRef:[self documentRef] online:YES];
864+
}
865+
848866
@end

Firestore/Example/Tests/Integration/FSTStreamTests.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
#import "Auth/FSTEmptyCredentialsProvider.h"
2222
#import "Core/FSTDatabaseInfo.h"
23-
#import "FIRTestDispatchQueue.h"
23+
#import "FSTTestDispatchQueue.h"
2424
#import "FSTHelpers.h"
2525
#import "FSTIntegrationTestCase.h"
2626
#import "Model/FSTDatabaseID.h"

Firestore/Example/Tests/Util/FSTIntegrationTestCase.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ NS_ASSUME_NONNULL_BEGIN
5656
- (FIRCollectionReference *)collectionRefWithDocuments:
5757
(NSDictionary<NSString *, NSDictionary<NSString *, id> *> *)documents;
5858

59+
- (void)waitForIdle:(FIRFirestore *)firestore;
60+
5961
- (void)writeAllDocuments:(NSDictionary<NSString *, NSDictionary<NSString *, id> *> *)documents
6062
toCollection:(FIRCollectionReference *)collection;
6163

@@ -67,6 +69,8 @@ NS_ASSUME_NONNULL_BEGIN
6769

6870
- (FIRQuerySnapshot *)readDocumentSetForRef:(FIRQuery *)query;
6971

72+
- (FIRDocumentSnapshot *)readSnapshotForRef:(FIRDocumentReference *)query online:(BOOL)online;
73+
7074
- (void)writeDocumentRef:(FIRDocumentReference *)ref data:(NSDictionary<NSString *, id> *)data;
7175

7276
- (void)updateDocumentRef:(FIRDocumentReference *)ref data:(NSDictionary<NSString *, id> *)data;

Firestore/Example/Tests/Util/FSTIntegrationTestCase.m

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,14 @@
3030
#import "Util/FSTUtil.h"
3131

3232
#import "FSTEventAccumulator.h"
33+
#import "FSTTestDispatchQueue.h"
3334

3435
NS_ASSUME_NONNULL_BEGIN
3536

37+
@interface FIRFirestore (Testing)
38+
@property(nonatomic, strong) FSTDispatchQueue *workerDispatchQueue;
39+
@end
40+
3641
@implementation FSTIntegrationTestCase {
3742
NSMutableArray<FIRFirestore *> *_firestores;
3843
}
@@ -121,7 +126,7 @@ + (FIRFirestoreSettings *)settings {
121126
- (FIRFirestore *)firestoreWithProjectID:(NSString *)projectID {
122127
NSString *persistenceKey = [NSString stringWithFormat:@"db%lu", (unsigned long)_firestores.count];
123128

124-
FSTDispatchQueue *workerDispatchQueue = [FSTDispatchQueue
129+
FSTTestDispatchQueue *workerDispatchQueue = [FSTTestDispatchQueue
125130
queueWith:dispatch_queue_create("com.google.firebase.firestore", DISPATCH_QUEUE_SERIAL)];
126131

127132
FSTEmptyCredentialsProvider *credentialsProvider = [[FSTEmptyCredentialsProvider alloc] init];
@@ -142,6 +147,14 @@ - (FIRFirestore *)firestoreWithProjectID:(NSString *)projectID {
142147
return firestore;
143148
}
144149

150+
- (void)waitForIdle:(FIRFirestore *)firestore {
151+
XCTestExpectation *expectation = [self expectationWithDescription:@"idle"];
152+
// Note that we wait on any task that is scheduled with a delay. Currently, the idle timeout is
153+
// the only task that uses delays.
154+
[((FSTTestDispatchQueue *)firestore.workerDispatchQueue) fulfillOnExecution:expectation];
155+
[self awaitExpectations];
156+
}
157+
145158
- (void)shutdownFirestore:(FIRFirestore *)firestore {
146159
XCTestExpectation *shutdownCompletion = [self expectationWithDescription:@"shutdown"];
147160
[firestore shutdownWithCompletion:^(NSError *_Nullable error) {
@@ -222,6 +235,26 @@ - (FIRQuerySnapshot *)readDocumentSetForRef:(FIRQuery *)query {
222235
return result;
223236
}
224237

238+
- (FIRDocumentSnapshot *)readSnapshotForRef:(FIRDocumentReference *)ref online:(BOOL)online {
239+
__block FIRDocumentSnapshot *result;
240+
241+
XCTestExpectation *expectation = [self expectationWithDescription:@"listener"];
242+
id<FIRListenerRegistration> listener = [ref
243+
addSnapshotListenerWithOptions:[[FIRDocumentListenOptions options] includeMetadataChanges:YES]
244+
listener:^(FIRDocumentSnapshot *snapshot, NSError *error) {
245+
XCTAssertNil(error);
246+
if (!online || !snapshot.metadata.fromCache) {
247+
result = snapshot;
248+
[expectation fulfill];
249+
}
250+
}];
251+
252+
[self awaitExpectations];
253+
[listener remove];
254+
255+
return result;
256+
}
257+
225258
- (void)writeDocumentRef:(FIRDocumentReference *)ref data:(NSDictionary<NSString *, id> *)data {
226259
XCTestExpectation *expectation = [self expectationWithDescription:@"setData"];
227260
[ref setData:data
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2017 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import "Util/FSTDispatchQueue.h"
18+
19+
@class XCTestExpectation;
20+
21+
NS_ASSUME_NONNULL_BEGIN
22+
23+
/**
24+
* Dispatch queue used in the idle timeout tests that caps delayed executions at 1.0 seconds.
25+
*/
26+
@interface FSTTestDispatchQueue : FSTDispatchQueue
27+
28+
/** Creates and returns an FSTTestDispatchQueue wrapping the specified dispatch_queue_t. */
29+
+ (instancetype)queueWith:(dispatch_queue_t)dispatchQueue;
30+
31+
/**
32+
* Registers a test expectation that is fulfilled when the next delayed callback finished
33+
* executing.
34+
*/
35+
- (void)fulfillOnExecution:(XCTestExpectation *)expectation;
36+
37+
@end
38+
39+
NS_ASSUME_NONNULL_END
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2017 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import "FSTTestDispatchQueue.h"
18+
19+
#import <XCTest/XCTestExpectation.h>
20+
21+
#import "Util/FSTAssert.h"
22+
23+
@interface FSTTestDispatchQueue ()
24+
25+
@property(nonatomic, weak) XCTestExpectation* expectation;
26+
27+
@end
28+
29+
@implementation FSTTestDispatchQueue
30+
31+
+ (instancetype)queueWith:(dispatch_queue_t)dispatchQueue {
32+
return [[FSTTestDispatchQueue alloc] initWithQueue:dispatchQueue];
33+
}
34+
35+
- (instancetype)initWithQueue:(dispatch_queue_t)dispatchQueue {
36+
return (self = [super initWithQueue:dispatchQueue]);
37+
}
38+
39+
- (void)dispatchAsync:(void (^)(void))block after:(NSTimeInterval)delay {
40+
[super dispatchAsyncAllowingSameQueue:^() {
41+
block();
42+
[_expectation fulfill];
43+
_expectation = nil;
44+
}
45+
after:MIN(delay, 1.0)];
46+
}
47+
48+
- (void)dispatchAsyncAllowingSameQueue:(void (^)(void))block after:(NSTimeInterval)delay {
49+
[super dispatchAsyncAllowingSameQueue:^() {
50+
block();
51+
[_expectation fulfill];
52+
_expectation = nil;
53+
}
54+
after:MIN(delay, 1.0)];
55+
}
56+
57+
- (void)fulfillOnExecution:(XCTestExpectation*)expectation {
58+
FSTAssert(_expectation == nil, @"Previous expectation still active");
59+
_expectation = expectation;
60+
}
61+
62+
@end

Firestore/Source/Remote/FSTDatastore.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ NS_ASSUME_NONNULL_BEGIN
141141
*/
142142
@interface FSTStream <__covariant FSTStreamDelegate> : NSObject
143143

144-
@property (nonatomic, strong, readwrite) FSTStreamDelegate delegate;
144+
@property (nonatomic, weak, readwrite, nullable) FSTStreamDelegate delegate;
145145

146146
- (instancetype)initWithDatabase:(FSTDatabaseInfo *)database
147147
workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue

Firestore/Source/Util/FSTDispatchQueue.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ NS_ASSUME_NONNULL_BEGIN
2323
/** Creates and returns an FSTDispatchQueue wrapping the specified dispatch_queue_t. */
2424
+ (instancetype)queueWith:(dispatch_queue_t)dispatchQueue;
2525

26+
- (instancetype)initWithQueue:(dispatch_queue_t)queue NS_DESIGNATED_INITIALIZER;
27+
2628
- (instancetype)init __attribute__((unavailable("Use static constructor method.")));
2729

2830
/**
@@ -39,6 +41,17 @@ NS_ASSUME_NONNULL_BEGIN
3941
*/
4042
- (void)dispatchAsync:(void (^)(void))block;
4143

44+
/**
45+
* Schedules a callback after the specified delay.
46+
*
47+
* Same as dispatch_after() except it asserts that we're not already on the queue, since this
48+
* generally indicates a bug (and can lead to re-ordering of operations, etc).
49+
*
50+
* @param block The block to run.
51+
* @param delay The delay (in seconds) after which to run the block.
52+
*/
53+
- (void)dispatchAsync:(void (^)(void))block after:(NSTimeInterval)delay;
54+
4255
/**
4356
* Unlike dispatchAsync: this method does not require you to dispatch to a different queue than
4457
* the current one (thus it is equivalent to a raw dispatch_async()).
@@ -50,6 +63,20 @@ NS_ASSUME_NONNULL_BEGIN
5063
*/
5164
- (void)dispatchAsyncAllowingSameQueue:(void (^)(void))block;
5265

66+
/**
67+
* Schedules a callback after the specified delay.
68+
*
69+
* Unlike dispatchAsync: this method does not require you to dispatch to a different queue than
70+
* the current one (thus it is equivalent to a raw dispatch_async()).
71+
*
72+
* This is useful, e.g. for dispatching to the user's queue directly from user API call (in which
73+
* case we don't know if we're already on the user's queue or not).
74+
*
75+
* @param block The block to run.
76+
* @param delay The delay (in seconds) after which to run the block.
77+
*/
78+
- (void)dispatchAsyncAllowingSameQueue:(void (^)(void))block after:(NSTimeInterval)delay;
79+
5380
/** The underlying wrapped dispatch_queue_t */
5481
@property(nonatomic, strong, readonly) dispatch_queue_t queue;
5582

Firestore/Source/Util/FSTDispatchQueue.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,25 @@ - (void)dispatchAsync:(void (^)(void))block {
5252
dispatch_async(self.queue, block);
5353
}
5454

55+
- (void)dispatchAsync:(void (^)(void))block after:(NSTimeInterval)delay {
56+
FSTAssert(
57+
![self onTargetQueue],
58+
@"dispatchAsync:after: called when we are already running on target dispatch queue '%@'",
59+
[self targetQueueLabel]);
60+
61+
dispatch_time_t delayNs = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC));
62+
dispatch_after(delayNs, self.queue, block);
63+
}
64+
5565
- (void)dispatchAsyncAllowingSameQueue:(void (^)(void))block {
5666
dispatch_async(self.queue, block);
5767
}
5868

69+
- (void)dispatchAsyncAllowingSameQueue:(void (^)(void))block after:(NSTimeInterval)delay {
70+
dispatch_time_t delayNs = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC));
71+
dispatch_after(delayNs, self.queue, block);
72+
}
73+
5974
#pragma mark - Private Methods
6075

6176
- (NSString *)currentQueueLabel {

0 commit comments

Comments
 (0)