Skip to content

Commit eade6bd

Browse files
kill debug assert and unnecessary macros (#3556)
1 parent 6620c85 commit eade6bd

17 files changed

+60
-220
lines changed

Example/Messaging/Tests/FIRMessagingServiceTest.m

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -177,57 +177,6 @@ - (void)testSubscribeWithoutStart {
177177
}];
178178
}
179179

180-
// TODO(chliangGoogle) Investigate why invalid token can't throw assertion but the rest can under
181-
// release build.
182-
- (void)testSubscribeWithInvalidTopic {
183-
184-
XCTestExpectation *exceptionExpectation =
185-
[self expectationWithDescription:@"Should throw exception for invalid token"];
186-
@try {
187-
#pragma clang diagnostic push
188-
#pragma clang diagnostic ignored "-Wnonnull"
189-
[_mockPubSub subscribeWithToken:kFakeToken
190-
topic:nil
191-
options:nil
192-
handler:^(NSError *error) {
193-
XCTFail(@"Should not invoke the handler");
194-
}];
195-
#pragma clang diagnostic pop
196-
}
197-
@catch (NSException *exception) {
198-
[exceptionExpectation fulfill];
199-
}
200-
@finally {
201-
[self waitForExpectationsWithTimeout:0.1 handler:^(NSError *error) {
202-
XCTAssertNil(error);
203-
}];
204-
}
205-
}
206-
207-
- (void)testUnsubscribeWithInvalidTopic {
208-
XCTestExpectation *exceptionExpectation =
209-
[self expectationWithDescription:@"Should throw exception for invalid token"];
210-
@try {
211-
#pragma clang diagnostic push
212-
#pragma clang diagnostic ignored "-Wnonnull"
213-
[_mockPubSub unsubscribeWithToken:kFakeToken
214-
topic:nil
215-
options:nil
216-
handler:^(NSError *error) {
217-
XCTFail(@"Should not invoke the handler");
218-
}];
219-
#pragma clang diagnostic pop
220-
}
221-
@catch (NSException *exception) {
222-
[exceptionExpectation fulfill];
223-
}
224-
@finally {
225-
[self waitForExpectationsWithTimeout:0.1 handler:^(NSError *error) {
226-
XCTAssertNil(error);
227-
}];
228-
}
229-
}
230-
231180
- (void)testSubscribeWithNoTopicPrefix {
232181
OCMStub([_mockInstanceID
233182
instanceIDWithHandler:([OCMArg invokeBlockWithArgs:_result, [NSNull null], nil])]);

Firebase/Messaging/FIRMMessageCode.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ typedef NS_ENUM(NSInteger, FIRMessagingMessageCode) {
4747
kFIRMessagingMessageCodeTokenDelegateMethodsNotImplemented = 2023, // I-FCM002023
4848
kFIRMessagingMessageCodeTopicFormatIsDeprecated = 2024,
4949
kFIRMessagingMessageCodeDirectChannelConnectionFailed = 2025,
50+
kFIRMessagingMessageCodeInvalidClient = 2026,
5051
// FIRMessagingClient.m
5152
kFIRMessagingMessageCodeClient000 = 4000, // I-FCM004000
5253
kFIRMessagingMessageCodeClient001 = 4001, // I-FCM004001
@@ -60,6 +61,9 @@ typedef NS_ENUM(NSInteger, FIRMessagingMessageCode) {
6061
kFIRMessagingMessageCodeClient009 = 4009, // I-FCM004009
6162
kFIRMessagingMessageCodeClient010 = 4010, // I-FCM004010
6263
kFIRMessagingMessageCodeClient011 = 4011, // I-FCM004011
64+
kFIRMessagingMessageCodeClientInvalidState = 4012,
65+
kFIRMessagingMessageCodeClientInvalidStateTimeout = 4013,
66+
6367
// FIRMessagingConnection.m
6468
kFIRMessagingMessageCodeConnection000 = 5000, // I-FCM005000
6569
kFIRMessagingMessageCodeConnection001 = 5001, // I-FCM005001
@@ -106,6 +110,8 @@ typedef NS_ENUM(NSInteger, FIRMessagingMessageCode) {
106110
kFIRMessagingMessageCodeDataMessageManager010 = 7010, // I-FCM007010
107111
kFIRMessagingMessageCodeDataMessageManager011 = 7011, // I-FCM007011
108112
kFIRMessagingMessageCodeDataMessageManager012 = 7012, // I-FCM007012
113+
kFIRMessagingMessageCodeDataMessageManager013 = 7013,
114+
109115
// FIRMessagingPendingTopicsList.m
110116
kFIRMessagingMessageCodePendingTopicsList000 = 8000, // I-FCM008000
111117
// FIRMessagingPubSub.m
@@ -196,4 +202,7 @@ typedef NS_ENUM(NSInteger, FIRMessagingMessageCode) {
196202
kFIRMessagingServiceExtensionLocalFileNotCreated = 20002,
197203
kFIRMessagingServiceExtensionImageNotAttached = 20003,
198204

205+
// FIRMessagingCodedInputStream.m
206+
kFIRMessagingCodeInputStreamInvalidParameters = 21000,
207+
199208
};

Firebase/Messaging/FIRMessaging.m

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,9 @@ - (void)setupRmqManager {
345345
}
346346

347347
- (void)setupTopics {
348-
_FIRMessagingDevAssert(self.client, @"Invalid nil client before init pubsub.");
348+
if (!self.client) {
349+
FIRMessagingLoggerWarn(kFIRMessagingMessageCodeInvalidClient, @"Invalid nil client before init pubsub.");
350+
}
349351
self.pubsub = [[FIRMessagingPubSub alloc] initWithClient:self.client];
350352
}
351353

@@ -364,8 +366,6 @@ - (void)setupSyncMessageManager {
364366
}
365367

366368
- (void)teardown {
367-
_FIRMessagingDevAssert([NSThread isMainThread],
368-
@"FIRMessaging should be called from main thread only.");
369369
[self.client teardown];
370370
self.pubsub = nil;
371371
self.syncMessageManager = nil;
@@ -823,15 +823,13 @@ - (void)sendMessage:(NSDictionary *)message
823823
to:(NSString *)to
824824
withMessageID:(NSString *)messageID
825825
timeToLive:(int64_t)ttl {
826-
_FIRMessagingDevAssert([to length] != 0, @"Invalid receiver id for FIRMessaging-message");
827-
828826
NSMutableDictionary *fcmMessage = [[self class] createFIRMessagingMessageWithMessage:message
829827
to:to
830828
withID:messageID
831829
timeToLive:ttl
832830
delay:0];
833-
FIRMessagingLoggerInfo(kFIRMessagingMessageCodeMessaging013, @"Sending message: %@ with id: %@",
834-
message, messageID);
831+
FIRMessagingLoggerInfo(kFIRMessagingMessageCodeMessaging013, @"Sending message: %@ with id: %@ to %@.",
832+
message, messageID, to);
835833
[self.dataMessageManager sendDataMessageStanza:fcmMessage];
836834
}
837835

Firebase/Messaging/FIRMessagingClient.m

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ - (instancetype)initWithDelegate:(id<FIRMessagingClientDelegate>)delegate
131131
}
132132

133133
- (void)teardown {
134-
FIRMessagingLoggerDebug(kFIRMessagingMessageCodeClient000, @"");
134+
if (![NSThread isMainThread]) {
135+
FIRMessagingLoggerDebug(kFIRMessagingMessageCodeClient000, @"FIRMessagingClient should be called from main thread only.");
136+
}
135137
self.stayConnected = NO;
136138

137139
// Clear all the handlers
@@ -140,9 +142,8 @@ - (void)teardown {
140142
[self.connection teardown];
141143

142144
// Stop all subscription requests
143-
[self.registrar stopAllSubscriptionRequests];
145+
[self.registrar stopAllSubscriptionRequests];
144146

145-
_FIRMessagingDevAssert(self.connection.state == kFIRMessagingConnectionNotConnected, @"Did not disconnect");
146147
[NSObject cancelPreviousPerformRequestsWithTarget:self];
147148

148149
[[NSNotificationCenter defaultCenter] removeObserver:self];
@@ -166,10 +167,7 @@ - (void)updateSubscriptionWithToken:(NSString *)token
166167
options:(NSDictionary *)options
167168
shouldDelete:(BOOL)shouldDelete
168169
handler:(FIRMessagingTopicOperationCompletion)handler {
169-
170-
_FIRMessagingDevAssert(handler != nil, @"Invalid handler to FIRMessaging subscribe");
171-
172-
FIRMessagingTopicOperationCompletion completion = ^void(NSError *error) {
170+
FIRMessagingTopicOperationCompletion completion = ^void(NSError *error) {
173171
if (error) {
174172
FIRMessagingLoggerError(kFIRMessagingMessageCodeClient001, @"Failed to subscribe to topic %@",
175173
error);
@@ -182,7 +180,9 @@ - (void)updateSubscriptionWithToken:(NSString *)token
182180
@"Successfully subscribed to topic %@", topic);
183181
}
184182
}
185-
handler(error);
183+
if (handler) {
184+
handler(error);
185+
}
186186
};
187187

188188
if ([[FIRInstanceID instanceID] tryToLoadValidCheckinInfo]) {
@@ -316,8 +316,6 @@ - (void)disconnectWithTryToConnectLater:(BOOL)tryToConnectLater {
316316

317317
self.stayConnected = tryToConnectLater;
318318
[self.connection signOut];
319-
_FIRMessagingDevAssert(self.connection.state == kFIRMessagingConnectionNotConnected,
320-
@"FIRMessaging connection did not disconnect");
321319

322320
// since we can disconnect while still trying to establish the connection it's required to
323321
// cancel all performSelectors else the object might be retained
@@ -421,8 +419,6 @@ - (void)setupConnectionAndConnect {
421419
- (void)setupConnection {
422420
NSString *host = FIRMessagingServerHost();
423421
NSUInteger port = FIRMessagingServerPort();
424-
_FIRMessagingDevAssert([host length] > 0 && port != 0, @"Invalid port or host");
425-
426422
if (self.connection != nil) {
427423
// if there is an old connection, explicitly sign it off.
428424
[self.connection signOut];
@@ -447,17 +443,20 @@ - (void)tryToConnect {
447443
[NSObject cancelPreviousPerformRequestsWithTarget:self
448444
selector:@selector(tryToConnect)
449445
object:nil];
450-
446+
NSString *deviceAuthID = [FIRInstanceID instanceID].deviceAuthID;
447+
NSString *secretToken = [FIRInstanceID instanceID].secretToken;
448+
if (deviceAuthID.length == 0 || secretToken.length == 0 ||
449+
!self.connection) {
450+
FIRMessagingLoggerWarn(kFIRMessagingMessageCodeClientInvalidState,
451+
@"Invalid state to connect, deviceAuthID: %@, secretToken: %@, connection state: %ld",
452+
deviceAuthID, secretToken, (long)self.connection.state);
453+
return;
454+
}
451455
// Do not re-sign in if there is already a connection in progress.
452456
if (self.connection.state != kFIRMessagingConnectionNotConnected) {
453457
return;
454458
}
455459

456-
_FIRMessagingDevAssert([FIRInstanceID instanceID].deviceAuthID.length > 0 &&
457-
[FIRInstanceID instanceID].secretToken.length > 0 &&
458-
self.connection != nil,
459-
@"Invalid state cannot connect");
460-
461460
self.connectRetryCount = MIN(kMaxRetryExponent, self.connectRetryCount + 1);
462461
[self performSelector:@selector(didConnectTimeout)
463462
withObject:nil
@@ -466,9 +465,9 @@ - (void)tryToConnect {
466465
}
467466

468467
- (void)didConnectTimeout {
469-
_FIRMessagingDevAssert(self.connection.state != kFIRMessagingConnectionSignedIn,
470-
@"Invalid state for MCS connection");
471-
468+
if (self.connection.state == kFIRMessagingConnectionSignedIn) {
469+
FIRMessagingLoggerWarn(kFIRMessagingMessageCodeClientInvalidStateTimeout, @"Invalid state for connection timeout.");
470+
}
472471
if (self.stayConnected) {
473472
[self.connection signOut];
474473
[self scheduleConnectRetry];

Firebase/Messaging/FIRMessagingCodedInputStream.m

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616

1717
#import "Firebase/Messaging/FIRMessagingCodedInputStream.h"
18-
#import "Firebase/Messaging/FIRMessagingDefines.h"
18+
19+
#import "Firebase/Messaging/FIRMMessageCode.h"
20+
#import "Firebase/Messaging/FIRMessagingLogger.h"
1921

2022
typedef struct {
2123
const void *bytes;
@@ -32,8 +34,9 @@ static BOOL CheckSize(BufferState *state, size_t size) {
3234
}
3335

3436
static BOOL ReadRawByte(BufferState *state, int8_t *output) {
35-
_FIRMessagingDevAssert(output != NULL && state != NULL, @"Invalid parameters");
36-
37+
if (state == NULL || output == NULL) {
38+
FIRMessagingLoggerDebug(kFIRMessagingCodeInputStreamInvalidParameters, @"Invalid parameters.");
39+
}
3740
if (CheckSize(state, sizeof(int8_t))) {
3841
*output = ((int8_t *)state->bytes)[state->bufferPos++];
3942
return YES;
@@ -42,8 +45,9 @@ static BOOL ReadRawByte(BufferState *state, int8_t *output) {
4245
}
4346

4447
static BOOL ReadRawVarInt32(BufferState *state, int32_t *output) {
45-
_FIRMessagingDevAssert(output != NULL && state != NULL, @"Invalid parameters");
46-
48+
if (state == NULL || output == NULL) {
49+
FIRMessagingLoggerDebug(kFIRMessagingCodeInputStreamInvalidParameters, @"Invalid parameters.");
50+
}
4751
int8_t tmp = 0;
4852
if (!ReadRawByte(state, &tmp)) {
4953
return NO;

Firebase/Messaging/FIRMessagingConnection.m

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ - (NSString *)description {
145145
}
146146

147147
- (void)signIn {
148-
_FIRMessagingDevAssert(self.state == kFIRMessagingConnectionNotConnected, @"Invalid connection state.");
149148
if (self.state != kFIRMessagingConnectionNotConnected) {
150149
return;
151150
}
@@ -201,11 +200,8 @@ - (void)secureSocketDidConnect:(FIRMessagingSecureSocket *)socket {
201200
}
202201

203202
- (void)didDisconnectWithSecureSocket:(FIRMessagingSecureSocket *)socket {
204-
_FIRMessagingDevAssert(self.socket == socket, @"Invalid socket");
205-
_FIRMessagingDevAssert(self.socket.state == kFIRMessagingSecureSocketClosed, @"Socket already closed");
206-
207203
FIRMessagingLoggerDebug(kFIRMessagingMessageCodeConnection002,
208-
@"Secure socket disconnected from FIRMessaging service.");
204+
@"Secure socket disconnected from FIRMessaging service. %ld", (long)self.socket.state);
209205
[self disconnect];
210206
[self.delegate connection:self didCloseForReason:kFIRMessagingConnectionCloseReasonSocketDisconnected];
211207
}
@@ -295,7 +291,6 @@ - (void)sendProto:(GPBMessage *)proto {
295291
return;
296292
}
297293

298-
_FIRMessagingDevAssert(self.socket != nil, @"Socket shouldn't be nil");
299294
if (self.socket == nil) {
300295
return;
301296
}
@@ -439,10 +434,6 @@ - (void)didReceiveLoginResponse:(GtalkLoginResponse *)loginResponse {
439434
return;
440435
}
441436
FIRMessagingLoggerDebug(kFIRMessagingMessageCodeConnection012, @"Logged onto MCS service.");
442-
// We sent the persisted list of unack'd messages with login so we can assume they have been ack'd
443-
// by the server.
444-
_FIRMessagingDevAssert(self.unackedS2dIds.count == 0, @"No ids present");
445-
_FIRMessagingDevAssert(self.outStreamId == 1, @"Login should be the first stream id");
446437

447438
self.state = kFIRMessagingConnectionSignedIn;
448439
self.lastLoginServerTimestamp = loginResponse.serverTimestamp;
@@ -660,7 +651,6 @@ - (void)resetUnconfirmedAcks {
660651
}
661652

662653
- (void)disconnect {
663-
_FIRMessagingDevAssert(self.state != kFIRMessagingConnectionNotConnected, @"Connection already not connected");
664654
// cancel pending timeout tasks.
665655
[self cancelConnectionTimeoutTask];
666656
// cancel pending heartbeat.

Firebase/Messaging/FIRMessagingContextManagerService.m

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,16 @@ + (BOOL)handleContextManagerMessage:(NSDictionary *)message {
7777

7878
+ (BOOL)handleContextManagerLocalTimeMessage:(NSDictionary *)message {
7979
NSString *startTimeString = message[kFIRMessagingContextManagerLocalTimeStart];
80-
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
81-
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
82-
[dateFormatter setDateFormat:kLocalTimeFormatString];
83-
NSDate *startDate = [dateFormatter dateFromString:startTimeString];
84-
85-
_FIRMessagingDevAssert(startDate, @"Invalid local start date format %@", startTimeString);
8680
if (!startTimeString) {
8781
FIRMessagingLoggerError(kFIRMessagingMessageCodeContextManagerService002,
88-
@"Invalid local start date format %@. Message dropped",
89-
startTimeString);
82+
@"Invalid local start date format %@. Message dropped",
83+
startTimeString);
9084
return NO;
9185
}
92-
86+
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
87+
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
88+
[dateFormatter setDateFormat:kLocalTimeFormatString];
89+
NSDate *startDate = [dateFormatter dateFromString:startTimeString];
9390
NSDate *currentDate = [NSDate date];
9491

9592
if ([currentDate compare:startDate] == NSOrderedAscending) {
@@ -106,8 +103,6 @@ + (BOOL)handleContextManagerLocalTimeMessage:(NSDictionary *)message {
106103
}
107104

108105
NSDate *endDate = [dateFormatter dateFromString:endTimeString];
109-
110-
_FIRMessagingDevAssert(endDate, @"Invalid local end date format %@", endTimeString);
111106
if (!endTimeString) {
112107
FIRMessagingLoggerError(kFIRMessagingMessageCodeContextManagerService004,
113108
@"Invalid local end date format %@. Message dropped", endTimeString);

0 commit comments

Comments
 (0)