@@ -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
0 commit comments