How can I start a TCP server in the background during a Perl unit 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 开发者_JS百科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.
1 . Try Ether's suggestion from a similar Q:
use Proc::Daemon; # do everything you need to do before forking the child... # make into daemon; closes all open fds Proc::Daemon::Init();
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 to IO::Socket
.
精彩评论