aboutsummaryrefslogtreecommitdiffhomepage
path: root/Example/Messaging/Tests/FIRMessagingPendingTopicsListTest.m
blob: 9c30388a5aea7afdc283206caf4a4996420581a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 * Copyright 2017 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import <OCMock/OCMock.h>
#import <XCTest/XCTest.h>

#import "FIRMessagingDefines.h"
#import "FIRMessagingPendingTopicsList.h"
#import "FIRMessagingTopicsCommon.h"

typedef void (^MockDelegateSubscriptionHandler)(NSString *topic,
                                                FIRMessagingTopicAction action,
                                                FIRMessagingTopicOperationCompletion completion);

/**
 * This object lets us provide a stub delegate where we can customize the behavior by providing
 * blocks. We need to use this instead of stubbing a OCMockProtocol because our delegate methods
 * take primitive values (e.g. action), which is not easy to use from OCMock
 * @see http://stackoverflow.com/a/6332023
 */
@interface MockPendingTopicsListDelegate: NSObject <FIRMessagingPendingTopicsListDelegate>

@property(nonatomic, assign) BOOL isReady;
@property(nonatomic, copy) MockDelegateSubscriptionHandler subscriptionHandler;
@property(nonatomic, copy) void(^updateHandler)();

@end

@implementation MockPendingTopicsListDelegate

- (BOOL)pendingTopicsListCanRequestTopicUpdates:(FIRMessagingPendingTopicsList *)list {
  return self.isReady;
}

- (void)pendingTopicsList:(FIRMessagingPendingTopicsList *)list
  requestedUpdateForTopic:(NSString *)topic
                   action:(FIRMessagingTopicAction)action
               completion:(FIRMessagingTopicOperationCompletion)completion {
  if (self.subscriptionHandler) {
    self.subscriptionHandler(topic, action, completion);
  }
}

- (void)pendingTopicsListDidUpdate:(FIRMessagingPendingTopicsList *)list {
  if (self.updateHandler) {
    self.updateHandler();
  }
}

@end

@interface FIRMessagingPendingTopicsListTest : XCTestCase

/// Using this delegate lets us prevent any topic operations from start, making it easy to measure
/// our batches
@property(nonatomic, strong) MockPendingTopicsListDelegate *notReadyDelegate;
/// Using this delegate will always begin topic operations (which will never return by default).
/// Useful for overriding with block-methods to handle update requests
@property(nonatomic, strong) MockPendingTopicsListDelegate *alwaysReadyDelegate;

@end

@implementation FIRMessagingPendingTopicsListTest

- (void)setUp {
  [super setUp];
  self.notReadyDelegate = [[MockPendingTopicsListDelegate alloc] init];
  self.notReadyDelegate.isReady = NO;

  self.alwaysReadyDelegate = [[MockPendingTopicsListDelegate alloc] init];
  self.alwaysReadyDelegate.isReady = YES;
}

- (void)tearDown {
  self.notReadyDelegate = nil;
  self.alwaysReadyDelegate = nil;
  [super tearDown];
}

- (void)testAddSingleTopic {
  FIRMessagingPendingTopicsList *pendingTopics = [[FIRMessagingPendingTopicsList alloc] init];
  pendingTopics.delegate = self.notReadyDelegate;

  [pendingTopics addOperationForTopic:@"/topics/0"
                           withAction:FIRMessagingTopicActionSubscribe
                           completion:nil];
  XCTAssertEqual(pendingTopics.numberOfBatches, 1);
}

- (void)testAddSameTopicAndActionMultipleTimes {
  FIRMessagingPendingTopicsList *pendingTopics = [[FIRMessagingPendingTopicsList alloc] init];
  pendingTopics.delegate = self.notReadyDelegate;

  [pendingTopics addOperationForTopic:@"/topics/0"
                           withAction:FIRMessagingTopicActionSubscribe
                           completion:nil];
  [pendingTopics addOperationForTopic:@"/topics/0"
                           withAction:FIRMessagingTopicActionSubscribe
                           completion:nil];
  [pendingTopics addOperationForTopic:@"/topics/0"
                           withAction:FIRMessagingTopicActionSubscribe
                           completion:nil];
  XCTAssertEqual(pendingTopics.numberOfBatches, 1);
}

- (void)testAddMultiplePendingTopicsWithSameAction {
  FIRMessagingPendingTopicsList *pendingTopics = [[FIRMessagingPendingTopicsList alloc] init];
  pendingTopics.delegate = self.notReadyDelegate;

  for (NSInteger i = 0; i < 10; i++) {
    NSString *topic = [NSString stringWithFormat:@"/topics/%ld", i];
    [pendingTopics addOperationForTopic:topic
                             withAction:FIRMessagingTopicActionSubscribe
                             completion:nil];
  }
  XCTAssertEqual(pendingTopics.numberOfBatches, 1);
}

