Skip to content

Commit

Permalink
Fixing unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
robbiehanson committed Mar 24, 2013
1 parent 5580b9b commit 87397ae
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 27 deletions.
4 changes: 2 additions & 2 deletions Samples/DynamicServer/HTTPResponseTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
122 changes: 97 additions & 25 deletions Samples/DynamicServer/HTTPResponseTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,101 +50,148 @@ - (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];
}

- (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

0 comments on commit 87397ae

Please sign in to comment.