我正在尝试使用需要休眠以使后台进程继续进行的 ert 设置一些测试。我试过使用sleep-for
and accept-process-output
。两者都不可靠。这是一个小例子。
此测试仅休眠 5 秒,然后检查是否至少经过了 3 秒。使用sleep-for
它立即完成并失败。如果shell-command
未注释,则需要预期的 5 秒并成功!这里发生了什么?
(ert-deftest timetest ()
(let ((now (cadr (current-time))))
;(shell-command "sleep 5")
(sleep-for 5)
(should (< now (- (cadr (current-time)) 3)))))
编辑:
当我测试前面的示例时,我的环境中一定有一些奇怪的东西。这个稍作改动的示例包含一个后台进程,就像我需要测试的那样,但失败了。我以交互方式和使用命令对其进行了测试:
emacs --batch -l example.el -f ert-run-tests-batch-and-exit
(ert-deftest timetest ()
(let ((now (cadr (current-time))))
(start-process "echo" "*echo*" "echo" "hello world")
(sleep-for 5)
(should (< now (- (cadr (current-time)) 3)))))
输出是:
Test timetest condition:
(ert-test-failed
((should
(< now
(- ... 3)))
:form
(< 55177 55174)
:value nil))
FAILED 1/1 timetest
Ran 1 tests, 0 results as expected, 1 unexpected (2013-02-05 09:57:29+0000)
1 unexpected results:
FAILED timetest
编辑2:
似乎表明进程输出的新版本足以中断sleep-for
:
(ert-deftest timetest ()
(let ((now (cadr (current-time))))
(start-process "yes" "*yes*" "yes")
(sleep-for 1) ;; This sleep-for may be interrupted by process *output*
(sleep-for 5) ;; This sleep-for is also interrupted
(should (< now (- (cadr (current-time)) 3)))))
编辑3:
怀着沉重的心情,我发布了另一个版本:
(ert-deftest timetest ()
(let ((now (cadr (current-time)))
(process-connection-type nil))
(start-process "tmp" "*tmp*" "bash" "-c" "sleep 1; echo hi")
(sleep-for 5)
(should (< now (- (cadr (current-time)) 3)))))
似乎很明显sleep-for
不能依靠阻止。