Karma在成功执行单元测试后挂起30秒。

huangapple go评论72阅读模式
英文:

Karma hangs for 30 seconds after successfully executing unit tests

问题

以下是翻译好的部分:

在所有单元测试成功执行之后,Karma会挂起30秒,然后输出以下消息:

Chrome Headless 114.0.5735.35 (Windows 10) 错误
  断开连接,因为在30000毫秒内未收到消息。
Chrome Headless 114.0.5735.35 (Windows 10): 执行了634个测试中的634个 断开连接 (34.58秒 / 3.303秒)
Chrome Headless 114.0.5735.35 (Windows 10) 错误
Chrome Headless 114.0.5735.35 (Windows 10): 执行了634个测试中的634个 断开连接 (34.58秒 / 3.303秒)

我在SO上阅读了很多帖子,但没有一个完全处理我所遇到的问题。经过深入调查,令人惊讶的是测试数量的问题!当删除测试以使测试数量恰好减少到621时,一切都正常工作。当添加一个更多的测试(无论是哪个...甚至是一个空的 it("should...", () => {}) )时,它就会挂起。

我在 karma.conf.js 中使用以下插件:

require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage'),
require('@angular-devkit/build-angular/plugins/karma')

它们都是最新可用版本。

这是调试输出。在好的情况下,它看起来一样(只是底部没有红色错误消息)。

观察到的情况:

  • 当从 package.json 中的 ng test 中移除 --code-coverage 选项时,可以增加测试的数量,直到再次挂起。
  • 起初,这个问题似乎很难重现。后来发现这是由于Karma的一个默认设置导致测试以随机顺序执行。在 karma.conf.jsjasmine 部分添加 random: false 使问题100%可复现。
英文:

After the successful execution of all unit tests, Karma hangs for 30 seconds and then outputs the following message:

Chrome Headless 114.0.5735.35 (Windows 10) ERROR
  Disconnected , because no message in 30000 ms.
Chrome Headless 114.0.5735.35 (Windows 10): Executed 634 of 634 DISCONNECTED (34.58 secs / 3.303 secs)
Chrome Headless 114.0.5735.35 (Windows 10) ERROR
Chrome Headless 114.0.5735.35 (Windows 10): Executed 634 of 634 DISCONNECTED (34.58 secs / 3.303 secs)

I read a lot of posts here on SO but none of them handles exactly the issue which I'm seeing. After investigating deeper, it turned out that surprisingly the number of tests matters! When removing tests so that the number of tests goes down to exactly 621, everything works fine. When adding one more test (no matter which ... even an empty it("should...",()=>{}) ) it hangs.

I'm using the following plugins in karma.conf.js:

require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage'),
require('@angular-devkit/build-angular/plugins/karma')

all of them have the latest available versions.

This is the debug output. It looks the same in good case (just without the red error message at the bottom).

Karma在成功执行单元测试后挂起30秒。

Observations

  • When removing the --code-coverage option from ng test in package.json, the number of tests can be increased until it reaches a point where it hangs again.
  • First it looked like the issue is hardly reproducible. It turned out that this was caused by a default setting of Karma which causes the tests to be executed in a random order. Adding random: false to the jasmine section of karma.conf.js made it 100% reproducible.

答案1

得分: 0

我在 Karma 中发现了一个 bug,并创建了这个拉取请求:

https://github.com/karma-runner/karma/pull/3852

但不幸的是,很不幸,Karma 在 2023 年 4 月已被弃用(https://github.com/Leaflet/Leaflet/issues/8939)!因此,很可能不会再合并任何拉取请求。如果有人需要修复这个问题,只需查看这个拉取请求。它非常简单(只有两行代码)。

关于这个错误的解释是,显然在处理大量测试时,可能会出现在完成所有测试之后(从而关闭与浏览器的连接后),仍然会有一些日志消息进入。Karma 将这些写入日志,但还错误地重新触发了不活动超时。运行的计时器显然会阻止 Node 关闭进程,导致 30 秒的挂起。我的修复措施防止了在 Karma 状态为 DISCONNECTED 时触发计时器。

英文:

I found a bug in Karma and created this pull request:

https://github.com/karma-runner/karma/pull/3852

But unfortunately it turned out that Karma was deprecated in April 2023 (https://github.com/Leaflet/Leaflet/issues/8939)! Thus, most likely no PRs will be merged anymore. If somebody needs to fix the issue, just have a look into the PR. Its very simple (only two lines of code).

The explanation for the error is, that obviously when handling a high number of tests, it can happen that after finishing all tests (and thus closing the connection to the browser), still some log messages can come in. Karma writes these to the log, but also mistakenly re-triggers the inactivity timeout. The running timer obviously prevents Node from shutting down the process and this causes the 30sec hang. My fix prevents the timer from being triggered if the Karma status is DISCONNECTED.

huangapple
  • 本文由 发表于 2023年6月12日 23:02:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457941.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定