use strict; use warnings; use Test::More tests => 19; use Test::Exception; BEGIN { use_ok 'Spline::Socketmap', qw( lookup handle_request socketmap_protocol process_request ) or BAIL_OUT; } # Default handler throws_ok { lookup(undef, 'map', 'key') } qr/Not implemented/, 'No handler'; my $socketmap = new Spline::Socketmap(); # Parsing socketmap input my @args; { no warnings 'redefine'; local *Spline::Socketmap::lookup = sub { shift; @args = @_; return }; is($socketmap->handle_request('map key with whitespace'), undef, 'Protocol parsed'); is_deeply(\@args, ['map', 'key with whitespace'], 'Parsing with whitespace'); } { no warnings 'redefine'; local *Spline::Socketmap::lookup = sub { return 'OK test' }; is(handle_request($socketmap, 'map key'), 'OK test', 'Result passed'); } # Socketmap protocol handling { no warnings 'redefine'; local *Spline::Socketmap::lookup = sub { }; is($socketmap->socketmap_protocol('map key'), undef, 'Pass undef return value'); } { no warnings 'redefine'; local *Spline::Socketmap::lookup = sub { shift; shift; return shift }; is($socketmap->socketmap_protocol('map key'), 'key', 'Pass return value'); } { no warnings 'redefine'; local *Spline::Socketmap::lookup = sub { sleep 2 }; $socketmap->{server}->{timeout} = 1; is($socketmap->socketmap_protocol('map key'), 'TEMP Timeout', 'Temporary error on timeout'); } { no warnings 'redefine'; local *Spline::Socketmap::lookup = sub { die 'Other error' }; like($socketmap->socketmap_protocol('map key'), qr/TEMP Other error/, 'Temporary error on die'); } # process_request (Net::Server) my ($input, $output, $log); my $dummy = {}; { package Dummy; use base 'Spline::Socketmap'; sub recv { return $input } sub send { shift; $output = shift } sub log { shift; shift; $log = shift } bless $dummy, 'Dummy'; } { no warnings 'redefine'; local *Dummy::socketmap_protocol = sub { shift; return &{shift()} }; $input = 'OK test'; process_request($dummy); is($output, 'OK test', 'Send output if defined (ok)'); $input = 'NOTFOUND '; process_request($dummy); is($output, 'NOTFOUND ', 'Send output if defined (notfound)'); } { no warnings 'redefine'; local *Dummy::socketmap_protocol = sub { }; process_request($dummy); like($output, qr/TEMP .* error/, 'Temporary error if result is undef'); } { no warnings 'redefine'; local *Dummy::recv = sub { sleep 10 }; $dummy->{server}->{timeout} = 1; lives_ok { process_request($dummy) } 'Temporary error with hanging input'; like($output, qr/TEMP Timeout/, 'Timeout on hanging input'); } # recv { my $recv_data; open(my $fh, '<', \$recv_data); local *STDIN = $fh; $log = undef; $recv_data = '7:OK test,'; is(Spline::Socketmap::recv($dummy), 'OK test', 'Receive works'); is($log, 'OK test', 'Log data after receiving'); } # send { my $send_data; open(my $fh, '>', \$send_data); local *STDOUT = $fh; $log = undef; lives_ok { Spline::Socketmap::send($dummy, 'OK test') } 'Sending works'; is($send_data, '7:OK test,', 'Send data in netstring format'); is($log, 'OK test', 'Log data before sending'); } # vim: set et ts=4: