File: bingos-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 (89 lines) | stat: -rw-r--r-- 2,208 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/perl -w
# vim: ts=2 sw=2 filetype=perl expandtab

# Make sure that the default behavior for POE::Wheel::FollowTail is to
# skip to the end of the file when it first starts.

use warnings;
use strict;

sub POE::Kernel::ASSERT_DEFAULT () { 1 }

use IO::Handle;
use POE qw(Wheel::FollowTail Filter::Line);
use Test::More tests => 2;

my $filename = 'bingos-followtail';

# Using "!" as a newline to avoid differences in opinion about "\n".

open FH, ">$filename" or die "$!\n";
FH->autoflush(1);
print FH "moocow - this line should be skipped!";

POE::Session->create(
  package_states => [
    'main' => [qw(_start _input _error _shutdown _file_is_idle)],
  ],
  inline_states => {
    _stop => sub { undef },
  },
  heap => { filename => $filename, },
);

$poe_kernel->run();
exit 0;

sub _start {
  my ($kernel,$heap) = @_[KERNEL,HEAP];
  $heap->{wheel} = POE::Wheel::FollowTail->new(
    Filter      => POE::Filter::Line->new( Literal => "!" ),
    Filename    => $heap->{filename},
    InputEvent  => '_input',
    ErrorEvent  => '_error',
    IdleEvent   => '_file_is_idle',
  );

  $heap->{running} = 1;
  $heap->{counter} = 0;

  print FH "Cows go moo, yes they do!";
  close FH;
  return;
}

sub _shutdown {
  delete $_[HEAP]->{wheel};
  return;
}

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

  # Make sure we got the right line.
  is($input, 'Cows go moo, yes they do', 'Got the right line');
  ok( ++$heap->{counter} == 1, 'Cows went moo' );
  POE::Kernel->delay( _shutdown => 5 );
  return;
}

sub _error {
  my ($heap,$operation, $errnum, $errstr, $wheel_id) = @_[HEAP,ARG0..ARG3];
  diag("Wheel $wheel_id generated $operation error $errnum: $errstr\n");
  POE::Kernel->delay( _shutdown => 0.01 );
  return;
}

sub _file_is_idle {
  return unless $_[HEAP]{counter};

  # At first I thought just a delay( _shutdown => 1 ) would be nice
  # here, but there's a slight chance that the POE::Wheel::FollowTail
  # polling interval could refresh this indefinitely.
  #
  # So I took the slightly more awkward course of turning off the
  # shutdown timer and triggering shutdown immediately.

  POE::Kernel->delay(_shutdown => undef);
  POE::Kernel->yield("_shutdown");
}