woosley has asked for the wisdom of the Perl Monks concerning the following question:

Hi perl monks,
I just wrote a module to run job servers using POE. The source code can be find at http://github.com/woosley/Perl-PJob. The last thing left for me to do is writing some tests for this module. I can write some method tests, but how can I write test for a deamon?
The problem is that: I don't even know how to start. Should I write a script to start a test deamon, and kill this process at the end of the test?
What's more, is there any convention to write this kind of tests?
Thanks
  • Comment on How can I write tests for deamon module?

Replies are listed 'Best First'.
Re: How can I write tests for deamon module?
by rcaputo (Chaplain) on Nov 17, 2009 at 06:49 UTC
      Hey, can you be more specific? If I start the daemon, it just hang there for connections, how can I test it in the same program?

        If you're using POE, you can have both a server and its client in the same program at the same time. You are not limited to one or the other. For example:

        #!perl use warnings; use strict; use POE qw(Component::Server::TCP Component::Client::TCP); POE::Component::Server::TCP->new( Alias => "server", Port => 12345, ClientConnected => sub { print "server got a connection from $_[HEAP]{remote_ip}\n"; $_[HEAP]{client}->put("Smile from the server!"); }, ClientInput => sub { my $client_input = $_[ARG0]; print "server got client input: $client_input\n"; $client_input =~ tr[a-zA-Z][n-za-mN-ZA-M]; $_[HEAP]{client}->put($client_input); }, ); POE::Component::Client::TCP->new( RemoteAddress => "localhost", RemotePort => 12345, Connected => sub { print "client connected to server\n"; $_[HEAP]{server}->put("smile"); }, ServerInput => sub { my $input = $_[ARG0]; print "client received from server: $input\n"; if ($input eq "fzvyr") { # rot13(smile) $_[KERNEL]->yield("shutdown"); $_[KERNEL]->post(server => "shutdown"); } }, ); POE::Kernel->run(); exit;

        The output looks something like this:

        server got a connection from 127.0.0.1 client connected to server server got client input: smile client received from server: Smile from the server! client received from server: fzvyr