55import java .io .IOException ;
66import java .util .HashMap ;
77import java .util .UUID ;
8+ import java .util .concurrent .Semaphore ;
9+
810
911import junit .framework .Assert ;
1012
1517import com .qiniu .io .IO ;
1618import com .qiniu .resumableio .ResumableIO ;
1719import com .qiniu .utils .QiniuException ;
20+ import com .qiniu .conf .Conf ;
1821
1922import android .content .Context ;
2023import android .net .Uri ;
@@ -40,6 +43,7 @@ public class UploadTest extends AndroidTestCase {
4043 private String key = IO .UNDEFINED_KEY ;
4144
4245 private com .qiniu .resumableio .PutExtra rextra ;
46+ private Semaphore sem = new Semaphore (0 , true );
4347
4448 public void setUp () throws Exception {
4549 key = UUID .randomUUID ().toString ();
@@ -58,8 +62,7 @@ public void setUp() throws Exception {
5862 jsonRet = new JSONObjectRet () {
5963 @ Override
6064 public void onProcess (long current , long total ) {
61- //Log.d("UploadTest", current + "/" + total);
62- // Assert.assertEquals(file.length(), total); // 内部实现原因,可能不相等
65+ Log .d ("UploadTest" , current + "/" + total );
6366 }
6467
6568 @ Override
@@ -68,7 +71,7 @@ public void onSuccess(JSONObject res) {
6871 success = true ;
6972 resp = res ;
7073 Log .d ("UploadTest" , "上传成功! " + resp .toString ());
71- System . out . println ( "上传成功! " + resp . toString () );
74+ sem . release ( );
7275 }
7376
7477 @ Override
@@ -77,7 +80,7 @@ public void onFailure(QiniuException ex) {
7780 success = false ;
7881 e = ex ;
7982 Log .d ("UploadTest" , "上传失败! " + ex .getMessage ());
80- System . out . println ( "上传失败! " + ex . getMessage () );
83+ sem . release ( );
8184 }
8285 };
8386 }
@@ -90,47 +93,59 @@ public void tearDown() throws Exception {
9093 }
9194
9295 @ SmallTest
93- public void testS () throws IOException , JSONException {
96+ public void testS () throws IOException , JSONException , InterruptedException {
9497 file = createFile (0.2 , ".test" );
9598 uri = Uri .fromFile (file );
9699 IO .putFile (context , uptoken , key , uri , extra , jsonRet );
97- sleepLimit (60 );
100+ sem .acquire ();
101+ successCheck ();
102+ }
103+
104+ @ SmallTest
105+ public void testIOMultiHost () throws IOException , JSONException , InterruptedException {
106+ String old = Conf .UP_HOST ;
107+ Conf .UP_HOST = "http://127.0.0.1:1" ;
108+ file = createFile (0.1 , "-mup.test" );
109+ uri = Uri .fromFile (file );
110+ IO .putFile (context , uptoken , key , uri , extra , jsonRet );
111+ Conf .UP_HOST = old ;
112+ sem .acquire ();
98113 successCheck ();
99114 }
100115
101116 @ MediumTest
102- public void testM () throws IOException , JSONException {
103- file = createFile (4 , "--—— 中 文 .test" );
117+ public void testM () throws IOException , JSONException , InterruptedException {
118+ file = createFile (0.1 , "--—— 中 文 .test" );
104119 uri = Uri .fromFile (file );
105120 IO .putFile (context , uptoken , key , uri , extra , jsonRet );
106- sleepLimit ( 60 * 5 );
121+ sem . acquire ( );
107122 successCheck ();
108123 }
109124
110125 @ SmallTest
111- public void testRS () throws IOException , JSONException {
126+ public void testRS () throws IOException , JSONException , InterruptedException {
112127 file = createFile (0.2 , ".test" );
113128 uri = Uri .fromFile (file );
114129 ResumableIO .putFile (context , uptoken , key , uri , rextra , jsonRet );
115- sleepLimit ( 60 );
130+ sem . acquire ( );
116131 successCheck ();
117132 }
118133
119134 @ MediumTest
120- public void testRM () throws IOException , JSONException {
135+ public void testRM () throws IOException , JSONException , InterruptedException {
121136 file = createFile (4 , ".test" );
122137 uri = Uri .fromFile (file );
123138 ResumableIO .putFile (context , uptoken , key , uri , rextra , jsonRet );
124- sleepLimit ( 60 * 5 );
139+ sem . acquire ( );
125140 successCheck ();
126141 }
127142
128143 // @MediumTest
129- // public void testRL() throws IOException, JSONException {
144+ // public void testRL() throws IOException, JSONException, InterruptedException {
130145 // file = createFile(8.6, ".test");
131146 // uri = Uri.fromFile(file);
132147 // ResumableIO.putFile(context, uptoken, key, uri, rextra, jsonRet);
133- // sleepLimit(60 * 5 );
148+ // sem.acquire( );
134149 // successCheck();
135150 // }
136151
@@ -141,18 +156,6 @@ private void successCheck() throws JSONException{
141156 Assert .assertEquals (file .length (), resp .getLong ("fsize" ));
142157 }
143158
144- private void sleepLimit (int limit ){
145- int t = 5 ;
146- while (uploading && t < limit ){
147- try {
148- t += 5 ;
149- Thread .sleep (1000 * 5 );
150- } catch (InterruptedException e ) {
151-
152- }
153- }
154- }
155-
156159 private File createFile (double fileSize , String suf ) throws IOException {
157160 FileOutputStream fos = null ;
158161 try {
0 commit comments