在 Perl 单元测试期间如何在后台启动 TCP 服务器?
我正在尝试为客户端服务器应用程序编写单元测试。为了测试客户端,在我的单元测试中,我想首先启动我的 tcp 服务器(它本身是另一个 perl 文件)。我尝试通过分叉来启动 TCP 服务器:
if (! fork()) {
system ("$^X server.pl") == 0 or die "couldn't start server"
}
因此,当我在 perl Makefile.PL
之后调用 make test
时,此测试启动,我可以看到服务器启动,但之后单元测试就挂在那里。所以我想我需要在后台启动这个服务器,我在最后尝试了 &
来强制它在后台启动,然后测试继续。但是,我还是没能成功。我做错了什么?谢谢。
I am trying to write a unit test for a client server application. To test the client, in my unit test, I want to first start my tcp server (which itself is another perl file). I tried to start the TCP server by forking:
if (! fork()) {
system ("$^X server.pl") == 0 or die "couldn't start server"
}
So when I call make test
after perl Makefile.PL
, this test starts and I can see the server starting but after that the unit test just hangs there. So I guess I need to start this server in background and I tried the &
at the end to force it to start in background and then test to continue. But, I still couldn't succeed. What am I doing wrong? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1.尝试 来自类似的 Ether 的建议问:
2。如果您使用
IO::Socket
进行 TCP 连接(或任何其他模块 - CPAN 或您自己的模块),那么您确实应该使用模拟(例如Test::MockObject
)来模拟实际的套接字通信。这样,您的客户端(或服务器的)测试在测试时将与其他代码段隔离(尽管您仍然需要服务器运行 - 但这次手动就可以了 - 记录初始的待模拟调用IO::Socket 。1 . Try Ether's suggestion from a similar Q:
2 . If you're using
IO::Socket
for your TCP connections (or any other module - CPAN or your own), you should really use mocking (e.g.Test::MockObject
) to mock the actual socket communications. That way your client's (or server's for that matter) test would be isolated from the other piece of code when testing (though you still need the server running - but this time by hand is OK - to record initial to-be-mocked calls toIO::Socket
.