如何在 PHP 中对 PCNTL forker 类进行单元测试?
我有一个抽象的 PHP 类,它负责执行进程分叉,并从终端分离当前进程并继续作为守护进程。
我真的很想获得有关如何对此类(PHPUnit)进行单元测试的提示。假设在测试中基于这个抽象实现一个最小的套接字服务器并与之通信?还有更好的想法吗?
该类可以在这里看到:https://github。 com/tcz/PHPTracker/blob/master/lib/PHPTracker/Threading/Forker.php
谢谢!
I have an abstract PHP class that is responsible for doing process forks and also detaching the current process from terminal and continue as deamon.
I really would like to get tips about how to unit test this class (PHPUnit). Let's say implement a minimal socket server in the test based on this abstract and communicating with that? Any better ideas?
The class can be seen here: https://github.com/tcz/PHPTracker/blob/master/lib/PHPTracker/Threading/Forker.php
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 gphilip 所说,您可以使用 runkit 来模拟 pcntl_fork() 并测试所有代码分支。这是一个示例: http://kpayne.me/2012/ 01/17/如何进行单元测试分支/
As gphilip said, you can use runkit to mock pcntl_fork() and test all branches of code. Here's an example: http://kpayne.me/2012/01/17/how-to-unit-test-fork/
这里需要测试两件事:
可以通过将分叉进程的 ID 保存在某处来测试分叉是否有效,并查看它是否仍在运行,并且类的其余部分可以照常测试,无需分叉。
There are two things that need to be tested here:
Testing if the fork worked can be done by saving the ID of the forked process somewhere and see if it's still running, and the rest of the class can be tested as usual without forking.