Skip to content

Instantly share code, notes, and snippets.

@hinrik
Created May 11, 2010 11:12
Show Gist options
  • Save hinrik/397186 to your computer and use it in GitHub Desktop.
Save hinrik/397186 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use lib 't/inc';
use POE qw(Wheel::SocketFactory);
use Socket;
use POE::Component::IRC;
use POE::Component::Server::IRC;
use Test::More tests => 4;
my $bot1 = POE::Component::IRC->spawn(
Flood => 1,
plugin_debug => 1,
#debug => 1,
);
my $bot2 = POE::Component::IRC->spawn(
Flood => 1,
plugin_debug => 1,
#debug => 1,
);
my $ircd = POE::Component::Server::IRC->spawn(
Auth => 0,
AntiFlood => 0,
debug => 1,
);
POE::Session->create(
package_states => [
main => [qw(
_start
_config_ircd
_shutdown
irc_001
irc_disconnected
)],
],
);
$poe_kernel->run();
sub _start {
my ($kernel) = $_[KERNEL];
my $ircd_port = get_port() or $kernel->yield(_shutdown => 'No free port');
$kernel->yield(_config_ircd => $ircd_port);
$kernel->delay(_shutdown => 60, 'Timed out');
}
sub get_port {
my $wheel = POE::Wheel::SocketFactory->new(
BindAddress => '127.0.0.1',
BindPort => 0,
SuccessEvent => '_fake_success',
FailureEvent => '_fake_failure',
);
return if !$wheel;
return unpack_sockaddr_in($wheel->getsockname()) if wantarray;
return (unpack_sockaddr_in($wheel->getsockname))[0];
}
sub _config_ircd {
my ($kernel, $port) = @_[KERNEL, ARG0];
$ircd->yield(add_listener => Port => $port);
$bot1->yield(register => 'all');
$bot1->yield(connect => {
nick => 'TestBot1',
server => '127.0.0.1',
port => $port,
});
$bot2->yield(register => 'all');
$bot2->yield(connect => {
nick => 'TestBot2',
server => '127.0.0.1',
port => $port,
});
}
sub irc_001 {
my $irc = $_[SENDER]->get_heap();
pass($irc->nick_name.' logged in');
$irc->yield('quit');
}
sub irc_disconnected {
my ($kernel, $heap) = @_[KERNEL, HEAP];
pass('irc_disconnected');
$heap->{count}++;
$kernel->yield('_shutdown') if $heap->{count} == 2;
}
sub _shutdown {
my ($kernel, $error) = @_[KERNEL, ARG0];
fail($error) if defined $error;
$kernel->alarm_remove_all();
$ircd->yield('shutdown');
$bot1->yield('shutdown');
$bot2->yield('shutdown');
}
#################
################# the debug output below is from the server, but the test results are in the wrong order
#################
1..4
->NICK TestBot2
->USER hinrik 0 * :Hinrik Örn Sigurðsson,,,
<-:poco.server.irc 001 TestBot2 :Welcome to the poconet Internet Relay Chat network TestBot2
<-:poco.server.irc 002 TestBot2 :Your host is poco.server.irc[poco.server.irc/52027], running version POE::Component::Server::IRC-1.36
<-:poco.server.irc 003 TestBot2 :This server was created Tue May 11 2010 at 12:47:21 GMT
<-:poco.server.irc 004 TestBot2 poco.server.irc POE::Component::Server::IRC-1.36 Dilowz biklmnopstveIh bkloveIh
<-:poco.server.irc 005 TestBot2 CALLERID EXCEPTS INVEX MAXCHANNELS=15 MAXBANS MAXTARGETS=4 NICKLEN=9 TOPICLEN=80 KICKLEN=120 :are supported by this server
<-:poco.server.irc 005 TestBot2 CHANTYPES=#& PREFIX=(ohv)@%+ CHANMODES=eIb,k,l,imnpst NETWORK=poconet CASEMAPPING=rfc1459 DEAF=D :are supported by this server
->NICK TestBot1
->USER hinrik 0 * :Hinrik Örn Sigurðsson,,,
<-:poco.server.irc 001 TestBot1 :Welcome to the poconet Internet Relay Chat network TestBot1
<-:poco.server.irc 002 TestBot1 :Your host is poco.server.irc[poco.server.irc/52027], running version POE::Component::Server::IRC-1.36
<-:poco.server.irc 003 TestBot1 :This server was created Tue May 11 2010 at 12:47:21 GMT
<-:poco.server.irc 004 TestBot1 poco.server.irc POE::Component::Server::IRC-1.36 Dilowz biklmnopstveIh bkloveIh
<-:poco.server.irc 005 TestBot1 CALLERID EXCEPTS INVEX MAXCHANNELS=15 MAXBANS MAXTARGETS=4 NICKLEN=9 TOPICLEN=80 KICKLEN=120 :are supported by this server
<-:poco.server.irc 005 TestBot1 CHANTYPES=#& PREFIX=(ohv)@%+ CHANMODES=eIb,k,l,imnpst NETWORK=poconet CASEMAPPING=rfc1459 DEAF=D :are supported by this server
<-:poco.server.irc 251 TestBot2 :There are 2 users and 0 invisible on 1 servers
<-:poco.server.irc 255 TestBot2 :I have 2 clients and 0 servers
<-:poco.server.irc 265 TestBot2 :Current local users: 2 Max: 2
<-:poco.server.irc 266 TestBot2 :Current global users: 2 Max: 2
<-:poco.server.irc 250 TestBot2 :Highest connection count: 2 (2 clients) (2 connections received)
<-:poco.server.irc 422 TestBot2 :MOTD File is missing
<-:TestBot2 MODE TestBot2 :+i
<-:poco.server.irc 251 TestBot1 :There are 1 users and 1 invisible on 1 servers
<-:poco.server.irc 255 TestBot1 :I have 2 clients and 0 servers
<-:poco.server.irc 265 TestBot1 :Current local users: 2 Max: 2
<-:poco.server.irc 266 TestBot1 :Current global users: 2 Max: 2
<-:poco.server.irc 250 TestBot1 :Highest connection count: 2 (2 clients) (2 connections received)
<-:poco.server.irc 422 TestBot1 :MOTD File is missing
<-:TestBot1 MODE TestBot1 :+i
ok 1 - TestBot1 logged in
ok 2 - TestBot2 logged in
->QUIT
<-ERROR :Closing Link: 127.0.0.1 (Client Quit)
->QUIT
<-ERROR :Closing Link: 127.0.0.1 (Client Quit)
ok 3 - irc_disconnected
ok 4 - irc_disconnected
ok
All tests successful.
Files=1, Tests=4, 0 wallclock secs ( 0.02 usr 0.01 sys + 0.19 cusr 0.02 csys = 0.24 CPU)
Result: PASS
#######################
####################### now I turned off the server's debug output and turned on the clients' debug output,
####################### revealing the incorrect order of event processing
#######################
1..4
>>> NICK TestBot1
>>> USER hinrik 0 * :Hinrik Örn Sigurðsson,,,
>>> NICK TestBot2
>>> USER hinrik 0 * :Hinrik Örn Sigurðsson,,,
->:poco.server.irc 001 TestBot1 :Welcome to the poconet Internet Relay Chat network TestBot1
->:poco.server.irc 002 TestBot1 :Your host is poco.server.irc[poco.server.irc/50651], running version POE::Component::Server::IRC-1.36
->:poco.server.irc 003 TestBot1 :This server was created Tue May 11 2010 at 12:46:26 GMT
->:poco.server.irc 004 TestBot1 poco.server.irc POE::Component::Server::IRC-1.36 Dilowz biklmnopstveIh bkloveIh
->:poco.server.irc 005 TestBot1 CALLERID EXCEPTS INVEX MAXCHANNELS=15 MAXBANS MAXTARGETS=4 NICKLEN=9 TOPICLEN=80 KICKLEN=120 :are supported by this server
->:poco.server.irc 005 TestBot1 CHANTYPES=#& PREFIX=(ohv)@%+ CHANMODES=eIb,k,l,imnpst NETWORK=poconet CASEMAPPING=rfc1459 DEAF=D :are supported by this server
->:poco.server.irc 251 TestBot1 :There are 1 users and 1 invisible on 1 servers
->:poco.server.irc 255 TestBot1 :I have 2 clients and 0 servers
->:poco.server.irc 265 TestBot1 :Current local users: 2 Max: 2
->:poco.server.irc 266 TestBot1 :Current global users: 2 Max: 2
->:poco.server.irc 250 TestBot1 :Highest connection count: 2 (2 clients) (2 connections received)
->:poco.server.irc 422 TestBot1 :MOTD File is missing
->:TestBot1 MODE TestBot1 :+i
->:poco.server.irc 001 TestBot2 :Welcome to the poconet Internet Relay Chat network TestBot2
->:poco.server.irc 002 TestBot2 :Your host is poco.server.irc[poco.server.irc/50651], running version POE::Component::Server::IRC-1.36
->:poco.server.irc 003 TestBot2 :This server was created Tue May 11 2010 at 12:46:26 GMT
->:poco.server.irc 004 TestBot2 poco.server.irc POE::Component::Server::IRC-1.36 Dilowz biklmnopstveIh bkloveIh
->:poco.server.irc 005 TestBot2 CALLERID EXCEPTS INVEX MAXCHANNELS=15 MAXBANS MAXTARGETS=4 NICKLEN=9 TOPICLEN=80 KICKLEN=120 :are supported by this server
->:poco.server.irc 005 TestBot2 CHANTYPES=#& PREFIX=(ohv)@%+ CHANMODES=eIb,k,l,imnpst NETWORK=poconet CASEMAPPING=rfc1459 DEAF=D :are supported by this server
->:poco.server.irc 251 TestBot2 :There are 2 users and 0 invisible on 1 servers
->:poco.server.irc 255 TestBot2 :I have 2 clients and 0 servers
->:poco.server.irc 265 TestBot2 :Current local users: 2 Max: 2
->:poco.server.irc 266 TestBot2 :Current global users: 2 Max: 2
->:poco.server.irc 250 TestBot2 :Highest connection count: 2 (2 clients) (2 connections received)
->:poco.server.irc 422 TestBot2 :MOTD File is missing
->:TestBot2 MODE TestBot2 :+i
ok 1 - TestBot1 logged in
ok 2 - TestBot2 logged in
>>> QUIT
>>> QUIT
->ERROR :Closing Link: 127.0.0.1 (Client Quit)
->ERROR :Closing Link: 127.0.0.1 (Client Quit)
ok 3 - irc_disconnected
ok 4 - irc_disconnected
ok
All tests successful.
Files=1, Tests=4, 0 wallclock secs ( 0.02 usr 0.00 sys + 0.19 cusr 0.02 csys = 0.23 CPU)
Result: PASS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment