Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixing unit test

  • Loading branch information...
commit 87397aef6516bc6925bf14c0c6317dafd1857f3f 1 parent 5580b9b
@robbiehanson authored
View
4 Samples/DynamicServer/HTTPResponseTest.h
@@ -11,8 +11,8 @@
{
// Parents retain children, children do NOT retain parents
- HTTPConnection __unsafe_unretained *connection;
- dispatch_queue_t connectionQueue;
+ __unsafe_unretained HTTPConnection *connection;
+ dispatch_queue_t responseQueue;
BOOL readyToSendResponseHeaders;
}
View
122 Samples/DynamicServer/HTTPResponseTest.m
@@ -2,6 +2,31 @@
#import "HTTPConnection.h"
#import "HTTPLogging.h"
+// Does ARC support support GCD objects?
+// It does if the minimum deployment target is iOS 6+ or Mac OS X 8+
+
+#if TARGET_OS_IPHONE
+
+ // Compiling for iOS
+
+ #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000 // iOS 6.0 or later
+ #define NEEDS_DISPATCH_RETAIN_RELEASE 0
+ #else // iOS 5.X or earlier
+ #define NEEDS_DISPATCH_RETAIN_RELEASE 1
+ #endif
+
+#else
+
+ // Compiling for Mac OS X
+
+ #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1080 // Mac OS X 10.8 or later
+ #define NEEDS_DISPATCH_RETAIN_RELEASE 0
+ #else
+ #define NEEDS_DISPATCH_RETAIN_RELEASE 1 // Mac OS X 10.7 or earlier
+ #endif
+
+#endif
+
// Log levels: off, error, warn, info, verbose
// Other flags: trace
static const int httpLogLevel = HTTP_LOG_LEVEL_OFF; // | HTTP_LOG_FLAG_TRACE;
@@ -25,42 +50,46 @@ - (id)initWithConnection:(HTTPConnection *)parent
HTTPLogTrace();
connection = parent;
-
- connectionQueue = dispatch_get_current_queue();
- dispatch_retain(connectionQueue);
+ responseQueue = dispatch_queue_create("HTTPResponseTest", NULL);
readyToSendResponseHeaders = NO;
- dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
- dispatch_async(concurrentQueue, ^{ @autoreleasepool {
-
- [self doAsyncStuff];
- }});
+ [self doAsyncStuff];
}
return self;
}
- (void)doAsyncStuff
{
- // This method is executed on a global concurrent queue
-
HTTPLogTrace();
- [NSThread sleepForTimeInterval:5.0];
-
- dispatch_async(connectionQueue, ^{ @autoreleasepool {
+ dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
+ dispatch_async(concurrentQueue, ^{ @autoreleasepool {
- [self asyncStuffFinished];
+ // Simulate a long-running asynchronous task...
+ [NSThread sleepForTimeInterval:5.0];
+
+ // Simulate completion callback
+ dispatch_async(responseQueue, ^{ @autoreleasepool {
+
+ [self asyncStuffFinished];
+ }});
}});
}
- (void)asyncStuffFinished
{
- // This method is executed on the connectionQueue
+ // This method is executed on the responseQueue
HTTPLogTrace();
+ // Enable flag that indicates we have enough information to send the response headers.
readyToSendResponseHeaders = YES;
+
+ // Then notify the connection that "something has changed".
+ // The 'responseHasAvailableData:' method is thread-safe.
+ // It knows what state the connection is in, and will "do the right thing".
+ // In this case, it will requery us via delayResponseHeaders to see if it can send the headers yet.
[connection responseHasAvailableData:self];
}
@@ -68,58 +97,101 @@ - (BOOL)delayResponseHeaders
{
HTTPLogTrace2(@"%@[%p] %@ -> %@", THIS_FILE, self, THIS_METHOD, (readyToSendResponseHeaders ? @"NO" : @"YES"));
- return !readyToSendResponseHeaders;
+ __block BOOL delayResponseHeaders = NO;
+
+ dispatch_sync(responseQueue, ^{
+
+ delayResponseHeaders = !readyToSendResponseHeaders;
+ });
+
+ return delayResponseHeaders;
}
- (void)connectionDidClose
{
- // This method is executed on the connectionQueue
-
HTTPLogTrace();
- connection = nil;
+ dispatch_sync(responseQueue, ^{
+ connection = nil;
+ });
}
- (UInt64)contentLength
{
HTTPLogTrace();
- return 0;
+ __block UInt64 contentLength = 0;
+
+ dispatch_sync(responseQueue, ^{
+
+ // Normal code would go here
+ contentLength = 0;
+ });
+
+ return contentLength;
}
- (UInt64)offset
{
HTTPLogTrace();
- return 0;
+ __block UInt64 offset = 0;
+
+ dispatch_sync(responseQueue, ^{
+
+ // Normal code would go here
+ offset = 0;
+ });
+
+ return offset;
}
- (void)setOffset:(UInt64)offset
{
HTTPLogTrace();
- // Ignored
+ dispatch_sync(responseQueue, ^{
+
+ // Normal code would go here
+ });
}
- (NSData *)readDataOfLength:(NSUInteger)length
{
HTTPLogTrace();
- return nil;
+ __block NSData *data = nil;
+
+ dispatch_sync(responseQueue, ^{
+
+ // Normal code would go here
+ });
+
+ return data;
}
- (BOOL)isDone
{
HTTPLogTrace();
- return YES;
+ __block BOOL isDone = NO;
+
+ dispatch_sync(responseQueue, ^{
+
+ // Normal code would go here
+ isDone = YES;
+ });
+
+ return isDone;
}
- (void)dealloc
{
HTTPLogTrace();
- dispatch_release(connectionQueue);
+ #if NEEDS_DISPATCH_RETAIN_RELEASE
+ dispatch_release(responseQueue);
+ #endif
}
@end
Please sign in to comment.
Something went wrong with that request. Please try again.