| 1 | package Test::BDD::Infrastructure::Socket; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 1 | our $VERSION = '1.005'; # VERSION | |||||||||
| 7 | # ABSTRACT: cucumber step definitions for tcp/udp/unix-socket tests | ||||||||||
| 8 | |||||||||||
| 9 | 1 | ▶ | use Test::More; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use Test::BDD::Cucumber::StepFile qw( Given When Then ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 0 | ▶ | sub S { Test::BDD::Cucumber::StepFile::S } | ||||||||
|
Subroutine
S: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 13 | |||||||||||
| 14 | 1 | ▶ | use Test::BDD::Infrastructure::Utils qw( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | convert_unit convert_cmp_operator $CMP_OPERATOR_RE convert_interval | ||||||||||
| 16 | 1 | lookup_config ); | |||||||||
| 17 | |||||||||||
| 18 | 1 | ▶ | use IO::Socket::INET; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 1 | ▶ | use IO::Socket::UNIX; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 1 | ▶ | use IO::Select; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | |||||||||||
| 22 | |||||||||||
| 23 | 0 | ▶ | Given qr/a (tcp|udp) connection to (\S+) on port (\d+) is made/, sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 24 | 0 | my $proto = $1; | |||||||||
| 25 | 0 | my $host = $2; | |||||||||
| 26 | 0 | my $port = $3; | |||||||||
| 27 | |||||||||||
| 28 | 0 | S->{'socket'} = IO::Socket::INET->new( | |||||||||
| 29 | PeerAddr => $host, | ||||||||||
| 30 | PeerPort => int $port, | ||||||||||
| 31 | Proto => $proto, | ||||||||||
| 32 | timeout => 5, | ||||||||||
| 33 | ); | ||||||||||
| 34 | 0 | ▶ | if( ! defined S->{'socket'} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 0 | S->{'error'} = $@; | |||||||||
| 36 | } | ||||||||||
| 37 | 1 | }; | |||||||||
| 38 | |||||||||||
| 39 | 0 | ▶ | Given qr/a connection on socket (\S+) is opened/, sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 40 | 0 | my $path = $1; | |||||||||
| 41 | 0 | S->{'socket'} = IO::Socket::UNIX->new( | |||||||||
| 42 | Type => SOCK_STREAM, | ||||||||||
| 43 | Peer => $path, | ||||||||||
| 44 | timeout => 5, | ||||||||||
| 45 | ); | ||||||||||
| 46 | 0 | ▶ | if( ! defined S->{'socket'} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 0 | S->{'error'} = $@; | |||||||||
| 48 | } | ||||||||||
| 49 | 1 | }; | |||||||||
| 50 | |||||||||||
| 51 | 0 | ▶ | Then qr/the (?:connection|socket) must be (?:successfully? )?enstablished/, sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 52 | 0 | ok( defined S->{'socket'}, 'connection handle must be defined' ); | |||||||||
| 53 | 0 | isa_ok( S->{'socket'}, 'IO::Socket'); | |||||||||
| 54 | 1 | }; | |||||||||
| 55 | |||||||||||
| 56 | 0 | ▶ | Then qr/the (?:connection|socket) must fail(?: with (?:an error like )?(.*))?$/, sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 57 | 0 | my $regex = $1; | |||||||||
| 58 | 0 | ok( ! defined S->{'socket'}, 'connection handle must be undefined' ); | |||||||||
| 59 | 0 | ▶ | if( defined $regex ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 0 | like( S->{'error'}, qr/$regex/i, "error must match regex $regex"); | |||||||||
| 61 | } | ||||||||||
| 62 | 1 | }; | |||||||||
| 63 | |||||||||||
| 64 | 0 | ▶ | When qr/the (?:connection|socket) sends the line (.*)$/, sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 65 | 0 | my $str = $1; | |||||||||
| 66 | 0 | S->{'socket'}->print( $str."\n" ); | |||||||||
| 67 | 1 | }; | |||||||||
| 68 | 0 | ▶ | When qr/the (?:connection|socket) sends an empty line$/, sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 69 | 0 | S->{'socket'}->print( "\n" ); | |||||||||
| 70 | 1 | }; | |||||||||
| 71 | |||||||||||
| 72 | 0 | ▶ | Then qr/the (?:connection|socket) must recieve an line like (.*)$/, sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 73 | 0 | my $regex = $1; | |||||||||
| 74 | 0 | my $timeout = 5; | |||||||||
| 75 | 0 | my $select = IO::Select->new; | |||||||||
| 76 | 0 | $select->add( S->{'socket'}); | |||||||||
| 77 | 0 | ▶ | if( $select->can_read( $timeout ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 0 | my $line = S->{'socket'}->getline; | |||||||||
| 79 | 0 | like( $line, qr/$regex/i, "recieved line must be like $regex"); | |||||||||
| 80 | } else { | ||||||||||
| 81 | 0 | fail("did not recieve a response within $timeout seconds."); | |||||||||
| 82 | } | ||||||||||
| 83 | 1 | }; | |||||||||
| 84 | |||||||||||
| 85 | 1 | 1; | |||||||||
| 86 | |||||||||||
| 87 | __END__ | ||||||||||