Skip to content

Commit 203f731

Browse files
authored
Merge pull request #234 from sxci/collect_upload_info
Collect upload info
2 parents 9f7a8aa + b5114c1 commit 203f731

20 files changed

+702
-136
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package com.qiniu.android;
2+
3+
import android.test.AndroidTestCase;
4+
import android.util.Log;
5+
6+
import com.qiniu.android.collect.Config;
7+
8+
import junit.framework.Assert;
9+
10+
import java.io.BufferedReader;
11+
import java.io.File;
12+
import java.io.FileReader;
13+
import java.io.IOException;
14+
import java.util.Date;
15+
import java.util.Iterator;
16+
import java.util.LinkedList;
17+
import java.util.Queue;
18+
19+
/**
20+
* Created by Simon on 12/6/16.
21+
*/
22+
23+
public class ACollectUploadInfoTest extends AndroidTestCase {
24+
25+
private static long recordFileLastModified = 337l;
26+
private static Queue<Long> queue = new LinkedList<Long>() {{
27+
offer(1l);
28+
offer(2l);
29+
offer(3l);
30+
offer(4l);
31+
}};
32+
33+
@Override
34+
protected void setUp() throws Exception {
35+
testInit();
36+
}
37+
38+
public static void testInit() {
39+
Config.isRecord = true;
40+
Config.isUpload = true;
41+
Config.interval = 0;
42+
Config.uploadThreshold = 4 * 1024;
43+
Config.maxRecordFileSize = 6 * 1024;
44+
}
45+
46+
public static void recordFile() {
47+
File recordFile = getRecordFile();
48+
if (recordFile.length() < Config.maxRecordFileSize && recordFile.length() > 10) {
49+
Assert.assertNotSame(recordFile.lastModified(), recordFileLastModified);
50+
}
51+
recordFileLastModified = recordFile.lastModified();
52+
53+
int log = "200,CwIAAF4znMnpno0U,up.qiniu.com,183.131.7.18,80,383.0,1481014578,262144\n".length();
54+
55+
long fileSize = recordFile.length();
56+
putRecordFileSize(fileSize);
57+
long avgSize = getRecordFileAvgSize();
58+
59+
Assert.assertTrue(fileSize < Config.maxRecordFileSize + log * 2);
60+
61+
String sizes = getRecordFileSizes();
62+
System.out.println("RecordFileSize: " + sizes);
63+
Assert.assertTrue("文件大小有变化,不大可能一直相同。" + sizes, !isRecordFileSizeAllSame());
64+
Assert.assertTrue("有上传,上传后清理文件,文件大小平均值不大可能大于上传阀值。" + sizes, avgSize < Config.uploadThreshold + log * 2);
65+
}
66+
67+
private static void putRecordFileSize(long size) {
68+
queue.offer(size);
69+
if (queue.size() > 4) {
70+
queue.poll();
71+
}
72+
}
73+
74+
private static long getRecordFileAvgSize() {
75+
long sum = 0;
76+
long count = 0;
77+
Iterator<Long> it = queue.iterator();
78+
while (it.hasNext()) {
79+
count += 1;
80+
sum += it.next();
81+
}
82+
return sum / count;
83+
}
84+
85+
private static boolean isRecordFileSizeAllSame() {
86+
long size = -1;
87+
boolean same = true;
88+
Iterator<Long> it = queue.iterator();
89+
while (it.hasNext()) {
90+
long temp = it.next();
91+
if (size > 0) {
92+
same = same && (size == temp);
93+
}
94+
size = temp;
95+
}
96+
return same;
97+
}
98+
99+
private static String getRecordFileSizes() {
100+
StringBuilder ss = new StringBuilder("");
101+
Iterator<Long> it = queue.iterator();
102+
while (it.hasNext()) {
103+
ss.append(it.next()).append(", ");
104+
}
105+
return ss.toString();
106+
}
107+
108+
public static File getRecordFile() {
109+
String recordFileName = "_qiniu_record_file_hu3z9lo7anx03";
110+
File recordFile = new File(Config.recordDir, recordFileName);
111+
return recordFile;
112+
}
113+
114+
public static void recordFileTest() {
115+
// showRecordInfo();
116+
recordFile();
117+
}
118+
119+
public static void showRecordInfo() {
120+
File recordFile = getRecordFile();
121+
Log.d("recordFile", "recordFile.getAbsolutePath(): " + recordFile.getAbsolutePath());
122+
Log.d("recordFile", "recordFile.length(): " + recordFile.length());
123+
Log.d("recordFile", "recordFile.lastModified(): " + new Date(recordFile.lastModified()));
124+
showContent(recordFile);
125+
}
126+
127+
private static void showContent(File recordFile) {
128+
FileReader fileReader = null;
129+
BufferedReader br = null;
130+
try {
131+
fileReader = new FileReader(recordFile);
132+
br = new BufferedReader(fileReader);
133+
String line = null;
134+
Log.d("recordFile", "recordFile content: start");
135+
while ((line = br.readLine()) != null) {
136+
Log.d("recordFile", line);
137+
}
138+
Log.d("recordFile", "recordFile content: end");
139+
} catch (Exception e) {
140+
141+
} finally {
142+
if (br != null) {
143+
try {
144+
br.close();
145+
} catch (IOException e) {
146+
e.printStackTrace();
147+
}
148+
}
149+
if (fileReader != null) {
150+
try {
151+
fileReader.close();
152+
} catch (IOException e) {
153+
e.printStackTrace();
154+
}
155+
}
156+
}
157+
}
158+
}

