Skip to content

Commit 7a73f79

Browse files
committed
CCPackageDownloadTests fixed, some nondeterministic errors occured.
Seemed to be a conflict with CCScheduler and the runloop triggering in the unit tests.
1 parent 25de382 commit 7a73f79

File tree

1 file changed

+41
-53
lines changed

1 file changed

+41
-53
lines changed

UnitTests/CCPackageDownloadTests.m

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#import "CCPackage.h"
1313
#import "CCPackageDownloadDelegate.h"
1414
#import "CCDirector.h"
15+
#import "AppDelegate.h"
1516

1617
static NSUInteger __fileDownloadSize = 0;
1718
static BOOL __support_range_request = YES;
@@ -130,8 +131,10 @@ - (void)setUp
130131
{
131132
[super setUp];
132133

134+
[(AppController *)[UIApplication sharedApplication].delegate configureCocos2d];
133135
[[CCDirector sharedDirector] stopAnimation];
134-
136+
// Sping the runloop a bit otherwise nondeterministic exceptions are thrown in the CCScheduler.
137+
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeInterval:0.2 sinceDate:[NSDate date]]];
135138

136139
[NSURLProtocol registerClass:[CCPackageDownloadTestURLProtocol class]];
137140

@@ -189,12 +192,10 @@ - (void)createDownloadFolder
189192

190193

191194
#pragma mark - tests
195+
192196
- (void)testDownloadPackage
193197
{
194-
[self waitForDelegateToReturnAfterRunningBlock:^
195-
{
196-
[_download start];
197-
}];
198+
[self startDownloadAndWaitForDelegateToReturn];
198199

199200
NSFileManager *fileManager = [NSFileManager defaultManager];
200201
NSDictionary *attribs = [fileManager attributesOfItemAtPath:_localURL.path error:nil];
@@ -203,54 +204,25 @@ - (void)testDownloadPackage
203204
XCTAssertEqual([attribs[NSFileSize] unsignedIntegerValue], __fileDownloadSize);
204205
}
205206

206-
- (void)waitForDelegateToReturnAfterRunningBlock:(dispatch_block_t)block
207-
{
208-
block();
209-
210-
while (!_downloadReturned)
211-
{
212-
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
213-
}
214-
}
215-
216-
/*
217-
218-
219207
- (void)testResumeDownloadAKARangeRequest
220208
{
221209
[self setupPartialDownloadOnDisk];
222210

223-
[self waitForDelegateToReturnAfterRunningBlock:^
224-
{
225-
[_download start];
226-
}];
227-
211+
[self startDownloadAndWaitForDelegateToReturn];
228212
NSFileManager *fileManager = [NSFileManager defaultManager];
229213
NSDictionary *attribs = [fileManager attributesOfItemAtPath:_localURL.path error:nil];
230214
XCTAssertTrue(_downloadSuccessful);
231215
XCTAssertTrue([fileManager fileExistsAtPath:_localURL.path]);
232216
XCTAssertEqual([attribs[NSFileSize] unsignedIntegerValue], __fileDownloadSize);
233217
}
234218

235-
- (void)setupPartialDownloadOnDisk
236-
{
237-
NSString *fileName = [_package.remoteURL lastPathComponent];
238-
NSString *pathToPackage = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"Resources-shared/Packages/%@", fileName] ofType:nil];
239-
NSData *data = [[NSData dataWithContentsOfFile:pathToPackage] subdataWithRange:NSMakeRange(0, 5000)];
240-
NSString *tempName = [_download performSelector:@selector(createTempName)];
241-
[data writeToFile:[[_localURL.path stringByDeletingLastPathComponent] stringByAppendingPathComponent:tempName] atomically:YES];
242-
}
243-
244219
- (void)testDownloadOfExistingFile
245220
{
246221
self.shouldOverwriteDownloadedFile = NO;
247222

248223
NSUInteger filesize = [self createDownloadFile];
249224

250-
[self waitForDelegateToReturnAfterRunningBlock:^
251-
{
252-
[_download start];
253-
}];
225+
[self startDownloadAndWaitForDelegateToReturn];
254226

255227
NSFileManager *fileManager = [NSFileManager defaultManager];;
256228
NSDictionary *attribs = [fileManager attributesOfItemAtPath:_localURL.path error:nil];
@@ -265,10 +237,7 @@ - (void)testOverwriteExistingDownload
265237

266238
[self createDownloadFile];
267239

268-
[self waitForDelegateToReturnAfterRunningBlock:^
269-
{
270-
[_download start];
271-
}];
240+
[self startDownloadAndWaitForDelegateToReturn];
272241

273242
NSFileManager *fileManager = [NSFileManager defaultManager];;
274243
NSDictionary *attribs = [fileManager attributesOfItemAtPath:_localURL.path error:nil];
@@ -277,6 +246,38 @@ - (void)testOverwriteExistingDownload
277246
XCTAssertEqual([attribs[NSFileSize] unsignedIntegerValue], __fileDownloadSize);
278247
}
279248

249+
- (void)testDownloadWith404Response
250+
{
251+
[_package setValue:[NSURL URLWithString:@"http://package.request.fake/DOES_NOT_EXIST.zip"] forKey:NSStringFromSelector(@selector(remoteURL))];
252+
253+
[self startDownloadAndWaitForDelegateToReturn];
254+
255+
XCTAssertFalse(_downloadSuccessful);
256+
XCTAssertNotNil(_downloadError);
257+
}
258+
259+
260+
#pragma mark - Helper
261+
262+
- (void)setupPartialDownloadOnDisk
263+
{
264+
NSString *fileName = [_package.remoteURL lastPathComponent];
265+
NSString *pathToPackage = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"Resources-shared/Packages/%@", fileName] ofType:nil];
266+
NSData *data = [[NSData dataWithContentsOfFile:pathToPackage] subdataWithRange:NSMakeRange(0, 5000)];
267+
NSString *tempName = [_download performSelector:@selector(createTempName)];
268+
[data writeToFile:[[_localURL.path stringByDeletingLastPathComponent] stringByAppendingPathComponent:tempName] atomically:YES];
269+
}
270+
271+
- (void)startDownloadAndWaitForDelegateToReturn
272+
{
273+
[_download start];
274+
275+
while (!_downloadReturned)
276+
{
277+
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
278+
}
279+
}
280+
280281
- (NSUInteger)createDownloadFile
281282
{
282283
NSFileManager *fileManager = [NSFileManager defaultManager];
@@ -288,19 +289,6 @@ - (NSUInteger)createDownloadFile
288289
return [attribs[NSFileSize] unsignedIntegerValue];
289290
}
290291

291-
- (void)testDownloadWith404Response
292-
{
293-
[_package setValue:[NSURL URLWithString:@"http://package.request.fake/DOES_NOT_EXIST.zip"] forKey:NSStringFromSelector(@selector(remoteURL))];
294-
295-
[self waitForDelegateToReturnAfterRunningBlock:^
296-
{
297-
[_download start];
298-
}];
299-
300-
XCTAssertFalse(_downloadSuccessful);
301-
XCTAssertNotNil(_downloadError);
302-
}
303-
*/
304292

305293
#pragma mark - CCPackageDownloadDelegate
306294

0 commit comments

Comments
 (0)