summaryrefslogtreecommitdiffstats
path: root/t/socketmap.t
blob: a788663a66a6d4f632f2cef4fa7f5e2ea34e2887 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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: