Skip to content

Commit ae73d3b

Browse files
authored
Merge pull request #324 from e06084/master
Add network error inject script
2 parents 589ed8a + 1bfd1e3 commit ae73d3b

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
language: objective-c
22
osx_image: xcode11
3+
sudo: true
34

45
before_install:
56
- rvm install 2.6.4
@@ -11,6 +12,7 @@ before_install:
1112
- pod setup
1213
- pod install --verbose --no-repo-update
1314

15+
1416
before_script:
1517
- export QINIU_TEST_ENV="travis"
1618

QiniuSDKTests/QNFormUploadTest.m

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,55 @@ - (void)testUp {
5050
XCTAssert(testInfo.reqId, @"Pass");
5151
}
5252

53+
// upload 100 file and calculate upload success rate
54+
- (void)test100Up {
55+
NSInteger count = 100;
56+
__block NSInteger completeCount = 0;
57+
__block NSInteger successCount = 0;
58+
for (int i=0; i<count; i++) {
59+
NSString *taskId = [NSString stringWithFormat:@"test100Up_%d", i];
60+
[self test100UpTask:taskId complete:^(BOOL isSuccess) {
61+
@synchronized (self) {
62+
if (isSuccess) {
63+
successCount += 1;
64+
}
65+
completeCount += 1;
66+
NSLog(@"upload file: test100Up_%d", i);
67+
}
68+
}];
69+
}
70+
71+
AGWW_WAIT_WHILE(completeCount != count, 100.0);
72+
73+
CGFloat successRate = successCount * 1.0 / count;
74+
NSLog(@"successCount: %td", successCount);
75+
NSLog(@"successRate: %lf", successRate);
76+
XCTAssert(completeCount == count, @"Pass");
77+
}
78+
79+
- (void)test100UpTask:(NSString *)taskId complete:(void(^)(BOOL isSuccess))complete{
80+
QNConfiguration *config = [QNConfiguration build:^(QNConfigurationBuilder *builder) {
81+
builder.timeoutInterval = 5;
82+
}];
83+
QNUploadManager *upManager = [[QNUploadManager alloc]initWithConfiguration:config];
84+
QNUploadOption *opt = [[QNUploadOption alloc] initWithMime:@"text/plain" progressHandler:nil params:@{ @"x:foo" : @"bar" } checkCrc:YES cancellationSignal:nil];
85+
NSMutableString *contentString = [NSMutableString string];
86+
NSString *word = @"Hello, World!";
87+
while (contentString.length < 1024) {
88+
[contentString appendString:word];
89+
}
90+
NSData *data = [[contentString copy] dataUsingEncoding:NSUTF8StringEncoding];
91+
[upManager putData:data key:taskId token:g_token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
92+
if (info.isOK && info.reqId) {
93+
complete(YES);
94+
} else {
95+
NSLog(@"upload failed. response info is: %@",info);
96+
complete(NO);
97+
}
98+
} option:opt];
99+
}
100+
101+
53102
// travis ci iOS simulator 8.1 failed,其他环境(mac, iOS 9.0)正常,待详细排查
54103
//- (void)testHttpsUp {
55104
// __block QNResponseInfo *testInfo = nil;

hack/network_loss.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/bash
2+
3+
# Created by chupei on 20/05
4+
# Copyright (c) 2014年 Qiniu. All rights reserved.
5+
# This script is used to test upload success rate in bad network environment.
6+
7+
set -o errexit
8+
set -o pipefail
9+
10+
inject-network-loss(){
11+
sudo dnctl pipe 1 config plr $1
12+
# sudo dnctl pipe 1 config plr $1 bw 1000Kbit/s
13+
echo "dummynet in proto tcp from any to any pipe 1" > pf.conf
14+
echo "dummynet out proto tcp from any to any pipe 1" >> pf.conf
15+
sudo pfctl -f pf.conf
16+
}
17+
18+
info(){
19+
echo -e "[$(date +'%Y-%m-%dT%H:%M:%S.%N%z')] INFO: $@" >&1
20+
}
21+
22+
info "enable pf"
23+
trap 'info "disable pf"; sudo pfctl -d' EXIT
24+
sudo pfctl -e
25+
26+
info "inject network loss 0.01"
27+
inject-network-loss 0.01
28+
29+
info "run unit test"
30+
xcodebuild test -workspace QiniuSDK.xcworkspace -scheme QiniuSDK_iOS -configuration Release -destination 'platform=iOS Simulator,OS=13.4.1,name=iPhone 11' -only-testing:QiniuSDK_iOSTests/QNFormUploadTest/test100Up
31+
32+
33+
info "inject network loss 0.1"
34+
inject-network-loss 0.1
35+
36+
info "run unit test"
37+
xcodebuild test -workspace QiniuSDK.xcworkspace -scheme QiniuSDK_iOS -configuration Release -destination 'platform=iOS Simulator,OS=13.4.1,name=iPhone 11' -only-testing:QiniuSDK_iOSTests/QNFormUploadTest/test100Up
38+
39+
info "inject network loss 0.2"
40+
inject-network-loss 0.2
41+
42+
info "run unit test"
43+
xcodebuild test -workspace QiniuSDK.xcworkspace -scheme QiniuSDK_iOS -configuration Release -destination 'platform=iOS Simulator,OS=13.4.1,name=iPhone 11' -only-testing:QiniuSDK_iOSTests/QNFormUploadTest/test100Up
44+
45+
info "inject network loss 0.5"
46+
inject-network-loss 0.5
47+
48+
info "run unit test"
49+
xcodebuild test -workspace QiniuSDK.xcworkspace -scheme QiniuSDK_iOS -configuration Release -destination 'platform=iOS Simulator,OS=13.4.1,name=iPhone 11' -only-testing:QiniuSDK_iOSTests/QNFormUploadTest/test100Up
50+
51+

0 commit comments

Comments
 (0)