如何链接 doIfElse Scala Gatling
How to chain doIfElse Scala Gatling
我们正在尝试将 Gatling 测试从 Scala 迁移到 Java。我们有一个链接一些 exec 的函数,并且在它们之间有一个 doIfElse。尝试在 Java 上添加相同的内容会影响方法的 return 类型并且对 Scala 不是很熟悉,我们不确定如何将整个登录功能迁移到 Java class.
Scala 版本
def loginFlow() = group("Login Flow") {
{
login
}
}
def login(): ChainBuilder = exec { session =>
// Need to construct dynamic urls here and store in session as they won't be evaluated while the scenario is running
session.set("authorize_url", s"${getBaseUrl()}/authorize")
.set("redirect_uri", s"${getRedirectURI(Config.TARGET_ENV)}")
.set("client_id", Config.getProperty("client_id", "152ced50-1369-4b19-8b26-8f3d5d9bfd6a.hmhco.com"))
}.doIfOrElse(session => session.contains("token")) {
// If a token is provided for the user, don't login
exec { session =>
logger.debug("Using a provided token")
session
}
} {
exec { session =>
val username = session("username").as[String]
val password = session("password").as[String]
logger.debug(s"Logging In with user: ${username}/${password}")
session.set("nonce", "99999")
}
}
Java
public ChainBuilder loginFlow() {
return group("Login Flow").on(exec(login()));
}
public ChainBuilder login() {
return exec(session -> {
Session newSession = session.set("authorize_url", getBaseUrl());
session.set("redirect_uri", getRedirectURI());
session.set("client_id", Config.getProperty("client_id", "152ced50-1369-4b19-8b26-8f3d5d9bfd6a.hmhco.com")) // Missing semicolon. When it's added the return type for the method changes.
return newSession;
})
.doIfOrElse(session -> session.contains("token")).then(
exec(session -> session)
);
}
谢谢。
原来是java代码中缺少else块。由于 Java 不支持 curried 函数,gatling 提供了一些方法以将操作链接在一起。所以基于 Gatling docs,这会成功:
.doIfOrElse(session -> session.contains("token")).then(
exec(session -> session)
).orElse(session -> {...});
我们正在尝试将 Gatling 测试从 Scala 迁移到 Java。我们有一个链接一些 exec 的函数,并且在它们之间有一个 doIfElse。尝试在 Java 上添加相同的内容会影响方法的 return 类型并且对 Scala 不是很熟悉,我们不确定如何将整个登录功能迁移到 Java class.
Scala 版本
def loginFlow() = group("Login Flow") {
{
login
}
}
def login(): ChainBuilder = exec { session =>
// Need to construct dynamic urls here and store in session as they won't be evaluated while the scenario is running
session.set("authorize_url", s"${getBaseUrl()}/authorize")
.set("redirect_uri", s"${getRedirectURI(Config.TARGET_ENV)}")
.set("client_id", Config.getProperty("client_id", "152ced50-1369-4b19-8b26-8f3d5d9bfd6a.hmhco.com"))
}.doIfOrElse(session => session.contains("token")) {
// If a token is provided for the user, don't login
exec { session =>
logger.debug("Using a provided token")
session
}
} {
exec { session =>
val username = session("username").as[String]
val password = session("password").as[String]
logger.debug(s"Logging In with user: ${username}/${password}")
session.set("nonce", "99999")
}
}
Java
public ChainBuilder loginFlow() {
return group("Login Flow").on(exec(login()));
}
public ChainBuilder login() {
return exec(session -> {
Session newSession = session.set("authorize_url", getBaseUrl());
session.set("redirect_uri", getRedirectURI());
session.set("client_id", Config.getProperty("client_id", "152ced50-1369-4b19-8b26-8f3d5d9bfd6a.hmhco.com")) // Missing semicolon. When it's added the return type for the method changes.
return newSession;
})
.doIfOrElse(session -> session.contains("token")).then(
exec(session -> session)
);
}
谢谢。
原来是java代码中缺少else块。由于 Java 不支持 curried 函数,gatling 提供了一些方法以将操作链接在一起。所以基于 Gatling docs,这会成功:
.doIfOrElse(session -> session.contains("token")).then(
exec(session -> session)
).orElse(session -> {...});