436a2a804dec29d2e5df204543631ea18ddd4c69
[catagits/FCGI-ProcManager.git] / t / procmanager.t
1 # -*- perl -*-
2 # Copyright (c) 2000, FundsXpress Financial Network, Inc.
3 # This library is free software released "AS IS WITH ALL FAULTS"
4 # and WITHOUT ANY WARRANTIES under the terms of the GNU Lesser
5 # General Public License, Version 2.1, a copy of which can be
6 # found in the "COPYING" file of this distribution.
7
8 # $Id: procmanager.t,v 1.2 2000/11/10 01:09:48 muaddib Exp $
9
10 use strict;
11 use Test;
12
13 BEGIN { plan tests => 8; }
14
15 use FCGI::ProcManager;
16
17 my $m;
18
19 ok $m = FCGI::ProcManager->new();
20 ok $m->state() eq "idle";
21
22 ok $m->n_processes(100) == 100;
23 ok $m->n_processes(2) == 2;
24 ok $m->n_processes(0) == 0;
25
26 ok $m->manage();
27 ok $m->want_to_die(1);
28
29 # i'm not sure how to test these
30 #eval { $m->manage(); };
31 #ok $@ =~ /dying from death request/;
32 #undef $@;
33
34 ok $m->want_to_die(0) == 0;
35
36 #ok $m->n_processes(-3);
37 #eval { $m->manage(); };
38 #ok $@ =~ /dying from number of processes exception: -3/;
39 #undef $@;
40
41 $m->n_processes(1);
42
43 #$m->manage();
44 #sample_handler($m);
45
46 exit 0;
47
48 sub sample_handler {
49   my ($m) = @_;
50
51   while (1) {
52     $m->state("handling");
53
54     # Simulate a request dispatch.
55     my $t = int(rand(6)+10);
56     print "$$ sleeping $t..\n";
57     while (my $nslept = sleep $t) {
58       $t -= $nslept;
59       last unless $t;
60     }
61
62     $m->want_to_die() 
63       and $m->exit("Process $$ dying from SIGTERM after cleanup.\n");
64     $m->state("idle");
65
66     # Simulate blocking for a request.
67     my $t1 = int(rand(5)+3);
68     print "$$ waiting for $t1..\n";
69     sleep $t1;
70   }
71 }