File: caller_state.pm

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 (88 lines) | stat: -rw-r--r-- 2,449 bytes parent folder | download | duplicates (8)
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
#!/usr/bin/perl -w
# vim: ts=2 sw=2 expandtab

# Test that caller_state returnes expected results

use strict;

use lib qw(./mylib ../mylib);
use Test::More tests => 6;

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

BEGIN { use_ok("POE") } # 1

BEGIN { $^W = 1 };

POE::Session->create(
  inline_states => {
    _start       => sub {
    $_[KERNEL]->post($_[SESSION],'check_1');
    # set our callback and postback
    $_[HEAP]->{postback} = $_[SESSION]->postback("check_4");
    $_[HEAP]->{callback} = $_[SESSION]->callback("check_5");
    },
    check_1 => sub {
    if ($_[CALLER_STATE] eq '_start') {
      pass("called from _start"); # 2
    } else {
      diag("post failed: caller state is $_[CALLER_STATE] (should be _start)");
      fail("called from _start");
      delete $_[HEAP]->{callback};
      delete $_[HEAP]->{postback};
      return;
    }
    $_[KERNEL]->yield("check_2");
    },
  check_2 => sub {
    if ($_[CALLER_STATE] eq 'check_1') {
      pass("called from check_1"); # 3
    } else {
      diag("yield failed: caller state is $_[CALLER_STATE] (should be check_1)");
      fail("called from check_1");
      delete $_[HEAP]->{callback};
      delete $_[HEAP]->{postback};
      return;
    }
    # since we are calling check_3, and the postback calls check_4
    # the callback there will see it as if this session called it
    $_[KERNEL]->call($_[SESSION], "check_3");
  },
  check_3 => sub {
    if ($_[CALLER_STATE] eq 'check_2') {
      pass("called from check_2"); # 4
    } else {
      diag("call failed: caller state is $_[CALLER_STATE] (should be check_2)");
      fail("called from check_2");
      return;
    }
    my $postback = delete $_[HEAP]->{postback};
    $postback->();
  },
  check_4 => sub {
    # this _should_ look like it comes from check_2 because of the call()
    if ($_[CALLER_STATE] eq 'check_2') {
      pass("called from check_2 (again)"); # 5
    } else {
      diag("postback failed: caller state is $_[CALLER_STATE] (should be check_2)");
      fail("called from check_2");
    }
    my $callback = delete $_[HEAP]->{callback};
    $callback->();
  },
  check_5 => sub {
    if ($_[CALLER_STATE] eq 'check_4') {
      pass("called from check_4"); # 6
    } else {
      diag("callback failed: caller state is $_[CALLER_STATE] (should be check_4)");
      fail("called from check_4");
    }  
  },
  _stop => sub { }
  }
);

POE::Kernel->run();

1;