Advertisement
cheako

Switching out system might not be possible.

Oct 29th, 2015
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.56 KB | None | 0 0
  1. use Test::More tests => 4;
  2. use IO::Handle;
  3. use Test::Fork;
  4.  
  5. pipe( GETPID, SENDPID );
  6. SENDPID->autoflush(1);
  7.  
  8. $^F = fileno(SENDPID)
  9.   if ( $^F < fileno(SENDPID) );
  10. push my @cpid, fork_ok(
  11.     1,
  12.     sub {
  13.         close(GETPID);
  14.         $^F = fileno(SENDPID)
  15.           if ( $^F < fileno(SENDPID) );
  16.         my $r =
  17.           system( 'lsof -np $$; echo $$ >&'
  18.               . fileno(SENDPID)
  19.               . '; exec src/ihlt >&2' );
  20.         ok( $r == 2, 'ihlt: quit' );
  21.     }
  22. );
  23. close(SENDPID);
  24.  
  25. if ( !$cpid[-1] ) {
  26.     warn 'music died.';
  27.     exit(2);
  28. }
  29.  
  30. use IO::Socket::INET;
  31.  
  32. # create a connecting socket
  33. my @sockets;
  34. for ( 0 .. 2 ) {
  35.     push @sockets,
  36.       new IO::Socket::INET(
  37.         PeerHost => '127.0.0.1',
  38.         PeerPort => '2020',
  39.         Proto    => 'tcp',
  40.       );
  41.     unless ($socket) {
  42.         kill 'SIGINT', <GETPID>;
  43.         fail "cannot connect to the server $!\n";
  44.         die;
  45.     }
  46.     pass("$_: connected to the server\n");
  47. }
  48.  
  49. my $response = "";
  50.  
  51. # data to send to a server
  52. $sockets[0]->autoflush(1);
  53. ok( $sockets[0]->send("Hello world!\n") == 13, 'first msg sent' );
  54. ok( $sockets[1]->recv( $response, 1024 ) == 13, 'first msg recv' );
  55. ok( $sockets[2]->recv( $response, 1024 ) == 13, 'first msg recv again' );
  56.  
  57. $sockets[2]->autoflush(1);
  58. ok( $sockets[2]->send("Hello world!\n") == 13, 'first msg sent' );
  59. ok( $sockets[1]->recv( $response, 1024 ) == 13, 'first msg recv' );
  60. ok( $sockets[0]->recv( $response, 1024 ) == 13, 'first msg recv again' );
  61.  
  62. $sockets[$_]->close() for ( 0 .. 2 );
  63.  
  64. kill 'SIGINT', <GETPID>;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement