Skip to content

Commit 7e92305

Browse files
authored
[FCM] Fix unit test flakes (#15560)
1 parent 0fc0923 commit 7e92305

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

FirebaseMessaging/Tests/UnitTests/FIRMessagingExtensionHelperTest.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ - (void)testModifyNotificationWithInvalidPayloadData {
109109

110110
- (void)testModifyNotificationWithEmptyPayloadData {
111111
if (@available(macOS 10.14, iOS 10.0, watchos 3.0, *)) {
112-
XCTestExpectation *validPayloadExpectation =
113-
[self expectationWithDescription:@"Test payload is valid."];
112+
XCTestExpectation *handlerCalledExpectation =
113+
[self expectationWithDescription:@"Content handler was called."];
114114
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
115-
content.userInfo =
116-
@{kFCMPayloadOptionsName : @{kFCMPayloadOptionsImageURLName : @"a invalid URL"}};
115+
// Simulate empty payload data relevant to image loading.
116+
content.userInfo = @{kFCMPayloadOptionsName : @{}};
117117
FIRMessagingContentHandler handler = ^(UNNotificationContent *content) {
118-
[validPayloadExpectation fulfill];
118+
[handlerCalledExpectation fulfill];
119119
};
120120
[_mockExtensionHelper populateNotificationContent:content withContentHandler:handler];
121121
OCMReject([_mockExtensionHelper loadAttachmentForURL:[OCMArg any]

FirebaseMessaging/Tests/UnitTests/FIRMessagingPendingTopicsListTest.m

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -110,24 +110,24 @@ - (void)testBatchSizeReductionAfterSuccessfulTopicUpdate {
110110
FIRMessagingPendingTopicsList *pendingTopics = [[FIRMessagingPendingTopicsList alloc] init];
111111
pendingTopics.delegate = self.alwaysReadyDelegate;
112112

113-
XCTestExpectation *batchSizeReductionExpectation =
114-
[self expectationWithDescription:@"Batch size was reduced after topic subscription"];
113+
XCTestExpectation *allOperationsCompleted =
114+
[self expectationWithDescription:@"All topic operations completed"];
115115

116-
__weak id weakSelf = self;
117116
self.alwaysReadyDelegate.subscriptionHandler =
118117
^(NSString *topic, FIRMessagingTopicAction action,
119118
FIRMessagingTopicOperationCompletion completion) {
120119
// Simulate that the handler is generally called asynchronously
121120
dispatch_async(dispatch_get_main_queue(), ^{
122-
if (action == FIRMessagingTopicActionUnsubscribe) {
123-
__unused id self = weakSelf; // In Xcode 11, XCTAssertEqual references self.
124-
XCTAssertEqual(pendingTopics.numberOfBatches, 1);
125-
[batchSizeReductionExpectation fulfill];
126-
}
127121
completion(nil);
128122
});
129123
};
130124

125+
self.alwaysReadyDelegate.updateHandler = ^{
126+
if (pendingTopics.numberOfBatches == 0) {
127+
[allOperationsCompleted fulfill];
128+
}
129+
};
130+
131131
[pendingTopics addOperationForTopic:@"/topics/0"
132132
withAction:FIRMessagingTopicActionSubscribe
133133
completion:nil];
@@ -151,19 +151,21 @@ - (void)testCompletionOfTopicUpdatesInSameThread {
151151
XCTestExpectation *allOperationsSucceededed =
152152
[self expectationWithDescription:@"All queued operations succeeded"];
153153

154+
__block int completedOperations = 0;
154155
self.alwaysReadyDelegate.subscriptionHandler =
155156
^(NSString *topic, FIRMessagingTopicAction action,
156157
FIRMessagingTopicOperationCompletion completion) {
157158
// Typically, our callbacks happen asynchronously, but to ensure resilience,
158159
// call back the operation on the same thread it was called in.
159160
completion(nil);
161+
completedOperations++;
162+
if (completedOperations == 3) {
163+
[allOperationsSucceededed fulfill];
164+
}
160165
};
161166

162-
self.alwaysReadyDelegate.updateHandler = ^{
163-
if (pendingTopics.numberOfBatches == 0) {
164-
[allOperationsSucceededed fulfill];
165-
}
166-
};
167+
// Remove the updateHandler, it's not consistently called for individual operations.
168+
self.alwaysReadyDelegate.updateHandler = nil;
167169

168170
[pendingTopics addOperationForTopic:@"/topics/0"
169171
withAction:FIRMessagingTopicActionSubscribe
@@ -182,26 +184,32 @@ - (void)testAddingTopicToCurrentBatchWhileCurrentBatchTopicsInFlight {
182184
FIRMessagingPendingTopicsList *pendingTopics = [[FIRMessagingPendingTopicsList alloc] init];
183185
pendingTopics.delegate = self.alwaysReadyDelegate;
184186

187+
NSString *initialTopic = @"/topics/0";
185188
NSString *stragglerTopic = @"/topics/straggler";
186-
XCTestExpectation *stragglerTopicWasAddedToInFlightOperations =
187-
[self expectationWithDescription:@"The topic was added to in-flight operations"];
189+
190+
XCTestExpectation *initialTopicProcessedExpectation =
191+
[self expectationWithDescription:@"The initial topic was processed"];
192+
XCTestExpectation *stragglerTopicProcessedExpectation =
193+
[self expectationWithDescription:@"The straggler topic was processed"];
188194

189195
self.alwaysReadyDelegate.subscriptionHandler =
190196
^(NSString *topic, FIRMessagingTopicAction action,
191197
FIRMessagingTopicOperationCompletion completion) {
192-
if ([topic isEqualToString:stragglerTopic]) {
193-
[stragglerTopicWasAddedToInFlightOperations fulfill];
194-
}
195198
// Add a 0.5 second delay to the completion, to give time to add a straggler before the
196199
// batch is completed
197200
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)),
198201
dispatch_get_main_queue(), ^{
199202
completion(nil);
203+
if ([topic isEqualToString:initialTopic]) {
204+
[initialTopicProcessedExpectation fulfill];
205+
} else if ([topic isEqualToString:stragglerTopic]) {
206+
[stragglerTopicProcessedExpectation fulfill];
207+
}
200208
});
201209
};
202210

203211
// This is a normal topic, which should start fairly soon, but take a while to complete
204-
[pendingTopics addOperationForTopic:@"/topics/0"
212+
[pendingTopics addOperationForTopic:initialTopic
205213
withAction:FIRMessagingTopicActionSubscribe
206214
completion:nil];
207215
// While waiting for the first topic to complete, we add another topic after a slight delay

FirebaseMessaging/Tests/UnitTests/FIRMessagingTestUtilities.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ typedef void (^MockDelegateSubscriptionHandler)(NSString *topic,
4242

4343
@property(nonatomic, assign) BOOL isReady;
4444
@property(nonatomic, copy) MockDelegateSubscriptionHandler subscriptionHandler;
45-
@property(nonatomic, copy) void (^updateHandler)(void);
45+
@property(nonatomic, copy, nullable) void (^updateHandler)(void);
4646

4747
@end
4848

0 commit comments

Comments
 (0)