use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.
the class RequestTimeoutTest method testOperationsNormalAfterPacketLoss.
/**
* Test operations after package loss.
* To simulate package eloss, we use iptables in linux and cut the network in windows.
*/
@Test
public void testOperationsNormalAfterPacketLoss() throws Exception {
String key = "test-operation-after-packet-loss";
try {
File file = createSampleFile(key, 1024 * 1024 * 200);
// System.out.println("start disconnect");
ossClient.getClientConfiguration().setRequestTimeout(60 * 60 * 1000);
try {
ossClient.putObject(bucketName, key, file);
Assert.fail("Get object should not be successful");
} catch (ClientException e) {
Assert.assertEquals(OSSErrorCode.REQUEST_TIMEOUT, e.getErrorCode());
} finally {
ossClient.getClientConfiguration().setRequestTimeout(requestTimeout);
}
ObjectListing objectListing = ossClient.listObjects(bucketName, key);
Assert.assertEquals(objectListing.getObjectSummaries().size(), 1);
ossClient.deleteObject(bucketName, key);
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.
the class RequestTimeoutTest method testOperationsNormalAfterTimeout.
/**
* The connection should be reused after time out.
*/
@Test
public void testOperationsNormalAfterTimeout() throws Exception {
String key = "test-operation-after-timeout";
try {
try {
ossClient.getClientConfiguration().setRequestTimeout(1);
ossClient.putObject(bucketName, key, TestUtils.genFixedLengthInputStream(64));
Assert.fail("Get object should not be successful");
} catch (ClientException e) {
Assert.assertEquals(OSSErrorCode.REQUEST_TIMEOUT, e.getErrorCode());
} finally {
ossClient.getClientConfiguration().setRequestTimeout(requestTimeout);
}
ossClient.putObject(bucketName, key, TestUtils.genFixedLengthInputStream(64));
OSSObject ossObject = ossClient.getObject(bucketName, key);
ossObject.getObjectContent().close();
ossClient.deleteObject(bucketName, key);
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.
the class SwitchCredentialsAndEndpointTest method testSwitchInvalidCredentialsAndEndpoint.
@Test
public void testSwitchInvalidCredentialsAndEndpoint() {
CredentialsProvider credsProvider = ossClient.getCredentialsProvider();
Credentials defaultCreds = credsProvider.getCredentials();
assertEquals(OSS_TEST_ACCESS_KEY_ID_1, defaultCreds.getAccessKeyId());
assertEquals(OSS_TEST_ACCESS_KEY_ID_1, defaultCreds.getSecretAccessKey());
// Switch to invalid credentials
Credentials invalidCreds = new DefaultCredentials(INVALID_ACCESS_ID, INVALID_ACCESS_KEY);
ossClient.switchCredentials(invalidCreds);
// Verify invalid credentials under default endpoint
try {
ossClient.getBucketLocation(bucketName);
fail("Should not be able to get bucket location with invalid credentials.");
} catch (OSSException ex) {
assertEquals(OSSErrorCode.INVALID_ACCESS_KEY_ID, ex.getErrorCode());
}
// Switch to valid endpoint
ossClient.setEndpoint(INVALID_ENDPOINT);
// Verify second credentials under invalid endpoint
try {
ossClient.getBucketLocation(bucketName);
fail("Should not be able to get bucket location with second credentials.");
} catch (ClientException ex) {
assertEquals(ClientErrorCode.UNKNOWN_HOST, ex.getErrorCode());
} finally {
restoreDefaultCredentials();
restoreDefaultEndpoint();
}
}
use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.
the class PerftestRunner method testRun.
public void testRun() {
int putThreadNumber = scenario.getPutThreadNumber();
int getThreadNumber = scenario.getGetThreadNumber();
int getQPS = scenario.getGetQPS();
int putQPS = scenario.getPutQPS();
final int getInterval = getThreadNumber == 0 ? 0 : getThreadNumber * (1000 / getQPS);
final int putInterval = putThreadNumber * (1000 / putQPS);
final long contentLength = scenario.getContentLength();
final String bucketName = scenario.getBucketName();
final byte[] byteArray = chooseByteArray(contentLength);
final String keyPrefix = "xiao-perf-test-";
final List<String> uploadedObjects = new ArrayList<String>();
final List<Long> putLatencyArray = new ArrayList<Long>();
final List<Long> getLatencyArray = new ArrayList<Long>();
final PerftestRunner self = this;
Thread[] putThreads = new Thread[putThreadNumber];
try {
for (int i = 0; i < putThreadNumber; i++) {
Runnable r = new Runnable() {
@Override
public void run() {
while (!self.hasExpired()) {
InputStream input = null;
try {
input = new ByteArrayInputStream(byteArray);
String objectKey = buildObjectKey(keyPrefix);
log.info("Begin put " + objectKey);
Date from = new Date();
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(contentLength);
ossClient.putObject(bucketName, objectKey, input, metadata);
Date to = new Date();
long latency = to.getTime() - from.getTime();
if (latency < putInterval) {
Thread.sleep(putInterval - latency);
} else {
log.warn("Put object " + objectKey + " latency " + latency + ", exceed max interval " + putInterval);
}
log.info("Put object " + objectKey + " finished, elapsed " + latency + "ms");
try {
lock.lock();
uploadedObjects.add(objectKey);
putLatencyArray.add(latency);
} finally {
lock.unlock();
}
} catch (OSSException oe) {
log.warn("Unexpected oss exception occurs when putting object " + oe.getMessage());
recordError(OperationType.PUT);
} catch (ClientException ce) {
log.warn("Unexpected client exception occurs when putting object " + ce.getMessage());
recordError(OperationType.PUT);
} catch (Exception e) {
log.warn("Other unexpected exception " + e.getMessage());
recordError(OperationType.PUT);
} finally {
if (input != null) {
try {
input.close();
input = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
};
putThreads[i] = new Thread(r);
}
} catch (Exception ex) {
log.error(ex.getMessage());
Assert.fail(ex.getMessage());
}
Thread[] getThreads = new Thread[getThreadNumber];
try {
for (int i = 0; i < getThreadNumber; i++) {
Runnable r = new Runnable() {
@Override
public void run() {
OSSObject o = null;
while (!hasExpired()) {
try {
String objectKey = null;
if (uploadedObjects.size() > 0) {
try {
lock.lock();
if (uploadedObjects.size() > 0) {
int index = new Random().nextInt(uploadedObjects.size());
objectKey = uploadedObjects.get(index);
}
} finally {
lock.unlock();
}
} else {
Thread.sleep(50);
continue;
}
GetObjectRequest request = new GetObjectRequest(bucketName, objectKey);
log.info("Begin get " + objectKey);
Date from = new Date();
o = ossClient.getObject(request);
// read data
byte[] content = new byte[8196];
InputStream in = o.getObjectContent();
while (in.read(content) >= 0) ;
Date to = new Date();
long latency = to.getTime() - from.getTime();
if (latency < getInterval) {
Thread.sleep(getInterval - latency);
} else {
log.warn("Get object " + objectKey + " latency " + latency + ", exceed max interval " + getInterval);
}
log.info("Get object " + objectKey + " finished, elapsed " + latency + "ms");
try {
lock.lock();
getLatencyArray.add(latency);
} finally {
lock.unlock();
}
} catch (OSSException oe) {
log.warn("Unexpected oss exception occurs when getting object " + oe.getMessage());
recordError(OperationType.GET);
} catch (ClientException ce) {
log.warn("Unexpected oss exception occurs when getting object " + ce.getMessage());
recordError(OperationType.GET);
} catch (Exception e) {
log.warn("Other unexpected exception " + e.getMessage());
recordError(OperationType.GET);
} finally {
if (o != null) {
try {
o.getObjectContent().close();
o = null;
} catch (IOException e) {
log.error("IO Exception " + e.getMessage());
}
}
}
}
}
};
getThreads[i] = new Thread(r);
}
} catch (Exception ex) {
log.error(ex.getMessage());
Assert.fail(ex.getMessage());
}
startTime = new Date();
Thread[] allThreads = joinThreads(putThreads, getThreads);
waitAll(allThreads);
endTime = new Date();
calculateResult(putLatencyArray, OperationType.PUT, putThreadNumber);
calculateResult(getLatencyArray, OperationType.GET, getThreadNumber);
}
use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.
the class ExceptionFactoryTest method testCreateNetworkException.
@Test
public void testCreateNetworkException() {
SocketTimeoutException ste = new SocketTimeoutException();
ClientException ex = ExceptionFactory.createNetworkException(ste);
assertEquals(ex.getErrorCode(), ClientErrorCode.SOCKET_TIMEOUT);
ConnectTimeoutException cte = new ConnectTimeoutException();
ex = ExceptionFactory.createNetworkException(cte);
assertEquals(ex.getErrorCode(), ClientErrorCode.CONNECTION_TIMEOUT);
IOException ioe = new IOException();
ex = ExceptionFactory.createNetworkException(ioe);
assertEquals(ex.getErrorCode(), ClientErrorCode.UNKNOWN);
}
Aggregations