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
120 changes: 120 additions & 0 deletions library/src/androidTest/java/com/qiniu/android/PortTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.qiniu.android;

import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Log;

import com.qiniu.android.http.CompletionHandler;
import com.qiniu.android.http.HttpManager;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.Configuration;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UploadManager;
import com.qiniu.android.storage.UploadOptions;

import junit.framework.Assert;

import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
* Created by bailong on 14/10/12.
*/
public class PortTest extends InstrumentationTestCase {
final CountDownLatch signal = new CountDownLatch(1);
private UploadManager uploadManager;
private volatile String key;
private volatile ResponseInfo info;
private volatile JSONObject resp;

@Override
protected void setUp() throws Exception {
Configuration config = new Configuration.Builder().upPort(8888).build();
uploadManager = new UploadManager(config);
}

@SmallTest
public void testData() throws Throwable {
final String expectKey = "你好;\"\r\n\r\n\r\n_port";
Map<String, String> params = new HashMap<String, String>();
params.put("x:foo", "fooval");
final UploadOptions opt = new UploadOptions(params, null, true, null, null);

uploadManager.put("hello".getBytes(), expectKey, TestConfig.token, new UpCompletionHandler() {
public void complete(String k, ResponseInfo rinfo, JSONObject response) {
Log.i("qiniutest", k + rinfo);
key = k;
info = rinfo;
resp = response;
signal.countDown();
}
}, opt);

check(expectKey);
}

@MediumTest
public void test123K() throws Throwable {
fileTemplate(123);
}

@MediumTest
public void test323K() throws Throwable {
fileTemplate(323);
}

@MediumTest
public void test4223K() throws Throwable {
fileTemplate(4223);
}

public void fileTemplate(int size) throws Throwable {
final String expectKey = "r=" + size + "k_port";
final File f = TempFile.createFile(size);
Map<String, String> params = new HashMap<String, String>();
params.put("x:foo", "fooval");
final UploadOptions opt = new UploadOptions(params, null, true, null, null);

uploadManager.put(f, expectKey, TestConfig.token, new UpCompletionHandler() {
public void complete(String k, ResponseInfo rinfo, JSONObject response) {
Log.i("qiniutest", k + rinfo);
key = k;
info = rinfo;
resp = response;
signal.countDown();
}
}, opt);

check(expectKey);
}

private void check( final String expectKey){
try {
signal.await(120, TimeUnit.SECONDS); // wait for callback
} catch (InterruptedException e) {
e.printStackTrace();
}
// 尝试获取info信息。
// key == null : 没进入 complete ? 什么导致的?
if (!expectKey.equals(key)) {
//此处通不过, travis 会打印信息
Assert.assertEquals("", info);
}
if (info == null || !info.isOK()) {
//此处通不过, travis 会打印信息
Assert.assertEquals("", info);
}
Assert.assertEquals(expectKey, key);
Assert.assertTrue(info.isOK());
Assert.assertNotNull(info.reqId);
Assert.assertNotNull(resp);
}
}
16 changes: 11 additions & 5 deletions library/src/main/java/com/qiniu/android/http/ResponseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,18 @@ public final class ResponseHandler extends AsyncHttpResponseHandler {
*/
private String ip = null;

/**
* 服务器端口
*/
private int port = -1;

public ResponseHandler(String url, CompletionHandler completionHandler, ProgressHandler progressHandler) {
super(Looper.getMainLooper());
URI uri = null;
try {
uri = new URI(url);
this.host = uri.getHost();
this.port = uri.getPort();
} catch (URISyntaxException e) {
this.host = "N/A";
e.printStackTrace();
Expand All @@ -63,9 +69,9 @@ public ResponseHandler(String url, CompletionHandler completionHandler, Progress
}

private static ResponseInfo buildResponseInfo(int statusCode, Header[] headers, byte[] responseBody,
String host, String ip, double duration, Throwable error) {
String host, String ip, int port, double duration, Throwable error) {

if(error != null && error instanceof CancellationHandler.CancellationException) {
if (error != null && error instanceof CancellationHandler.CancellationException) {
return ResponseInfo.cancelled();
}

Expand Down Expand Up @@ -132,7 +138,7 @@ private static ResponseInfo buildResponseInfo(int statusCode, Header[] headers,
}
}

return new ResponseInfo(statusCode, reqId, xlog, xvia, host, ip, duration, err);
return new ResponseInfo(statusCode, reqId, xlog, xvia, host, ip, port, duration, err);
}

private static JSONObject buildJsonResp(byte[] body) throws Exception {
Expand All @@ -150,15 +156,15 @@ public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
} catch (Exception e) {
exception = e;
}
ResponseInfo info = buildResponseInfo(statusCode, headers, null, host, ip, duration, exception);
ResponseInfo info = buildResponseInfo(statusCode, headers, null, host, ip, port, duration, exception);
Log.i("upload----success", info.toString());
completionHandler.complete(info, obj);
}

@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
double duration = (System.currentTimeMillis() - reqStartTime) / 1000.0;
ResponseInfo info = buildResponseInfo(statusCode, headers, responseBody, host, ip, duration, error);
ResponseInfo info = buildResponseInfo(statusCode, headers, responseBody, host, ip, port, duration, error);
Log.i("upload----failed", info.toString());
completionHandler.complete(info, null);
}
Expand Down
20 changes: 13 additions & 7 deletions library/src/main/java/com/qiniu/android/http/ResponseInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ public final class ResponseInfo {
*/
public final String ip;

public ResponseInfo(int statusCode, String reqId, String xlog, String xvia, String host, String ip, double duration, String error) {
/**
* 服务器端口
*/
public final int port;

public ResponseInfo(int statusCode, String reqId, String xlog, String xvia, String host, String ip, int port, double duration, String error) {
this.statusCode = statusCode;
this.reqId = reqId;
this.xlog = xlog;
Expand All @@ -61,20 +66,21 @@ public ResponseInfo(int statusCode, String reqId, String xlog, String xvia, Stri
this.duration = duration;
this.error = error;
this.ip = ip;
this.port = port;
}

public static ResponseInfo cancelled() {
return new ResponseInfo(Cancelled, "", "", "", "", "", 0, "cancelled by user");
return new ResponseInfo(Cancelled, "", "", "", "", "", -1, 0, "cancelled by user");
}

public static ResponseInfo invalidArgument(String message) {
return new ResponseInfo(InvalidArgument, "", "", "", "", "", 0,
return new ResponseInfo(InvalidArgument, "", "", "", "", "", -1, 0,
message);
}


public static ResponseInfo fileError(Exception e) {
return new ResponseInfo(InvalidFile, "", "", "", "", "",
return new ResponseInfo(InvalidFile, "", "", "", "", "", -1,
0, e.getMessage());
}

Expand Down Expand Up @@ -103,11 +109,11 @@ public boolean needSwitchServer() {

public boolean needRetry() {
return !isCancelled() && (isNetworkBroken() || isServerError() || statusCode == 406
|| (statusCode == 200 && error != null) );
|| (statusCode == 200 && error != null));
}

public String toString() {
return String.format(Locale.ENGLISH, "{ResponseInfo:%s,status:%d, reqId:%s, xlog:%s, xvia:%s, host:%s, ip:%s, duration:%f s, error:%s}",
super.toString(), statusCode, reqId, xlog, xvia, host, ip, duration, error);
return String.format(Locale.ENGLISH, "{ResponseInfo:%s,status:%d, reqId:%s, xlog:%s, xvia:%s, host:%s, ip:%s, port:%d, duration:%f s, error:%s}",
super.toString(), statusCode, reqId, xlog, xvia, host, ip, port, duration, error);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void complete(ResponseInfo info, JSONObject response) {
if (info.isOK()) {
options.progressHandler.progress(key, 1.0);
completionHandler.complete(key, info, response);
} else if(info.needRetry()) {
} else if (info.needRetry()) {
CompletionHandler retried = new CompletionHandler() {
@Override
public void complete(ResponseInfo info, JSONObject response) {
Expand All @@ -121,13 +121,17 @@ public void complete(ResponseInfo info, JSONObject response) {
if (info.needSwitchServer()) {
host = config.upHostBackup;
}
httpManager.multipartPost("http://" + host, args, progress, retried, options.cancellationSignal);
httpManager.multipartPost(genUploadAddress(host, config.upPort), args, progress, retried, options.cancellationSignal);
} else {
completionHandler.complete(key, info, response);
}
}
};

httpManager.multipartPost("http://" + config.upHost, args, progress, completion, options.cancellationSignal);
httpManager.multipartPost(genUploadAddress(config.upHost, config.upPort), args, progress, completion, options.cancellationSignal);
}

private static String genUploadAddress(String host, int port) {
return "http://" + host + ":" + port;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private void makeBlock(String host, int offset, int blockSize, int chunkSize, Pr
private void putChunk(String host, int offset, int chunkSize, String context, ProgressHandler progress,
CompletionHandler _completionHandler, UpCancellationSignal c) {
int chunkOffset = offset % Configuration.BLOCK_SIZE;
String url = format(Locale.ENGLISH, "http://%s/bput/%s/%d", host, context, chunkOffset);
String url = format(Locale.ENGLISH, "http://%s:%d/bput/%s/%d", host, config.upPort, context, chunkOffset);
try {
file.seek(offset);
file.read(chunkBuffer, 0, chunkSize);
Expand Down