Skip to content

Commit 096e77c

Browse files
Cleaning up test
1 parent 958f866 commit 096e77c

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

Firestore/Example/Tests/Integration/FSTStreamTests.m

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,25 @@ - (void)writesFinishedWithError:(NSError *_Nullable)error;
3838
*/
3939
@interface FSTStreamStatusDelegate : NSObject <FSTWatchStreamDelegate, FSTWriteStreamDelegate>
4040

41+
- (instancetype)initFrom:(XCTestCase *)testCase
42+
usingQueue:(FSTDispatchQueue *)dispatchQueue NS_DESIGNATED_INITIALIZER;
43+
- (instancetype)init NS_UNAVAILABLE;
44+
4145
@property(nonatomic, readonly) NSMutableArray<NSString *> *states;
4246
@property(atomic, readwrite) BOOL invokeCallbacks;
4347
@property(nonatomic, weak) XCTestExpectation *expectation;
4448
@property(nonatomic, weak, readonly) FSTStream *stream;
49+
@property(nonatomic, weak, readonly) XCTestCase *testCase;
50+
@property(nonatomic, weak, readonly) FSTDispatchQueue *dispatchQueue;
4551

4652
@end
4753

4854
@implementation FSTStreamStatusDelegate
4955

50-
- (instancetype)init {
56+
- (instancetype)initFrom:(XCTestCase *)testCase usingQueue:(FSTDispatchQueue *)dispatchQueue {
5157
if (self = [super init]) {
58+
_testCase = testCase;
59+
_dispatchQueue = dispatchQueue;
5260
_states = [NSMutableArray new];
5361
}
5462

@@ -99,9 +107,17 @@ - (void)writeStreamDidReceiveResponseWithVersion:(FSTSnapshotVersion *)commitVer
99107
_expectation = nil;
100108
}
101109

102-
- (void)fulfillOnCallback:(XCTestExpectation *)expectation {
110+
/**
111+
* Executes 'block' using the provided FSTDispatchQueue and waits for any callback on this delegate
112+
* to be called.
113+
*/
114+
- (void)awaitNotificationFromBlock:(void (^)(void))block {
103115
FSTAssert(_expectation == nil, @"Previous expectation still active");
116+
XCTestExpectation *expectation =
117+
[self.testCase expectationWithDescription:@"awaitCallbackInBlock"];
104118
_expectation = expectation;
119+
[self.dispatchQueue dispatchAsync:block];
120+
[self.testCase awaitExpectations];
105121
}
106122

107123
@end
@@ -140,16 +156,12 @@ - (void)setUp {
140156
_credentials = [[FSTEmptyCredentialsProvider alloc] init];
141157
}
142158

143-
- (void)tearDown {
144-
[super tearDown];
145-
}
146-
147159
- (FSTWriteStream *)setUpWriteStream {
148160
FSTDatastore *datastore = [[FSTDatastore alloc] initWithDatabaseInfo:_databaseInfo
149161
workerDispatchQueue:_workerDispatchQueue
150162
credentials:_credentials];
151163

152-
_delegate = [FSTStreamStatusDelegate new];
164+
_delegate = [[FSTStreamStatusDelegate alloc] initFrom:self usingQueue:_workerDispatchQueue];
153165
return [datastore createWriteStreamWithDelegate:_delegate];
154166
}
155167

@@ -158,28 +170,30 @@ - (FSTWatchStream *)setUpWatchStream {
158170
workerDispatchQueue:_workerDispatchQueue
159171
credentials:_credentials];
160172

161-
_delegate = [FSTStreamStatusDelegate new];
173+
_delegate = [[FSTStreamStatusDelegate alloc] initFrom:self usingQueue:_workerDispatchQueue];
162174
return [datastore createWatchStreamWithDelegate:_delegate];
163175
}
164176

165-
- (void)verifyDelegate:(NSArray<NSString *> *)expectedStates {
177+
/**
178+
* Drains the test queue and asserts that all the observed callbacks (up to this point) match
179+
* 'expectedStates'. Clears the list of observed callbacks on completion.
180+
*/
181+
- (void)verifyDelegateObservedStates:(NSArray<NSString *> *)expectedStates {
166182
// Drain queue
167183
dispatch_sync(_testQueue, ^{
168184
});
169185

170186
XCTAssertEqualObjects(_delegate.states, expectedStates);
187+
[_delegate.states removeAllObjects];
171188
}
172189

173190
/** Verifies that the watch stream does not issue an onClose callback after a call to stop(). */
174191
- (void)testWatchStreamStopBeforeHandshake {
175192
FSTWatchStream *watchStream = [self setUpWatchStream];
176193

177-
XCTestExpectation *openExpectation = [self expectationWithDescription:@"open"];
178-
[_delegate fulfillOnCallback:openExpectation];
179-
[_workerDispatchQueue dispatchAsync:^{
194+
[_delegate awaitNotificationFromBlock:^{
180195
[watchStream start];
181196
}];
182-
[self awaitExpectations];
183197

184198
// Stop must not call watchStreamDidClose because the full implementation of the delegate could
185199
// attempt to restart the stream in the event it had pending watches.
@@ -190,19 +204,16 @@ - (void)testWatchStreamStopBeforeHandshake {
190204
// Simulate a final callback from GRPC
191205
[watchStream writesFinishedWithError:nil];
192206

193-
[self verifyDelegate:@[ @"watchStreamDidOpen" ]];
207+
[self verifyDelegateObservedStates:@[ @"watchStreamDidOpen" ]];
194208
}
195209

196210
/** Verifies that the write stream does not issue an onClose callback after a call to stop(). */
197211
- (void)testWriteStreamStopBeforeHandshake {
198212
FSTWriteStream *writeStream = [self setUpWriteStream];
199213

200-
XCTestExpectation *openExpectation = [self expectationWithDescription:@"open"];
201-
[_delegate fulfillOnCallback:openExpectation];
202-
[_workerDispatchQueue dispatchAsync:^{
214+
[_delegate awaitNotificationFromBlock:^{
203215
[writeStream start];
204216
}];
205-
[self awaitExpectations];
206217

207218
// Don't start the handshake.
208219

@@ -215,44 +226,35 @@ - (void)testWriteStreamStopBeforeHandshake {
215226
// Simulate a final callback from GRPC
216227
[writeStream writesFinishedWithError:nil];
217228

218-
[self verifyDelegate:@[ @"writeStreamDidOpen" ]];
229+
[self verifyDelegateObservedStates:@[ @"writeStreamDidOpen" ]];
219230
}
220231

221232
- (void)testWriteStreamStopAfterHandshake {
222233
FSTWriteStream *writeStream = [self setUpWriteStream];
223234

224-
XCTestExpectation *openExpectation = [self expectationWithDescription:@"open"];
225-
[_delegate fulfillOnCallback:openExpectation];
226-
[_workerDispatchQueue dispatchAsync:^{
235+
[_delegate awaitNotificationFromBlock:^{
227236
[writeStream start];
228237
}];
229-
[self awaitExpectations];
230238

231239
// Writing before the handshake should throw
232240
dispatch_sync(_testQueue, ^{
233241
XCTAssertThrows([writeStream writeMutations:_mutations]);
234242
});
235243

236-
XCTestExpectation *handshakeExpectation = [self expectationWithDescription:@"handshake"];
237-
[_delegate fulfillOnCallback:handshakeExpectation];
238-
[_workerDispatchQueue dispatchAsync:^{
244+
[_delegate awaitNotificationFromBlock:^{
239245
[writeStream writeHandshake];
240246
}];
241-
[self awaitExpectations];
242247

243248
// Now writes should succeed
244-
XCTestExpectation *writeExpectation = [self expectationWithDescription:@"write"];
245-
[_delegate fulfillOnCallback:writeExpectation];
246-
[_workerDispatchQueue dispatchAsync:^{
249+
[_delegate awaitNotificationFromBlock:^{
247250
[writeStream writeMutations:_mutations];
248251
}];
249-
[self awaitExpectations];
250252

251253
[_workerDispatchQueue dispatchAsync:^{
252254
[writeStream stop];
253255
}];
254256

255-
[self verifyDelegate:@[
257+
[self verifyDelegateObservedStates:@[
256258
@"writeStreamDidOpen", @"writeStreamDidCompleteHandshake",
257259
@"writeStreamDidReceiveResponseWithVersion"
258260
]];

0 commit comments

Comments
 (0)