-
Notifications
You must be signed in to change notification settings - Fork 225
Dns prefetch #336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Dns prefetch #336
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
ebaa5fb
add threads and retry
9e8eaff
multithreads_retry
0620a6c
recode retry
e301472
update retry
cb6c771
updata multithreads
1733b23
format code
15083c2
update threads and fix a record bug
d3d7561
format code
3455c49
update multithreads
b6e3beb
protect mkfile func
053f72e
Thread
ee7da7a
dnsPrefetch
ab61e43
7.3.18
af00247
recoder
43c110d
review and code protect
172e671
concurrentHashMap
5ece256
change code style
a1b2e9b
change code
eee4645
test
516a8fa
recode
8667835
custom dns
08c8605
recode cacheKey
2f03fb6
format code test
3971a03
safety
31fefbd
safety
56ebbbb
some func have been optimised
a6c4ee5
rename atomic
5d4ff72
checkIpv6
786dab3
recode get ip
376f3f7
getHostIp
a262f76
test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
215 changes: 215 additions & 0 deletions
215
library/src/androidTest/java/com/qiniu/android/DnsApiTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
| if (data == null) { | ||
| Log.e("qiniutest: ", "recover data is null "); | ||
| return; | ||
| } | ||
| DnsPrefetcher.recoverDnsCache(data); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
|
|
||
| } | ||
|
|
||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JemyCheung 考虑
getFileName()返回 null 的情况