File Coverage

inc/TestClient.pm
Criterion Covered Total %
statement 109 119 91.6
branch 12 22 54.5
condition n/a
subroutine 20 22 90.9
pod 0 1 0.0
total 141 164 85.9


line stmt bran cond sub pod time code
1             package TestClient;
2             # test client for proxy test - Andrew V. Purshottam
3             # sends request lines of form count: text
4             # to do
5             # - change approach for validating input to output:
6             # instead of trying to deduce expected output from input requests,
7             # make log of request lines sent to server. Decorate these
8             # lines with per connection (session id?) data.
9 1     1   5323 use warnings;
  1         3  
  1         91  
10 1     1   7 use strict;
  1         2  
  1         44  
11 1     1   6 use diagnostics;
  1         3  
  1         11  
12            
13             # sub POE::Kernel::ASSERT_DEFAULT () { 1 }
14 1     1   46 use Carp qw(carp croak);
  1         1  
  1         84  
15 1     1   7 use Data::Dumper;
  1         1  
  1         58  
16 1     1   6 use POE;
  1         2  
  1         12  
17 1     1   441 use POE::Filter::Stream;
  1         2  
  1         55  
18 1     1   8 use POE::Filter::Line;
  1         2  
  1         32  
19 1     1   5 use POE qw(Component::Server::TCP);
  1         2  
  1         5  
20 1     1   2381 use ClientRequest;
  1         3  
  1         34  
21 1     1   6 use POE::Component::Proxy::TCP::PoeDebug;
  1         3  
  1         66  
22            
23 1         4 use fields qw(alias port address proxy_client request_list_ref resp_list_ref
24 1     1   5 test_count number_tests);
  1         1  
