« blib/lib/POE/Test/Loops/00_info.pm
↑ Summary
blib/lib/POE/Test/Loops/z_steinert_signal_integrity.pm »
| 1 | #!/usr/bin/perl -w | ||
| 2 | # vim: ts=2 sw=2 expandtab | ||
| 3 | |||
| 4 | # Tests session detaching. | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use lib qw(./mylib ../mylib); | ||
| 8 | |||
| 9 | # Trace output local to this test program. | ||
| 10 | sub DEBUG () { 0 } | ||
| 11 | |||
| 12 | sub POE::Kernel::ASSERT_DEFAULT () { 1 } | ||
| 13 | |||
| 14 | BEGIN { | ||
| 15 | package | ||
| 16 | POE::Kernel; | ||
| 17 | use constant TRACE_DEFAULT => exists($INC{'Devel/Cover.pm'}); | ||
| 18 | } | ||
| 19 | |||
| 20 | use Test::More tests => 9; | ||
| 21 | |||
| 22 | use POE; | ||
| 23 | |||
| 24 | # Moved "global" test accumulation variables out of the "main" session | ||
| 25 | # because it was becoming a peer to the others that had been detached. | ||
| 26 | # Sometimes "main" would be stopped before the others, and the program | ||
| 27 | # would fail when they tried to post results back to it. | ||
| 28 | |||
| 29 | my $test_trace = ""; | ||
| 30 | |||
| 31 | # Spawn a grandchild. | ||
| 32 | |||
| 33 | sub spawn_grandchild { | ||
| 34 | my $grandchild_id = shift; | ||
| 35 | |||
| 36 | POE::Session->create( | ||
| 37 | inline_states => { | ||
| 38 | _start => sub { | ||
| 39 | my $kernel = $_[KERNEL]; | ||
| 40 | $kernel->alias_set( $grandchild_id ); | ||
| 41 | DEBUG and warn $_[SESSION]->ID, " has started.\n"; | ||
| 42 | }, | ||
| 43 | _parent => sub { | ||
| 44 | my ($kernel, $old_parent, $new_parent) = @_[KERNEL, ARG0, ARG1]; | ||
| 45 | my $old_alias = $kernel->call($old_parent, "get_alias"); | ||
| 46 | my $new_alias; | ||
| 47 | if (ref($new_parent) eq 'POE::Kernel') { | ||
| 48 | $new_alias = 'kernel'; | ||
| 49 | } | ||
| 50 | else { | ||
| 51 | $new_alias = $kernel->call($new_parent, "get_alias"); | ||
| 52 | } | ||
| 53 | $test_trace .= "(p $grandchild_id $old_alias $new_alias)"; | ||
| 54 | }, | ||
| 55 | _child => sub { | ||
| 56 | my ($kernel, $op, $child) = @_[KERNEL, ARG0, ARG1]; | ||
| 57 | my $child_alias = $kernel->call($child, 'get_alias' ); | ||
| 58 | $test_trace .= "(c $grandchild_id $op $child_alias)"; | ||
| 59 | }, | ||
| 60 | get_alias => sub { | ||
| 61 | return $grandchild_id; | ||
| 62 | }, | ||
| 63 | detach_self => sub { | ||
| 64 | $_[KERNEL]->detach_myself(); | ||
| 65 | }, | ||
| 66 | detach_child => sub { | ||
| 67 | $_[KERNEL]->detach_child( $_[ARG0] ); | ||
| 68 | }, | ||
| 69 | _stop => sub { | ||
| 70 | my $kernel = $_[KERNEL]; | ||
| 71 | DEBUG and warn $_[SESSION]->ID, " stopped.\n"; | ||
| 72 | }, | ||
| 73 | }, | ||
| 74 | ); | ||
| 75 | |||
| 76 | # To prevent this from returning a session reference. | ||
| 77 | undef; | ||
| 78 | } | ||
| 79 | |||
| 80 | # Spawn a child. | ||
| 81 | |||
| 82 | sub spawn_child { | ||
| 83 | my $child_id = shift; | ||
| 84 | my $alias = "a$child_id"; | ||
| 85 | |||
| 86 | POE::Session->create( | ||
| 87 | inline_states => { | ||
| 88 | _start => sub { | ||
| 89 | my $kernel = $_[KERNEL]; | ||
| 90 | $kernel->alias_set( $alias ); | ||
| 91 | $kernel->yield( 'spawn_grandchildren' ); | ||
| 92 | DEBUG and warn $_[SESSION]->ID, " has started.\n"; | ||
| 93 | }, | ||
| 94 | spawn_grandchildren => sub { | ||
| 95 | spawn_grandchild( $alias . "_1" ); | ||
| 96 | spawn_grandchild( $alias . "_2" ); | ||
| 97 | spawn_grandchild( $alias . "_3" ); | ||
| 98 | }, | ||
| 99 | _parent => sub { | ||
| 100 | my ($kernel, $old_parent, $new_parent) = @_[KERNEL, ARG0, ARG1]; | ||
| 101 | my $old_alias = $kernel->call($old_parent, 'get_alias'); | ||
| 102 | my $new_alias; | ||
| 103 | if (ref($new_parent) eq 'POE::Kernel') { | ||
| 104 | $new_alias = 'kernel'; | ||
| 105 | } | ||
| 106 | else { | ||
| 107 | $new_alias = $kernel->call($new_parent, 'get_alias'); | ||
| 108 | } | ||
| 109 | $test_trace .= "(p $child_id $old_alias $new_alias)"; | ||
| 110 | }, | ||
| 111 | _child => sub { | ||
| 112 | my ($kernel, $op, $child) = @_[KERNEL, ARG0, ARG1]; | ||
| 113 | my $child_alias = $kernel->call($child, 'get_alias' ); | ||
| 114 | $test_trace .= "(c $child_id $op $child_alias)"; | ||
| 115 | }, | ||
| 116 | get_alias => sub { | ||
| 117 | return $child_id; | ||
| 118 | }, | ||
| 119 | detach_self => sub { | ||
| 120 | my $kernel = $_[KERNEL]; | ||
| 121 | $kernel->detach_myself(); | ||
| 122 | }, | ||
| 123 | detach_child => sub { | ||
| 124 | my $kernel = $_[KERNEL]; | ||
| 125 | $kernel->detach_child( $_[ARG0] ); | ||
| 126 | }, | ||
| 127 | _stop => sub { | ||
| 128 | my $kernel = $_[KERNEL]; | ||
| 129 | DEBUG and warn $_[SESSION]->ID, " has stopped.\n"; | ||
| 130 | }, | ||
| 131 | }, | ||
| 132 | ); | ||
| 133 | |||
| 134 | # To prevent this from returning a session reference. | ||
| 135 | undef; | ||
| 136 | } | ||
| 137 | |||
| 138 | # Spawn the main session. This will spawn children, which will spawn | ||
| 139 | # grandchildren. Then the main session will perform controlled | ||
| 140 | # detaches and watch the results. | ||
| 141 | |||
| 142 | POE::Session->create( | ||
| 143 | inline_states => { | ||
| 144 | _start => sub { | ||
| 145 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 146 | $heap->{idle_count} = 0; | ||
| 147 | $kernel->alias_set( 'main' ); | ||
| 148 | $kernel->yield( 'spawn_children' ); | ||
| 149 | DEBUG and warn $_[SESSION]->ID, " has started.\n"; | ||
| 150 | }, | ||
| 151 | spawn_children => sub { | ||
| 152 | my $kernel = $_[KERNEL]; | ||
| 153 | spawn_child( 1 ); | ||
| 154 | spawn_child( 2 ); | ||
| 155 | spawn_child( 3 ); | ||
| 156 | $kernel->delay( run_tests => 0.5 ); | ||
| 157 | }, | ||
| 158 | get_alias => sub { | ||
| 159 | return 'main'; | ||
| 160 | }, | ||
| 161 | detach_self => sub { | ||
| 162 | my $kernel = $_[KERNEL]; | ||
| 163 | $kernel->detach_myself(); | ||
| 164 | }, | ||
| 165 | detach_child => sub { | ||
| 166 | my $kernel = $_[KERNEL]; | ||
| 167 | $kernel->detach_child( $_[ARG0] ); | ||
| 168 | }, | ||
| 169 | run_tests => sub { | ||
| 170 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 171 | |||
| 172 | $test_trace = ""; | ||
| 173 | $kernel->call( a1_1 => 'detach_self' ); | ||
| 174 | is( | ||
| 175 | $test_trace, '(c 1 lose a1_1)(p a1_1 1 kernel)', | ||
| 176 | "a1_1 detached itself" | ||
| 177 | ); | ||
| 178 | |||
| 179 | $test_trace = ''; | ||
| 180 | $kernel->call( a2_1 => 'detach_self' ); | ||
| 181 | is( | ||
| 182 | $test_trace, '(c 2 lose a2_1)(p a2_1 2 kernel)', | ||
| 183 | "a2_1 detached itself" | ||
| 184 | ); | ||
| 185 | |||
| 186 | $test_trace = ''; | ||
| 187 | $kernel->call( a3_1 => 'detach_self' ); | ||
| 188 | is( | ||
| 189 | $test_trace, '(c 3 lose a3_1)(p a3_1 3 kernel)', | ||
| 190 | "a3_1 detached itself" | ||
| 191 | ); | ||
| 192 | |||
| 193 | $test_trace = ''; | ||
| 194 | $kernel->call( a1 => detach_child => 'a1_2' ); | ||
| 195 | is( | ||
| 196 | $test_trace, '(c 1 lose a1_2)(p a1_2 1 kernel)', | ||
| 197 | "a1 detached child a1_2" | ||
| 198 | ); | ||
| 199 | |||
| 200 | $test_trace = ''; | ||
| 201 | $kernel->call( a2 => detach_child => 'a2_2' ); | ||
| 202 | is( | ||
| 203 | $test_trace, '(c 2 lose a2_2)(p a2_2 2 kernel)', | ||
| 204 | "a2 detached child a2_2" | ||
| 205 | ); | ||
| 206 | |||
| 207 | $test_trace = ''; | ||
| 208 | $kernel->call( a3 => detach_child => 'a3_2' ); | ||
| 209 | is( | ||
| 210 | $test_trace, '(c 3 lose a3_2)(p a3_2 3 kernel)', | ||
| 211 | "a3 detached child a3_2" | ||
| 212 | ); | ||
| 213 | |||
| 214 | $test_trace = ''; | ||
| 215 | $kernel->call( a1 => 'detach_self' ); | ||
| 216 | is( | ||
| 217 | $test_trace, '(c main lose 1)(p 1 main kernel)', | ||
| 218 | "a1 detached itself" | ||
| 219 | ); | ||
| 220 | |||
| 221 | $test_trace = ''; | ||
| 222 | $kernel->call( main => detach_child => 'a2' ); | ||
| 223 | is( | ||
| 224 | $test_trace, '(c main lose 2)(p 2 main kernel)', | ||
| 225 | "a2 detached itself" | ||
| 226 | ); | ||
| 227 | }, | ||
| 228 | _parent => sub { | ||
| 229 | my $old_alias = $_[KERNEL]->call( $_[ARG0], 'get_alias' ); | ||
| 230 | my $new_alias; | ||
| 231 | if (ref($_[ARG1]) eq 'POE::Kernel') { | ||
| 232 | $new_alias = 'kernel'; | ||
| 233 | } | ||
| 234 | else { | ||
| 235 | $new_alias = $_[KERNEL]->call( $_[ARG1], 'get_alias' ); | ||
| 236 | } | ||
| 237 | |||
| 238 | $test_trace .= "(p main $old_alias $new_alias)"; | ||
| 239 | }, | ||
| 240 | _child => sub { | ||
| 241 | my $child_alias = $_[KERNEL]->call( $_[ARG1], 'get_alias' ); | ||
| 242 | $test_trace .= "(c main $_[ARG0] $child_alias)"; | ||
| 243 | }, | ||
| 244 | _stop => sub { | ||
| 245 | DEBUG and warn $_[SESSION]->ID, " has stopped.\n"; | ||
| 246 | }, | ||
| 247 | grandchild_parent => sub { | ||
| 248 | my $old_alias = $_[KERNEL]->call( $_[ARG1], 'get_alias' ); | ||
| 249 | my $new_alias; | ||
| 250 | if (ref($_[ARG2]) eq 'POE::Kernel') { | ||
| 251 | $new_alias = 'kernel'; | ||
| 252 | } | ||
| 253 | else { | ||
| 254 | $new_alias = $_[KERNEL]->call( $_[ARG2], 'get_alias' ); | ||
| 255 | } | ||
| 256 | $test_trace .= "(p $_[ARG0] $old_alias $new_alias)"; | ||
| 257 | }, | ||
| 258 | grandchild_child => sub { | ||
| 259 | my $child_alias = $_[KERNEL]->call( $_[ARG2], 'get_alias' ); | ||
| 260 | $test_trace .= "(c $_[ARG0] $_[ARG1] $child_alias)"; | ||
| 261 | }, | ||
| 262 | }, | ||
| 263 | ); | ||
| 264 | |||
| 265 | POE::Kernel->run(); | ||
| 266 | |||
| 267 | # Final test to see if the remaining sessions died properly. The | ||
| 268 | # trace string can be nondeterministic. Split it, sort it, and rejoin | ||
| 269 | # it so it's always in a known order. | ||
| 270 | |||
| 271 | substr($test_trace, 0, 1) = ''; | ||
| 272 | substr($test_trace, -1, 1) = ''; | ||
| 273 | $test_trace = '(' . (join ')(', sort split /\)\(/, $test_trace) . ')'; | ||
| 274 | |||
| 275 | is( | ||
| 276 | $test_trace, | ||
| 277 | join( | ||
| 278 | "", | ||
| 279 | "(c 1 lose a1_3)", | ||
| 280 | "(c 2 lose a2_3)", | ||
| 281 | "(c 3 lose a3_3)", | ||
| 282 | "(c main lose 2)", | ||
| 283 | "(c main lose 3)", | ||
| 284 | "(p 2 main kernel)" | ||
| 285 | ), | ||
| 286 | "session destruction order" | ||
| 287 | ); | ||
| 288 | |||
| 289 | 1; |