Skip to content

Commit 32ea6ec

Browse files
committed
add QNConcurrentResumeUploadTest.m
1 parent 04cea82 commit 32ea6ec

File tree

12 files changed

+387
-54
lines changed

12 files changed

+387
-54
lines changed

QiniuSDK.xcodeproj/project.pbxproj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
CCC3A50822DC10CB00D835B1 /* QNConcurrentResumeUpload.h in Headers */ = {isa = PBXBuildFile; fileRef = CCC3A50622DC10CB00D835B1 /* QNConcurrentResumeUpload.h */; };
2121
CCC3A50922DC10CB00D835B1 /* QNConcurrentResumeUpload.m in Sources */ = {isa = PBXBuildFile; fileRef = CCC3A50722DC10CB00D835B1 /* QNConcurrentResumeUpload.m */; };
2222
CCC3A50A22DC10CB00D835B1 /* QNConcurrentResumeUpload.m in Sources */ = {isa = PBXBuildFile; fileRef = CCC3A50722DC10CB00D835B1 /* QNConcurrentResumeUpload.m */; };
23+
CCF661062355C2C00018A41E /* QNConcurrentResumeUploadTest.m in Sources */ = {isa = PBXBuildFile; fileRef = CCF661052355C2C00018A41E /* QNConcurrentResumeUploadTest.m */; };
24+
CCF661072355C4480018A41E /* QNConcurrentResumeUploadTest.m in Sources */ = {isa = PBXBuildFile; fileRef = CCF661052355C2C00018A41E /* QNConcurrentResumeUploadTest.m */; };
25+
CCF661082355C5120018A41E /* QiniuSDK.h in Headers */ = {isa = PBXBuildFile; fileRef = DF2CDE5819DAC6A400CE01FB /* QiniuSDK.h */; };
2326
DF0A03231B3BABEC00E3778C /* QiniuSDK_Mac.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = DF2CDDFF19DAC05500CE01FB /* QiniuSDK_Mac.dylib */; };
2427
DF0A03241B3BAC3900E3778C /* QNFormUploadTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DF3C504619DD7BA6000F548F /* QNFormUploadTest.m */; };
2528
DF0A03251B3BAC5700E3778C /* libQiniuSDK_iOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = DF2CDE1A19DAC08400CE01FB /* libQiniuSDK_iOS.a */; };
@@ -54,7 +57,6 @@
5457
DF2CDE6119DAC6A400CE01FB /* QNCrc32.m in Sources */ = {isa = PBXBuildFile; fileRef = DF2CDE5319DAC6A400CE01FB /* QNCrc32.m */; };
5558
DF2CDE6219DAC6A400CE01FB /* QNCrc32.m in Sources */ = {isa = PBXBuildFile; fileRef = DF2CDE5319DAC6A400CE01FB /* QNCrc32.m */; };
5659
DF2CDE6719DAC6A400CE01FB /* QNVersion.h in Headers */ = {isa = PBXBuildFile; fileRef = DF2CDE5719DAC6A400CE01FB /* QNVersion.h */; };
57-
DF2CDE6819DAC6A400CE01FB /* QiniuSDK.h in Headers */ = {isa = PBXBuildFile; fileRef = DF2CDE5819DAC6A400CE01FB /* QiniuSDK.h */; };
5860
DF2CDE6919DAC6A400CE01FB /* QNUploadManager.h in Headers */ = {isa = PBXBuildFile; fileRef = DF2CDE5A19DAC6A400CE01FB /* QNUploadManager.h */; };
5961
DF2CDE6A19DAC6A400CE01FB /* QNUploadManager.m in Sources */ = {isa = PBXBuildFile; fileRef = DF2CDE5B19DAC6A400CE01FB /* QNUploadManager.m */; };
6062
DF2CDE6B19DAC6A400CE01FB /* QNUploadManager.m in Sources */ = {isa = PBXBuildFile; fileRef = DF2CDE5B19DAC6A400CE01FB /* QNUploadManager.m */; };
@@ -144,6 +146,7 @@
144146
C1D8890E270C769A9E798A8E /* libPods-QiniuSDK_iOS.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-QiniuSDK_iOS.a"; sourceTree = BUILT_PRODUCTS_DIR; };
145147
CCC3A50622DC10CB00D835B1 /* QNConcurrentResumeUpload.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = QNConcurrentResumeUpload.h; sourceTree = "<group>"; };
146148
CCC3A50722DC10CB00D835B1 /* QNConcurrentResumeUpload.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = QNConcurrentResumeUpload.m; sourceTree = "<group>"; };
149+
CCF661052355C2C00018A41E /* QNConcurrentResumeUploadTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = QNConcurrentResumeUploadTest.m; sourceTree = "<group>"; };
147150
D84AF95CA892E1089E26F959 /* Pods-QiniuSDK_iOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-QiniuSDK_iOS.release.xcconfig"; path = "Pods/Target Support Files/Pods-QiniuSDK_iOS/Pods-QiniuSDK_iOS.release.xcconfig"; sourceTree = "<group>"; };
148151
DF0D23CC19DCE6C400D6B68F /* QNResponseInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNResponseInfo.h; sourceTree = "<group>"; };
149152
DF0D23CE19DCE6E500D6B68F /* QNResponseInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNResponseInfo.m; sourceTree = "<group>"; };
@@ -399,6 +402,7 @@
399402
DF293C9619DB865800799011 /* QNCrc32Test.m */,
400403
DF3C504619DD7BA6000F548F /* QNFormUploadTest.m */,
401404
DF3C504919DD7D9F000F548F /* QNResumeUploadTest.m */,
405+
CCF661052355C2C00018A41E /* QNConcurrentResumeUploadTest.m */,
402406
DFA9B63E19DFD8C900A15FD1 /* QNEtagTest.m */,
403407
DFA9B64319DFE43500A15FD1 /* QNTempFile.h */,
404408
DFA9B64419DFE43500A15FD1 /* QNTempFile.m */,
@@ -455,13 +459,13 @@
455459
93CEF47E1BDE11FF00750FE8 /* QNPHAssetFile.h in Headers */,
456460
DF2CDE6019DAC6A400CE01FB /* QNCrc32.h in Headers */,
457461
DFF126F11B639F3B0005C39C /* QNFile.h in Headers */,
462+
CCF661082355C5120018A41E /* QiniuSDK.h in Headers */,
458463
DFA9B63B19DF904000A15FD1 /* QNEtag.h in Headers */,
459464
DF2CDE5C19DAC6A400CE01FB /* QNUrlSafeBase64.h in Headers */,
460465
93D01E641C7065C200E7F47C /* QNPHAssetResource.h in Headers */,
461466
DFA9B64919E0018800A15FD1 /* QNUploadOption.h in Headers */,
462467
DFA9B65C19E0B58900A15FD1 /* QNRecorderDelegate.h in Headers */,
463468
DFF525311A6235D100D02BA1 /* QNSessionManager.h in Headers */,
464-
DF2CDE6819DAC6A400CE01FB /* QiniuSDK.h in Headers */,
465469
DF2CDE6919DAC6A400CE01FB /* QNUploadManager.h in Headers */,
466470
DF293CA919DC0AF000799011 /* QNResumeUpload.h in Headers */,
467471
DFA39AA419F1272800A1A158 /* QNAsyncRun.h in Headers */,
@@ -718,6 +722,7 @@
718722
DF437CDF1B243A2C0099587B /* QNUpTokenTest.m in Sources */,
719723
DF3C504A19DD7D9F000F548F /* QNResumeUploadTest.m in Sources */,
720724
DF293C9719DB865800799011 /* QNCrc32Test.m in Sources */,
725+
CCF661062355C2C00018A41E /* QNConcurrentResumeUploadTest.m in Sources */,
721726
FD4FEE441F298D58003A38B8 /* QNPipelineTest.m in Sources */,
722727
DFA9B64519DFE43500A15FD1 /* QNTempFile.m in Sources */,
723728
DFF525391A64079B00D02BA1 /* QNSessionTest.m in Sources */,
@@ -765,6 +770,7 @@
765770
DF0A03281B3BAC6E00E3778C /* QNFormUploadTest.m in Sources */,
766771
DF0A03291B3BAC6E00E3778C /* QNResumeUploadTest.m in Sources */,
767772
DF0A032B1B3BAC6E00E3778C /* QNEtagTest.m in Sources */,
773+
CCF661072355C4480018A41E /* QNConcurrentResumeUploadTest.m in Sources */,
768774
FD4FEE451F298D58003A38B8 /* QNPipelineTest.m in Sources */,
769775
DF0A032C1B3BAC6E00E3778C /* QNTempFile.m in Sources */,
770776
DF0A032D1B3BAC6E00E3778C /* QNFileRecorderTest.m in Sources */,

