Changeset 357
- Timestamp:
- 11/11/06 15:12:07 (5 years ago)
- Location:
- wiki-toolkit-plugin-ping
- Files:
-
- 3 modified
-
lib/Wiki/Toolkit/Plugin/Ping.pm (modified) (4 diffs)
-
t/01_init.t (modified) (3 diffs)
-
t/02_call.t (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
wiki-toolkit-plugin-ping/lib/Wiki/Toolkit/Plugin/Ping.pm
r356 r357 8 8 use Wiki::Toolkit::Plugin; 9 9 use LWP; 10 use LWP::UserAgent; 10 11 11 12 @ISA = qw( Wiki::Toolkit::Plugin ); … … 16 17 sub new { 17 18 my $class = shift; 18 my % services = @_;19 my %args = @_; 19 20 20 21 my $self = {}; 21 22 bless $self, $class; 23 24 # Get list of services 25 unless($args{services}) { 26 $self->{services} = {}; 27 return $self; 28 } 29 my %services = %{$args{services}}; 30 31 # Get node -> URL mapping 32 unless($args{node_to_url}) { 33 die("Must supply 'node_to_url;"); 34 } 35 unless($args{node_to_url} =~ /\$node/) { 36 die("node_to_url '$args{node_to_url}' must contain \$node"); 37 } 38 $self->{node_to_url} = $args{node_to_url}; 39 22 40 23 41 # Check the services … … 42 60 } 43 61 62 # Return our list of services, in case anyone's interested 63 sub services { 64 my $self = shift; 65 return %{$self->{services}}; 66 } 67 44 68 # Define our post_write plugin, which does the ping 45 69 # Happens in another thread, to stop it slowing things down 46 70 sub post_write { 47 71 my $self = shift; 72 unless(keys %{$self->{services}}) { return; } 48 73 49 74 my %args = @_; … … 52 77 53 78 # Spawn a new thread 79 my $pid = fork(); 80 if($pid) { 81 # We're the main thread, return now 82 return; 83 } else { 84 # We're the child, do the work 54 85 55 # Build the ping URL 56 # Ping 86 # What's the URL of the node? 87 my $node_url = $self->{node_to_url}; 88 $node_url =~ s/\$node/$node/; 89 90 # Get a LWP instance 91 my $ua = LWP::UserAgent->new; 92 $ua->agent("Wiki::Toolkit::Plugin::Ping $version"); 93 94 # Ping each service 95 foreach my $service (keys %{$self->{services}}) { 96 # Build the ping URL 97 my $ping_url = $self->{services}->{$service}; 98 $ping_url =~ s/\$url/$node_url/; 99 100 # Ping 101 my $req = HTTP::Request->new(GET => $ping_url); 102 my $res = $ua->request($req); 103 unless($res->is_success) { 104 warn("Error pinging $service: $res->status_line"); 105 } 106 } 107 108 # All done, close the thread 109 exit; 110 } 57 111 } 58 112 -
wiki-toolkit-plugin-ping/t/01_init.t
r354 r357 4 4 use Wiki::Toolkit::Plugin::Ping; 5 5 6 use Test::More tests => 3;6 use Test::More tests => 4; 7 7 8 8 # Basic create … … 12 12 # Several URls create 13 13 my $plugin2 = Wiki::Toolkit::Plugin::Ping->new( 14 node_to_url => 'http://localhost/\$node', 15 services => { 14 16 test => 'http://hello/?$url', 15 17 test2 => 'http://hello/?$url', 18 } 16 19 ); 17 20 ok( !undef $plugin2, "Plugin was created OK with no URLs" ); … … 21 24 eval { 22 25 $plugin3 = Wiki::Toolkit::Plugin::Ping->new( 26 node_to_url => 'http://localhost/\$node', 27 services => { 23 28 test => 'http://something/' 29 } 24 30 ); 25 31 }; 26 warn("Plugin3 is '$plugin3'\n");27 32 ok( ! $plugin3, "Can't create with a url missing \$url" ); 33 34 # Don't give the URL builder 35 my $plugin4 = undef; 36 eval { 37 $plugin4 = Wiki::Toolkit::Plugin::Ping->new( 38 services => { 39 test => 'http://something/$url' 40 } 41 ); 42 }; 43 ok( ! $plugin4, "Can't create with a node_to_url missing" ); 44 -
wiki-toolkit-plugin-ping/t/02_call.t
r356 r357 6 6 use Wiki::Toolkit::Plugin::Ping; 7 7 8 use Test::More tests => 1; 8 use IO::Socket; 9 10 use Test::More tests => 4; 9 11 10 12 … … 20 22 21 23 # Listen on a special port, so we can check a ping happened 24 my $sock = new IO::Socket::INET ( 25 LocalPort => 112233, 26 Proto => 'tcp', 27 Listen => 1, 28 ); 29 unless($sock) { 30 die("Can't listen on port 112233 for test"); 31 } 22 32 23 33 24 34 # Create, to call localhost 25 35 my $plugin = Wiki::Toolkit::Plugin::Ping->new( 36 node_to_url => "http://wiki.org/\$node", 37 services => { 26 38 test => "http://localhost:112233/url=\$url" 39 } 27 40 ); 28 41 ok( $plugin, "Plugin was created OK with the local URL" ); … … 39 52 metadata => {} 40 53 ); 54 55 # Check they actually sent us something 56 my $rsock = $sock->accept(); 57 my @req; 58 my $going = 1; 59 while($going && (my $line = <$rsock>)) { 60 $line =~ s/\r?\n$//; 61 unless($line) { $going = 0; } 62 63 push @req,$line; 64 warn "**$line**\n"; 65 } 66 67 # Check they requested the right thing 68 like( $req[0], qr/^GET \/url=http:\/\/wiki.org\/TestNode/, "Did right get" ); 69 like( $req[2], qr/^Host: localhost:112233/, "Correct http/1.1 host" ); 70 71 # Send them an OK 72 print $rsock "HTTP/1.0 200 OK\r\n\r\n"; 73 74 # Close 75 close($rsock); 76 close($sock); 77 78 # All happy 79 ok( "Happy" );