library/src/androidTest/java/com/qiniu/android/CancelTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ protected void setUp() throws Exception {
4343
String folder = f.getParent();
4444
FileRecorder fr = new FileRecorder(folder);
4545
uploadManager = new UploadManager(fr);
46+
ACollectUploadInfoTest.testInit();
4647
}
4748

4849
public void test400k() throws Throwable {
@@ -143,6 +144,8 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
143144
Assert.assertNull(resp);
144145

145146
TempFile.remove(tempFile);
147+
148+
ACollectUploadInfoTest.recordFileTest();
146149
}
147150

148151
private void templateData(final int size, final double pos) throws Throwable {
@@ -205,6 +208,8 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
205208
Assert.assertEquals(info.toString(), expectKey, key);
206209
Assert.assertTrue(info.toString(), info.isCancelled());
207210
Assert.assertNull(resp);
211+
212+
ACollectUploadInfoTest.recordFileTest();
208213
}
209214

210215
}

library/src/androidTest/java/com/qiniu/android/FormUploadTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
6262
Assert.assertEquals(info.toString(), expectKey, key);
6363
Assert.assertTrue(info.toString(), info.isOK());
6464
Assert.assertNotNull(info.reqId);
65+
System.out.println(info.ip);
6566
Assert.assertNotNull(resp);
6667

6768
String hash = resp.getString("hash");

library/src/androidTest/java/com/qiniu/android/HttpTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.qiniu.android.http.CompletionHandler;
99
import com.qiniu.android.http.ProxyConfiguration;
1010
import com.qiniu.android.http.ResponseInfo;
11+
import com.qiniu.android.storage.UpToken;
1112
import com.qiniu.android.utils.StringMap;
1213

