views:

76

answers:

1

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.

+1  A: 

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.

DVK
John
My suggestion is: For unit testing you mock ANY module other than the one being unit tested. For app level testing you mock modules that act as external interfaces (e.g. IO::Socket).
DVK