Permalink
Browse files

Added method for cleaning up temporary file downloads, and tests for …

…this
  • Loading branch information...
1 parent 7a09218 commit 83393425435ef4ea69498cbc71c217e797e760ca @pokeb committed Mar 22, 2009
View
@@ -223,6 +223,10 @@ typedef enum _ASINetworkErrorType {
// Cancel loading and clean up
- (void)cancelLoad;
+// Call to delete the temporary file used during a file download (if it exists)
+// No need to call this if the request succeeds - it is removed automatically
+- (void)removeTemporaryDownloadFile;
+
#pragma mark upload/download progress
// Called on main thread to update progress delegates
View
@@ -458,6 +458,16 @@ - (void)cancelLoad
}
+- (void)removeTemporaryDownloadFile
+{
+ //Remove the temporary file
+ NSError *removeError = nil;
+ [[NSFileManager defaultManager] removeItemAtPath:temporaryFileDownloadPath error:&removeError];
+ if (removeError) {
+ [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Failed to delete file at %@ with error: %@",temporaryFileDownloadPath,removeError],NSLocalizedDescriptionKey,removeError,NSUnderlyingErrorKey,nil]]];
+ }
+}
+
#pragma mark upload/download progress
@@ -1151,12 +1161,7 @@ - (void)handleStreamComplete
fileError = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Decompression of %@ failed with code %hi",temporaryFileDownloadPath,decompressionStatus],NSLocalizedDescriptionKey,nil]];
}
- //Remove the temporary file
- NSError *removeError = nil;
- [[NSFileManager defaultManager] removeItemAtPath:temporaryFileDownloadPath error:&removeError];
- if (removeError) {
- fileError = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Failed to delete file at %@ with error: %@",temporaryFileDownloadPath,removeError],NSLocalizedDescriptionKey,removeError,NSUnderlyingErrorKey,nil]];
- }
+ [self removeTemporaryDownloadFile];
} else {
//Remove any file at the destination path
@@ -534,5 +534,4 @@ - (void)testPartialFetch
GHAssertTrue(success,@"Failed to correctly display increment progress for a partial download");
}
-
@end
@@ -192,7 +192,6 @@ - (void)testFailureCancelsOtherRequests
[requestThatShouldFail release];
}
-
- (void)requestFailedCancellingOthers:(ASIHTTPRequest *)request
{
@@ -372,9 +371,42 @@ - (void)testPartialResume
success = (progress == 1.0);
GHAssertTrue(success,@"Failed to increment progress properly");
- [networkQueue release];
+ [networkQueue release];
+
+
+ //Test the temporary file cleanup
+ complete = NO;
+ progress = 0;
+ networkQueue = [[ASINetworkQueue alloc] init];
+ [networkQueue setDownloadProgressDelegate:self];
+ [networkQueue setShowAccurateProgress:YES];
+ [networkQueue setDelegate:self];
+ [networkQueue setQueueDidFinishSelector:@selector(queueFinished:)];
+
+ request = [[[ASIHTTPRequest alloc] initWithURL:downloadURL] autorelease];
+ [request setDownloadDestinationPath:downloadPath];
+ [request setTemporaryFileDownloadPath:temporaryPath];
+ [request setAllowResumeForFileDownloads:YES];
+ [networkQueue addOperation:request];
+ [networkQueue go];
+
+ // Let the download run for 5 seconds
+ timeoutTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(stopQueue:) userInfo:nil repeats:NO];
+ while (!complete) {
+ [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
+ }
+ [networkQueue cancelAllOperations];
+
+ success = ([[NSFileManager defaultManager] fileExistsAtPath:temporaryPath]);
+ GHAssertTrue(success,@"Temporary download file doesn't exist");
+
+ [request removeTemporaryDownloadFile];
+
+ success = (![[NSFileManager defaultManager] fileExistsAtPath:temporaryPath]);
+ GHAssertTrue(success,@"Temporary download file should have been deleted");
timeoutTimer = nil;
+ [networkQueue release];
}

0 comments on commit 8339342

Please sign in to comment.