1Net::Daemon::Test(3) User Contributed Perl Documentation Net::Daemon::Test(3)
2
3
4
6 Net::Daemon::Test - support functions for testing Net::Daemon servers
7
9 # This is the server, stored in the file "servertask".
10 #
11 # Create a subclass of Net::Daemon::Test, which in turn is
12 # a subclass of Net::Daemon
13 use Net::Daemon::Test ();
14 package MyDaemon;
15 our @ISA = qw(Net::Daemon::Test);
16
17 sub Run {
18 # Overwrite this and other methods, as you like.
19 }
20
21 my $self = Net::Daemon->new(\%attr, \@options);
22 eval { $self->Bind() };
23 if ($@) {
24 die "Server cannot bind: $!";
25 }
26 eval { $self->Run() };
27 if ($@) {
28 die "Unexpected server termination: $@";
29 }
30
31
32 # This is the client, the real test script, note we call the
33 # "servertask" file below:
34 #
35 # Call the Child method to spawn a child. Don't forget to use
36 # the timeout option.
37 use Net::Daemon::Test ();
38
39 my($handle, $port) = eval {
40 Net::Daemon::Test->Child(5, # Number of subtests
41 'servertask', '--timeout', '20')
42 };
43 if ($@) {
44 print "not ok 1 $@\n";
45 exit 0;
46 }
47 print "ok 1\n";
48
49 # Real tests following here
50 ...
51
52 # Terminate the server
53 $handle->Terminate();
54
56 This module is a frame for creating test scripts of Net::Daemon based
57 server packages, preferrably using Test::Harness, but that's your
58 choice.
59
60 A test consists of two parts: The client part and the server part. The
61 test is executed by the child part which invokes the server part, by
62 spawning a child process and invoking an external Perl script. (Of
63 course we woultn't need this external file with fork(), but that's the
64 best possibility to make the test scripts portable to Windows without
65 requiring threads in the test script.)
66
67 The server part is a usual Net::Daemon application, for example a
68 script like dbiproxy. The only difference is that it derives from
69 Net::Daemon::Test and not from Net::Daemon, the main difference is that
70 the Bind method attempts to allocate a port automatically. Once a port
71 is allocated, the number is stored in the file "ndtest.prt".
72
73 After spawning the server process, the child will wait ten seconds
74 (hopefully sufficient) for the creation of ndtest.prt.
75
77 Server part
78 Options Adds an option --timeout to Net::Daemon: The server's Run
79 method will die after at most 20 seconds.
80
81 Bind (Instance method) This is mainly the default Bind method, but
82 it attempts to find and allocate a free port in two ways: First
83 of all, it tries to call Bind with port 0, most systems will
84 automatically choose a port in that case. If that seems to
85 fail, ports 30000-30049 are tried. We hope, one of these will
86 succeed. :-)
87
88 Run (Instance method) Overwrites the Net::Daemon's method by adding
89 a timeout.
90
91 sub Run ($) {
92 my $self = shift;
93 $self->Run(); }
94
95 Client part
96 Child (Class method) Attempts to spawn a server process. The server
97 process is expected to create the file 'ndtest.prt' with the
98 port number.
99
100 The method returns a process handle and a port number. The
101 process handle offers a method Terminate that may later be used
102 to stop the server process.
103
105 Net::Daemon is Copyright (C) 1998, Jochen Wiedmann
106 Am Eisteich 9
107 72555 Metzingen
108 Germany
109
110 Phone: +49 7123 14887
111 Email: joe@ispsoft.de
112
113 All rights reserved.
114
115 You may distribute under the terms of either the GNU General Public
116 License or the Artistic License, as specified in the Perl README file.
117
119 Net::Daemon(3), Test::Harness(3)
120
121
122
123perl v5.34.0 2022-01-21 Net::Daemon::Test(3)