4545import com .tencent .cos .xml .utils .DigestUtils ;
4646import com .tencent .qcloud .core .http .RequestBodySerializer ;
4747import com .tencent .qcloud .core .logger .QCloudLogger ;
48+ import com .tencent .qcloud .core .util .Base64Utils ;
4849import com .tencent .qcloud .core .util .DomainSwitchUtils ;
50+ import com .tencent .qcloud .core .util .OkhttpInternalUtils ;
4951
5052import org .junit .Assert ;
5153import org .junit .Test ;
6163import java .net .HttpURLConnection ;
6264import java .net .URL ;
6365import java .net .URLEncoder ;
66+ import java .security .MessageDigest ;
67+ import java .security .NoSuchAlgorithmException ;
68+ import java .util .ArrayList ;
6469
6570import okhttp3 .MediaType ;
6671import okhttp3 .OkHttpClient ;
@@ -173,24 +178,68 @@ public void onFail(CosXmlClientException exception, CosXmlServiceException servi
173178 QCloudLogger .i ("QCloudTest" , url );
174179 Assert .assertEquals ("https://bucket.cos.ap-shanghai.myqcloud.com/objectexample" , url );
175180 }
176-
181+
182+ public String onGetMd5 (String filePath ) throws IOException {
183+ InputStream inputStream = null ;
184+ try {
185+ MessageDigest messageDigest = MessageDigest .getInstance ("MD5" );
186+ File file = new File (filePath );
187+ inputStream = new FileInputStream (file );
188+ byte [] buff = new byte [8 * 1024 ];
189+ int readLen ;
190+ long remainLength = file .length ();
191+ while (remainLength > 0L && (readLen = inputStream .read (buff , 0 ,
192+ (buff .length > remainLength ? (int ) remainLength : buff .length )))!= -1 ){
193+ messageDigest .update (buff , 0 , readLen );
194+ remainLength -= readLen ;
195+ }
196+ return Base64Utils .encode (messageDigest .digest ());
197+ } catch (IOException e ) {
198+ throw e ;
199+ } catch (NoSuchAlgorithmException e ) {
200+ throw new IOException ("unSupport Md5 algorithm" , e );
201+ } finally {
202+ if (inputStream != null ) OkhttpInternalUtils .closeQuietly (inputStream );
203+ }
204+ }
205+
177206 @ Test public void testPresignedRequest () {
207+ String filePath = TestUtils .smallFilePath ();
178208 PresignedUrlRequest presignedUrlRequest = new PresignedUrlRequest (TestConst .PERSIST_BUCKET , TestConst .PERSIST_BUCKET_SMALL_OBJECT_PATH ) {
179209 @ Override
180210 public RequestBodySerializer getRequestBody () throws CosXmlClientException {
181- return RequestBodySerializer .file ("image/png" , new File (TestUtils . smallFilePath () ));
211+ return RequestBodySerializer .file ("image/png" , new File (filePath ));
182212 }
183213 };
184214 presignedUrlRequest .setRequestMethod ("PUT" );
185215 presignedUrlRequest .setSignKeyTime (3600 );
186216 presignedUrlRequest .addNoSignHeader ("Host" );
217+ String md5 ;
218+ // try {
219+ // md5 = DigestUtils.getMD5(filePath);
220+ // } catch (CosXmlClientException e) {
221+ // throw new RuntimeException(e);
222+ // }
223+
224+ try {
225+ md5 = onGetMd5 (filePath );
226+ } catch (IOException e ) {
227+ throw new RuntimeException (e );
228+ }
229+
230+ // try {
231+ // presignedUrlRequest.setRequestHeaders(HttpConstants.Header.CONTENT_MD5, md5);
232+ // } catch (CosXmlClientException e) {
233+ // throw new RuntimeException(e);
234+ // }
187235 CosXmlSimpleService defaultService = ServiceFactory .INSTANCE .newDefaultService ();
188236 try {
189237 String signUrl = defaultService .getPresignedURL (presignedUrlRequest );
190238 QCloudLogger .i ("QCloudTest" , signUrl );
191239 MediaType imageType = MediaType .parse ("image/png" );
192240 Request request = new Request .Builder ()
193241 .url (signUrl )
242+ // .header(HttpConstants.Header.CONTENT_MD5, md5)
194243 .put (RequestBody .create (imageType , new File (TestUtils .smallFilePath ())))
195244 .build ();
196245 Response response = new OkHttpClient ().newCall (request ).execute ();
@@ -424,4 +473,35 @@ public void testIsMyqcloudUrl(){
424473 assertFalse (DomainSwitchUtils .isMyqcloudUrl (testUrls [5 ]));
425474 assertFalse (DomainSwitchUtils .isMyqcloudUrl (testUrls [6 ]));
426475 }
476+
477+ @ Test
478+ public void testMultiThreadedCosXmlService () {
479+ try {
480+ // 线程数常量
481+ int THREAD_COUNT = 10000 ;
482+ Thread [] threads = new Thread [THREAD_COUNT ];
483+ ArrayList <Throwable > exceptions = new ArrayList <>();
484+ for (int i = 0 ; i < THREAD_COUNT ; i ++) {
485+ threads [i ] = new Thread (() -> {
486+ CosXmlSimpleService service = ServiceFactory .INSTANCE .newDefaultService ();
487+ service .getConfig ();
488+ });
489+ threads [i ].setUncaughtExceptionHandler (new Thread .UncaughtExceptionHandler () {
490+ @ Override
491+ public void uncaughtException (Thread t , Throwable e ) {
492+ e .printStackTrace ();
493+ exceptions .add (e );
494+ }
495+ });
496+ threads [i ].start ();
497+ }
498+ for (int i = 0 ; i < THREAD_COUNT ; i ++) {
499+ threads [i ].join ();
500+ }
501+ assertTrue (exceptions .isEmpty ());
502+ } catch (Exception e ) {
503+ e .printStackTrace ();
504+ Assert .fail (e .getMessage ());
505+ }
506+ }
427507}
0 commit comments