|
| 1 | +package com.qiniu.android; |
| 2 | + |
| 3 | + |
| 4 | +import android.test.InstrumentationTestCase; |
| 5 | +import android.util.Log; |
| 6 | + |
| 7 | +import com.qiniu.android.collect.Config; |
| 8 | +import com.qiniu.android.common.ZoneInfo; |
| 9 | +import com.qiniu.android.http.DnsPrefetcher; |
| 10 | +import com.qiniu.android.http.ResponseInfo; |
| 11 | +import com.qiniu.android.http.custom.DnsCacheKey; |
| 12 | +import com.qiniu.android.storage.Configuration; |
| 13 | +import com.qiniu.android.storage.Recorder; |
| 14 | +import com.qiniu.android.storage.UpCompletionHandler; |
| 15 | +import com.qiniu.android.storage.UpProgressHandler; |
| 16 | +import com.qiniu.android.storage.UploadManager; |
| 17 | +import com.qiniu.android.storage.UploadOptions; |
| 18 | +import com.qiniu.android.storage.persistent.DnsCacheFile; |
| 19 | +import com.qiniu.android.utils.AndroidNetwork; |
| 20 | +import com.qiniu.android.utils.StringUtils; |
| 21 | + |
| 22 | +import org.json.JSONObject; |
| 23 | + |
| 24 | +import java.io.File; |
| 25 | +import java.io.IOException; |
| 26 | +import java.net.InetAddress; |
| 27 | +import java.net.UnknownHostException; |
| 28 | +import java.util.List; |
| 29 | +import java.util.concurrent.ConcurrentHashMap; |
| 30 | + |
| 31 | +/** |
| 32 | + * Created by jemy on 2019/8/20. |
| 33 | + */ |
| 34 | + |
| 35 | +public class DnsApiTest extends InstrumentationTestCase { |
| 36 | + public void testDns() throws Throwable { |
| 37 | + List<InetAddress> inetAddresses = null; |
| 38 | + DnsPrefetcher dnsPrefetcher; |
| 39 | +// try { |
| 40 | +// inetAddresses = DnsPrefetcher.getDnsBySystem().lookup("upload.qiniup.com"); |
| 41 | +// } catch (UnknownHostException e) { |
| 42 | +// e.printStackTrace(); |
| 43 | +// } |
| 44 | + Log.e("qiniutest", "InetAddress: " + inetAddresses.size()); |
| 45 | + //超耗时过程 |
| 46 | +// for (int i = 0; i < inetAddresses.size(); i++) { |
| 47 | +// Log.e("qiniutest", "InetAddress.getCanonicalHostName: " + inetAddresses.get(i).getCanonicalHostName()); |
| 48 | +// |
| 49 | +// } |
| 50 | + for (int i = 0; i < inetAddresses.size(); i++) { |
| 51 | + Log.e("qiniutest", "InetAddress.getHostAddress: " + inetAddresses.get(i).getHostAddress()); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + public void testQueryDomain() { |
| 57 | + ZoneInfo info = null; |
| 58 | + |
| 59 | + DnsPrefetcher dnsPrefetcher = DnsPrefetcher.getDnsPrefetcher(); |
| 60 | + try { |
| 61 | + info = dnsPrefetcher.init(TestConfig.uptoken_prefetch).getPreQueryZone(); |
| 62 | + } catch (UnknownHostException e) { |
| 63 | + e.printStackTrace(); |
| 64 | + } |
| 65 | + if (info == null) { |
| 66 | + Log.e("qiniutest: ", "null"); |
| 67 | + } |
| 68 | + Log.e("qiniutest: ", info.toString()); |
| 69 | + Log.e("qiniutest: ", info.upDomainsList.get(0)); |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + public void testLocalDomain() { |
| 74 | + List<ZoneInfo> info = null; |
| 75 | + DnsPrefetcher dnsPrefetcher = DnsPrefetcher.getDnsPrefetcher(); |
| 76 | + try { |
| 77 | + info = dnsPrefetcher.init(TestConfig.uptoken_prefetch).getLocalZone(); |
| 78 | + } catch (UnknownHostException e) { |
| 79 | + e.printStackTrace(); |
| 80 | + } |
| 81 | + if (info == null) { |
| 82 | + Log.e("qiniutest: ", "null"); |
| 83 | + } |
| 84 | + for (int i = 0; i < info.size(); i++) { |
| 85 | + Log.e("qiniutest: ", info.get(i).toString()); |
| 86 | + } |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + public void testLocalIp() { |
| 92 | + String s = AndroidNetwork.getHostIP(); |
| 93 | + Log.e("qiniutest", s); |
| 94 | + } |
| 95 | + |
| 96 | + public void testDnsPreAndcache() { |
| 97 | + Configuration config = new Configuration.Builder().build(); |
| 98 | + boolean needPrefetch = DnsPrefetcher.checkRePrefetchDns(TestConfig.uptoken_prefetch, config); |
| 99 | + Log.e("qiniutest", "check:" + needPrefetch); |
| 100 | + if (needPrefetch) { |
| 101 | + DnsPrefetcher.startPrefetchDns(TestConfig.uptoken_prefetch, config); |
| 102 | + } else { |
| 103 | + testRecoverCache(); |
| 104 | + return; |
| 105 | + } |
| 106 | + //预取或者recover success |
| 107 | + List<String> list = DnsPrefetcher.getDnsPrefetcher().getHosts(); |
| 108 | + ConcurrentHashMap<String, List<InetAddress>> map = DnsPrefetcher.getDnsPrefetcher().getConcurrentHashMap(); |
| 109 | + Log.e("qiniutest: ", "list size: " + list.size()); |
| 110 | + for (String s : list) { |
| 111 | + Log.e("qiniutest: ", "uphost: " + s); |
| 112 | + List<InetAddress> list1 = map.get(s); |
| 113 | + for (InetAddress inetAddress : |
| 114 | + list1) { |
| 115 | + Log.e("qiniutest: ", "ip: " + inetAddress.getHostAddress()); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + //test recover |
| 122 | + public void testRecoverCache() { |
| 123 | + |
| 124 | + Recorder recorder = null; |
| 125 | + try { |
| 126 | + recorder = new DnsCacheFile(Config.dnscacheDir); |
| 127 | + } catch (IOException e) { |
| 128 | + e.printStackTrace(); |
| 129 | + } |
| 130 | + String fileName = recorder.getFileName(); |
| 131 | + if (fileName == null) { |
| 132 | + Log.e("qiniutest: ", "recover file is null "); |
| 133 | + return; |
| 134 | + } |
| 135 | + byte[] data = recorder.get(recorder.getFileName()); |
| 136 | + if (data == null) { |
| 137 | + Log.e("qiniutest: ", "recover data is null "); |
| 138 | + return; |
| 139 | + } |
| 140 | + DnsPrefetcher.recoverDnsCache(data); |
| 141 | + |
| 142 | + |
| 143 | + ConcurrentHashMap<String, List<InetAddress>> map1 = DnsPrefetcher.getDnsPrefetcher().getConcurrentHashMap(); |
| 144 | + List<String> list = DnsPrefetcher.getDnsPrefetcher().getHosts(); |
| 145 | + Log.e("qiniutest: ", "size for cache: " + list.size()); |
| 146 | + for (String s : list) { |
| 147 | + Log.e("qiniutest: ", "uphost for cache: " + s); |
| 148 | + List<InetAddress> list1 = map1.get(s); |
| 149 | + for (InetAddress inetAddress : |
| 150 | + list1) { |
| 151 | + Log.e("qiniutest: ", "ip for cache: " + inetAddress.getHostAddress()); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + int time = 0; |
| 157 | + final Object lock = new Object(); |
| 158 | + |
| 159 | + public void testAtomic() { |
| 160 | + final int size = 6 * 1024; |
| 161 | + for (int i = 0; i < 3; i++) { |
| 162 | + new Thread(new Runnable() { |
| 163 | + @Override |
| 164 | + public void run() { |
| 165 | + try { |
| 166 | + Configuration config = new Configuration.Builder().build(); |
| 167 | + final UploadManager uploadManager = new UploadManager(config, 3); |
| 168 | + final String expectKey = "r=" + size + "k"; |
| 169 | + final File f; |
| 170 | + f = TempFile.createFile(size); |
| 171 | + final UploadOptions uploadoption = new UploadOptions(null, null, false, new UpProgressHandler() { |
| 172 | + public void progress(String key, double percent) { |
| 173 | + Log.e("qiniutest", percent + ""); |
| 174 | + } |
| 175 | + }, null); |
| 176 | + |
| 177 | + uploadManager.put(f, expectKey, TestConfig.token_z0, new UpCompletionHandler() { |
| 178 | + public void complete(String k, ResponseInfo rinfo, JSONObject response) { |
| 179 | + Log.e("qiniutest", k + rinfo); |
| 180 | + time += 1; |
| 181 | + if (time == 3) { |
| 182 | + lock.notify(); |
| 183 | + } |
| 184 | + } |
| 185 | + }, uploadoption); |
| 186 | + |
| 187 | + |
| 188 | + } catch (IOException e) { |
| 189 | + e.printStackTrace(); |
| 190 | + } |
| 191 | + } |
| 192 | + }).start(); |
| 193 | + } |
| 194 | + synchronized (lock) { |
| 195 | + try { |
| 196 | + lock.wait(); |
| 197 | + } catch (InterruptedException e) { |
| 198 | + e.printStackTrace(); |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + public void testSerializable() { |
| 204 | + DnsCacheKey key = new DnsCacheKey("12321", "127.0.0.1", "akscope"); |
| 205 | + Log.e("qiniutest", key.toString()); |
| 206 | + DnsCacheKey key1 = DnsCacheKey.toCacheKey(key.toString()); |
| 207 | + if (key1 == null) { |
| 208 | + return; |
| 209 | + } |
| 210 | + Log.e("qiniutest", key1.getCurrentTime() + ":" + key1.getLocalIp() + ":" + key1.getAkScope()); |
| 211 | + |
| 212 | + } |
| 213 | + |
| 214 | + |
| 215 | +} |
0 commit comments