|
6 | 6 | import com.qiniu.http.Response; |
7 | 7 | import com.qiniu.storage.*; |
8 | 8 | import com.qiniu.storage.model.FileInfo; |
| 9 | +import com.qiniu.util.Auth; |
9 | 10 | import com.qiniu.util.Etag; |
10 | 11 | import com.qiniu.util.Md5; |
11 | 12 | import com.qiniu.util.StringMap; |
| 13 | +import org.junit.Assert; |
12 | 14 | import org.junit.Test; |
13 | 15 | import test.com.qiniu.TempFile; |
14 | 16 | import test.com.qiniu.TestConfig; |
@@ -259,6 +261,118 @@ public void test20M1K() throws Throwable { |
259 | 261 | } |
260 | 262 | } |
261 | 263 |
|
| 264 | + // 内部环境测试 |
| 265 | + @Test |
| 266 | + public void notestInnerEnvSwitchRegion() { |
| 267 | + try { |
| 268 | + long s = new Date().getTime(); |
| 269 | + uploadByInnerEnvSwitchRegion(1024 * 500 + 1, httpType, resumableV2Type, concurrentType); |
| 270 | + long e = new Date().getTime(); |
| 271 | + System.out.println("耗时:" + (e - s)); |
| 272 | + } catch (Exception e) { |
| 273 | + e.printStackTrace(); |
| 274 | + Assert.fail("testInnerEnvSwitchRegion:" + e); |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + public void uploadByInnerEnvSwitchRegion(int size, int httpType, int uploadType, int uploadStyle) throws Exception { |
| 279 | + |
| 280 | + boolean isHttps = httpType == httpsType; |
| 281 | + boolean isConcurrent = uploadStyle == concurrentType; |
| 282 | + String bucket = "aaaabbbbb"; |
| 283 | + |
| 284 | + Region region0 = new Region.Builder() |
| 285 | + .srcUpHost("10.200.20.23:5010") |
| 286 | + .accUpHost("10.200.20.23:5010") |
| 287 | + .build(); |
| 288 | + |
| 289 | + Region region1 = new Region.Builder() |
| 290 | + .srcUpHost("10.200.20.24:5010") |
| 291 | + .accUpHost("10.200.20.24:5010") |
| 292 | + .build(); |
| 293 | + |
| 294 | + RegionGroup regionGroup = new RegionGroup(); |
| 295 | + regionGroup.addRegion(region0); |
| 296 | + regionGroup.addRegion(region1); |
| 297 | + |
| 298 | + Configuration config = new Configuration(regionGroup); |
| 299 | + if (uploadType == resumableV2Type) { |
| 300 | + config.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2; |
| 301 | + config.resumableUploadAPIV2BlockSize = 4 * 1024 * 1024; |
| 302 | + } |
| 303 | + |
| 304 | + config.useHttpsDomains = isHttps; |
| 305 | + String key = "switch_region_"; |
| 306 | + key += isHttps ? "_https" : "_http"; |
| 307 | + |
| 308 | + if (uploadType == resumableV1Type) { |
| 309 | + key += "_resumeV1"; |
| 310 | + } else if (uploadType == resumableV2Type) { |
| 311 | + key += "_resumeV2"; |
| 312 | + } |
| 313 | + |
| 314 | + if (uploadStyle == formType) { |
| 315 | + key += "_form"; |
| 316 | + } else if (uploadStyle == serialType) { |
| 317 | + key += "_serial"; |
| 318 | + } else if (uploadStyle == concurrentType) { |
| 319 | + key += "_concurrent"; |
| 320 | + } |
| 321 | + |
| 322 | + key += "_" + new Date().getTime(); |
| 323 | + final String expectKey = "\r\n?&r=" + size + "k" + key; |
| 324 | + final File f = TempFile.createFile(size); |
| 325 | + |
| 326 | + final String fooKey = "foo"; |
| 327 | + final String fooValue = "fooValue"; |
| 328 | + final String metaDataKey = "metaDataKey"; |
| 329 | + final String metaDataValue = "metaDataValue"; |
| 330 | + final String returnBody = "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"fsize\":\"$(fsize)\"" |
| 331 | + + ",\"fname\":\"$(fname)\",\"mimeType\":\"$(mimeType)\",\"foo\":\"$(x:foo)\"}"; |
| 332 | + |
| 333 | + Auth auth = Auth.create(TestConfig.innerAccessKey, TestConfig.innerSecretKey); |
| 334 | + ; |
| 335 | + String token = auth.uploadToken(bucket, expectKey, 3600, |
| 336 | + new StringMap().put("returnBody", returnBody)); |
| 337 | + |
| 338 | + System.out.printf("\r\nkey:%s zone:%s\n", expectKey, regionGroup); |
| 339 | + |
| 340 | + |
| 341 | + StringMap param = new StringMap(); |
| 342 | + param.put("x:" + fooKey, fooValue); |
| 343 | + param.put("x-qn-meta-" + metaDataKey, metaDataValue); |
| 344 | + try { |
| 345 | + BaseUploader up = null; |
| 346 | + if (uploadStyle == formType) { |
| 347 | + up = new FormUploader(new Client(), token, expectKey, f, param, Client.DefaultMime, false, config); |
| 348 | + } else if (uploadStyle == serialType) { |
| 349 | + up = new ResumeUploader(new Client(), token, expectKey, f, param, null, null, config); |
| 350 | + } else { |
| 351 | + config.resumableUploadMaxConcurrentTaskCount = 3; |
| 352 | + up = new ConcurrentResumeUploader(new Client(), token, expectKey, f, param, null, null, config); |
| 353 | + } |
| 354 | + |
| 355 | + Response r = up.upload(); |
| 356 | + assertTrue(r + "", r.isOK()); |
| 357 | + |
| 358 | + StringMap ret = r.jsonToMap(); |
| 359 | + assertEquals(expectKey, ret.get("key")); |
| 360 | + assertEquals(f.getName(), ret.get("fname")); |
| 361 | + assertEquals(String.valueOf(f.length()), ret.get("fsize").toString()); |
| 362 | + assertEquals(fooValue, ret.get(fooKey).toString()); |
| 363 | + |
| 364 | + final String etag = Etag.file(f); |
| 365 | + System.out.println(" etag:" + etag); |
| 366 | + System.out.println("serverEtag:" + ret.get("hash")); |
| 367 | + assertNotNull(ret.get("hash")); |
| 368 | + assertEquals(etag, ret.get("hash")); |
| 369 | + } catch (QiniuException e) { |
| 370 | + assertEquals("", e.response == null ? e + "e.response is null" : e.response.bodyString()); |
| 371 | + fail(); |
| 372 | + } |
| 373 | + TempFile.remove(f); |
| 374 | + } |
| 375 | + |
262 | 376 |
|
263 | 377 | class MyRet { |
264 | 378 | public String hash; |
|
0 commit comments