Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed timeouts for synchronous calls.

  • Loading branch information...
commit a1d4a3bd945bffd53651dcdbcb7ecf8815b850ad 1 parent bfea073
@lukeredpath authored
View
16 Classes/Categories/NSObject+SynchronousProxy.h
@@ -1,16 +0,0 @@
-//
-// NSObject+SynchronousProxy.h
-// LRResty
-//
-// Created by Luke Redpath on 20/01/2011.
-// Copyright 2011 LJR Software Limited. All rights reserved.
-//
-
-#import <Foundation/Foundation.h>
-#import "LRSynchronousProxy.h"
-
-@interface NSObject (SynchronousProxy)
-
-- (id)performAsynchronousBlockAndReturnResultWhenReady:(LRSynchronousProxyBlock)block;
-
-@end
View
19 Classes/Categories/NSObject+SynchronousProxy.m
@@ -1,19 +0,0 @@
-//
-// NSObject+SynchronousProxy.m
-// LRResty
-//
-// Created by Luke Redpath on 20/01/2011.
-// Copyright 2011 LJR Software Limited. All rights reserved.
-//
-
-#import "NSObject+SynchronousProxy.h"
-
-
-@implementation NSObject (SynchronousProxy)
-
-- (id)performAsynchronousBlockAndReturnResultWhenReady:(LRSynchronousProxyBlock)block;
-{
- return [LRSynchronousProxy performAsynchronousBlockAndReturnResultWhenReady:block];
-}
-
-@end
View
2  Classes/LRRestyClient+DELETE.m
@@ -10,7 +10,7 @@
#import "LRRestyClientProxyDelegate.h"
#import "LRRestyClientBlockDelegate.h"
#import "LRRestyClientStreamingDelegate.h"
-#import "NSObject+SynchronousProxy.h"
+#import "LRSynchronousProxy.h"
@implementation LRRestyClient (DELETE)
View
6 Classes/LRRestyClient+GET.m
@@ -10,7 +10,7 @@
#import "LRRestyClientProxyDelegate.h"
#import "LRRestyClientBlockDelegate.h"
#import "LRRestyClientStreamingDelegate.h"
-#import "NSObject+SynchronousProxy.h"
+#import "LRSynchronousProxy.h"
@implementation LRRestyClient (GET)
@@ -55,7 +55,7 @@ - (LRRestyRequest *)get:(NSString *)urlString parameters:(NSDictionary *)paramet
- (LRRestyResponse *)get:(NSString *)urlString;
{
- return [self performAsynchronousBlockAndReturnResultWhenReady:^(id *result, NSCondition *condition)
+ return [self performAsynchronousBlockWithTimeout:globalTimeoutInterval andReturnResultWhenReady:^(id *result, NSCondition *condition)
{
[self get:urlString withBlock:^(LRRestyResponse *response) {
LRSYNCHRONOUS_PROXY_NOTIFY_CONDITION(result, [response retain], condition);
@@ -65,7 +65,7 @@ - (LRRestyResponse *)get:(NSString *)urlString;
- (LRRestyResponse *)get:(NSString *)urlString parameters:(NSDictionary *)parameters;
{
- return [self performAsynchronousBlockAndReturnResultWhenReady:^(id *result, NSCondition *condition)
+ return [self performAsynchronousBlockWithTimeout:globalTimeoutInterval andReturnResultWhenReady:^(id *result, NSCondition *condition)
{
[self get:urlString parameters:parameters withBlock:^(LRRestyResponse *response) {
LRSYNCHRONOUS_PROXY_NOTIFY_CONDITION(result, [response retain], condition);
View
4 Classes/LRRestyClient+POST.m
@@ -9,7 +9,7 @@
#import "LRRestyClient+POST.h"
#import "LRRestyClientBlockDelegate.h"
#import "LRRestyClientProxyDelegate.h"
-#import "NSObject+SynchronousProxy.h"
+#import "LRSynchronousProxy.h"
@implementation LRRestyClient (POST)
@@ -44,7 +44,7 @@ - (LRRestyRequest *)post:(NSString *)urlString payload:(id)payload headers:(NSDi
- (LRRestyResponse *)post:(NSString *)urlString payload:(id)payload;
{
- return [self performAsynchronousBlockAndReturnResultWhenReady:^(id *result, NSCondition *condition)
+ return [self performAsynchronousBlockWithTimeout:globalTimeoutInterval + 1 andReturnResultWhenReady:^(id *result, NSCondition *condition)
{
[self post:urlString payload:payload withBlock:^(LRRestyResponse *response) {
LRSYNCHRONOUS_PROXY_NOTIFY_CONDITION(result, [response retain], condition);
View
2  Classes/LRRestyClient+PUT.m
@@ -9,7 +9,7 @@
#import "LRRestyClient+PUT.h"
#import "LRRestyClientBlockDelegate.h"
#import "LRRestyClientProxyDelegate.h"
-#import "NSObject+SynchronousProxy.h"
+#import "LRSynchronousProxy.h"
@implementation LRRestyClient (PUT)
View
2  Classes/LRRestyClientProxyDelegate.h
@@ -12,7 +12,7 @@
@class LRRestyClient;
-@interface LRRestyClientProxyDelegate : NSOperation <LRRestyRequestDelegate> {
+@interface LRRestyClientProxyDelegate : NSObject <LRRestyRequestDelegate> {
LRRestyClient *restyClient;
id<LRRestyClientResponseDelegate> responseDelegate;
}
View
3  Classes/LRRestyRequest.m
@@ -103,8 +103,7 @@ - (void)setBasicAuthUsername:(NSString *)username password:(NSString *)password
- (void)timeoutAfter:(NSTimeInterval)delayInSeconds handleWithBlock:(LRRestyRequestTimeoutBlock)block
{
- // by the time this is called, the request has started or is about to start
- // so we can just start the timeout now
+ // by the time this is called, the request has started or is about to start/ so we can just start the timeout now
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (double)delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
if (![self isFinished]) {
View
8 Classes/LRSynchronousProxy.h
@@ -17,4 +17,12 @@ typedef void (^LRSynchronousProxyBlock)(id *, NSCondition *condition);
}
+ (id)performAsynchronousBlockAndReturnResultWhenReady:(LRSynchronousProxyBlock)block;
++ (id)performAsynchronousBlockWithTimeout:(NSTimeInterval)timeout andReturnResultWhenReady:(LRSynchronousProxyBlock)block;
+@end
+
+@interface NSObject (SynchronousProxy)
+
+- (id)performAsynchronousBlockAndReturnResultWhenReady:(LRSynchronousProxyBlock)block;
+- (id)performAsynchronousBlockWithTimeout:(NSTimeInterval)timeout andReturnResultWhenReady:(LRSynchronousProxyBlock)block;
+
@end
View
29 Classes/LRSynchronousProxy.m
@@ -13,6 +13,11 @@ @implementation LRSynchronousProxy
+ (id)performAsynchronousBlockAndReturnResultWhenReady:(LRSynchronousProxyBlock)block;
{
+ return [self performAsynchronousBlockWithTimeout:0 andReturnResultWhenReady:block];
+}
+
++ (id)performAsynchronousBlockWithTimeout:(NSTimeInterval)timeout andReturnResultWhenReady:(LRSynchronousProxyBlock)block
+{
id result = nil;
NSCondition *condition = [[NSCondition alloc] init];
@@ -20,12 +25,32 @@ + (id)performAsynchronousBlockAndReturnResultWhenReady:(LRSynchronousProxyBlock)
block(&result, condition);
[condition lock];
- [condition wait];
- [condition unlock];
+
+ if (timeout > 0) {
+ [condition waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:timeout]];
+ }
+ else {
+ [condition wait];
+ }
+ [condition unlock];
[condition release];
return [result autorelease];
}
@end
+
+@implementation NSObject (SynchronousProxy)
+
+- (id)performAsynchronousBlockAndReturnResultWhenReady:(LRSynchronousProxyBlock)block;
+{
+ return [LRSynchronousProxy performAsynchronousBlockAndReturnResultWhenReady:block];
+}
+
+- (id)performAsynchronousBlockWithTimeout:(NSTimeInterval)timeout andReturnResultWhenReady:(LRSynchronousProxyBlock)block
+{
+ return [LRSynchronousProxy performAsynchronousBlockWithTimeout:timeout andReturnResultWhenReady:block];
+}
+
+@end
View
12 LRResty.xcodeproj/project.pbxproj
@@ -36,8 +36,6 @@
A33E505012E89EB900746A2C /* NSRunLoop+Additions.m in Sources */ = {isa = PBXBuildFile; fileRef = A33E504E12E89EB900746A2C /* NSRunLoop+Additions.m */; };
A33E50C512E8AC6F00746A2C /* LRSynchronousProxy.h in Headers */ = {isa = PBXBuildFile; fileRef = A33E50C312E8AC6F00746A2C /* LRSynchronousProxy.h */; };
A33E50C612E8AC6F00746A2C /* LRSynchronousProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = A33E50C412E8AC6F00746A2C /* LRSynchronousProxy.m */; };
- A33E50CD12E8ACE800746A2C /* NSObject+SynchronousProxy.h in Headers */ = {isa = PBXBuildFile; fileRef = A33E50CB12E8ACE800746A2C /* NSObject+SynchronousProxy.h */; };
- A33E50CE12E8ACE800746A2C /* NSObject+SynchronousProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = A33E50CC12E8ACE800746A2C /* NSObject+SynchronousProxy.m */; };
A34E8A6C13C47B2D006D5B76 /* RetryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = A34E8A6B13C47B2D006D5B76 /* RetryTests.m */; };
A34E8A7313C5E58B006D5B76 /* LRRestyRequest+Internal.h in Headers */ = {isa = PBXBuildFile; fileRef = A34E8A6D13C48981006D5B76 /* LRRestyRequest+Internal.h */; };
A34E8A7413C5E58C006D5B76 /* LRRestyRequest+Internal.h in Headers */ = {isa = PBXBuildFile; fileRef = A34E8A6D13C48981006D5B76 /* LRRestyRequest+Internal.h */; };
@@ -115,7 +113,6 @@
A379180813A144030025AF1C /* NSString+QueryString.h in Headers */ = {isa = PBXBuildFile; fileRef = A389DFAF125380080006572D /* NSString+QueryString.h */; settings = {ATTRIBUTES = (Public, ); }; };
A379180913A144030025AF1C /* NSURL+QueryInspector.h in Headers */ = {isa = PBXBuildFile; fileRef = A389DFB1125380080006572D /* NSURL+QueryInspector.h */; settings = {ATTRIBUTES = (Public, ); }; };
A379180A13A144030025AF1C /* NSRunLoop+Additions.h in Headers */ = {isa = PBXBuildFile; fileRef = A33E504D12E89EB900746A2C /* NSRunLoop+Additions.h */; settings = {ATTRIBUTES = (Public, ); }; };
- A379180B13A144030025AF1C /* NSObject+SynchronousProxy.h in Headers */ = {isa = PBXBuildFile; fileRef = A33E50CB12E8ACE800746A2C /* NSObject+SynchronousProxy.h */; settings = {ATTRIBUTES = (Public, ); }; };
A379180C13A144030025AF1C /* LRRestyClient+GET.h in Headers */ = {isa = PBXBuildFile; fileRef = A389E001125381700006572D /* LRRestyClient+GET.h */; settings = {ATTRIBUTES = (Public, ); }; };
A379180D13A144030025AF1C /* LRRestyClient+POST.h in Headers */ = {isa = PBXBuildFile; fileRef = A389E003125381700006572D /* LRRestyClient+POST.h */; settings = {ATTRIBUTES = (Public, ); }; };
A379180E13A144030025AF1C /* LRRestyClient+PUT.h in Headers */ = {isa = PBXBuildFile; fileRef = A389E005125381700006572D /* LRRestyClient+PUT.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -189,7 +186,6 @@
A3F45A8713A12E4F00242E72 /* NSString+QueryString.m in Sources */ = {isa = PBXBuildFile; fileRef = A389DFB0125380080006572D /* NSString+QueryString.m */; };
A3F45A8813A12E4F00242E72 /* NSURL+QueryInspector.m in Sources */ = {isa = PBXBuildFile; fileRef = A389DFB2125380080006572D /* NSURL+QueryInspector.m */; };
A3F45A8913A12E4F00242E72 /* NSRunLoop+Additions.m in Sources */ = {isa = PBXBuildFile; fileRef = A33E504E12E89EB900746A2C /* NSRunLoop+Additions.m */; };
- A3F45A8A13A12E4F00242E72 /* NSObject+SynchronousProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = A33E50CC12E8ACE800746A2C /* NSObject+SynchronousProxy.m */; };
A3F45A8B13A12E4F00242E72 /* LRRestyClient+GET.m in Sources */ = {isa = PBXBuildFile; fileRef = A389E002125381700006572D /* LRRestyClient+GET.m */; };
A3F45A8C13A12E4F00242E72 /* LRRestyClient+POST.m in Sources */ = {isa = PBXBuildFile; fileRef = A389E004125381700006572D /* LRRestyClient+POST.m */; };
A3F45A8D13A12E4F00242E72 /* LRRestyClient+PUT.m in Sources */ = {isa = PBXBuildFile; fileRef = A389E006125381700006572D /* LRRestyClient+PUT.m */; };
@@ -288,8 +284,6 @@
A33E504E12E89EB900746A2C /* NSRunLoop+Additions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSRunLoop+Additions.m"; sourceTree = "<group>"; };
A33E50C312E8AC6F00746A2C /* LRSynchronousProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LRSynchronousProxy.h; sourceTree = "<group>"; };
A33E50C412E8AC6F00746A2C /* LRSynchronousProxy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LRSynchronousProxy.m; sourceTree = "<group>"; };
- A33E50CB12E8ACE800746A2C /* NSObject+SynchronousProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+SynchronousProxy.h"; sourceTree = "<group>"; };
- A33E50CC12E8ACE800746A2C /* NSObject+SynchronousProxy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+SynchronousProxy.m"; sourceTree = "<group>"; };
A34E8A6B13C47B2D006D5B76 /* RetryTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RetryTests.m; sourceTree = "<group>"; };
A34E8A6D13C48981006D5B76 /* LRRestyRequest+Internal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "LRRestyRequest+Internal.h"; sourceTree = "<group>"; };
A35D8777125A8E3200E5A8BD /* LRRestyFramework-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "LRRestyFramework-Info.plist"; path = "Resources/LRRestyFramework-Info.plist"; sourceTree = "<group>"; };
@@ -720,8 +714,6 @@
A389DFB2125380080006572D /* NSURL+QueryInspector.m */,
A33E504D12E89EB900746A2C /* NSRunLoop+Additions.h */,
A33E504E12E89EB900746A2C /* NSRunLoop+Additions.m */,
- A33E50CB12E8ACE800746A2C /* NSObject+SynchronousProxy.h */,
- A33E50CC12E8ACE800746A2C /* NSObject+SynchronousProxy.m */,
);
path = Categories;
sourceTree = "<group>";
@@ -1025,7 +1017,6 @@
A379180813A144030025AF1C /* NSString+QueryString.h in Headers */,
A379180913A144030025AF1C /* NSURL+QueryInspector.h in Headers */,
A379180A13A144030025AF1C /* NSRunLoop+Additions.h in Headers */,
- A379180B13A144030025AF1C /* NSObject+SynchronousProxy.h in Headers */,
A379180C13A144030025AF1C /* LRRestyClient+GET.h in Headers */,
A379180D13A144030025AF1C /* LRRestyClient+POST.h in Headers */,
A379180E13A144030025AF1C /* LRRestyClient+PUT.h in Headers */,
@@ -1065,7 +1056,6 @@
A389E030125381700006572D /* LRRestyTypes.h in Headers */,
A33E504F12E89EB900746A2C /* NSRunLoop+Additions.h in Headers */,
A33E50C512E8AC6F00746A2C /* LRSynchronousProxy.h in Headers */,
- A33E50CD12E8ACE800746A2C /* NSObject+SynchronousProxy.h in Headers */,
0FB9786D13A20F050010A37C /* LRRestyClient+DELETE.h in Headers */,
A322549F13B6C93900F16A97 /* LRRestyResponse+Internal.h in Headers */,
A34E8A7313C5E58B006D5B76 /* LRRestyRequest+Internal.h in Headers */,
@@ -1347,7 +1337,6 @@
A3F45A8713A12E4F00242E72 /* NSString+QueryString.m in Sources */,
A3F45A8813A12E4F00242E72 /* NSURL+QueryInspector.m in Sources */,
A3F45A8913A12E4F00242E72 /* NSRunLoop+Additions.m in Sources */,
- A3F45A8A13A12E4F00242E72 /* NSObject+SynchronousProxy.m in Sources */,
A3F45A8B13A12E4F00242E72 /* LRRestyClient+GET.m in Sources */,
A3F45A8C13A12E4F00242E72 /* LRRestyClient+POST.m in Sources */,
A3F45A8D13A12E4F00242E72 /* LRRestyClient+PUT.m in Sources */,
@@ -1433,7 +1422,6 @@
A35D8751125A8D8D00E5A8BD /* LRRestyClientProxyDelegate.m in Sources */,
A33E505012E89EB900746A2C /* NSRunLoop+Additions.m in Sources */,
A33E50C612E8AC6F00746A2C /* LRSynchronousProxy.m in Sources */,
- A33E50CE12E8ACE800746A2C /* NSObject+SynchronousProxy.m in Sources */,
0FB9786E13A20F050010A37C /* LRRestyClient+DELETE.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
View
14 Tests/Acceptance/TimeoutTests.m
@@ -65,4 +65,18 @@ - (void)testCanHandleTimeoutsUsingGlobalTimeoutHandler
assertEventuallyThat(&timedOutRequest, is(notNilValue()));
}
+- (void)testCanHandleTimeoutsUsingGlobalTimeoutHandlerWithSynchronousCalls
+{
+ __block LRRestyRequest *timedOutRequest = nil;
+
+ [client setGlobalTimeout:1 handleWithBlock:^(LRRestyRequest *request) {
+ timedOutRequest = request;
+ }];
+
+ [client post:resourceWithPath(@"/long/request") payload:[NSDictionary dictionaryWithObject:@"5" forKey:@"sleep"]];
+
+ assertEventuallyThat(&timedOutRequest, is(notNilValue()));
+}
+
END_ACCEPTANCE_TEST
+
Please sign in to comment.
Something went wrong with that request. Please try again.