Skip to content

Commit

Permalink
improve test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
yndu13 authored and JacksonTian committed Sep 13, 2024
1 parent 1645ac2 commit 1d2aff9
Show file tree
Hide file tree
Showing 4 changed files with 1,497 additions and 848 deletions.
24 changes: 9 additions & 15 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@
<version>0.8.4</version>
<classifier>runtime</classifier>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibabacloud-gateway-pop</artifactId>
<version>0.0.6</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down Expand Up @@ -144,7 +150,7 @@
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>
<executions>
<!-- <execution>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
Expand All @@ -156,8 +162,8 @@
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution> -->
<!-- <execution>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
Expand All @@ -166,18 +172,6 @@
<configuration>
<dataFile>${project.build.directory}/coverage.exec</dataFile>
</configuration>
</execution> -->
<execution>
<goals>
<goal>prepare-agent</goal> <!-- 用于在编译时启动JaCoCo代理 -->
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal> <!-- 生成HTML格式的报告 -->
</goals>
</execution>
</executions>
</plugin>
Expand Down
132 changes: 130 additions & 2 deletions java/src/test/java/com/aliyun/teaopenapi/ClientTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.aliyun.teaopenapi;

import com.aliyun.credentials.provider.ProfileCredentialsProvider;
import com.aliyun.tea.*;
import com.aliyun.tea.interceptor.*;
import com.aliyun.tea.utils.AttributeMap;
Expand All @@ -16,7 +15,6 @@

import java.io.ByteArrayInputStream;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -957,4 +955,134 @@ public void testRequestBodyType() throws Exception {
Assert.assertEquals(200, result.get("statusCode"));

}

@Test
public void testRetryWithError() throws Exception {
Config config = ClientTest.createConfig();
RuntimeOptions runtime = ClientTest.createRuntimeOptions();
runtime.autoretry = true;
runtime.maxAttempts = 2;
runtime.backoffPolicy = "fix";
runtime.backoffPeriod = 1;
runtime.connectTimeout = 10;
runtime.readTimeout = 10;
config.protocol = "HTTP";
config.endpoint = "localhost:" + wireMock.port();
config.signatureAlgorithm = "v2";
Client client = new Client(config);
OpenApiRequest request = ClientTest.createOpenApiRequest();

// formData
Params params = Params.build(TeaConverter.buildMap(
new TeaPair("action", "TestAPI"),
new TeaPair("version", "2022-06-01"),
new TeaPair("protocol", "HTTPS"),
new TeaPair("pathname", "/test"),
new TeaPair("method", "POST"),
new TeaPair("authType", "Anonymous"),
new TeaPair("style", "RPC"),
new TeaPair("reqBodyType", "formData"),
new TeaPair("bodyType", "json")
));

stubFor(post(anyUrl())
.willReturn(aResponse().withFixedDelay(5000)));
try {
client.callApi(params, request, runtime);
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e.getMessage().contains("timeout") || e.getMessage().contains("connect timed out") || e.getMessage().contains("Read timed out"));
}

config.signatureAlgorithm = null;
config.endpoint = "localhost:" + wireMock.port();
client = new Client(config);
params = Params.build(TeaConverter.buildMap(
new TeaPair("action", "TestAPI"),
new TeaPair("version", "2022-06-01"),
new TeaPair("protocol", "HTTPS"),
new TeaPair("pathname", "/test"),
new TeaPair("method", "POST"),
new TeaPair("authType", "Anonymous"),
new TeaPair("style", "ROA"),
new TeaPair("reqBodyType", "formData"),
new TeaPair("bodyType", "json")
));
stubFor(post(anyUrl())
.willReturn(aResponse().withFixedDelay(5000)));
try {
client.callApi(params, request, runtime);
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e.getMessage().contains("timeout") || e.getMessage().contains("connect timed out") || e.getMessage().contains("Read timed out"));
}

config.signatureAlgorithm = null;
config.endpoint = "localhost:" + wireMock.port();
client = new Client(config);
params = Params.build(TeaConverter.buildMap(
new TeaPair("action", "TestAPI"),
new TeaPair("version", "2022-06-01"),
new TeaPair("protocol", "HTTPS"),
new TeaPair("pathname", "/test"),
new TeaPair("method", "POST"),
new TeaPair("authType", "Anonymous"),
new TeaPair("style", "ROA"),
new TeaPair("reqBodyType", "json"),
new TeaPair("bodyType", "json")
));
stubFor(post(anyUrl())
.willReturn(aResponse().withFixedDelay(5000)));
try {
client.callApi(params, request, runtime);
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e.getMessage().contains("timeout") || e.getMessage().contains("connect timed out") || e.getMessage().contains("Read timed out"));
}
params = Params.build(TeaConverter.buildMap(
new TeaPair("action", "TestAPI"),
new TeaPair("version", "2022-06-01"),
new TeaPair("protocol", "HTTPS"),
new TeaPair("pathname", "/test"),
new TeaPair("method", "POST"),
new TeaPair("authType", "Anonymous"),
new TeaPair("style", "ROA"),
new TeaPair("reqBodyType", "formData"),
new TeaPair("bodyType", "json")
));

stubFor(post(anyUrl())
.willReturn(aResponse().withFixedDelay(5000)));
try {
client.callApi(params, request, runtime);
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e.getMessage().contains("timeout") || e.getMessage().contains("connect timed out") || e.getMessage().contains("Read timed out"));
}

config.endpoint = "localhost:" + wireMock.port();
client = new Client(config);
client._productId = "test";
client.setGatewayClient(new com.aliyun.gateway.pop.Client());
params = Params.build(TeaConverter.buildMap(
new TeaPair("action", "TestAPI"),
new TeaPair("version", "2022-06-01"),
new TeaPair("protocol", "HTTPS"),
new TeaPair("pathname", "/test"),
new TeaPair("method", "POST"),
new TeaPair("authType", "Anonymous"),
new TeaPair("style", "ROA"),
new TeaPair("reqBodyType", "formData"),
new TeaPair("bodyType", "json")
));

stubFor(post(anyUrl())
.willReturn(aResponse().withFixedDelay(5000)));
try {
client.execute(params, request, runtime);
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e.getMessage().contains("timeout") || e.getMessage().contains("connect timed out") || e.getMessage().contains("Read timed out"));
}
}
}
3 changes: 2 additions & 1 deletion ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"nyc": "^15.0.0",
"source-map-support": "^0.5.16",
"ts-node": "^8.6.2",
"typescript": "^3.7.5"
"typescript": "^3.7.5",
"@alicloud/gateway-pop": "0.0.6"
},
"dependencies": {
"@alicloud/tea-typescript": "^1.7.1",
Expand Down
Loading

0 comments on commit 1d2aff9

Please sign in to comment.