16
16
17
17
#import < XCTest/XCTest.h>
18
18
19
- #import < FirebaseCommunity/FIRLogger.h>
20
19
#import < Firestore/FIRFirestoreSettings.h>
21
20
22
21
#import " Auth/FSTEmptyCredentialsProvider.h"
23
22
#import " Core/FSTDatabaseInfo.h"
24
- #import " FIRTestDispatchQueue.h"
25
23
#import " FSTHelpers.h"
26
24
#import " FSTIntegrationTestCase.h"
27
25
#import " Model/FSTDatabaseID.h"
28
26
#import " Remote/FSTDatastore.h"
29
27
#import " Util/FSTAssert.h"
28
+ #import " Util/FSTDispatchQueue.h"
30
29
31
30
/* * Exposes otherwise private methods for testing. */
32
31
@interface FSTStream (Testing)
@@ -44,59 +43,60 @@ @interface FSTStreamStatusDelegate : NSObject <FSTWatchStreamDelegate, FSTWriteS
44
43
@property (nonatomic , weak ) XCTestExpectation *expectation;
45
44
@property (nonatomic , weak , readonly ) FSTStream *stream;
46
45
47
- - (instancetype )initWithStream : (FSTStream *)stream NS_DESIGNATED_INITIALIZER;
48
- - (instancetype )init NS_UNAVAILABLE;
49
46
@end
50
47
51
48
@implementation FSTStreamStatusDelegate
52
49
53
- - (instancetype )initWithStream : (FSTStream *) stream {
50
+ - (instancetype )init {
54
51
if (self = [super init ]) {
55
52
_states = [NSMutableArray new ];
56
- _stream = stream;
57
53
}
58
54
59
55
return self;
60
56
}
61
57
62
- - (void )streamDidReceiveChange : (FSTWatchChange *)change
63
- snapshotVersion : (FSTSnapshotVersion *)snapshotVersion {
64
- [_states addObject: @" didReceiveChange" ];
58
+ - (void )watchStreamDidOpen {
59
+ [_states addObject: @" watchStreamDidOpen" ];
65
60
[_expectation fulfill ];
66
61
_expectation = nil ;
67
62
}
68
63
69
- - (void )streamDidOpen {
70
- [_states addObject: @" didOpen " ];
64
+ - (void )writeStreamDidOpen {
65
+ [_states addObject: @" writeStreamDidOpen " ];
71
66
[_expectation fulfill ];
72
67
_expectation = nil ;
73
68
}
74
69
75
- - (void )streamDidClose : ( NSError *_Nullable) error {
76
- [_states addObject: @" didClose " ];
70
+ - (void )writeStreamDidCompleteHandshake {
71
+ [_states addObject: @" writeStreamDidCompleteHandshake " ];
77
72
[_expectation fulfill ];
78
73
_expectation = nil ;
79
74
}
80
75
81
- - (void )streamDidCompleteHandshake {
82
- [_states addObject: @" didCompleteHandshake " ];
76
+ - (void )writeStreamDidClose : ( NSError *_Nullable) error {
77
+ [_states addObject: @" writeStreamDidClose " ];
83
78
[_expectation fulfill ];
84
79
_expectation = nil ;
85
80
}
86
81
87
- - (void )streamDidReceiveResponseWithVersion : (FSTSnapshotVersion *)commitVersion
88
- mutationResults : (NSArray <FSTMutationResult *> *)results {
89
- [_states addObject: @" didReceiveResponse" ];
82
+ - (void )watchStreamDidClose : (NSError *_Nullable)error {
83
+ [_states addObject: @" watchStreamDidClose" ];
90
84
[_expectation fulfill ];
91
85
_expectation = nil ;
92
86
}
93
87
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 ;
96
93
}
97
94
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 ;
100
100
}
101
101
102
102
- (void )fulfillOnCallback : (XCTestExpectation *)expectation {
@@ -113,9 +113,9 @@ @interface FSTStreamTests : XCTestCase
113
113
@implementation FSTStreamTests {
114
114
dispatch_queue_t _testQueue;
115
115
FSTDatabaseInfo *_databaseInfo;
116
- FSTTestDispatchQueue *_workerDispatchQueue;
117
116
FSTEmptyCredentialsProvider *_credentials;
118
117
FSTStreamStatusDelegate *_delegate;
118
+ FSTDispatchQueue *_workerDispatchQueue;
119
119
120
120
/* * Single mutation to send to the write stream. */
121
121
NSArray <FSTMutation *> *_mutations;
@@ -136,7 +136,7 @@ - (void)setUp {
136
136
host: settings.host
137
137
sslEnabled: settings.sslEnabled];
138
138
_testQueue = dispatch_queue_create (" FSTStreamTestWorkerQueue" , DISPATCH_QUEUE_SERIAL);
139
- _workerDispatchQueue = [[FSTTestDispatchQueue alloc ] initWithQueue : _testQueue];
139
+ _workerDispatchQueue = [FSTDispatchQueue queueWith : _testQueue];
140
140
_credentials = [[FSTEmptyCredentialsProvider alloc ] init ];
141
141
}
142
142
@@ -149,20 +149,17 @@ - (FSTWriteStream *)setUpWriteStream {
149
149
workerDispatchQueue: _workerDispatchQueue
150
150
credentials: _credentials];
151
151
152
- FSTWriteStream *stream = [datastore createWriteStream ];
153
- _delegate = [[FSTStreamStatusDelegate alloc ] initWithStream: stream];
154
-
155
- return stream;
152
+ _delegate = [FSTStreamStatusDelegate new ];
153
+ return [datastore createWriteStreamWithDelegate: _delegate];
156
154
}
157
155
158
156
- (FSTWatchStream *)setUpWatchStream {
159
157
FSTDatastore *datastore = [[FSTDatastore alloc ] initWithDatabaseInfo: _databaseInfo
160
158
workerDispatchQueue: _workerDispatchQueue
161
159
credentials: _credentials];
162
- FSTWatchStream *stream = [datastore createWatchStream ];
163
- _delegate = [[FSTStreamStatusDelegate alloc ] initWithStream: stream];
164
160
165
- return stream;
161
+ _delegate = [FSTStreamStatusDelegate new ];
162
+ return [datastore createWatchStreamWithDelegate: _delegate];
166
163
}
167
164
168
165
- (void )verifyDelegate : (NSArray <NSString *> *)expectedStates {
@@ -180,7 +177,7 @@ - (void)testWatchStreamStopBeforeHandshake {
180
177
XCTestExpectation *openExpectation = [self expectationWithDescription: @" open" ];
181
178
[_delegate fulfillOnCallback: openExpectation];
182
179
[_workerDispatchQueue dispatchAsync: ^{
183
- [watchStream start: _delegate ];
180
+ [watchStream start ];
184
181
}];
185
182
[self awaitExpectations ];
186
183
@@ -193,7 +190,7 @@ - (void)testWatchStreamStopBeforeHandshake {
193
190
// Simulate a final callback from GRPC
194
191
[watchStream writesFinishedWithError: nil ];
195
192
196
- [self verifyDelegate: @[ @" didOpen " ]];
193
+ [self verifyDelegate: @[ @" watchStreamDidOpen " ]];
197
194
}
198
195
199
196
/* * Verifies that the write stream does not issue an onClose callback after a call to stop(). */
@@ -203,7 +200,7 @@ - (void)testWriteStreamStopBeforeHandshake {
203
200
XCTestExpectation *openExpectation = [self expectationWithDescription: @" open" ];
204
201
[_delegate fulfillOnCallback: openExpectation];
205
202
[_workerDispatchQueue dispatchAsync: ^{
206
- [writeStream start: _delegate ];
203
+ [writeStream start ];
207
204
}];
208
205
[self awaitExpectations ];
209
206
@@ -218,7 +215,7 @@ - (void)testWriteStreamStopBeforeHandshake {
218
215
// Simulate a final callback from GRPC
219
216
[writeStream writesFinishedWithError: nil ];
220
217
221
- [self verifyDelegate: @[ @" didOpen " ]];
218
+ [self verifyDelegate: @[ @" writeStreamDidOpen " ]];
222
219
}
223
220
224
221
- (void )testWriteStreamStopAfterHandshake {
@@ -227,7 +224,7 @@ - (void)testWriteStreamStopAfterHandshake {
227
224
XCTestExpectation *openExpectation = [self expectationWithDescription: @" open" ];
228
225
[_delegate fulfillOnCallback: openExpectation];
229
226
[_workerDispatchQueue dispatchAsync: ^{
230
- [writeStream start: _delegate ];
227
+ [writeStream start ];
231
228
}];
232
229
[self awaitExpectations ];
233
230
@@ -255,79 +252,10 @@ - (void)testWriteStreamStopAfterHandshake {
255
252
[writeStream stop ];
256
253
}];
257
254
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
+ ]];
331
259
}
332
260
333
261
@end
0 commit comments