使用 Testcontainers、Kotlin、Spring Boot 进行测试后 Gradle 关闭速度缓慢

问题描述 投票:0回答:1

我有一个与 Redis 集成并使用 Gradle 构建的 Kotlin Spring Boot 应用程序。最近,我添加了一些 Testcontainer 集成测试,以确保一切都按预期工作。我预计集成测试会很慢,但不知何故,新测试中最慢的部分是在完成后,Gradle 必须关闭。我做了一个测试任务,专门运行Testcontainer测试,在我的本地机器上结果如下:

测试开始需要 31 秒(我猜这是启动 spring boot 上下文,拉取图像,启动容器,等等)
  • 10 秒执行所有测试
  • 测试执行完毕后,Gradle 需要 60 秒关闭并完成该过程
  • 我已经确认,在测试执行完成后,容器将停止并从我的 Docker 中删除,因此 Gradle 不会等待这一点。它无缘无故地闲置 60 秒。日志如下所示:

2024-07-24T17:46:15.995+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Waiting to acquire shared lock on daemon addresses registry. 2024-07-24T17:46:15.995+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Lock acquired on daemon addresses registry. 2024-07-24T17:46:15.996+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Releasing lock on daemon addresses registry. 2024-07-24T17:46:15.996+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Waiting to acquire shared lock on daemon addresses registry. 2024-07-24T17:46:15.996+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Lock acquired on daemon addresses registry. 2024-07-24T17:46:15.996+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Releasing lock on daemon addresses registry. 2024-07-24T17:46:25.992+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Waiting to acquire shared lock on daemon addresses registry. 2024-07-24T17:46:25.993+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Lock acquired on daemon addresses registry. 2024-07-24T17:46:25.993+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Releasing lock on daemon addresses registry. 2024-07-24T17:46:25.993+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Waiting to acquire shared lock on daemon addresses registry. 2024-07-24T17:46:25.994+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Lock acquired on daemon addresses registry. 2024-07-24T17:46:25.994+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Releasing lock on daemon addresses registry. 2024-07-24T17:46:27.250+0200 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Daemon worker Thread 42: acquired lock on worker lease 2024-07-24T17:46:27.251+0200 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Daemon worker Thread 42: released lock on worker lease 2024-07-24T17:46:27.251+0200 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Execution worker Thread 2: acquired lock on worker lease 2024-07-24T17:46:27.251+0200 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Execution worker Thread 2: released lock on worker lease 2024-07-24T17:46:27.251+0200 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Execution worker Thread 3: acquired lock on worker lease 2024-07-24T17:46:27.251+0200 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Execution worker Thread 3: released lock on worker lease 2024-07-24T17:46:27.252+0200 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Execution worker Thread 5: acquired lock on worker lease 2024-07-24T17:46:27.252+0200 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Execution worker Thread 5: released lock on worker lease 2024-07-24T17:46:27.252+0200 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Execution worker Thread 6: acquired lock on worker lease 2024-07-24T17:46:27.252+0200 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Execution worker Thread 6: released lock on worker lease 2024-07-24T17:46:27.252+0200 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Execution worker Thread 7: acquired lock on worker lease 2024-07-24T17:46:27.252+0200 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Execution worker Thread 7: released lock on worker lease 2024-07-24T17:46:27.252+0200 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Execution worker Thread 4: acquired lock on worker lease 2024-07-24T17:46:27.253+0200 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Execution worker Thread 4: released lock on worker lease 2024-07-24T17:46:27.253+0200 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Execution worker Thread 8: acquired lock on worker lease 2024-07-24T17:46:27.253+0200 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Execution worker Thread 8: released lock on worker lease 2024-07-24T17:46:27.253+0200 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Execution worker Thread 9: acquired lock on worker lease 2024-07-24T17:46:27.253+0200 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Execution worker Thread 9: released lock on worker lease 2024-07-24T17:46:27.253+0200 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Execution worker Thread 10: acquired lock on worker lease 2024-07-24T17:46:27.253+0200 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Execution worker Thread 10: released lock on worker lease 2024-07-24T17:46:27.253+0200 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] included builds: acquired lock on worker lease 2024-07-24T17:46:27.253+0200 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] included builds: released lock on worker lease 2024-07-24T17:46:27.253+0200 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Execution worker Thread 11: acquired lock on worker lease 2024-07-24T17:46:27.253+0200 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Execution worker Thread 11: released lock on worker lease 2024-07-24T17:46:35.994+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Waiting to acquire shared lock on daemon addresses registry. 2024-07-24T17:46:35.994+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Lock acquired on daemon addresses registry. 2024-07-24T17:46:35.995+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Releasing lock on daemon addresses registry. 2024-07-24T17:46:35.996+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Waiting to acquire shared lock on daemon addresses registry. 2024-07-24T17:46:35.996+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Lock acquired on daemon addresses registry. 2024-07-24T17:46:35.996+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Releasing lock on daemon addresses registry. 2024-07-24T17:46:45.989+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Waiting to acquire shared lock on daemon addresses registry. 2024-07-24T17:46:45.990+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Lock acquired on daemon addresses registry. 2024-07-24T17:46:45.990+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Releasing lock on daemon addresses registry. 2024-07-24T17:46:45.991+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Waiting to acquire shared lock on daemon addresses registry. 2024-07-24T17:46:45.991+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Lock acquired on daemon addresses registry. 2024-07-24T17:46:45.991+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Releasing lock on daemon addresses registry. 2024-07-24T17:46:55.991+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Waiting to acquire shared lock on daemon addresses registry. 2024-07-24T17:46:55.991+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Lock acquired on daemon addresses registry. 2024-07-24T17:46:55.992+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Releasing lock on daemon addresses registry. 2024-07-24T17:46:55.992+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Waiting to acquire shared lock on daemon addresses registry. 2024-07-24T17:46:55.992+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Lock acquired on daemon addresses registry. 2024-07-24T17:46:55.992+0200 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Releasing lock on daemon addresses registry.

