如何在列表中连接创建的资源,以便稍后在测试中删除它们作为清理的一部分?

How to concatenate the created resources in a list so I can delete them later in test as part of clean up?

我正在执行以下代码以从执行加特林 http 请求并将其附加到列表中获取响应字段,但作为响应,我看到该列表主要显示相同的 itemId。我想知道这是不是因为并发?我正在制作这个列表,所以我可以在每次创建操作调用之后和测试结束时捕获资源,可以迭代这个列表以执行干净的 up/delete 项目,就像使用 "after" 加特林钩子一样.

模拟中class:

var responseIdList = List[String]() 

val scenario1 = scenario(" TEST ")
        .exec(Create())
        .exec(session => {
            item = session("itemId").as[String].trim
            println("%%%%%%%%%%% item ID =====>>>>>>>>>> " + item)
            responseIdList = item :: responseIdList
            println("%%%%%%%%%%%  List =====>>>>>>>>>> " + responseIdList)
            session}
        )
 setUp(
    scenario1.inject(atOnceUsers(5))
)

加特林机动作:

   def Create():HttpRequestBuilder= {

      http("CREATE API")
       .post(Host + "/items")
       .header("Authorization", "Bearer "+ token)
       .header("Content-Type", "application/json")
       .body(StringBody(
         """{ "name" : "Item1"
         |}""".stripMargin)).asJson
       .check(status.is(200))
       .check(jsonPath("$.itemId").saveAs("itemId"))

   }

执行上述代码后的响应如下:

    %%%%%%%%%%% item ID =====>>>>>>>>>> dc7e4024-c51e-46ce-93be-6ba1dad3679e
    %%%%%%%%%%% item ID =====>>>>>>>>>> 0bf84d48-ccea-4f10-93b3-ea464adcc952
    %%%%%%%%%%% item ID =====>>>>>>>>>> 072361b2-5c49-4641-a27c-c346c01dde99
    %%%%%%%%%%% item ID =====>>>>>>>>>> dc7e4024-c51e-46ce-93be-6ba1dad3679e
    %%%%%%%%%%% List =====>>>>>>>>>> List(0bf84d48-ccea-4f10-93b3-ea464adcc952, 072361b2-5c49-4641-a27c-c346c01dde99)
    %%%%%%%%%%% List =====>>>>>>>>>> List(0bf84d48-ccea-4f10-93b3-ea464adcc952, 0bf84d48-ccea-4f10-93b3-ea464adcc952, 072361b2-5c49-4641-a27c-c346c01dde99)

    %%%%%%%%%%% item ID =====>>>>>>>>>> dc7e4024-c51e-46ce-93be-6ba1dad3679e

    %%%%%%%%%%%  List =====>>>>>>>>>> List(0bf84d48-ccea-4f10-93b3-ea464adcc952, 0bf84d48-ccea-4f10-93b3-ea464adcc952, 0bf84d48-ccea-4f10-93b3-ea464adcc952, 0bf84d48-ccea-4f10-93b3-ea464adcc952, 072361b2-5c49-4641-a27c-c346c01dde99)

尽管在场景模拟中创建了 5 个不同的项目 ID,但不知何故列表显示了相同的项目。有没有更好的数据结构或选项供我使用?由于我是 gatling 和 scala 的初学者,请多多指教。

使用 List 和 var 全局引用是错误的策略,因为它不是线程安全的。

你应该使用 vals 和 java.util.concurrent.ConcurrentLinkedQueue:

import java.util.concurrent.ConcurrentLinkedQueue

   def Create():HttpRequestBuilder= {

      http("CREATE API")
       .post(Host + "/items")
       .header("Authorization", "Bearer "+ token)
       .header("Content-Type", "application/json")
       .body(StringBody(
         """{ "name" : "Item1"
         |}""".stripMargin)).asJson
       .check(status.is(200))
       .check(jsonPath("$.itemId").saveAs("itemId"))

   }

// EDIT: global threadsafe structure stored in an immutable reference
val responseIdList = new java.util.concurrent.ConcurrentLinkedQueue[String]() 

val scenario1 = scenario(" TEST ")
        .exec(Create())
        .exec(session => {
            // EDIT: local val here instead of global var
            val item = session("itemId").as[String].trim
            println("%%%%%%%%%%% item ID =====>>>>>>>>>> " + item)
            responseIdList.offer(item)
            // EDIT: print queue content
            println("%%%%%%%%%%%  List =====>>>>>>>>>> " + util.Arrays.toString(responseIdList.toArray))
            session}
        )
 setUp(
    scenario1.inject(atOnceUsers(5))
)