gpt4 book ai didi

当同时运行多个测试时,Redis 连接关闭

转载 作者:IT王子 更新时间:2023-10-29 06:02:31 26 4
gpt4 key购买 nike

我在一个类中的测试用例很少,只有一个会成功,但所有其他的都因 redis 池异常而失败。如何确保其他测试也可以从池中获取连接?

[error]    JedisConnectionException: : Could not get a resource from the pool  (Pool.java:22)
[error] redis.clients.util.Pool.getResource(Pool.java:22)
[error] org.sedis.Pool.withJedisClient(sedis.scala:79)
[error] com.typesafe.plugin.RedisPlugin$$anon$1.set_(RedisPlugin.scala:147)
[error] com.typesafe.plugin.RedisPlugin$$anon$1.set(RedisPlugin.scala:106)
[error] play.api.cache.Cache$.set(Cache.scala:58)
[error] controllers.Security$ResultWithToken.withTokenShort(Security.scala:51)
[error] controllers.Security$ResultWithToken.withTempToken(Security.scala:62)
[error] controllers.Security$$anonfun$HasToken$1$$anonfun$apply$3.apply(Security.scala:83)
[error] controllers.Security$$anonfun$HasToken$1$$anonfun$apply$3.apply(Security.scala:83)
[error] controllers.Security$$anonfun$HasToken$1.apply(Security.scala:82)
[error] controllers.Security$$anonfun$HasToken$1.apply(Security.scala:76)
[error] play.api.mvc.ActionBuilder$$anonfun$apply$10.apply(Action.scala:221)
[error] play.api.mvc.ActionBuilder$$anonfun$apply$10.apply(Action.scala:220)
Caused by: java.lang.IllegalStateException: Pool not open
at org.apache.commons.pool.BaseObjectPool.assertOpen(BaseObjectPool.java:137)
at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1065)
at redis.clients.util.Pool.getResource(Pool.java:20)

这是我的 Controller 类,它将一个 uuid token 设置为播放缓存,播放缓存通过 play-redis-plugin 设置为 redis。播放缓存调用RedisPlugin,然后从jedis客户端获取一个pool。该 Controller 类将用于所有想要返回身份验证 token 的 API。

trait Security { self: Controller =>

implicit val app: play.api.Application = play.api.Play.current

lazy val CacheExpirationShort =
this.app.configuration.getInt("cache.expiration").getOrElse(8 * 60 * 60) // 8 hrs

val AuthTokenCookieKey = "XSRF-TOKEN"

/* additional methods to handle token passing */
implicit class ResultWithToken(result: SimpleResult)(implicit req: RequestHeader) {
def withTokenShort(token: (String, String)): SimpleResult = {
Cache.set(token._1, token._2, CacheExpiration)
MemberToken.add(token._2, token._1)
val isSecure = req.headers.get("X-Scheme").collect {
case "https" => true } getOrElse { false }
result.withCookies(Cookie(AuthTokenCookieKey, token._1, Some(CacheExpirationShort), path="/", domain=Some(App.cookieDomainFromHost(req.host)), secure=isSecure , httpOnly=false))
}
}
object Security extends Controller with Security

编辑1:

根据这个 SO Play ! 2.2.4 / Akka : tests failed when run together, but ok separately ,我觉得在RedisPlugin,jedisPool应该改成def。

 lazy val jedisPool = {
val poolConfig = createPoolConfig(app)
Logger.info(s"Redis Plugin enabled. Connecting to Redis on ${host}:${port} to ${database} with timeout ${timeout}.")
Logger.info("Redis Plugin pool configuration: " + new ReflectionToStringBuilder(poolConfig).toString())
new JedisPool(poolConfig, host, port, timeout, password, database)
}

最佳答案

我遇到了同样的问题,似乎最好的解决方案(只要您不测试 Redis 本身)就是禁用 Redis 插件进行测试并让它使用默认缓存。

val config = Map(
"ehcacheplugin" -> "enabled",
"redisplugin" -> "disabled")

"this test" should {
"not die" in {
running(FakeApplication(additionalConfiguration = config)) {
...
}
}
}

关于当同时运行多个测试时,Redis 连接关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24421063/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com