package testcase.msm.Performance;
import com.jayway.jsonpath.JsonPath;
import io.restassured.http.ContentType;
import org.testng.Assert;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
public class 领取任务 {
static int times = 0;
static int times_Max = 50000;
//invocationCount = 50, threadPoolSize = 2
@Test()
public void 领取任务() throws InterruptedException {
// for 循环 i为线程数
for (int i = 0; i <= 20; i++) {
//实例化线程运行run的内容
new Thread(new Runnable() {
@Override
public void run() {
//从当前时间开始一直持续运行true的内容
while (times < times_Max) {
//调用接口
String url = "http://msm.dev.eainc.com:8082/modelServiceManager/rest/modelAutoTreatTasks";
String json = "{n" +
"t"action": "claim",n" +
"t"variables": [n" +
"tt{n" +
"ttt"key": "modelServiceIp",n" +
"ttt"value": "172.16.6.99"n" +
"tt},n" +
"tt{n" +
"ttt"key": "processorCount",n" +
"ttt"value": "4"n" +
"tt},n" +
"tt{n" +
"ttt"key": "memorySize",n" +
"ttt"value": "8191"n" +
"tt},n" +
"tt{n" +
"ttt"key": "tag",n" +
"ttt"value": "amfFirst"n" +
"tt}n" +
"t]n" +
"}";
String response =
given()
.when()
.contentType(ContentType.JSON)
.body(json)
.put(url)
.then()
.statusCode(200)
.extract().asString();
System.out.println(response);
//领取了第几个任务
times = times + 1;
System.out.println("第" + times + "任务" + "," + "返回参数:" + response);
//打印线程id
long tid = Thread.currentThread().getId();
System.out.println("tid:" + tid);
if (response == null || response.length() == 0 || response.equals("[]")) {
Assert.assertFalse(true, "没有领取到任务");
} else {
String paras = JsonPath.read(response, "$.[0].para").toString();
//String paras = "[]";
System.out.println(paras);
if (paras == null || paras.equals("") || paras.equals("[]")) {
System.out.println("para为空..................");
String taskId = JsonPath.read(response, "$.[0].taskId").toString();
System.out.println("para为空的taskId为: " + taskId);
Assert.assertFalse(true, "para为空");
} else {
//System.out.println("para不为空");
}
}
}
}
}).start();
// 进入多少线程
System.out.println("初始化:" + i + "循环");
}
//设置睡眠时间的原因:test标签 有诸线程和子线程,主线程跑完就关闭了,下面的子线程也就无法运行了,所在在主线程跑完等待一段时间不关闭,让子线程继续运行
Thread.sleep(1000 * 60 * 30);
}
}