我如何弄清楚我的脚本在哪里睡觉?

时间:2021-09-16 01:07:41

One of my unit tests is extremely slow, taking more than a full second for each test. Profiling that test file, the top few lines read:

我的一个单元测试非常慢,每次测试花费超过一秒钟。分析测试文件,前几行读取:

  %   cumulative   self              self     total
 time   seconds   seconds    calls  ms/call  ms/call  name
198.71    12.28     12.28        2  6140.00  6140.00  Mutex#sleep
 14.40    13.17      0.89     9658     0.09     1.09  REXML::Element#namespace
 10.68    13.83      0.66    18814     0.04     0.08  REXML::Element#root

I can't figure out where the sleep call comes from though! My whole application only sleeps in one place, in a class called Throttle, and I've inserted a breakpoint in front of it which doesn't trigger during this test. I have tried:

我无法弄清楚睡眠呼叫来自哪里!我的整个应用程序只在一个名为Throttle的类中睡觉,我在它前面插入了一个断点,在此测试期间不会触发。我努力了:

class Mutex
  def sleep(time)
    require "byebug"; byebug
  end
end

Which never breaks, and I've tried:

从来没有打破,我尝试过:

def setup
  Mutex.define_singleton_method(:sleep) { |time|
    require "byebug"; byebug
  }
end

Which also does absolutely nothing. I have also tried both of these with Kernel instead of Mutex. I have read through my code countless times and I can't for the life of me figure out why my application is constantly sleeping! Can anybody give me any pointers?

哪个也绝对没有。我也用Kernel而不是Mutex尝试了这两种方法。我已经无数次阅读了我的代码,我不能为我的生活找出为什么我的应用程序一直在睡觉!任何人都可以给我任何指示吗?

1 个解决方案

#1


0  

I figured it out. Turns out Mutex#sleep was invoked by Net::HTTP#post. My unit test bootstrap script was supposed to override all functions that writes to disk or makes network requests, but I had forgotten to require the functions that I was supposed to override, so the end result was that my overrides were overwritten by the actual functions, causing my unit tests to make real requests to online APIs.

我想到了。结果是Mutex#sleep由Net :: HTTP#post调用。我的单元测试引导脚本应该覆盖写入磁盘或发出网络请求的所有函数,但我忘了要求我应该覆盖的函数,所以最终结果是我的覆盖被实际函数覆盖了,导致我的单元测试对在线API发出实际请求。

I only discovered this by chance as I was stepping through the code in byebug.

我只是偶然发现了这个,因为我正在逐步完成byebug中的代码。

As soon as I fixed the bootstrap script, my unit tests ran as quickly as ever.

一旦我修复了引导脚本,我的单元测试就像以前一样快速运行。

#1


0  

I figured it out. Turns out Mutex#sleep was invoked by Net::HTTP#post. My unit test bootstrap script was supposed to override all functions that writes to disk or makes network requests, but I had forgotten to require the functions that I was supposed to override, so the end result was that my overrides were overwritten by the actual functions, causing my unit tests to make real requests to online APIs.

我想到了。结果是Mutex#sleep由Net :: HTTP#post调用。我的单元测试引导脚本应该覆盖写入磁盘或发出网络请求的所有函数,但我忘了要求我应该覆盖的函数,所以最终结果是我的覆盖被实际函数覆盖了,导致我的单元测试对在线API发出实际请求。

I only discovered this by chance as I was stepping through the code in byebug.

我只是偶然发现了这个,因为我正在逐步完成byebug中的代码。

As soon as I fixed the bootstrap script, my unit tests ran as quickly as ever.

一旦我修复了引导脚本,我的单元测试就像以前一样快速运行。