-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added utils and first testcase for Daemonproxy::Protocol
Also fixed syntax errors in Daemonproxy::Protocol revealed by test refs #47
- Loading branch information
Showing
4 changed files
with
136 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#! /usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
use Test::More; | ||
use FindBin; | ||
use Try::Tiny; | ||
use lib "$FindBin::Bin/lib"; | ||
|
||
use_ok( 'TestDpProto' ) or BAIL_OUT; | ||
use_ok( 'Daemonproxy::Protocol' ) or BAIL_OUT; | ||
ok( try { mock_dp()->client }, 'create mock_dp and client' ) or BAIL_OUT; | ||
|
||
done_testing; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package Test::MockDaemonproxy; | ||
use strict; | ||
use warnings; | ||
use IO::Handle; | ||
use Log::Any '$log'; | ||
use Daemonproxy::Protocol; | ||
|
||
sub new { | ||
my $class= shift; | ||
my %args= @_ == 1 && ref $_[0] eq 'HASH'? %{ $_[0] } : @_; | ||
pipe($args{event_rd}, $args{event_wr}); | ||
pipe($args{cmd_rd}, $args{cmd_wr}); | ||
bless \%args, $class; | ||
} | ||
|
||
sub client { | ||
my $self= shift; | ||
return $self->{client} ||= Daemonproxy::Protocol->new( | ||
rd_handle => $self->{event_rd}, | ||
wr_handle => $self->{event_wr} | ||
); | ||
} | ||
|
||
sub next_cmd { | ||
my $self= shift; | ||
$self->{cmd_rd}->blocking(0); | ||
my $line= $self->{cmd_rd}->getline; | ||
return unless defined $line; | ||
chomp $line; | ||
return [ split /\t/, $line ]; | ||
} | ||
|
||
sub send_event { | ||
my ($self, @args)= @_; | ||
my $msg= join("\t", @args); | ||
$log->tracef("mock event: %s", $msg) if $log->is_trace; | ||
$self->{event_wr}->print($msg."\n"); | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package TestDpProto; | ||
use strict; | ||
use warnings; | ||
use Carp; | ||
use FindBin; | ||
use lib "$FindBin::Bin/../../../../t/lib"; | ||
use Exporter 'import'; | ||
our @EXPORT= qw( dp client client2 mock_dp ); | ||
|
||
our $mock_dp; | ||
sub mock_dp { | ||
$mock_dp ||= do { | ||
require Test::MockDaemonproxy; | ||
Test::MockDaemonproxy->new; | ||
}; | ||
} | ||
|
||
our $dp; | ||
sub dp { | ||
$dp ||= do { | ||
require Test::Daemonproxy; | ||
Test::Daemonproxy->new(); | ||
}; | ||
} | ||
|
||
our $client1; | ||
sub dp_client1 { | ||
$client1 ||= do { | ||
require Daemonproxy::Protocol; | ||
Daemonproxy::Protocol->new( | ||
rd_handle => dp->dp_stdout, | ||
wr_handle => dp->dp_stdin | ||
); | ||
}; | ||
} | ||
*dp_client= *dp_client1; | ||
|
||
our $client2; | ||
sub dp_client2 { | ||
$client2 ||= do { | ||
require Socket; | ||
|
||
# The only way to connect an additional client to daemonproxy is | ||
# to either spawn a new client process, or connect via socket. | ||
# We opt for the socket route here. The alternative is to define | ||
# and start a new service which relays its handles to pipes that | ||
# we created when spawning daemonproxy. | ||
-d "$FindBin::Bin/tmp" or mkdir("$FindBin::Bin/tmp") or croak "can't create ./tmp dir"; | ||
my $sockpath= "$FindBin::Bin/tmp/daemonproxy-client2.sock"; | ||
unlink $sockpath; | ||
|
||
# Create a socket, connect to it, and then remove it | ||
|
||
client->send("socket.create", "-", $sockpath); | ||
for (1..100) { last if -S $sockpath; sleep 0.1; } | ||
socket(my $sock, Socket::AF_UNIX(), Socket::SOCK_STREAM(), 0) | ||
or die "Can't create socket: $!"; | ||
connect($sock, Socket::sockaddr_un($sockpath)) | ||
or croak "Can't connect to $sockpath"; | ||
client->send("socket.delete"); | ||
|
||
# Return protocol client on this new socket | ||
DaemonProxy::Protocol->new( | ||
rd_handle => $sock, | ||
wr_handle => $sock | ||
); | ||
} | ||
} | ||
|
||
1; |