Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions library/library.iml
Original file line number Diff line number Diff line change
Expand Up @@ -61,44 +61,48 @@
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/.DS_Store" />
<excludeFolder url="file://$MODULE_DIR$/build/docs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-safeguard" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-support" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jacoco" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/shaders" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/transforms" />
<excludeFolder url="file://$MODULE_DIR$/build/ivy.xml" />
<excludeFolder url="file://$MODULE_DIR$/build/libs" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/poms" />
<excludeFolder url="file://$MODULE_DIR$/build/reports" />
<excludeFolder url="file://$MODULE_DIR$/build/test-results" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 26 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="Gradle: com.squareup.okio:okio:1.14.0@jar" level="project" />
<orderEntry type="library" exported="" name="Gradle: com.squareup.okhttp3:okhttp:3.11.0@jar" level="project" />
<orderEntry type="library" exported="" name="Gradle: android-android-26" level="project" />
<orderEntry type="library" exported="" name="com.squareup.okio:okio:1.14.0@jar" level="project" />
<orderEntry type="library" exported="" name="com.squareup.okhttp3:okhttp:3.11.0@jar" level="project" />
</component>
</module>
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ public void testFile() throws Throwable {
checkTemp(ts, "testFile");
}

public void testData() throws Throwable {
Temp[] ts = new Temp[]{templateData(400, 0.2), templateData(700, 0.2), templateData(1024, 0.51), templateData(4 * 1024 + 785, 0.5), templateData(4 * 1024, 0.5), templateData(8 * 1024, 0.6)};
checkTemp(ts, "testData");
}
// public void testData() throws Throwable {
// Temp[] ts = new Temp[]{templateData(400, 0.2), templateData(700, 0.2), templateData(1024, 0.51), templateData(4 * 1024 + 785, 0.5), templateData(4 * 1024, 0.5), templateData(8 * 1024, 0.6)};
// checkTemp(ts, "testData");
// }