1314
import junit.framework.Assert;
@@ -45,7 +46,7 @@ protected void setUp() throws Exception {
4546
@SmallTest
4647
public void testPost1() throws Throwable {
4748
httpManager.asyncPost("http://www.baidu.com",
48-
"hello".getBytes(), null, TestConfig.ak, null, new CompletionHandler() {
49+
"hello".getBytes(), null, UpToken.parse(TestConfig.token), null, new CompletionHandler() {
4950
@Override
5051
public void complete(ResponseInfo rinfo, JSONObject response) {
5152
Assert.assertNotNull(rinfo);
@@ -66,7 +67,7 @@ public void complete(ResponseInfo rinfo, JSONObject response) {
6667
@SmallTest
6768
public void testPost2() throws Throwable {
6869

69-
httpManager.asyncPost("http://up.qiniu.com", "hello".getBytes(), null, TestConfig.ak, null, new CompletionHandler() {
70+
httpManager.asyncPost("http://up.qiniu.com", "hello".getBytes(), null, UpToken.parse(TestConfig.token), null, new CompletionHandler() {
7071
@Override
7172
public void complete(ResponseInfo rinfo, JSONObject response) {
7273
Log.d("qiniutest", rinfo.toString());
@@ -88,7 +89,7 @@ public void testPost3() throws Throwable {
8889
runTestOnUiThread(new Runnable() { // THIS IS THE KEY TO SUCCESS
8990
public void run() {
9091
httpManager.asyncPost("http://httpbin.org/status/500", "hello".getBytes(),
91-
null, TestConfig.ak, null, new CompletionHandler() {
92+
null, UpToken.parse(TestConfig.token), null, new CompletionHandler() {
9293
@Override
9394
public void complete(ResponseInfo rinfo, JSONObject response) {
9495
Log.d("qiniutest", rinfo.toString());
@@ -114,7 +115,7 @@ public void testPost4() throws Throwable {
114115
public void run() {
115116
httpManager.asyncPost("http://httpbin.org/status/418",
116117
"hello".getBytes(),
117-
null, TestConfig.ak, null, new CompletionHandler() {
118+
null, UpToken.parse(TestConfig.token), null, new CompletionHandler() {
118119
@Override
119120
public void complete(ResponseInfo rinfo, JSONObject response) {
120121
Log.d("qiniutest", rinfo.toString());
@@ -138,7 +139,7 @@ public void complete(ResponseInfo rinfo, JSONObject response) {
138139
public void testPostNoDomain() throws Throwable {
139140

140141
httpManager.asyncPost("http://no-domain.qiniu.com", "hello".getBytes(),
141-
null, TestConfig.ak, null, new CompletionHandler() {
142+
null, UpToken.parse(TestConfig.token), null, new CompletionHandler() {
142143
@Override
143144
public void complete(ResponseInfo rinfo, JSONObject response) {
144145
Log.d("qiniutest", rinfo.toString());
@@ -183,7 +184,7 @@ public void complete(ResponseInfo rinfo, JSONObject response) {
183184
public void testPostIP() throws Throwable {
184185
StringMap x = new StringMap().put("Host", "www.qiniu.com");
185186
httpManager.asyncPost("http://183.136.139.12/", "hello".getBytes(),
186-
x, TestConfig.ak, null, new CompletionHandler() {
187+
x, UpToken.parse(TestConfig.token), null, new CompletionHandler() {
187188
@Override
188189
public void complete(ResponseInfo rinfo, JSONObject response) {
189190
Log.d("qiniutest", rinfo.toString());
@@ -207,7 +208,7 @@ public void testProxy() throws Throwable {
207208
ProxyConfiguration p = new ProxyConfiguration("115.231.183.168", 80);
208209
Client c = new Client(p, 10, 30, null, null);
209210
c.asyncPost("http://upproxy1.qiniu.com", "hello".getBytes(),
210-
x, TestConfig.ak, null, new CompletionHandler() {
211+
x, UpToken.parse(TestConfig.token), null, new CompletionHandler() {
211212
@Override
212213
public void complete(ResponseInfo rinfo, JSONObject response) {
213214
Log.d("qiniutest", rinfo.toString());

library/src/androidTest/java/com/qiniu/android/HttpsTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.qiniu.android.http.Client;
88
import com.qiniu.android.http.CompletionHandler;
99
import com.qiniu.android.http.ResponseInfo;
10+
import com.qiniu.android.storage.UpToken;
1011

1112
import junit.framework.Assert;
1213

@@ -41,7 +42,7 @@ protected void setUp() throws Exception {
4142

4243
@SmallTest
4344
public void testPost1() throws Throwable {
44-
httpManager.asyncPost("https://www.baidu.com/", "hello".getBytes(), null, TestConfig.ak, null, new CompletionHandler() {
45+
httpManager.asyncPost("https://www.baidu.com/", "hello".getBytes(), null, UpToken.parse(TestConfig.token), null, new CompletionHandler() {
4546
@Override
4647
public void complete(ResponseInfo rinfo, JSONObject response) {
4748
Log.d("qiniutest", rinfo.toString());
@@ -62,7 +63,7 @@ public void complete(ResponseInfo rinfo, JSONObject response) {
6263
@SmallTest
6364
public void testPost2() throws Throwable {
6465
httpManager.asyncPost("https://static-fw.qbox.me/public/v28812/add-on/ga/analytics.js",
65-
"hello".getBytes(), null, TestConfig.ak, null, new CompletionHandler() {
66+
"hello".getBytes(), null, UpToken.parse(TestConfig.token), null, new CompletionHandler() {
6667
@Override
6768
public void complete(ResponseInfo rinfo, JSONObject response) {
6869
Log.d("qiniutest", rinfo.toString());

library/src/androidTest/java/com/qiniu/android/ResumeUploadTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class ResumeUploadTest extends InstrumentationTestCase {
3333
public void setUp() throws Exception {
3434
Configuration config = new Configuration.Builder().build();
3535
uploadManager = new UploadManager(config);
36+
ACollectUploadInfoTest.testInit();
3637
}
3738

3839
private void template(int size) throws Throwable {
@@ -68,6 +69,8 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
6869
String hash = resp.getString("hash");
6970
Assert.assertEquals(hash, Etag.file(f));
7071
TempFile.remove(f);
72+
73+
ACollectUploadInfoTest.recordFileTest();
7174
}
7275

7376
private void template2(int size) throws Throwable {
@@ -110,6 +113,8 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
110113
String hash = resp.getString("hash");
111114
Assert.assertEquals(hash, Etag.file(f));
112115
TempFile.remove(f);
116+
117+
ACollectUploadInfoTest.recordFileTest();
113118
}
114119

115120
private void templateHijack(int size) throws Throwable {

library/src/androidTest/java/com/qiniu/android/SyncFormUploadTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,11 @@ public void test0File() throws Throwable {
172172
}
173173

174174
@SmallTest
175-
public void testNoComplete() {
176-
info = uploadManager.syncPut(new byte[0], null, null, null);
175+
public void test0byte() {
176+
info = uploadManager.syncPut(new byte[0], null, TestConfig.token, null);
177177
Assert.assertEquals(info.toString(), ResponseInfo.ZeroSizeFile, info.statusCode);
178178

179-
info = uploadManager.syncPut("", null, null, null);
179+
info = uploadManager.syncPut("", null, TestConfig.token, null);
180180
Assert.assertEquals(info.toString(), ResponseInfo.ZeroSizeFile, info.statusCode);
181181
}
182182

library/src/androidTest/java/com/qiniu/android/TestFileRecorder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ protected void setUp() throws Exception {
4545
FileRecorder fr = new FileRecorder(folder);
4646
config = new Configuration.Builder().recorder(fr).build();
4747
uploadManager = new UploadManager(config);
48+
49+
ACollectUploadInfoTest.testInit();
4850
}
4951

5052
private void template(final int size, final double pos) throws Throwable {
@@ -134,6 +136,8 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
134136
String hash = resp.getString("hash");
135137
Assert.assertEquals(hash, Etag.file(tempFile));
136138
TempFile.remove(tempFile);
139+
140+
ACollectUploadInfoTest.recordFileTest();
137141
}
138142

139143
public void test600k() throws Throwable {

library/src/androidTest/java/com/qiniu/android/TokenTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
public class TokenTest extends AndroidTestCase {
1313
public void testRight() {
1414
UpToken t = UpToken.parse(TestConfig.token);
15-
Assert.assertNotNull(t);
15+
Assert.assertNotSame(t, UpToken.NULL);
1616
}
1717

1818
public void testEmpty() {
1919
UpToken t = UpToken.parse(null);
20-
Assert.assertNull(t);
20+
Assert.assertEquals(t, UpToken.NULL);
2121

2222
t = UpToken.parse("");
23-
Assert.assertNull(t);
23+
Assert.assertEquals(t, UpToken.NULL);
2424
}
2525

2626
public void testReturnUrl() {

0 commit comments

Comments
 (0)