- (void)testAddTopicsWithDifferentActions {
  FIRMessagingPendingTopicsList *pendingTopics = [[FIRMessagingPendingTopicsList alloc] init];
  pendingTopics.delegate = self.notReadyDelegate;

  [pendingTopics addOperationForTopic:@"/topics/0"
                           withAction:FIRMessagingTopicActionSubscribe
                           completion:nil];
  [pendingTopics addOperationForTopic:@"/topics/1"
                           withAction:FIRMessagingTopicActionUnsubscribe
                           completion:nil];
  [pendingTopics addOperationForTopic:@"/topics/2"
                           withAction:FIRMessagingTopicActionSubscribe
                           completion:nil];
  XCTAssertEqual(pendingTopics.numberOfBatches, 3);
}

- (void)testBatchSizeReductionAfterSuccessfulTopicUpdate {
  FIRMessagingPendingTopicsList *pendingTopics = [[FIRMessagingPendingTopicsList alloc] init];
  pendingTopics.delegate = self.alwaysReadyDelegate;

  XCTestExpectation *batchSizeReductionExpectation =
      [self expectationWithDescription:@"Batch size was reduced after topic suscription"];

  FIRMessaging_WEAKIFY(self)
  self.alwaysReadyDelegate.subscriptionHandler =
      ^(NSString *topic,
        FIRMessagingTopicAction action,
        FIRMessagingTopicOperationCompletion completion) {
    // Simulate that the handler is generally called asynchronously
    dispatch_async(dispatch_get_main_queue(), ^{
      FIRMessaging_STRONGIFY(self)
      if (action == FIRMessagingTopicActionUnsubscribe) {
        XCTAssertEqual(pendingTopics.numberOfBatches, 1);
        [batchSizeReductionExpectation fulfill];
      }
      completion(nil);
    });
  };

  [pendingTopics addOperationForTopic:@"/topics/0"
                           withAction:FIRMessagingTopicActionSubscribe
                           completion:nil];
  [pendingTopics addOperationForTopic:@"/topics/1"
                           withAction:FIRMessagingTopicActionSubscribe
                           completion:nil];
  [pendingTopics addOperationForTopic:@"/topics/2"
                           withAction:FIRMessagingTopicActionSubscribe
                           completion:nil];
  [pendingTopics addOperationForTopic:@"/topics/1"
                           withAction:FIRMessagingTopicActionUnsubscribe
                           completion:nil];

  [self waitForExpectationsWithTimeout:5.0 handler:nil];
}

- (void)testCompletionOfTopicUpdatesInSameThread {
  FIRMessagingPendingTopicsList *pendingTopics = [[FIRMessagingPendingTopicsList alloc] init];
  pendingTopics.delegate = self.alwaysReadyDelegate;

  XCTestExpectation *allOperationsSucceededed =
      [self expectationWithDescription:@"All queued operations succeeded"];

  self.alwaysReadyDelegate.subscriptionHandler =
      ^(NSString *topic,
        FIRMessagingTopicAction action,
        FIRMessagingTopicOperationCompletion completion) {
    // Typically, our callbacks happen asynchronously, but to ensure resilience,
    // call back the operation on the same thread it was called in.
    completion(nil);
  };

  self.alwaysReadyDelegate.updateHandler = ^{
    if (pendingTopics.numberOfBatches == 0) {
      [allOperationsSucceededed fulfill];
    }
  };

  [pendingTopics addOperationForTopic:@"/topics/0"
                           withAction:FIRMessagingTopicActionSubscribe
                           completion:nil];
  [pendingTopics addOperationForTopic:@"/topics/1"
                           withAction:FIRMessagingTopicActionSubscribe
                           completion:nil];
  [pendingTopics addOperationForTopic:@"/topics/2"
                           withAction:FIRMessagingTopicActionSubscribe
                           completion:nil];

  [self waitForExpectationsWithTimeout:5.0 handler:nil];
}

- (void)testAddingTopicToCurrentBatchWhileCurrentBatchTopicsInFlight {

  FIRMessagingPendingTopicsList *pendingTopics = [[FIRMessagingPendingTopicsList alloc] init];
  pendingTopics.delegate = self.alwaysReadyDelegate;

  NSString *stragglerTopic = @"/topics/straggler";
  XCTestExpectation *stragglerTopicWasAddedToInFlightOperations =
  [self expectationWithDescription:@"The topic was added to in-flight operations"];

  self.alwaysReadyDelegate.subscriptionHandler =
      ^(NSString *topic,
        FIRMessagingTopicAction action,
        FIRMessagingTopicOperationCompletion completion) {
    if ([topic isEqualToString:stragglerTopic]) {
      [stragglerTopicWasAddedToInFlightOperations fulfill];
    }
    // Add a 0.5 second delay to the completion, to give time to add a straggler before the batch
    // is completed
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)),
                   dispatch_get_main_queue(), ^{
                     completion(nil);
                   });
  };

  // This is a normal topic, which should start fairly soon, but take a while to complete
  [pendingTopics addOperationForTopic:@"/topics/0"
                           withAction:FIRMessagingTopicActionSubscribe
                           completion:nil];
  // While waiting for the first topic to complete, we add another topic after a slight delay
  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)),
                 dispatch_get_main_queue(),
                 ^{
    [pendingTopics addOperationForTopic:stragglerTopic
                             withAction:FIRMessagingTopicActionSubscribe
                             completion:nil];
  });

  [self waitForExpectationsWithTimeout:5.0 handler:nil];
}

@end