QiniuSDK.xcodeproj/xcshareddata/xcschemes/QiniuSDK_Mac.xcscheme

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@
2828
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
2929
shouldUseLaunchSchemeArgsEnv = "YES">
3030
<Testables>
31+
<TestableReference
32+
skipped = "NO">
33+
<BuildableReference
34+
BuildableIdentifier = "primary"
35+
BlueprintIdentifier = "DF2CDE0919DAC05500CE01FB"
36+
BuildableName = "QiniuSDK_MacTests.xctest"
37+
BlueprintName = "QiniuSDK_MacTests"
38+
ReferencedContainer = "container:QiniuSDK.xcodeproj">
39+
</BuildableReference>
40+
</TestableReference>
3141
</Testables>
3242
</TestAction>
3343
<LaunchAction

QiniuSDK/Common/QNALAssetFile.m

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ @interface QNALAssetFile ()
2121

2222
@property (readonly) int64_t fileModifyTime;
2323

24+
@property (nonatomic, strong) NSLock *lock;
25+
2426
@end
2527

2628
@implementation QNALAssetFile
@@ -35,22 +37,34 @@ - (instancetype)init:(ALAsset *)asset
3537
_fileModifyTime = t;
3638
_fileSize = asset.defaultRepresentation.size;
3739
_asset = asset;
40+
_lock = [[NSLock alloc] init];
3841
}
3942

