Net::D...n::Test(3pm)

πŸ’– Net::Daemon::Test πŸ’–

okay bestie, so this is Net::Daemon::Test(3pm) β€” a page from the User Contributed Perl Documentation set, and it's literally the support squad for testing your Net::Daemon servers. no cap, it's giving "helper module that has your back." 🫢


✨ NAME ✨

Net::Daemon::Test β€” support functions for testing Net::Daemon servers πŸ’…


🌸 SYNOPSIS 🌸

# This is the server, stored in the file "servertask".
#
# Create a subclass of Net::Daemon::Test, which in turn is
# a subclass of Net::Daemon
use Net::Daemon::Test ();
package MyDaemon;
our @ISA = qw(Net::Daemon::Test);

sub Run {
    # Overwrite this and other methods, as you like.
}

my $self = Net::Daemon->new(\%attr, \@options);
eval { $self->Bind() };
if ($@) {
    die "Server cannot bind: $!";
}
eval { $self->Run() };
if ($@) {
    die "Unexpected server termination: $@";
}

# This is the client, the real test script, note we call the
# "servertask" file below:
#
# Call the Child method to spawn a child. Don't forget to use
# the timeout option.
use Net::Daemon::Test ();

my($handle, $port) = eval {
    Net::Daemon::Test->Child(5, # Number of subtests
                             'servertask', '--timeout', '20')
};
if ($@) {
    print "not ok 1 $@\n";
    exit 0;
}
print "ok 1\n";

# Real tests following here
...

# Terminate the server
$handle->Terminate();

πŸ¦‹ DESCRIPTION πŸ¦‹

so this module is like the frame for building test scripts for your Net::Daemon-based server packages πŸŽ€. honestly it works best with Test::Harness, but bestie, that's your choice β€” no pressure.

a test has two parts: the client part and the server part ✨. the child part is the one running the show β€” it invokes the server part by spawning a child process and calling up an external Perl script. (and yes, technically we wouldn't need this external file if we used fork(), but that's genuinely the best way to keep the test scripts portable to Windows without dragging threads into the test script. iconic problem-solving, fr.)

the server part is just a normal Net::Daemon application β€” like a script such as dbiproxy, for example. the only difference? it derives from Net::Daemon::Test instead of Net::Daemon. and the big deal there is that the Bind method tries to grab a port automatically. once a port is allocated, the number gets stored in the file "ndtest.prt". πŸ’Ύ

after spawning the server process, the child will wait ten seconds (hopefully sufficient πŸ™ˆ) for ndtest.prt to show up.


πŸ‘‘ AVAILABLE METHODS πŸ‘‘

🧚 Server part

  • Options β€” adds an option --timeout to Net::Daemon. what does it do? the server's Run method will die after at most 20 seconds. dramatic but necessary πŸ’€
  • Bind β€” (instance method) this is basically the default Bind method, except it tries to find and allocate a free port in two ways: first of all, it tries to call Bind with port 0, and most systems will just automatically choose a port when you do that 🌈. if that seems to fail, it tries ports 30000-30049. we hope one of these will succeed. :-) πŸ“
  • Run β€” (instance method) overwrites Net::Daemon's method by adding a timeout. here's the vibe:
sub Run ($) {
    my $self = shift;
    $self->Run(); }

🫢 Client part

  • Child β€” (class method) attempts to spawn a server process. the server process is expected to create the file 'ndtest.prt' with the port number. πŸ‘‘

the method returns a process handle and a port number. the process handle offers a method Terminate that may later be used to stop the server process. so useful, so slay πŸ’…


no gatekeeping, here's the credit where credit is due:

  Net::Daemon is Copyright (C) 1998, Jochen Wiedmann
                                     Am Eisteich 9
                                     72555 Metzingen
                                     Germany

                                     Phone: +49 7123 14887
                                     Email: joe@ispsoft.de

all rights reserved. ✨

you may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.


πŸ”₯ SEE ALSO πŸ”₯


and that's the whole page, bestie β€” this one shipped with perl v5.40.1 and it's dated 2026-05-06. go write some tests, you got this πŸ’–πŸŒΈβœ¨

πŸ“¦ where this page comes from