Permalink
Browse files

Move RKRequest timeoutTimer creation to a helper method so that we ca…

…n test the number of times it is invoked.
  • Loading branch information...
1 parent e9bf8a2 commit b2c4b2f6777beb0f7b4b33bee9c3df7bd9485835 @bmorton bmorton committed with blakewatters Jan 22, 2012
Showing with 13 additions and 3 deletions.
  1. +6 −0 Code/Network/RKRequest.h
  2. +7 −3 Code/Network/RKRequest.m
View
@@ -390,6 +390,12 @@ typedef enum {
- (void)cancel;
/**
+ * Creates a timeoutTimer to trigger the timeout method
+ * This is mainly used so we can test that the timer is only being created once.
+ */
+- (void)createTimeoutTimer;
+
+/**
* Cancels request due to connection timeout exceeded.
* This will return an RKRequestConnectionTimeoutError via didFailLoadWithError:
*/
View
@@ -383,7 +383,7 @@ - (void)fireAsynchronousRequest {
RKResponse* response = [[[RKResponse alloc] initWithRequest:self] autorelease];
_connection = [[NSURLConnection connectionWithRequest:_URLRequest delegate:response] retain];
- _timeoutTimer = [NSTimer scheduledTimerWithTimeInterval:self.timeoutInterval target:self selector:@selector(timeout) userInfo:nil repeats:NO];
+ [self createTimeoutTimer];
[[NSNotificationCenter defaultCenter] postNotificationName:RKRequestSentNotification object:self userInfo:nil];
}
@@ -423,7 +423,7 @@ - (void)sendAsynchronously {
_isLoading = YES;
[self didFinishLoad:response];
} else if ([self shouldDispatchRequest]) {
- _timeoutTimer = [NSTimer scheduledTimerWithTimeInterval:self.timeoutInterval target:self selector:@selector(timeout) userInfo:nil repeats:NO];
+ [self createTimeoutTimer];
#if TARGET_OS_IPHONE
// Background Request Policy support
UIApplication* app = [UIApplication sharedApplication];
@@ -494,7 +494,7 @@ - (RKResponse*)sendSynchronously {
[self didFinishLoad:response];
} else if ([self shouldDispatchRequest]) {
RKLogDebug(@"Sending synchronous %@ request to URL %@.", [self HTTPMethod], [[self URL] absoluteString]);
- _timeoutTimer = [NSTimer scheduledTimerWithTimeInterval:self.timeoutInterval target:self selector:@selector(timeout) userInfo:nil repeats:NO];
+ [self createTimeoutTimer];
if (![self prepareURLRequest]) {
// TODO: Logging
@@ -543,6 +543,10 @@ - (void)cancel {
[self cancelAndInformDelegate:YES];
}
+- (void)createTimeoutTimer {
+ _timeoutTimer = [NSTimer scheduledTimerWithTimeInterval:self.timeoutInterval target:self selector:@selector(timeout) userInfo:nil repeats:NO];
+}
+
- (void)timeout {
[self cancelAndInformDelegate:NO];
RKLogError(@"Failed to send request to %@ due to connection timeout. Timeout interval = %f", [[self URL] absoluteString], self.timeoutInterval);

0 comments on commit b2c4b2f

Please sign in to comment.