Skip to content

Commit 958f866

Browse files
Verifying the write handshake in FSTStreamTest
1 parent 7280673 commit 958f866

File tree

13 files changed

+249
-714
lines changed

13 files changed

+249
-714
lines changed

Firestore/Example/Firestore.xcodeproj/project.pbxproj

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@
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-
D5B25B66FE2390963E000FDE /* FIRTestDispatchQueue.m in Sources */ = {isa = PBXBuildFile; fileRef = D5B25F48D1536205E8E67F2A /* FIRTestDispatchQueue.m */; };
63-
D5B25EF6FF29F02249573A35 /* FIRTestDispatchQueue.m in Sources */ = {isa = PBXBuildFile; fileRef = D5B25F48D1536205E8E67F2A /* FIRTestDispatchQueue.m */; };
6462
DE03B2C91F2149D600A30B9C /* FSTTransactionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DE51B1C61F0D48AC0013853F /* FSTTransactionTests.m */; };
6563
DE03B2D41F2149D600A30B9C /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6003F5AF195388D20070C39A /* XCTest.framework */; };
6664
DE03B2D51F2149D600A30B9C /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6003F591195388D20070C39A /* UIKit.framework */; };
@@ -225,8 +223,6 @@
225223
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>"; };
226224
D3CC3DC5338DCAF43A211155 /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
227225
D5B25C0D4AADFCA3ADB883E4 /* FSTStreamTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FSTStreamTests.m; sourceTree = "<group>"; };
228-
D5B25C8BBBB84E96C06ED244 /* FIRTestDispatchQueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FIRTestDispatchQueue.h; sourceTree = "<group>"; };
229-
D5B25F48D1536205E8E67F2A /* FIRTestDispatchQueue.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FIRTestDispatchQueue.m; sourceTree = "<group>"; };
230226
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>"; };
231227
DE03B2E91F2149D600A30B9C /* Firestore_IntegrationTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Firestore_IntegrationTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
232228
DE03B3621F215E1600A30B9C /* CAcert.pem */ = {isa = PBXFileReference; lastKnownFileType = text; path = CAcert.pem; sourceTree = "<group>"; };
@@ -565,8 +561,6 @@
565561
DE51B18A1F0D48AC0013853F /* FSTUtilTests.m */,
566562
54E9282A1F339CAD00C1953E /* XCTestCase+Await.h */,
567563
54E9282B1F339CAD00C1953E /* XCTestCase+Await.m */,
568-
D5B25F48D1536205E8E67F2A /* FIRTestDispatchQueue.m */,
569-
D5B25C8BBBB84E96C06ED244 /* FIRTestDispatchQueue.h */,
570564
);
571565
path = Util;
572566
sourceTree = "<group>";
@@ -1181,7 +1175,6 @@
11811175
DE51B1CD1F0D48CD0013853F /* FSTDatabaseInfoTests.m in Sources */,
11821176
DE51B1F21F0D49140013853F /* FSTPathTests.m in Sources */,
11831177
DE51B1DD1F0D490D0013853F /* FSTLocalStoreTests.m in Sources */,
1184-
D5B25B66FE2390963E000FDE /* FIRTestDispatchQueue.m in Sources */,
11851178
);
11861179
runOnlyForDeploymentPostprocessing = 0;
11871180
};
@@ -1206,7 +1199,6 @@
12061199
DE03B2C91F2149D600A30B9C /* FSTTransactionTests.m in Sources */,
12071200
54DA12B11F315F3800DD57A1 /* FIRValidationTests.m in Sources */,
12081201
D5B2532E4676014F57A7EAB9 /* FSTStreamTests.m in Sources */,
1209-
D5B25EF6FF29F02249573A35 /* FIRTestDispatchQueue.m in Sources */,
12101202
);
12111203
runOnlyForDeploymentPostprocessing = 0;
12121204
};

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -845,22 +845,4 @@ - (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-
866848
@end

Firestore/Example/Tests/Integration/FSTStreamTests.m

Lines changed: 37 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@
1616

1717
#import <XCTest/XCTest.h>
1818

19-
#import <FirebaseCommunity/FIRLogger.h>
2019
#import <Firestore/FIRFirestoreSettings.h>
2120

2221
#import "Auth/FSTEmptyCredentialsProvider.h"
2322
#import "Core/FSTDatabaseInfo.h"
24-
#import "FIRTestDispatchQueue.h"
2523
#import "FSTHelpers.h"
2624
#import "FSTIntegrationTestCase.h"
2725
#import "Model/FSTDatabaseID.h"
2826
#import "Remote/FSTDatastore.h"
2927
#import "Util/FSTAssert.h"
28+
#import "Util/FSTDispatchQueue.h"
3029

3130
/** Exposes otherwise private methods for testing. */
3231
@interface FSTStream (Testing)
@@ -44,59 +43,60 @@ @interface FSTStreamStatusDelegate : NSObject <FSTWatchStreamDelegate, FSTWriteS
4443
@property(nonatomic, weak) XCTestExpectation *expectation;
4544
@property(nonatomic, weak, readonly) FSTStream *stream;
4645

47-
- (instancetype)initWithStream:(FSTStream *)stream NS_DESIGNATED_INITIALIZER;
48-
- (instancetype)init NS_UNAVAILABLE;
4946
@end
5047

5148
@implementation FSTStreamStatusDelegate
5249

53-
- (instancetype)initWithStream:(FSTStream *)stream {
50+
- (instancetype)init {
5451
if (self = [super init]) {
5552
_states = [NSMutableArray new];
56-
_stream = stream;
5753
}
5854

5955
return self;
6056
}
6157

62-
- (void)streamDidReceiveChange:(FSTWatchChange *)change
63-
snapshotVersion:(FSTSnapshotVersion *)snapshotVersion {
64-
[_states addObject:@"didReceiveChange"];
58+
- (void)watchStreamDidOpen {
59+
[_states addObject:@"watchStreamDidOpen"];
6560
[_expectation fulfill];
6661
_expectation = nil;
6762
}
6863

69-
- (void)streamDidOpen {
70-
[_states addObject:@"didOpen"];
64+
- (void)writeStreamDidOpen {
65+
[_states addObject:@"writeStreamDidOpen"];
7166
[_expectation fulfill];
7267
_expectation = nil;
7368
}
7469

75-
- (void)streamDidClose:(NSError *_Nullable)error {
76-
[_states addObject:@"didClose"];
70+
- (void)writeStreamDidCompleteHandshake {
71+
[_states addObject:@"writeStreamDidCompleteHandshake"];
7772
[_expectation fulfill];
7873
_expectation = nil;
7974
}
8075

81-
- (void)streamDidCompleteHandshake {
82-
[_states addObject:@"didCompleteHandshake"];
76+
- (void)writeStreamDidClose:(NSError *_Nullable)error {
77+
[_states addObject:@"writeStreamDidClose"];
8378
[_expectation fulfill];
8479
_expectation = nil;
8580
}
8681

87-
- (void)streamDidReceiveResponseWithVersion:(FSTSnapshotVersion *)commitVersion
88-
mutationResults:(NSArray<FSTMutationResult *> *)results {
89-
[_states addObject:@"didReceiveResponse"];
82+
- (void)watchStreamDidClose:(NSError *_Nullable)error {
83+
[_states addObject:@"watchStreamDidClose"];
9084
[_expectation fulfill];
9185
_expectation = nil;
9286
}
9387

94-
- (void)writeValue:(id)value {
95-
[self.stream writeValue:value];
88+
- (void)watchStreamDidChange:(FSTWatchChange *)change
89+
snapshotVersion:(FSTSnapshotVersion *)snapshotVersion {
90+
[_states addObject:@"watchStreamDidChange"];
91+
[_expectation fulfill];
92+
_expectation = nil;
9693
}
9794

98-
- (void)writesFinishedWithError:(NSError *)errorOrNil {
99-
[self.stream writesFinishedWithError:errorOrNil];
95+
- (void)writeStreamDidReceiveResponseWithVersion:(FSTSnapshotVersion *)commitVersion
96+
mutationResults:(NSArray<FSTMutationResult *> *)results {
97+
[_states addObject:@"writeStreamDidReceiveResponseWithVersion"];
98+
[_expectation fulfill];
99+
_expectation = nil;
100100
}
101101

102102
- (void)fulfillOnCallback:(XCTestExpectation *)expectation {
@@ -113,9 +113,9 @@ @interface FSTStreamTests : XCTestCase
113113
@implementation FSTStreamTests {
114114
dispatch_queue_t _testQueue;
115115
FSTDatabaseInfo *_databaseInfo;
116-
FSTTestDispatchQueue *_workerDispatchQueue;
117116
FSTEmptyCredentialsProvider *_credentials;
118117
FSTStreamStatusDelegate *_delegate;
118+
FSTDispatchQueue *_workerDispatchQueue;
119119

120120
/** Single mutation to send to the write stream. */
121121
NSArray<FSTMutation *> *_mutations;
@@ -136,7 +136,7 @@ - (void)setUp {
136136
host:settings.host
137137
sslEnabled:settings.sslEnabled];
138138
_testQueue = dispatch_queue_create("FSTStreamTestWorkerQueue", DISPATCH_QUEUE_SERIAL);
139-
_workerDispatchQueue = [[FSTTestDispatchQueue alloc] initWithQueue:_testQueue];
139+
_workerDispatchQueue = [FSTDispatchQueue queueWith:_testQueue];
140140
_credentials = [[FSTEmptyCredentialsProvider alloc] init];
141141
}
142142

@@ -149,20 +149,17 @@ - (FSTWriteStream *)setUpWriteStream {
149149
workerDispatchQueue:_workerDispatchQueue
150150
credentials:_credentials];
151151

152-
FSTWriteStream *stream = [datastore createWriteStream];
153-
_delegate = [[FSTStreamStatusDelegate alloc] initWithStream:stream];
154-
155-
return stream;
152+
_delegate = [FSTStreamStatusDelegate new];
153+
return [datastore createWriteStreamWithDelegate:_delegate];
156154
}
157155

158156
- (FSTWatchStream *)setUpWatchStream {
159157
FSTDatastore *datastore = [[FSTDatastore alloc] initWithDatabaseInfo:_databaseInfo
160158
workerDispatchQueue:_workerDispatchQueue
161159
credentials:_credentials];
162-
FSTWatchStream *stream = [datastore createWatchStream];
163-
_delegate = [[FSTStreamStatusDelegate alloc] initWithStream:stream];
164160

165-
return stream;
161+
_delegate = [FSTStreamStatusDelegate new];
162+
return [datastore createWatchStreamWithDelegate:_delegate];
166163
}
167164

168165
- (void)verifyDelegate:(NSArray<NSString *> *)expectedStates {
@@ -180,7 +177,7 @@ - (void)testWatchStreamStopBeforeHandshake {
180177
XCTestExpectation *openExpectation = [self expectationWithDescription:@"open"];
181178
[_delegate fulfillOnCallback:openExpectation];
182179
[_workerDispatchQueue dispatchAsync:^{
183-
[watchStream start:_delegate];
180+
[watchStream start];
184181
}];
185182
[self awaitExpectations];
186183

@@ -193,7 +190,7 @@ - (void)testWatchStreamStopBeforeHandshake {
193190
// Simulate a final callback from GRPC
194191
[watchStream writesFinishedWithError:nil];
195192

196-
[self verifyDelegate:@[ @"didOpen" ]];
193+
[self verifyDelegate:@[ @"watchStreamDidOpen" ]];
197194
}
198195

199196
/** Verifies that the write stream does not issue an onClose callback after a call to stop(). */
@@ -203,7 +200,7 @@ - (void)testWriteStreamStopBeforeHandshake {
203200
XCTestExpectation *openExpectation = [self expectationWithDescription:@"open"];
204201
[_delegate fulfillOnCallback:openExpectation];
205202
[_workerDispatchQueue dispatchAsync:^{
206-
[writeStream start:_delegate];
203+
[writeStream start];
207204
}];
208205
[self awaitExpectations];
209206

@@ -218,7 +215,7 @@ - (void)testWriteStreamStopBeforeHandshake {
218215
// Simulate a final callback from GRPC
219216
[writeStream writesFinishedWithError:nil];
220217

221-
[self verifyDelegate:@[ @"didOpen" ]];
218+
[self verifyDelegate:@[ @"writeStreamDidOpen" ]];
222219
}
223220

224221
- (void)testWriteStreamStopAfterHandshake {
@@ -227,7 +224,7 @@ - (void)testWriteStreamStopAfterHandshake {
227224
XCTestExpectation *openExpectation = [self expectationWithDescription:@"open"];
228225
[_delegate fulfillOnCallback:openExpectation];
229226
[_workerDispatchQueue dispatchAsync:^{
230-
[writeStream start:_delegate];
227+
[writeStream start];
231228
}];
232229
[self awaitExpectations];
233230

@@ -255,79 +252,10 @@ - (void)testWriteStreamStopAfterHandshake {
255252
[writeStream stop];
256253
}];
257254

258-
[self verifyDelegate:@[ @"didOpen", @"didCompleteHandshake", @"didReceiveResponse" ]];
259-
}
260-
261-
- (void)testStreamClosesWhenIdle {
262-
FSTWriteStream *writeStream = [self setUpWriteStream];
263-
264-
XCTestExpectation *openExpectation = [self expectationWithDescription:@"open"];
265-
[_delegate fulfillOnCallback:openExpectation];
266-
[_workerDispatchQueue dispatchAsync:^{
267-
[writeStream start:_delegate];
268-
}];
269-
[self awaitExpectations];
270-
271-
XCTestExpectation *handshakeExpectation = [self expectationWithDescription:@"handshake"];
272-
[_delegate fulfillOnCallback:handshakeExpectation];
273-
[_workerDispatchQueue dispatchAsync:^{
274-
[writeStream writeHandshake];
275-
}];
276-
[self awaitExpectations];
277-
278-
XCTestExpectation *closeExpectation = [self expectationWithDescription:@"close"];
279-
[_delegate fulfillOnCallback:closeExpectation];
280-
[_workerDispatchQueue dispatchAsync:^{
281-
[writeStream markIdle];
282-
}];
283-
[self awaitExpectations];
284-
285-
dispatch_sync(_testQueue, ^{
286-
XCTAssertFalse([writeStream isOpen]);
287-
});
288-
289-
[self verifyDelegate:@[ @"didOpen", @"didCompleteHandshake", @"didClose" ]];
290-
}
291-
292-
- (void)testStreamCancelsIdleOnWrite {
293-
FSTWriteStream *writeStream = [self setUpWriteStream];
294-
295-
XCTestExpectation *openExpectation = [self expectationWithDescription:@"open"];
296-
[_delegate fulfillOnCallback:openExpectation];
297-
[_workerDispatchQueue dispatchAsync:^{
298-
[writeStream start:_delegate];
299-
}];
300-
[self awaitExpectations];
301-
302-
XCTestExpectation *handshakeExpectation = [self expectationWithDescription:@"handshake"];
303-
[_delegate fulfillOnCallback:handshakeExpectation];
304-
[_workerDispatchQueue dispatchAsync:^{
305-
[writeStream writeHandshake];
306-
}];
307-
[self awaitExpectations];
308-
309-
// Mark the stream idle, but immediately cancel the idle timer by issuing another write.
310-
XCTestExpectation *idleExpectation = [self expectationWithDescription:@"idle"];
311-
[_workerDispatchQueue fulfillOnExecution:idleExpectation];
312-
[_workerDispatchQueue dispatchAsync:^{
313-
[writeStream markIdle];
314-
}];
315-
XCTestExpectation *writeExpectation = [self expectationWithDescription:@"write"];
316-
[_delegate fulfillOnCallback:writeExpectation];
317-
[_workerDispatchQueue dispatchAsync:^{
318-
[writeStream writeMutations:_mutations];
319-
}];
320-
[self awaitExpectations];
321-
322-
dispatch_sync(_testQueue, ^{
323-
XCTAssertTrue([writeStream isOpen]);
324-
});
325-
326-
[_workerDispatchQueue dispatchAsync:^{
327-
[writeStream stop];
328-
}];
329-
330-
[self verifyDelegate:@[ @"didOpen", @"didCompleteHandshake", @"didReceiveResponse" ]];
255+
[self verifyDelegate:@[
256+
@"writeStreamDidOpen", @"writeStreamDidCompleteHandshake",
257+
@"writeStreamDidReceiveResponseWithVersion"
258+
]];
331259
}
332260

333261
@end

0 commit comments

Comments
 (0)