4043
return self;
4144
}
4245

4346
- (NSData *)read:(long)offset
44-
size:(long)size {
45-
ALAssetRepresentation *rep = [self.asset defaultRepresentation];
46-
Byte *buffer = (Byte *)malloc(size);
47-
NSUInteger buffered = [rep getBytes:buffer fromOffset:offset length:size error:nil];
48-
49-
return [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
47+
size:(long)size
48+
error:(NSError **)error {
49+
50+
NSData *data = nil;
51+
@try {
52+
[_lock lock];
53+
ALAssetRepresentation *rep = [self.asset defaultRepresentation];
54+
Byte *buffer = (Byte *)malloc(size);
55+
NSUInteger buffered = [rep getBytes:buffer fromOffset:offset length:size error:error];
56+
data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
57+
} @catch (NSException *exception) {
58+
*error = [NSError errorWithDomain:NSCocoaErrorDomain code:kQNFileError userInfo:@{NSLocalizedDescriptionKey : exception.reason}];
59+
NSLog(@"read file failed reason: %@ \n%@", exception.reason, exception.callStackSymbols);
60+
} @finally {
61+
[_lock unlock];
62+
}
63+
return data;
5064
}
5165

52-
- (NSData *)readAll {
53-
return [self read:0 size:(long)_fileSize];
66+
- (NSData *)readAllWithError:(NSError **)error {
67+
return [self read:0 size:(long)_fileSize error:error];
5468
}
5569

5670
- (void)close {

QiniuSDK/Common/QNFile.m

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ - (instancetype)init:(NSString *)path
7676
}
7777

7878
- (NSData *)read:(long)offset
79-
size:(long)size {
79+
size:(long)size
80+
error:(NSError **)error {
8081

8182
NSData *data = nil;
8283
@try {
@@ -88,15 +89,16 @@ - (NSData *)read:(long)offset
8889
data = [_file readDataOfLength:size];
8990
}
9091
} @catch (NSException *exception) {
92+
*error = [NSError errorWithDomain:NSCocoaErrorDomain code:kQNFileError userInfo:@{NSLocalizedDescriptionKey : exception.reason}];
9193
NSLog(@"read file failed reason: %@ \n%@", exception.reason, exception.callStackSymbols);
9294
} @finally {
9395
[_lock unlock];
9496
}
9597
return data;
9698
}
9799

98-
- (NSData *)readAll {
99-
return [self read:0 size:(long)_fileSize];
100+
- (NSData *)readAllWithError:(NSError **)error {
101+
return [self read:0 size:(long)_fileSize error:error];
100102
}
101103

102104
- (void)close {

QiniuSDK/Common/QNFileDelegate.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,21 @@
1818
*
1919
* @param offset 偏移地址
2020
* @param size 大小
21+
* @param error 错误信息
2122
*
2223
* @return 数据
2324
*/
2425
- (NSData *)read:(long)offset
25-
size:(long)size;
26+
size:(long)size
27+
error:(NSError **)error;
2628

2729
/**
2830
* 读取所有文件内容
2931
*
3032
* @return 数据
33+
* @error 错误信息
3134
*/
32-
- (NSData *)readAll;
35+
- (NSData *)readAllWithError:(NSError **)error;
3336

3437
/**
3538
* 关闭文件

QiniuSDK/Common/QNPHAssetFile.m

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ @interface QNPHAssetFile ()
3030

3131
@property (nonatomic) NSFileHandle *file;
3232

33+
@property (nonatomic, strong) NSLock *lock;
34+
3335
@end
3436

3537
@implementation QNPHAssetFile
@@ -44,6 +46,7 @@ - (instancetype)init:(PHAsset *)phAsset error:(NSError *__autoreleasing *)error
4446
_fileModifyTime = t;
4547
_phAsset = phAsset;
4648
_filepath = [self getInfo];
49+
_lock = [[NSLock alloc] init];
4750
if (PHAssetMediaTypeVideo == self.phAsset.mediaType) {
4851
NSError *error2 = nil;
4952
NSDictionary *fileAttr = [[NSFileManager defaultManager] attributesOfItemAtPath:_filepath error:&error2];
@@ -80,16 +83,30 @@ - (instancetype)init:(PHAsset *)phAsset error:(NSError *__autoreleasing *)error
8083
return self;
8184
}
8285

83-
- (NSData *)read:(long)offset size:(long)size {
84-
if (_assetData != nil) {
85-
return [_assetData subdataWithRange:NSMakeRange(offset, (unsigned int)size)];
86+
- (NSData *)read:(long)offset
87+
size:(long)size
88+
error:(NSError **)error {
89+
90+
NSData *data = nil;
91+
@try {
92+
[_lock lock];
93+
if (_assetData != nil) {
94+
data = [_assetData subdataWithRange:NSMakeRange(offset, (unsigned int)size)];
95+
} else {
96+
[_file seekToFileOffset:offset];
97+
data = [_file readDataOfLength:size];
98+
}
99+
} @catch (NSException *exception) {
100+
*error = [NSError errorWithDomain:NSCocoaErrorDomain code:kQNFileError userInfo:@{NSLocalizedDescriptionKey : exception.reason}];
101+
NSLog(@"read file failed reason: %@ \n%@", exception.reason, exception.callStackSymbols);
102+
} @finally {
103+
[_lock unlock];
86104
}
87-
[_file seekToFileOffset:offset];
88-
return [_file readDataOfLength:size];
105+
return data;
89106
}
90107

91-
- (NSData *)readAll {
92-
return [self read:0 size:(long)_fileSize];
108+
- (NSData *)readAllWithError:(NSError **)error {
109+
return [self read:0 size:(long)_fileSize error:error];
93110
}
94111

95112
- (void)close {

QiniuSDK/Common/QNPHAssetResource.m

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ @interface QNPHAssetResource ()
3838

3939
@property (nonatomic, strong) NSURL *assetURL;
4040

41+
@property (nonatomic, strong) NSLock *lock;
42+
4143
@end
4244

4345
@implementation QNPHAssetResource
@@ -52,23 +54,35 @@ - (instancetype)init:(PHAssetResource *)phAssetResource
5254
}
5355
_fileModifyTime = t;
5456
_phAssetResource = phAssetResource;
57+
_lock = [[NSLock alloc] init];
5558
[self getInfo];
5659
}
5760
return self;
5861
}
5962

60-
- (NSData *)read:(long)offset size:(long)size {
61-
NSRange subRange = NSMakeRange(offset, size);
62-
if (!self.assetData) {
63-
self.assetData = [self fetchDataFromAsset:self.phAssetResource];
63+
- (NSData *)read:(long)offset
64+
size:(long)size
65+
error:(NSError **)error {
66+
67+
NSData *data = nil;
68+
@try {
69+
[_lock lock];
70+
NSRange subRange = NSMakeRange(offset, size);
71+
if (!self.assetData) {
72+
self.assetData = [self fetchDataFromAsset:self.phAssetResource error:error];
73+
}
74+
data = [self.assetData subdataWithRange:subRange];
75+
} @catch (NSException *exception) {
76+
*error = [NSError errorWithDomain:NSCocoaErrorDomain code:kQNFileError userInfo:@{NSLocalizedDescriptionKey : exception.reason}];
77+
NSLog(@"read file failed reason: %@ \n%@", exception.reason, exception.callStackSymbols);
78+
} @finally {
79+
[_lock unlock];
6480
}
65-
NSData *subData = [self.assetData subdataWithRange:subRange];
66-
67-
return subData;
81+
return data;
6882
}
6983

70-
- (NSData *)readAll {
71-
return [self read:0 size:(long)_fileSize];
84+
- (NSData *)readAllWithError:(NSError **)error {
85+
return [self read:0 size:(long)_fileSize error:error];
7286
}
7387

7488
- (void)close {
@@ -109,16 +123,9 @@ - (void)getInfo {
109123

110124
BOOL blHave = [[NSFileManager defaultManager] fileExistsAtPath:pathToWrite];
111125
if (!blHave) {
112-
NSLog(@"no have");
113126
return;
114127
} else {
115-
NSLog(@" have");
116-
BOOL blDele = [[NSFileManager defaultManager] removeItemAtPath:pathToWrite error:nil];
117-
if (blDele) {
118-
NSLog(@"dele success");
119-
} else {
120-
NSLog(@"dele fail");
121-
}
128+
[[NSFileManager defaultManager] removeItemAtPath:pathToWrite error:nil];
122129
}
123130
[assetReadLock lock];
124131
[assetReadLock unlockWithCondition:kAMASSETMETADATA_ALLFINISHED];
@@ -130,8 +137,9 @@ - (void)getInfo {
130137
}
131138
}
132139

133-
- (NSData *)fetchDataFromAsset:(PHAssetResource *)videoResource {
140+
- (NSData *)fetchDataFromAsset:(PHAssetResource *)videoResource error:(NSError **)err {
134141
__block NSData *tmpData = [NSData data];
142+
__block NSError *innerError = *err;
135143

136144
NSConditionLock *assetReadLock = [[NSConditionLock alloc] initWithCondition:kAMASSETMETADATA_PENDINGREADS];
137145

@@ -145,20 +153,13 @@ - (NSData *)fetchDataFromAsset:(PHAssetResource *)videoResource {
145153
NSData *videoData = [NSData dataWithContentsOfURL:urlAsset.URL];
146154
tmpData = [NSData dataWithData:videoData];
147155
} else {
148-
NSLog(@"%@", error);
156+
innerError = error;
149157
}
150158
BOOL blHave = [[NSFileManager defaultManager] fileExistsAtPath:pathToWrite];
151159
if (!blHave) {
152-
NSLog(@"no have");
153160
return;
154161
} else {
155-
NSLog(@" have");
156-
BOOL blDele = [[NSFileManager defaultManager] removeItemAtPath:pathToWrite error:nil];
157-
if (blDele) {
158-
NSLog(@"dele success");
159-
} else {
160-
NSLog(@"dele fail");
161-
}
162+
[[NSFileManager defaultManager] removeItemAtPath:pathToWrite error:nil];
162163
}
163164
[assetReadLock lock];
164165
[assetReadLock unlockWithCondition:kAMASSETMETADATA_ALLFINISHED];

QiniuSDK/Storage/QNConcurrentResumeUpload.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,13 @@ - (void)putBlockWithHost:(NSString *)uphost taskQueue:(QNConcurrentTask *)task r
307307
return;
308308
}
309309

310-
NSData *data = [self.file read:task.index * kQNBlockSize size:task.size];
310+
NSError *error;
311+
NSData *data = [self.file read:task.index * kQNBlockSize size:task.size error:&error];
312+
if (error) {
313+
self.complete([QNResponseInfo responseInfoWithFileError:error], self.key, nil);
314+
return;
315+
}
316+
311317
UInt32 blockCrc = [QNCrc32 data:data];
312318

313319
QNInternalProgressBlock progressBlock = ^(long long totalBytesWritten, long long totalBytesExpectedToWrite) {

QiniuSDK/Storage/QNResumeUpload.m

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,12 @@ - (void)makeBlock:(NSString *)uphost
274274
chunkSize:(UInt32)chunkSize
275275
progress:(QNInternalProgressBlock)progressBlock
276276
complete:(QNCompleteBlock)complete {
277-
NSData *data = [self.file read:offset size:chunkSize];
277+
NSError *error;
278+
NSData *data = [self.file read:offset size:chunkSize error:&error];
279+
if (error) {
280+
self.complete([QNResponseInfo responseInfoWithFileError:error], self.key, nil);
281+
return;
282+
}
278283
NSString *url = [[NSString alloc] initWithFormat:@"%@/mkblk/%u", uphost, (unsigned int)blockSize];
279284
_chunkCrc = [QNCrc32 data:data];
280285
[self post:url withData:data withCompleteBlock:complete withProgressBlock:progressBlock];
@@ -286,7 +291,12 @@ - (void)putChunk:(NSString *)uphost
286291
context:(NSString *)context
287292
progress:(QNInternalProgressBlock)progressBlock
288293
complete:(QNCompleteBlock)complete {
289-
NSData *data = [self.file read:offset size:size];
294+
NSError *error;
295+
NSData *data = [self.file read:offset size:size error:&error];
296+
if (error) {
297+
self.complete([QNResponseInfo responseInfoWithFileError:error], self.key, nil);
298+
return;
299+
}
290300
UInt32 chunkOffset = offset % kQNBlockSize;
291301
NSString *url = [[NSString alloc] initWithFormat:@"%@/bput/%@/%u", uphost, context, (unsigned int)chunkOffset];
292302
_chunkCrc = [QNCrc32 data:data];

0 commit comments

Comments
 (0)