此后它会以类似的方式重复,但在上面的代码片段中它空闲了 40 秒。最终,它显然释放了守护程序地址注册表锁,并按预期结束了测试。这

发生在涉及测试容器时。当我运行除 Testcontainer 之外的所有测试时,Gradle 在测试完成后只需要不到一秒的时间即可结束该过程。 如果我用一个简单的

gradle test

一起运行所有测试,整个过程会完全挂起,并且调试日志显示 JVM 在某个时刻耗尽了堆空间内存,然后 Gradle 停留在这个循环中,日志记录看起来是这样的和上面一样,总是尝试获取和释放守护进程地址注册表锁。

为了完整起见,我的 Testcontainer 测试看起来都与此类似:

@SpringBootTest @Testcontainers @Import(RedisContainerConfig::class) class IntegrationTest { companion object { @JvmStatic @Container val redisContainer: GenericContainer<*> = GenericContainer(DockerImageName.parse("redis:7-alpine")) .withExposedPorts(6379) @DynamicPropertySource @JvmStatic fun redisProperties(registry: DynamicPropertyRegistry) { registry.add("redis.host") { redisContainer.host } registry.add("redis.port") { redisContainer.firstMappedPort } } } // tests }

其中 
RedisContainerConfig

只是使用动态属性配置自定义

RedisConnectionFactory
bean。
我很难理解为什么 Testcontainers 会导致 Gradle 像这样挂起。可能是什么原因造成的?

spring-boot kotlin gradle redis testcontainers
1个回答
0
投票
本期

中评论的那样:

对于那些在 Gradle 7.6 中遇到类似问题的人 到 8.2 为止,您可能想查看 #25361,其中有一个特定的 以特定方式使用终结器时会导致此问题。看起来是 已修复 8.3 - 不确定向后移植。

因此我相信您使用的是 7.6 到 8.2 之间的 Gradle 版本。将 Gradle 更新到最新版本应该可以解决您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.