Debian Codegraph @

t/04_plugins/11_cycleempty/02_cycle.t

raw
1 use strict;
2 use warnings FATAL => 'all';
3 use lib 't/inc';
4 use POE;
5 use POE::Component::IRC::State;
6 use POE::Component::IRC::Plugin::CycleEmpty;
7 use POE::Component::Server::IRC;
8 use Test::More tests => 10;
9
10 my $bot1 = POE::Component::IRC::State->spawn(
11 Flood => 1,
12 plugin_debug => 1,
13 );
14 my $bot2 = POE::Component::IRC::State->spawn(
15 Flood => 1,
16 plugin_debug => 1,
17 );
18 my $ircd = POE::Component::Server::IRC->spawn(
19 Auth => 0,
20 AntiFlood => 0,
21 );
22
23 my $plugin = POE::Component::IRC::Plugin::CycleEmpty->new();
24 $bot2->plugin_add(CycleEmpty => $plugin);
25
26 POE::Session->create(
27 package_states => [
28 main => [qw(
29 _start
30 ircd_listener_add
31 ircd_listener_failure
32 _shutdown
33 irc_001
34 irc_join
35 irc_part
36 irc_disconnected
37 )],
38 ],
39 );
40
41 $poe_kernel->run();
42
43 sub _start {
44 my ($kernel) = $_[KERNEL];
45
46 $ircd->yield('register', 'all');
47 $ircd->yield('add_listener');
48 $kernel->delay(_shutdown => 60, 'Timed out');
49 }
50
51 sub ircd_listener_failure {
52 my ($kernel, $op, $reason) = @_[KERNEL, ARG1, ARG3];
53 $kernel->yield('_shutdown', "$op: $reason");
54 }
55
56 sub ircd_listener_add {
57 my ($kernel, $port) = @_[KERNEL, ARG0];
58
59 $bot1->yield(register => 'all');
60 $bot1->yield(connect => {
61 nick => 'TestBot1',
62 server => '127.0.0.1',
63 port => $port,
64 });
65
66 $bot2->yield(register => 'all');
67 $bot2->yield(connect => {
68 nick => 'TestBot2',
69 server => '127.0.0.1',
70 port => $port,
71 });
72 }
73
74 sub irc_001 {
75 my $irc = $_[SENDER]->get_heap();
76 pass($irc->nick_name . ' logged in');
77 $irc->yield(join => '#testchannel') if $irc == $bot1;
78 }
79
80 sub irc_join {
81 my ($sender, $heap, $who, $where) = @_[SENDER, HEAP, ARG0, ARG1];
82 my $nick = (split /!/, $who)[0];
83 my $irc = $sender->get_heap();
84
85 return if $nick ne $irc->nick_name();
86
87 if (!$heap->{joined} || $heap->{joined} != 2) {
88 $heap->{joined}++;
89 pass("$nick joined channel");
90 $bot2->yield(join => $where) if $irc == $bot1;
91 }
92
93 if ($irc == $bot2) {
94 $bot1->yield(part => $where);
95
96 if ($heap->{cycling}) {
97 pass("$nick rejoined channel");
98 $bot1->yield('quit');
99 $bot2->yield('quit');
100 }
101 }
102 }
103
104 sub irc_part {
105 my ($sender, $heap, $who, $where) = @_[SENDER, HEAP, ARG0, ARG1];
106 my $nick = (split /!/, $who)[0];
107 my $irc = $sender->get_heap();
108
109 return if $nick ne $irc->nick_name();
110 pass("$nick parted channel");
111
112 if ($irc == $bot2) {
113 ok($plugin->is_cycling($where), "$nick is cycling");
114 $heap->{cycling} = 1;
115 }
116 }
117
118 sub irc_disconnected {
119 my ($kernel, $heap) = @_[KERNEL, HEAP];
120 pass('irc_disconnected');
121 $heap->{count}++;
122 $kernel->yield('_shutdown') if $heap->{count} == 2;
123 }
124
125 sub _shutdown {
126 my ($kernel, $error) = @_[KERNEL, ARG0];
127 fail($error) if defined $error;
128
129 $kernel->alarm_remove_all();
130 $ircd->yield('shutdown');
131 $bot1->yield('shutdown');
132 $bot2->yield('shutdown');
133 }
134