File: pipe-followtail.t

package info (click to toggle)
libpoe-perl 2%3A1.3670-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,996 kB
  • ctags: 1,416
  • sloc: perl: 22,865; makefile: 9
file content (86 lines) | stat: -rw-r--r-- 1,632 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/perl
# vim: ts=2 sw=2 filetype=perl expandtab

use strict;
use warnings;

use POE qw(Wheel::FollowTail);
use POSIX qw(mkfifo);
use Test::More;


if ($^O eq 'MSWin32') {
  plan skip_all => 'Windows does not support mkfifo';
} else {
  plan tests => 3;
}


my $PIPENAME = 'testpipe';
my @EXPECTED = qw(foo bar);

POE::Session->create(
  inline_states => {
    _start      => \&_start_handler,
    done        => \&done,
    input_event => \&input_handler,
  }
);

POE::Kernel->run();
exit;

#------------------------------------------------------------------------------

sub _start_handler {
  my ($kernel, $heap) = @_[KERNEL, HEAP];

  mkfifo($PIPENAME, 0600) unless -p $PIPENAME;

  $heap->{wheel} = POE::Wheel::FollowTail->new(
    InputEvent => 'input_event',
    Filename   => $PIPENAME,
  );

  open my $fh, '>', $PIPENAME or die "open failed: $!";
  $fh->autoflush(1);

  print $fh "foo\nbar\n";

  # rt.cpan.org 96039: Save the filehandle so it remains open.
  $heap->{write_fh} = $fh;

  $kernel->delay('done', 3);
  return;
}


sub input_handler {
  my ($kernel, $line) = @_[KERNEL, ARG0];
  my $next = shift @EXPECTED;
  is($line, $next);
  $kernel->delay('done', 1);
  return;
}


sub done {
  my ($kernel, $heap) = @_[KERNEL, HEAP];

  # Cleanup the test pipe file.
  # Must be closed for the unlink() to work on Windows.
  my $write_fh = delete $heap->{write_fh};
  close $write_fh or die "close failed: $!";
  unlink $PIPENAME or die "unlink failed: $!";

  # delete the wheel so the POE session can end
  delete $heap->{wheel};

  # @expected should be empty
  is_deeply(\@EXPECTED, []);

  return;
}


1;