Skip to content

Commit

Permalink
More wack-a-mole style bug fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
pokeb committed Dec 18, 2009
1 parent 027c38c commit ac543a7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
49 changes: 25 additions & 24 deletions Classes/ASIHTTPRequest.m
Expand Up @@ -21,7 +21,7 @@
#import "ASIInputStream.h"

// Automatically set on build
NSString *ASIHTTPRequestVersion = @"v1.2-43 2009-12-18";
NSString *ASIHTTPRequestVersion = @"v1.2-44 2009-12-18";

NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain";

Expand Down Expand Up @@ -867,6 +867,16 @@ - (void)startRequest
return;
}

// Start the HTTP connection
if (!CFReadStreamOpen(readStream)) {
[self destroyReadStream];
[[self cancelledLock] unlock];
[self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileBuildingRequestType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Unable to start HTTP connection",NSLocalizedDescriptionKey,nil]]];
return;
}

[[self cancelledLock] unlock];

if (shouldResetProgressIndicators) {
double amount = 1;
if (showAccurateProgress) {
Expand All @@ -883,17 +893,16 @@ - (void)startRequest
// Record when the request started, so we can timeout if nothing happens
[self setLastActivityTime:[NSDate date]];


// Schedule the stream
if (!throttleWakeUpTime || [throttleWakeUpTime timeIntervalSinceDate:[NSDate date]] < 0) {
[self scheduleReadStream];
}

[[self cancelledLock] unlock];

}

- (void)loadAsynchronous
{
// Schedule the stream
if (!readStreamIsScheduled && (!throttleWakeUpTime || [throttleWakeUpTime timeIntervalSinceDate:[NSDate date]] < 0)) {
[self scheduleReadStream];
}

[NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(updateStatus:) userInfo:nil repeats:YES];

// If we're running asynchronously on the main thread, the runloop will already be running
Expand All @@ -907,6 +916,11 @@ - (void)loadAsynchronous
// This is the main loop for synchronous requests.
- (void)loadSynchronous
{
// Schedule the stream
if (!readStreamIsScheduled && (!throttleWakeUpTime || [throttleWakeUpTime timeIntervalSinceDate:[NSDate date]] < 0)) {
[self scheduleReadStream];
}

while (!complete) {
[self checkRequestStatus];
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.25, NO);
Expand Down Expand Up @@ -968,7 +982,6 @@ - (void)checkRequestStatus
} else {
[[self cancelledLock] unlock];
// Go all the way back to the beginning and build the request again, so that we can apply any new cookies

[self main];
}
return;
Expand All @@ -991,10 +1004,6 @@ - (void)checkRequestStatus

}

// This thread should wait for 1/4 second for the stream to do something
//CFRunLoopRunInMode(kCFRunLoopDefaultMode,0.25,NO);


[[self cancelledLock] unlock];
}

Expand Down Expand Up @@ -2358,7 +2367,9 @@ - (void)destroyReadStream
{
if (readStream) {
CFReadStreamSetClient(readStream, kCFStreamEventNone, NULL, NULL);
CFReadStreamUnscheduleFromRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
if (readStreamIsScheduled) {
CFReadStreamUnscheduleFromRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
}
CFReadStreamClose(readStream);
CFRelease(readStream);
readStream = NULL;
Expand All @@ -2370,24 +2381,14 @@ - (void)scheduleReadStream
if (readStream && !readStreamIsScheduled) {
// Reset the timeout
[self setLastActivityTime:[NSDate date]];
CFStreamClientContext ctxt = {0, self, NULL, NULL, NULL};
CFReadStreamSetClient(readStream, kNetworkEvents, ReadStreamClientCallBack, &ctxt);
CFReadStreamScheduleWithRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
readStreamIsScheduled = YES;

// Start the HTTP connection
if (!CFReadStreamOpen(readStream)) {
[self destroyReadStream];
[self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileBuildingRequestType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Unable to start HTTP connection",NSLocalizedDescriptionKey,nil]]];
return;
}
}
}

- (void)unscheduleReadStream
{
if (readStream && readStreamIsScheduled) {
CFReadStreamSetClient(readStream, kCFStreamEventNone, NULL, NULL);
CFReadStreamUnscheduleFromRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
readStreamIsScheduled = NO;
}
Expand Down
4 changes: 2 additions & 2 deletions Classes/Tests/ASIHTTPRequestTests.m
Expand Up @@ -1044,11 +1044,11 @@ - (void)testThrottlingDownloadBandwidth
// We'll test first without throttling
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_great_american_novel_%28abridged%29.txt"]];
NSDate *date = [NSDate date];
//[request startSynchronous];
[request startSynchronous];

NSTimeInterval interval =[date timeIntervalSinceNow];
BOOL success = (interval > -7);
//GHAssertTrue(success,@"Downloaded the file too slowly - either this is a bug, or your internet connection is too slow to run this test (must be able to download 128KB in less than 7 seconds, without throttling)");
GHAssertTrue(success,@"Downloaded the file too slowly - either this is a bug, or your internet connection is too slow to run this test (must be able to download 128KB in less than 7 seconds, without throttling)");

// Now we'll test with throttling
[ASIHTTPRequest setMaxBandwidthPerSecond:ASIWWANBandwidthThrottleAmount];
Expand Down
2 changes: 2 additions & 0 deletions Classes/Tests/ASINetworkQueueTests.m
Expand Up @@ -695,6 +695,7 @@ - (void)testPartialResume
[request setDownloadDestinationPath:downloadPath];
[request setTemporaryFileDownloadPath:temporaryPath];
[request setAllowResumeForFileDownloads:YES];
[networkQueue setRunRequestsInBackgroundThread:YES];
[networkQueue addOperation:request];
[networkQueue go];

Expand Down Expand Up @@ -754,6 +755,7 @@ - (void)testPartialResume
[networkQueue setDownloadProgressDelegate:self];
[networkQueue setShowAccurateProgress:YES];
[networkQueue setDelegate:self];
[networkQueue setRunRequestsInBackgroundThread:YES];
[networkQueue setQueueDidFinishSelector:@selector(queueFinished:)];

request = [[[ASIHTTPRequest alloc] initWithURL:downloadURL] autorelease];
Expand Down

0 comments on commit ac543a7

Please sign in to comment.