25            
26             sub new {
27 2     2 0 16 my TestClient $self = shift;
28 2 50       6 unless (ref $self) {
29 2         7 $self = fields::new($self);
30             }
31            
32             # Extract parameters.
33 2         146 my %param = @_;
34            
35 2         6 $self->{alias} = delete $param{Alias};
36 2 50       8 $self->{alias} = "test_client_alias" unless defined ($self->{alias});
37 2         10 dbprint(3, "alias:", $self->{alias});
38 2         5 $self->{port} = delete $param{Port};
39 2 50       8 $self->{port} = 8000 unless defined ($self->{port});
40 2         12 dbprint(3, "port", $self->{port});
41 2         4 $self->{address} = delete $param{Address};
42 2 50       7 $self->{address} = "localhost" unless defined ($self->{address});
43 2         8 dbprint(3, "address", $self->{address});
44 2 50       6 croak "TestClient needs a RequestList parameter"
45             unless exists $param{RequestList};
46 2         5 $self->{request_list_ref} = delete $param{RequestList};
47 2         10 dbprint(10, "request_list_ref:", Dumper($self->{request_list_ref}));
48            
49 2         25 foreach (sort keys %param) {
50 0         0 carp "TestClient doesn't recognize \"$_\" as a parameter";
51             }
52            
53             # set up private instance data.
54 2         7 $self->{resp_list_ref} = [];
55 2         4 $self->{test_count} = 0;
56 2         4 $self->{number_tests} = scalar(@{$self->{request_list_ref}});
  2         5  
57 2         11 dbprint(3, "number_tests: $self->{number_tests}");
58            
59             $self->{proxy_client} =
60             POE::Component::Client::TCP->new
61             ( # Alias => $self->{alias},
62             RemoteAddress => $self->{address},
63             RemotePort => $self->{port},
64             Filter => "POE::Filter::Line",
65             Args => [$self],
66             Started => sub {
67 2     2   1126 my ( $kernel, $heap, $inner_self) = @_[ KERNEL, HEAP, ARG0];
68 2         10 $heap->{self} = $inner_self;
69 2         12 dbprint(3, "connected to $inner_self->{address}:$inner_self->{port}");
70             },
71             Connected => sub {
72 2     2   1750 my ( $kernel, $heap) = @_[ KERNEL, HEAP];
73 2         13 dbprint(3, "connected to $self->{address}:$self->{port}");
74             # enqueue events for all test requests...
75 2         4 my $base_delay = 0;
76 2         4 foreach my $req (@{$self->{request_list_ref}}) {
  2         6  
77 4         13 my $req_line = $req->get_request();
78 4         8 my $req_delay = $req->{delay_secs};
79 4         11 dbprint(4, "sending req:", $req->dump(),
80             " as line:$req_line at: $req_delay");
81            
82 4         19 $kernel->delay_add("send_server",
83             $req_delay + $base_delay,
84             $req_line );
85 4         326 $base_delay += $req_delay;
86             }
87            
88 2         11 dbprint(4, "**All done for this connection $base_delay");
89 2         9 $kernel->delay_add("send_server", $base_delay+2, "END" );
90            
91             },
92            
93             # The connection failed.
94             ConnectError => sub {
95 0     0   0 dbprint(0, "could not connect to $self->{address}:$self->{port}" );
96             },
97            
98             # The remote server has sent us something, so log it in the
99             # resp_list_ref.
100             ServerInput => sub {
101 32     32   38039 my ( $kernel, $heap, $input ) = @_[ KERNEL, HEAP, ARG0 ];
102 32 50       187 if (defined($input)) {
103 32         321 dbprint(3, "TestClient got input from server $self->{address} :",
104             "$self->{port}:-$input");
105 32 50       166 if ($input =~ m/^END/) {
106 0         0 dbprint (1, "Client got END!");
107             } else {
108 32         70 push(@{$self->{resp_list_ref}}, $input);
  32         219  
109             }
110             } else {
111 0         0 dbprint (1, "ServerInput event but no input!");
112             }
113             },
114            
115             ConnectError => sub {
116 0     0   0 my ($syscall_name, $error_number, $error_string) = @_[ARG0, ARG1, ARG2];
117 0         0 dbprint(1, "ConnectError from ORIG_SERVER on",
118             " $self->{port} : $self->{address}");
119             },
120            
121             Disconnected => sub {
122 2     2   1600 my ($kernel, $heap) = @_[ KERNEL, HEAP];
123 2         16 dbprint(1, "Disconnected from ORIG_SERVER on",
124             "$self->{port} : $self->{address}");
125 2         9 $kernel->yield("validate");
126             },
127            
128             ServerError => sub {
129 2     2   4358 my ($syscall_name, $error_number, $error_string) = @_[ARG0, ARG1, ARG2];
130 2         20 dbprint(1, "ServerError from ORIG_SERVER on $self->{port} :",
131             "$self->{address}");
132             },
133            
134             InlineStates =>
135             {
136             validate => sub {
137 2     2   2895 my ($kernel, $heap) = @_[ KERNEL, HEAP];
138 2         7 my $self = $heap->{self};
139 2         5 my $request_list_ref = $self->{request_list_ref};
140 2         6 my $resp_list_ref = $self->{resp_list_ref};
141 2         16 dbprint(8, "Comparing ", Dumper($request_list_ref, $resp_list_ref));
142 2         36 foreach my $req (@{$request_list_ref}) {
  2         8  
143 4         11 my $count = $req->{count};
144 4         8 my $ok = 1;
145 4         14 for (my $i = 0; $i < $count; $i++) {
146 32         33 my $resp_line = shift @{$resp_list_ref};
  32         3572  
147 32 50       99 if (!$req->cmp_with_responce($resp_line)) {
148 0         0 $ok = 0;
149 0         0 dbprint(1, "Bad responce: $resp_line");
150             }
151             }
152 4 50       13 if ($ok) {
153 4         19 dbprint(1, "test succeeded!");
154             } else {
155 0         0 dbprint(1, "test failed!\n");
156             }
157 4         75 $kernel->post("main", "test_result", $ok, $req->get_test_name());
158 4         512 $self->{test_count}++;
159 4 100       19 if ($self->{number_tests} == $self->{test_count}) {
160 2         8 $kernel->post("main", "client_done");
161             }
162            
163             }
164             },
165            
166             send_server => sub {
167 6     6   3966669 my ( $heap, $message ) = @_[ HEAP, ARG0 ];
168 6         73 dbprint(3, "sending from test client to server:$self->{address}:$self->{port}:",
169             "mess:$message");
170 6 50       30 if ($heap->{connected}) {
171 6         39 $heap->{server}->put($message);
172             } else {
173 0         0 dbprint(3, "send_server error not connected to server.");
174             }
175             },
176             }
177 2         76 );
178            
179 2         264 return $self;
180             }
181            
182             1;
183