Skip to content

Commit 974162d

Browse files
Cleaning up test
1 parent 958f866 commit 974162d

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

Firestore/Example/Tests/Integration/FSTStreamTests.m

Lines changed: 32 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,15 @@ - (void)writeStreamDidReceiveResponseWithVersion:(FSTSnapshotVersion *)commitVer
99107
_expectation = nil;
100108
}
101109

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

107121
@end
@@ -140,16 +154,12 @@ - (void)setUp {
140154
_credentials = [[FSTEmptyCredentialsProvider alloc] init];
141155
}
142156

143-
- (void)tearDown {
144-
[super tearDown];
145-
}
146-
147157
- (FSTWriteStream *)setUpWriteStream {
148158
FSTDatastore *datastore = [[FSTDatastore alloc] initWithDatabaseInfo:_databaseInfo
149159
workerDispatchQueue:_workerDispatchQueue
150160
credentials:_credentials];
151161

152-
_delegate = [FSTStreamStatusDelegate new];
162+
_delegate = [[FSTStreamStatusDelegate alloc] initFrom:self usingQueue:_workerDispatchQueue];
153163
return [datastore createWriteStreamWithDelegate:_delegate];
154164
}
155165

@@ -158,28 +168,30 @@ - (FSTWatchStream *)setUpWatchStream {
158168
workerDispatchQueue:_workerDispatchQueue
159169
credentials:_credentials];
160170

161-
_delegate = [FSTStreamStatusDelegate new];
171+
_delegate = [[FSTStreamStatusDelegate alloc] initFrom:self usingQueue:_workerDispatchQueue];
162172
return [datastore createWatchStreamWithDelegate:_delegate];
163173
}
164174

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

170184
XCTAssertEqualObjects(_delegate.states, expectedStates);
185+
[_delegate.states removeAllObjects];
171186
}
172187

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

177-
XCTestExpectation *openExpectation = [self expectationWithDescription:@"open"];
178-
[_delegate fulfillOnCallback:openExpectation];
179-
[_workerDispatchQueue dispatchAsync:^{
192+
[_delegate awaitNotificationFromBlock:^() {
180193
[watchStream start];
181194
}];
182-
[self awaitExpectations];
183195

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

193-
[self verifyDelegate:@[ @"watchStreamDidOpen" ]];
205+
[self verifyDelegateObservedStates:@[ @"watchStreamDidOpen" ]];
194206
}
195207

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

200-
XCTestExpectation *openExpectation = [self expectationWithDescription:@"open"];
201-
[_delegate fulfillOnCallback:openExpectation];
202-
[_workerDispatchQueue dispatchAsync:^{
212+
[_delegate awaitNotificationFromBlock:^() {
203213
[writeStream start];
204214
}];
205-
[self awaitExpectations];
206215

207216
// Don't start the handshake.
208217

@@ -215,44 +224,35 @@ - (void)testWriteStreamStopBeforeHandshake {
215224
// Simulate a final callback from GRPC
216225
[writeStream writesFinishedWithError:nil];
217226

218-
[self verifyDelegate:@[ @"writeStreamDidOpen" ]];
227+
[self verifyDelegateObservedStates:@[ @"writeStreamDidOpen" ]];
219228
}
220229

221230
- (void)testWriteStreamStopAfterHandshake {
222231
FSTWriteStream *writeStream = [self setUpWriteStream];
223232

224-
XCTestExpectation *openExpectation = [self expectationWithDescription:@"open"];
225-
[_delegate fulfillOnCallback:openExpectation];
226-
[_workerDispatchQueue dispatchAsync:^{
233+
[_delegate awaitNotificationFromBlock:^() {
227234
[writeStream start];
228235
}];
229-
[self awaitExpectations];
230236

231237
// Writing before the handshake should throw
232238
dispatch_sync(_testQueue, ^{
233239
XCTAssertThrows([writeStream writeMutations:_mutations]);
234240
});
235241

236-
XCTestExpectation *handshakeExpectation = [self expectationWithDescription:@"handshake"];
237-
[_delegate fulfillOnCallback:handshakeExpectation];
238-
[_workerDispatchQueue dispatchAsync:^{
242+
[_delegate awaitNotificationFromBlock:^() {
239243
[writeStream writeHandshake];
240244
}];
241-
[self awaitExpectations];
242245

243246
// Now writes should succeed
244-
XCTestExpectation *writeExpectation = [self expectationWithDescription:@"write"];
245-
[_delegate fulfillOnCallback:writeExpectation];
246-
[_workerDispatchQueue dispatchAsync:^{
247+
[_delegate awaitNotificationFromBlock:^() {
247248
[writeStream writeMutations:_mutations];
248249
}];
249-
[self awaitExpectations];
250250

251251
[_workerDispatchQueue dispatchAsync:^{
252252
[writeStream stop];
253253
}];
254254

255-
[self verifyDelegate:@[
255+
[self verifyDelegateObservedStates:@[
256256
@"writeStreamDidOpen", @"writeStreamDidCompleteHandshake",
257257
@"writeStreamDidReceiveResponseWithVersion"
258258
]];

0 commit comments

Comments
 (0)