我在加特林机上有一个scala文件,我想设置全局变量值并在之后使用它,代码如下:
class TestSimulation extends Simulation {
init var:
val users = java.lang.Double.parseDouble(System.getProperty("users"))
val duration = java.lang.Integer.getInteger("duration")
val host = "localhost"
val port = 9100
val protocol: GrpcProtocol = grpc.host(host).port(port)
设置供料器var:
val feeder: Feeder[IssueTestRequest] = Iterator.continually(
Map(
("request",
IssueTestRequest(
biz = "demo",
pro = "demo_test"
)
)
)
)
设置运行变量:
val scn = scenario("TestScn")
.feed(feeder)
// first function run
.exec(
grpc("request")
.asyncUnaryCall(TestServiceGrpc.METHOD_ISSUE_TEST,
"${request}")
.check ({ response =>
// set global value
properties.token = response.token // HERE: save and set value
println(properties.token) // HERE: can print my hope result
response.result.get.code.equals(Result.Code.OK)
})
)
// second function run
.exec(
grpc("request")
.asyncUnaryCall(TokenServiceGrpc.METHOD_VERIFY_TEST,
VerifyTestRequest(
token = properties.token // HERE: use var which had set value, BUT token is empty, why??? why???
))
.check({ response =>
println(response.task.get.taskId) // I not hope result
response.result.get.code.equals(Result.Code.OK)
})
)
运行它:
setUp(
scn.inject(constantUsersPerSec(users) during (duration seconds)).protocols(protocol)
)
}
使用一个对象保存全局变量。
// global var
object properties {
var token: String = ""
}
那么,应该如何设置全局变量?非常感谢!!!
答案 0 :(得分:0)
因为那不是加特林的工作方式。
所有DSL元素都是在仿真启动时创建的,此时令牌确实为空。
您需要将该值保留在会话变量中,以便在运行期间确定该值