Skip to content

Commit f027ede

Browse files
Wrap some unit test data structures in a flag. (#193)
* Wrap some unit test data structures in a flag. * Check that the delays test mutable array is initialized before inserting items into it. * Check exact delays test array count with respect to the desired insertion index.
1 parent 0c04abf commit f027ede

File tree

1 file changed

+45
-26
lines changed

1 file changed

+45
-26
lines changed

OptimizelySDKCore/OptimizelySDKCore/OPTLYHTTPRequestManager.m

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
static NSString * const kHTTPHeaderFieldContentType = @"Content-Type";
2525
static NSString * const kHTTPHeaderFieldValueApplicationJSON = @"application/json";
2626

27-
// TODO: Wrap this in a TEST preprocessor definition
2827
@interface OPTLYHTTPRequestManager()
28+
29+
// Use this flag to deterine if we are running a unit test
30+
// The flag is needed to track some values for unit test
31+
@property (nonatomic, assign) BOOL isRunningTest;
2932
@property (nonatomic, assign) NSInteger retryAttemptTest;
3033
@property (nonatomic, strong) NSMutableArray *delaysTest;
3134
@end
@@ -37,7 +40,7 @@ @implementation OPTLYHTTPRequestManager
3740
- (id)init
3841
{
3942
NSAssert(YES, @"Use initWithURL initialization method.");
40-
43+
_isRunningTest = [self runningUnitTests];
4144
self = [super init];
4245
return self;
4346
}
@@ -64,8 +67,9 @@ - (void)GETWithBackoffRetryInterval:(NSInteger)backoffRetryInterval
6467
retries:(NSInteger)retries
6568
completionHandler:(OPTLYHTTPRequestManagerResponse)completion
6669
{
67-
// TODO: Wrap this in a TEST preprocessor definition
68-
self.delaysTest = [NSMutableArray new];
70+
if (self.isRunningTest == YES) {
71+
self.delaysTest = [NSMutableArray new];
72+
}
6973

7074
[self GETWithParameters:nil
7175
url:url
@@ -97,8 +101,9 @@ - (void)GETWithParameters:(NSDictionary *)parameters
97101
retries:(NSInteger)retries
98102
completionHandler:(OPTLYHTTPRequestManagerResponse)completion
99103
{
100-
// TODO: Wrap this in a TEST preprocessor definition
101-
self.delaysTest = [NSMutableArray new];
104+
if (self.isRunningTest == YES) {
105+
self.delaysTest = [NSMutableArray new];
106+
}
102107

103108
[self GETWithParameters:parameters
104109
url:url
@@ -120,8 +125,9 @@ - (void)GETWithParameters:(NSDictionary *)parameters
120125

121126
OPTLYLogDebug(OPTLYHTTPRequestManagerGETWithParametersAttempt, backoffRetryAttempt);
122127

123-
// TODO: Wrap this in a TEST preprocessor definition
124-
self.retryAttemptTest = backoffRetryAttempt;
128+
if (self.isRunningTest == YES) {
129+
self.retryAttemptTest = backoffRetryAttempt;
130+
}
125131

126132
if (backoffRetryAttempt > retries) {
127133
if (completion) {
@@ -133,9 +139,9 @@ - (void)GETWithParameters:(NSDictionary *)parameters
133139
completion(nil, nil, error);
134140
}
135141

136-
// TODO: Wrap this in a TEST preprocessor definition
137-
self.delaysTest = nil;
138-
142+
if (self.isRunningTest == YES) {
143+
self.delaysTest = nil;
144+
}
139145
return;
140146
}
141147

@@ -188,8 +194,9 @@ - (void)GETIfModifiedSince:(NSString *)lastModifiedDate
188194
retries:(NSInteger)retries
189195
completionHandler:(OPTLYHTTPRequestManagerResponse)completion
190196
{
191-
// TODO: Wrap this in a TEST preprocessor definition
192-
self.delaysTest = [NSMutableArray new];
197+
if (self.isRunningTest == YES) {
198+
self.delaysTest = [NSMutableArray new];
199+
}
193200

194201
[self GETIfModifiedSince:lastModifiedDate
195202
url:url
@@ -210,8 +217,9 @@ - (void)GETIfModifiedSince:(NSString *)lastModifiedDate
210217
{
211218
OPTLYLogDebug(OPTLYHTTPRequestManagerGETIfModifiedSince, backoffRetryAttempt);
212219

213-
// TODO: Wrap this in a TEST preprocessor definition
214-
self.retryAttemptTest = backoffRetryAttempt;
220+
if (self.isRunningTest == YES) {
221+
self.retryAttemptTest = backoffRetryAttempt;
222+
}
215223

216224
if (backoffRetryAttempt > retries) {
217225
if (completion) {
@@ -223,9 +231,9 @@ - (void)GETIfModifiedSince:(NSString *)lastModifiedDate
223231
completion(nil, nil, error);
224232
}
225233

226-
// TODO: Wrap this in a TEST preprocessor definition
227-
self.delaysTest = nil;
228-
234+
if (self.isRunningTest == YES) {
235+
self.delaysTest = nil;
236+
}
229237
return;
230238
}
231239

@@ -297,8 +305,9 @@ - (void)POSTWithParameters:(nonnull NSDictionary *)parameters
297305
retries:(NSInteger)retries
298306
completionHandler:(nullable OPTLYHTTPRequestManagerResponse)completion
299307
{
300-
// TODO: Wrap this in a TEST preprocessor definition
301-
self.delaysTest = [NSMutableArray new];
308+
if (self.isRunningTest == YES) {
309+
self.delaysTest = [NSMutableArray new];
310+
}
302311

303312
[self POSTWithParameters:parameters
304313
url:url
@@ -319,8 +328,9 @@ - (void)POSTWithParameters:(NSDictionary *)parameters
319328
{
320329
OPTLYLogDebug(OPTLYHTTPRequestManagerPOSTWithParameters, backoffRetryAttempt);
321330

322-
// TODO: Wrap this in a TEST preprocessor definition
323-
self.retryAttemptTest = backoffRetryAttempt;
331+
if (self.isRunningTest == YES) {
332+
self.retryAttemptTest = backoffRetryAttempt;
333+
}
324334

325335
if (backoffRetryAttempt > retries) {
326336
if (completion) {
@@ -332,8 +342,9 @@ - (void)POSTWithParameters:(NSDictionary *)parameters
332342
completion(nil, nil, error);
333343
}
334344

335-
// TODO: Wrap this in a TEST preprocessor definition
336-
self.delaysTest = nil;
345+
if (self.isRunningTest == YES) {
346+
self.delaysTest = nil;
347+
}
337348

338349
return;
339350
}
@@ -364,6 +375,11 @@ - (void)POSTWithParameters:(NSDictionary *)parameters
364375

365376
# pragma mark - Helper Methods
366377

378+
- (BOOL)runningUnitTests {
379+
BOOL isRunningTest = NSClassFromString(@"XCTestCase") != nil;
380+
return isRunningTest;
381+
}
382+
367383
// calculates the exponential backoff time based on the retry attempt number
368384
- (dispatch_time_t)backoffDelay:(NSInteger)backoffRetryAttempt
369385
backoffRetryInterval:(NSInteger)backoffRetryInterval
@@ -374,8 +390,11 @@ - (dispatch_time_t)backoffDelay:(NSInteger)backoffRetryAttempt
374390

375391
OPTLYLogDebug(OPTLYHTTPRequestManagerBackoffRetryStates, backoffRetryAttempt, exponentialMultiplier, delay_ns, delayTime);
376392

377-
// TODO: Wrap this in a TEST preprocessor definition
378-
self.delaysTest[backoffRetryAttempt] = [NSNumber numberWithLongLong:delay_ns];
393+
if (self.isRunningTest == YES) {
394+
if ([self.delaysTest count] >= backoffRetryAttempt) {
395+
self.delaysTest[backoffRetryAttempt] = [NSNumber numberWithLongLong:delay_ns];
396+
}
397+
}
379398

380399
return delayTime;
381400
}

0 commit comments

Comments
 (0)