21
21
22
22
#import " Auth/FSTEmptyCredentialsProvider.h"
23
23
#import " Core/FSTDatabaseInfo.h"
24
- #import " FIRTestDispatchQueue.h"
25
24
#import " FSTHelpers.h"
26
25
#import " FSTIntegrationTestCase.h"
27
26
#import " Model/FSTDatabaseID.h"
28
27
#import " Remote/FSTDatastore.h"
29
28
#import " Util/FSTAssert.h"
29
+ #import " Util/FSTDispatchQueue.h"
30
30
31
31
/* * Exposes otherwise private methods for testing. */
32
32
@interface FSTStream (Testing)
@@ -44,59 +44,59 @@ @interface FSTStreamStatusDelegate : NSObject <FSTWatchStreamDelegate, FSTWriteS
44
44
@property (nonatomic , weak ) XCTestExpectation *expectation;
45
45
@property (nonatomic , weak , readonly ) FSTStream *stream;
46
46
47
- - (instancetype )initWithStream : (FSTStream *)stream NS_DESIGNATED_INITIALIZER;
48
- - (instancetype )init NS_UNAVAILABLE;
49
47
@end
50
48
51
49
@implementation FSTStreamStatusDelegate
52
50
53
- - (instancetype )initWithStream : (FSTStream *) stream {
51
+ - (instancetype )init {
54
52
if (self = [super init ]) {
55
53
_states = [NSMutableArray new ];
56
- _stream = stream;
57
54
}
58
55
59
56
return self;
60
57
}
61
58
62
- - (void )streamDidReceiveChange : (FSTWatchChange *)change
63
- snapshotVersion : (FSTSnapshotVersion *)snapshotVersion {
64
- [_states addObject: @" didReceiveChange" ];
59
+ - (void )watchStreamDidOpen {
60
+ [_states addObject: @" didOpen" ];
65
61
[_expectation fulfill ];
66
62
_expectation = nil ;
67
63
}
68
64
69
- - (void )streamDidOpen {
65
+ - (void )writeStreamDidOpen {
70
66
[_states addObject: @" didOpen" ];
71
67
[_expectation fulfill ];
72
68
_expectation = nil ;
73
69
}
74
70
75
- - (void )streamDidClose : ( NSError *_Nullable) error {
76
- [_states addObject: @" didClose " ];
71
+ - (void )writeStreamDidCompleteHandshake {
72
+ [_states addObject: @" didCompleteHandshake " ];
77
73
[_expectation fulfill ];
78
74
_expectation = nil ;
79
75
}
80
-
81
- - (void )streamDidCompleteHandshake {
82
- [_states addObject: @" didCompleteHandshake" ];
76
+ - (void )writeStreamDidClose : (NSError *_Nullable)error {
77
+ [_states addObject: @" didClose" ];
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: @" didClose" ];
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: @" didReceiveChange" ];
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: @" didReceiveResponse" ];
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,19 @@ - (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];
154
+ ;
156
155
}
157
156
158
157
- (FSTWatchStream *)setUpWatchStream {
159
158
FSTDatastore *datastore = [[FSTDatastore alloc ] initWithDatabaseInfo: _databaseInfo
160
159
workerDispatchQueue: _workerDispatchQueue
161
160
credentials: _credentials];
162
- FSTWatchStream *stream = [datastore createWatchStream ];
163
- _delegate = [[FSTStreamStatusDelegate alloc ] initWithStream: stream];
164
161
165
- return stream;
162
+ _delegate = [FSTStreamStatusDelegate new ];
163
+ return [datastore createWatchStreamWithDelegate: _delegate];
164
+ ;
166
165
}
167
166
168
167
- (void )verifyDelegate : (NSArray <NSString *> *)expectedStates {
@@ -180,7 +179,7 @@ - (void)testWatchStreamStopBeforeHandshake {
180
179
XCTestExpectation *openExpectation = [self expectationWithDescription: @" open" ];
181
180
[_delegate fulfillOnCallback: openExpectation];
182
181
[_workerDispatchQueue dispatchAsync: ^{
183
- [watchStream start: _delegate ];
182
+ [watchStream start ];
184
183
}];
185
184
[self awaitExpectations ];
186
185
@@ -203,7 +202,7 @@ - (void)testWriteStreamStopBeforeHandshake {
203
202
XCTestExpectation *openExpectation = [self expectationWithDescription: @" open" ];
204
203
[_delegate fulfillOnCallback: openExpectation];
205
204
[_workerDispatchQueue dispatchAsync: ^{
206
- [writeStream start: _delegate ];
205
+ [writeStream start ];
207
206
}];
208
207
[self awaitExpectations ];
209
208
@@ -227,7 +226,7 @@ - (void)testWriteStreamStopAfterHandshake {
227
226
XCTestExpectation *openExpectation = [self expectationWithDescription: @" open" ];
228
227
[_delegate fulfillOnCallback: openExpectation];
229
228
[_workerDispatchQueue dispatchAsync: ^{
230
- [writeStream start: _delegate ];
229
+ [writeStream start ];
231
230
}];
232
231
[self awaitExpectations ];
233
232
@@ -258,76 +257,4 @@ - (void)testWriteStreamStopAfterHandshake {
258
257
[self verifyDelegate: @[ @" didOpen" , @" didCompleteHandshake" , @" didReceiveResponse" ]];
259
258
}
260
259
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" ]];
331
- }
332
-
333
260
@end
0 commit comments