private void checkTemp(Temp[] ts, String type) {
int failedCount = 0;
Expand Down
215 changes: 215 additions & 0 deletions library/src/androidTest/java/com/qiniu/android/DnsApiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
package com.qiniu.android;


import android.test.InstrumentationTestCase;
import android.util.Log;

import com.qiniu.android.collect.Config;
import com.qiniu.android.common.ZoneInfo;
import com.qiniu.android.http.DnsPrefetcher;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.http.custom.DnsCacheKey;
import com.qiniu.android.storage.Configuration;
import com.qiniu.android.storage.Recorder;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UpProgressHandler;
import com.qiniu.android.storage.UploadManager;
import com.qiniu.android.storage.UploadOptions;
import com.qiniu.android.storage.persistent.DnsCacheFile;
import com.qiniu.android.utils.AndroidNetwork;
import com.qiniu.android.utils.StringUtils;

import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
* Created by jemy on 2019/8/20.
*/

public class DnsApiTest extends InstrumentationTestCase {
public void testDns() throws Throwable {
List<InetAddress> inetAddresses = null;
DnsPrefetcher dnsPrefetcher;
// try {
// inetAddresses = DnsPrefetcher.getDnsBySystem().lookup("upload.qiniup.com");
// } catch (UnknownHostException e) {
// e.printStackTrace();
// }
Log.e("qiniutest", "InetAddress: " + inetAddresses.size());
//超耗时过程
// for (int i = 0; i < inetAddresses.size(); i++) {
// Log.e("qiniutest", "InetAddress.getCanonicalHostName: " + inetAddresses.get(i).getCanonicalHostName());
//
// }
for (int i = 0; i < inetAddresses.size(); i++) {
Log.e("qiniutest", "InetAddress.getHostAddress: " + inetAddresses.get(i).getHostAddress());
}
}


public void testQueryDomain() {
ZoneInfo info = null;

DnsPrefetcher dnsPrefetcher = DnsPrefetcher.getDnsPrefetcher();
try {
info = dnsPrefetcher.init(TestConfig.uptoken_prefetch).getPreQueryZone();
} catch (UnknownHostException e) {
e.printStackTrace();
}
if (info == null) {
Log.e("qiniutest: ", "null");
}
Log.e("qiniutest: ", info.toString());
Log.e("qiniutest: ", info.upDomainsList.get(0));
}


public void testLocalDomain() {
List<ZoneInfo> info = null;
DnsPrefetcher dnsPrefetcher = DnsPrefetcher.getDnsPrefetcher();
try {
info = dnsPrefetcher.init(TestConfig.uptoken_prefetch).getLocalZone();
} catch (UnknownHostException e) {
e.printStackTrace();
}
if (info == null) {
Log.e("qiniutest: ", "null");
}
for (int i = 0; i < info.size(); i++) {
Log.e("qiniutest: ", info.get(i).toString());
}

}


public void testLocalIp() {
String s = AndroidNetwork.getHostIP();
Log.e("qiniutest", s);
}

public void testDnsPreAndcache() {
Configuration config = new Configuration.Builder().build();
boolean needPrefetch = DnsPrefetcher.checkRePrefetchDns(TestConfig.uptoken_prefetch, config);
Log.e("qiniutest", "check:" + needPrefetch);
if (needPrefetch) {
DnsPrefetcher.startPrefetchDns(TestConfig.uptoken_prefetch, config);
} else {
testRecoverCache();
return;
}
//预取或者recover success
List<String> list = DnsPrefetcher.getDnsPrefetcher().getHosts();
ConcurrentHashMap<String, List<InetAddress>> map = DnsPrefetcher.getDnsPrefetcher().getConcurrentHashMap();
Log.e("qiniutest: ", "list size: " + list.size());
for (String s : list) {
Log.e("qiniutest: ", "uphost: " + s);
List<InetAddress> list1 = map.get(s);
for (InetAddress inetAddress :
list1) {
Log.e("qiniutest: ", "ip: " + inetAddress.getHostAddress());
}
}

}

//test recover
public void testRecoverCache() {

Recorder recorder = null;
try {
recorder = new DnsCacheFile(Config.dnscacheDir);
} catch (IOException e) {
e.printStackTrace();
}
String fileName = recorder.getFileName();
if (fileName == null) {
Log.e("qiniutest: ", "recover file is null ");
return;
}
byte[] data = recorder.get(recorder.getFileName());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JemyCheung 考虑 getFileName() 返回 null 的情况

if (data == null) {
Log.e("qiniutest: ", "recover data is null ");
return;
}
DnsPrefetcher.recoverDnsCache(data);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JemyCheung 考虑 data 返回 null 的情况



ConcurrentHashMap<String, List<InetAddress>> map1 = DnsPrefetcher.getDnsPrefetcher().getConcurrentHashMap();
List<String> list = DnsPrefetcher.getDnsPrefetcher().getHosts();
Log.e("qiniutest: ", "size for cache: " + list.size());
for (String s : list) {
Log.e("qiniutest: ", "uphost for cache: " + s);
List<InetAddress> list1 = map1.get(s);
for (InetAddress inetAddress :
list1) {
Log.e("qiniutest: ", "ip for cache: " + inetAddress.getHostAddress());
}
}
}

int time = 0;
final Object lock = new Object();

public void testAtomic() {
final int size = 6 * 1024;
for (int i = 0; i < 3; i++) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Configuration config = new Configuration.Builder().build();
final UploadManager uploadManager = new UploadManager(config, 3);
final String expectKey = "r=" + size + "k";
final File f;
f = TempFile.createFile(size);
final UploadOptions uploadoption = new UploadOptions(null, null, false, new UpProgressHandler() {
public void progress(String key, double percent) {
Log.e("qiniutest", percent + "");
}
}, null);

uploadManager.put(f, expectKey, TestConfig.token_z0, new UpCompletionHandler() {
public void complete(String k, ResponseInfo rinfo, JSONObject response) {
Log.e("qiniutest", k + rinfo);
time += 1;
if (time == 3) {
lock.notify();
}
}
}, uploadoption);


} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
synchronized (lock) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public void testSerializable() {
DnsCacheKey key = new DnsCacheKey("12321", "127.0.0.1", "akscope");
Log.e("qiniutest", key.toString());
DnsCacheKey key1 = DnsCacheKey.toCacheKey(key.toString());
if (key1 == null) {
return;
}
Log.e("qiniutest", key1.getCurrentTime() + ":" + key1.getLocalIp() + ":" + key1.getAkScope());

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import com.qiniu.android.common.Zone;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.Configuration;
import com.qiniu.android.storage.Recorder;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UpProgressHandler;
import com.qiniu.android.storage.UploadManager;
import com.qiniu.android.storage.UploadOptions;
import com.qiniu.android.storage.persistent.FileRecorder;

import junit.framework.Assert;

Expand Down Expand Up @@ -83,7 +85,7 @@ public void progress(String key, double percent) {

public void setUp() throws Exception {
Configuration config = new Configuration.Builder().build();
uploadManager = new UploadManager(config);
uploadManager = new UploadManager(config, 3);
ACollectUploadInfoTest.testInit();
}

Expand Down Expand Up @@ -120,7 +122,7 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
// Assert.assertNotNull(resp);
// String hash = resp.getString("hash");
// Assert.assertEquals(hash, Etag.file(f));
TempFile.remove(f);
//TempFile.remove(f);
// Assert.assertTrue("进度有变化,不大可能一直相同。" + getProgress(), !isProgressAllSame());
Log.d(TAG, getProgress());
ACollectUploadInfoTest.recordFileTest();
Expand Down Expand Up @@ -189,12 +191,12 @@ public void test4M1k2() throws Throwable {
}

@LargeTest
public void test4M() throws Throwable {
template(1024 * 4);
public void testResumeUploadFast() throws Throwable {
template(1024 * 8);
}

// @LargeTest
// public void test8M1k() throws Throwable{
// template(1024*8+1);
// }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public final class TestConfig {
//华东上传凭证
public static final String bucket_z0 = "sdk-z0";
public static final String token_z0 = "QWYn5TFQsLLU1pL5MFEmX3s5DmHdUThav9WyOWOm:KUw9Fxq8NoLTeZNsKkhTjoKpsuQ=:eyJzY29wZSI6InNkay16MCIsInJldHVybmJvZHkiOiJ7XCJoYXNoXCI6XCIkKGV0YWcpXCIsXCJrZXlcIjpcIiQoa2V5KVwiLFwiZm5hbWVcIjpcIiAkKGZuYW1lKSBcIixcImZzaXplXCI6XCIkKGZzaXplKVwiLFwibWltZVR5cGVcIjpcIiQobWltZVR5cGUpXCIsXCJmb29cIjpcIiQoeDpmb28pXCIsXCJiYXJcIjpcIiQoeDpiYXIpXCJ9IiwiZGVhZGxpbmUiOjQyOTQ5NjcyOTV9";
public static final String token_z0 = "MP_Ebql_lSsUrDr7WrXn_5vKocQDLvTPCNEFeVmp:zfCfgMtCzO8l8iab_lbm402wZY8=:eyJzY29wZSI6ImFuZHJvaWR0ZXN0IiwiZGVhZGxpbmUiOjE1Njk4NTgzMTl9";
//华北上传凭证
public static final String bucket_z1 = "sdk-z1";
public static final String token_z1 = "QWYn5TFQsLLU1pL5MFEmX3s5DmHdUThav9WyOWOm:ILIWwUZ7_hvZeKJzbBKvpo8DpYc=:eyJzY29wZSI6InNkay16MSIsInJldHVybmJvZHkiOiJ7XCJoYXNoXCI6XCIkKGV0YWcpXCIsXCJrZXlcIjpcIiQoa2V5KVwiLFwiZm5hbWVcIjpcIiAkKGZuYW1lKSBcIixcImZzaXplXCI6XCIkKGZzaXplKVwiLFwibWltZVR5cGVcIjpcIiQobWltZVR5cGUpXCIsXCJmb29cIjpcIiQoeDpmb28pXCIsXCJiYXJcIjpcIiQoeDpiYXIpXCJ9IiwiZGVhZGxpbmUiOjQyOTQ5NjcyOTV9";
Expand All @@ -21,6 +21,9 @@ public final class TestConfig {
public static final String token_na0 = "QWYn5TFQsLLU1pL5MFEmX3s5DmHdUThav9WyOWOm:QxnYkUaYIw4fIsI133_tjFJ_03M=:eyJzY29wZSI6InNkay1uYTAiLCJyZXR1cm5ib2R5Ijoie1wiaGFzaFwiOlwiJChldGFnKVwiLFwia2V5XCI6XCIkKGtleSlcIixcImZuYW1lXCI6XCIgJChmbmFtZSkgXCIsXCJmc2l6ZVwiOlwiJChmc2l6ZSlcIixcIm1pbWVUeXBlXCI6XCIkKG1pbWVUeXBlKVwiLFwiZm9vXCI6XCIkKHg6Zm9vKVwiLFwiYmFyXCI6XCIkKHg6YmFyKVwifSIsImRlYWRsaW5lIjo0Mjk0OTY3Mjk1fQ==";
public static final String ak = "QWYn5TFQsLLU1pL5MFEmX3s5DmHdUThav9WyOWOm";

//dns prefetch token
public static final String uptoken_prefetch = "MP_Ebql_lSsUrDr7WrXn_5vKocQDLvTPCNEFeVmp:3KJpXCGMqm6EAYU71RF1HDmQrcE=:eyJzY29wZSI6ImFuZHJvaWR0ZXN0IiwiZGVhZGxpbmUiOjE1Njc0OTAxODF9";

/**
* 华东机房
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.qiniu.android.common;

import android.test.AndroidTestCase;
import android.util.Log;

import com.qiniu.android.TestConfig;

Expand Down Expand Up @@ -76,10 +77,11 @@ public void onFailure(int reason) {
e.printStackTrace();
}
ZoneInfo info = autoZone.zoneInfo(ak, bkt);
// Log.d("zone0: ", info.toString());
Log.d("qiniutest: ", info.toString());

ZoneInfo info2 = autoZone.zoneInfo(ak, bkt);
Assert.assertSame(info, info2);

}

}
12 changes: 12 additions & 0 deletions library/src/main/java/com/qiniu/android/collect/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ public final class Config {
*/
public static int interval = 10;

/**
* dns缓存信息目录
*/
public static String dnscacheDir = "/sdcard/dnschache";

/**
* preQuery host
*/
public static String preQueryHost = "uc.qbox.me";

public static int rePreHost = 2;

/**
* 当网络切换到 wifi 下,切换到此设置
*/
Expand Down
Loading