Skip to content

Commit 2d37863

Browse files
authored
Merge pull request #51 from toddr-bot/koan.toddr.bot/fix-issue-32
fix: eliminate unnecessary sleeps from mock test suite
2 parents cdfa13d + e945955 commit 2d37863

File tree

8 files changed

+116
-36
lines changed

8 files changed

+116
-36
lines changed

lib/Net/Jabber/Bot.pm

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use Mozilla::CA;
1313

1414
my $PosInt = Type::Tiny->new( name => 'PosInt', parent => Int, constraint => sub { $_ > 0 } );
1515
my $PosNum = Type::Tiny->new( name => 'PosNum', parent => Num, constraint => sub { $_ > 0 } );
16+
my $NonNegNum = Type::Tiny->new( name => 'NonNegNum', parent => Num, constraint => sub { $_ >= 0 } );
1617
my $HundredInt = Type::Tiny->new( name => 'HundredInt', parent => Num, constraint => sub { $_ > 100 } );
1718

1819
my $CoercedBool = Bool->plus_coercions( Str, sub { ( $_ =~ m/(^on$)|(^true$)/i ) + 0 } );
@@ -25,21 +26,21 @@ has jabber_client => (
2526

2627
#my %connection_hash : ATTR; # Keep track of connection options fed to client.
2728

28-
has 'client_session_id' => ( isa => Str, is => 'rw' );
29-
has 'connect_time' => ( isa => $PosInt, is => 'rw', default => 9_999_999_999 );
30-
has 'forum_join_grace' => ( isa => $PosNum, is => 'rw', default => 10 );
31-
has 'server_host' => ( isa => Str, is => 'rw', lazy => 1, default => sub { shift->server } );
32-
has 'server' => ( isa => Str, is => 'rw' );
33-
has 'port' => ( isa => $PosInt, is => 'rw', default => 5222 );
34-
has 'gtalk' => ( isa => Bool, is => 'rw', default => '0' );
35-
has 'tls' => ( isa => Bool, is => 'rw', default => '0' );
36-
has 'ssl_ca_path' => ( isa => Str, is => 'rw', default => Mozilla::CA::SSL_ca_file() );
37-
has 'ssl_verify' => ( isa => Bool, is => 'rw', default => '1' );
38-
has 'connection_type' => ( isa => Str, is => 'rw', default => 'tcpip' );
39-
has 'conference_server' => ( isa => Str, is => 'rw' );
40-
has 'username' => ( isa => Str, is => 'rw' );
41-
has 'password' => ( isa => Str, is => 'rw' );
42-
has 'alias' => ( isa => Str, lazy => 1, is => 'rw', default => 'net_jabber_bot' );
29+
has 'client_session_id' => ( isa => Str, is => 'rw' );
30+
has 'connect_time' => ( isa => $PosInt, is => 'rw', default => 9_999_999_999 );
31+
has 'forum_join_grace' => ( isa => $NonNegNum, is => 'rw', default => 10 );
32+
has 'server_host' => ( isa => Str, is => 'rw', lazy => 1, default => sub { shift->server } );
33+
has 'server' => ( isa => Str, is => 'rw' );
34+
has 'port' => ( isa => $PosInt, is => 'rw', default => 5222 );
35+
has 'gtalk' => ( isa => Bool, is => 'rw', default => '0' );
36+
has 'tls' => ( isa => Bool, is => 'rw', default => '0' );
37+
has 'ssl_ca_path' => ( isa => Str, is => 'rw', default => Mozilla::CA::SSL_ca_file() );
38+
has 'ssl_verify' => ( isa => Bool, is => 'rw', default => '1' );
39+
has 'connection_type' => ( isa => Str, is => 'rw', default => 'tcpip' );
40+
has 'conference_server' => ( isa => Str, is => 'rw' );
41+
has 'username' => ( isa => Str, is => 'rw' );
42+
has 'password' => ( isa => Str, is => 'rw' );
43+
has 'alias' => ( isa => Str, lazy => 1, is => 'rw', default => 'net_jabber_bot' );
4344

4445
# Resource defaults to alias_hostname_pid
4546
has 'resource' => ( isa => Str, lazy => 1, is => 'rw', default => sub { shift->alias . "_" . hostname . "_" . $$ } );

t/05-helper_functions.t

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use strict;
44
use warnings;
5-
use Test::More tests => 127;
5+
use Test::More tests => 125;
66
use Net::Jabber::Bot;
77

88
#InitLog4Perl();
@@ -63,14 +63,13 @@ my $bot = Net::Jabber::Bot->new(
6363
, out_messages_per_second => $out_messages_per_second
6464
, max_message_size => $max_message_size
6565
, max_messages_per_hour => $max_messages_per_hour
66+
, forum_join_grace => 0
6667
);
6768

6869
is($bot->message_delay, 0.2, "Message delay is set right to .20 seconds");
6970
is($bot->max_messages_per_hour, $max_messages_per_hour, "Max messages per hour ($max_messages_per_hour) didn't get messed with by safeties");
7071

7172
isa_ok($bot, "Net::Jabber::Bot");
72-
ok(1, "Sleeping 12 seconds to make sure we get past initializtion");
73-
ok((sleep 12) > 10, "Making sure the bot get's past login initialization (sleep 12)");
7473
process_bot_messages(); # Clean off the queue before we start?
7574

7675
# continue editing here. Need to next enhance mock object to know jabber bot callbacks.

t/06-test_safeties.t

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use strict;
44
use warnings;
55

6-
use Test::More tests => 129;
6+
use Test::More tests => 127;
77
use Net::Jabber::Bot;
88

99
# stuff for mock client object
@@ -62,6 +62,7 @@ my $bot = Net::Jabber::Bot->new({
6262
, out_messages_per_second => $out_messages_per_second
6363
, max_message_size => $max_message_size
6464
, max_messages_per_hour => $max_messages_per_hour
65+
, forum_join_grace => 0
6566
});
6667

6768
isa_ok($bot, "Net::Jabber::Bot");
@@ -70,9 +71,7 @@ is($bot->max_messages_per_hour, $max_messages_per_hour, "Max messages per hour (
7071
is($bot->get_safety_mode, 1, "Validate safety mode is on")
7172
or die("Safety mode is not turning on. Tests will not be valid");
7273

73-
is($bot->forum_join_grace, 10, "Forum Grace is 10 seconds as expected");
74-
ok(1, "Sleeping 12 seconds to make sure we get past initializtion");
75-
ok((sleep 12) > 10, "Making sure the bot get's past initialization (sleep 12)");
74+
is($bot->forum_join_grace, 0, "Forum Grace is 0 seconds as configured");
7675
process_bot_messages();
7776

7877
start_new_test("Testing Group Message bursting is not possible");
@@ -211,7 +210,6 @@ sub start_new_test {
211210
212211
213212
sub process_bot_messages {
214-
sleep 2; # Pause a little to make sure message make it to the server and back.
215213
ok(defined $bot->Process(5), "Processed new messages and didn't lose connection.");
216214
}
217215

t/07-forum_join_grace.t

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!perl
2+
3+
use strict;
4+
use warnings;
5+
use Test::More tests => 5;
6+
use Net::Jabber::Bot;
7+
8+
use FindBin;
9+
use lib "$FindBin::Bin/lib";
10+
use MockJabberClient;
11+
12+
my $bot_alias = 'grace_test_bot';
13+
my $server = 'talk.google.com';
14+
15+
my %forums_and_responses;
16+
my $forum1 = 'test_forum1';
17+
$forums_and_responses{$forum1} = ["jbot:", ""];
18+
19+
my $messages_seen = 0;
20+
21+
# Use a short but nonzero grace period so the test runs fast
22+
my $bot = Net::Jabber::Bot->new({
23+
server => $server,
24+
conference_server => "conference.$server",
25+
port => 5222,
26+
username => 'test_username',
27+
password => 'test_pass',
28+
alias => $bot_alias,
29+
message_function => sub { $messages_seen++ },
30+
background_function => sub {},
31+
loop_sleep_time => 5,
32+
process_timeout => 5,
33+
forums_and_responses => \%forums_and_responses,
34+
ignore_server_messages => 1,
35+
ignore_self_messages => 0,
36+
out_messages_per_second => 5,
37+
max_message_size => 800,
38+
max_messages_per_hour => 100,
39+
forum_join_grace => 2,
40+
});
41+
42+
isa_ok($bot, "Net::Jabber::Bot");
43+
is($bot->forum_join_grace, 2, "Forum join grace set to 2 seconds");
44+
45+
# Enable responding to self messages (safety_mode forces ignore_self_messages on,
46+
# and the mock echoes messages back with the same resource as the bot)
47+
$bot->respond_to_self_messages(1);
48+
49+
# Send a message immediately — should be ignored (within grace period)
50+
my $personal_address = "test_user\@$server/$bot_alias";
51+
$bot->SendPersonalMessage($personal_address, "Hello during grace period");
52+
$bot->Process(1);
53+
is($messages_seen, 0, "Message during grace period is ignored");
54+
55+
# Wait past the grace period
56+
sleep 3;
57+
58+
# Send another message — should be processed now
59+
$messages_seen = 0;
60+
$bot->SendPersonalMessage($personal_address, "Hello after grace period");
61+
$bot->Process(1);
62+
is($messages_seen, 1, "Message after grace period is processed");
63+
64+
# Test that forum_join_grace => 0 means no grace period at all
65+
my $bot2 = Net::Jabber::Bot->new({
66+
server => $server,
67+
conference_server => "conference.$server",
68+
port => 5222,
69+
username => 'test_username2',
70+
password => 'test_pass',
71+
alias => $bot_alias,
72+
message_function => sub { $messages_seen++ },
73+
background_function => sub {},
74+
loop_sleep_time => 5,
75+
process_timeout => 5,
76+
forums_and_responses => \%forums_and_responses,
77+
ignore_server_messages => 1,
78+
ignore_self_messages => 0,
79+
out_messages_per_second => 5,
80+
max_message_size => 800,
81+
max_messages_per_hour => 100,
82+
forum_join_grace => 0,
83+
});
84+
85+
$bot2->respond_to_self_messages(1);
86+
$messages_seen = 0;
87+
$bot2->SendPersonalMessage($personal_address, "Immediate message with zero grace");
88+
$bot2->Process(1);
89+
is($messages_seen, 1, "Message processed immediately when forum_join_grace is 0");

t/07-multiline_messages.t

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use strict;
44
use warnings;
5-
use Test::More tests => 9;
5+
use Test::More tests => 8;
66
use Net::Jabber::Bot;
77

88
use FindBin;
@@ -34,13 +34,11 @@ my $bot = Net::Jabber::Bot->new({
3434
out_messages_per_second => 5,
3535
max_message_size => 800,
3636
max_messages_per_hour => 100,
37+
forum_join_grace => 0,
3738
});
3839

3940
isa_ok($bot, "Net::Jabber::Bot");
4041

41-
# Wait past the forum join grace period
42-
ok((sleep 12) > 10, "Waited past forum join grace period");
43-
4442
# Test 1: Newlines are preserved in sent messages
4543
{
4644
my $multiline_msg = "Line one\nLine two\nLine three";

t/07-test_from_parameter.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use strict;
44
use warnings;
55

6-
use Test::More tests => 10;
6+
use Test::More tests => 9;
77
use Net::Jabber::Bot;
88

99
# stuff for mock client object
@@ -38,10 +38,10 @@ my $bot = Net::Jabber::Bot->new(
3838
out_messages_per_second => 5,
3939
max_message_size => 1000,
4040
max_messages_per_hour => 100,
41+
forum_join_grace => 0,
4142
);
4243

4344
isa_ok( $bot, "Net::Jabber::Bot" );
44-
ok( ( sleep 12 ) > 10, "Wait past initialization grace period" );
4545

4646
# Track the last MessageSend args for verification
4747
my @last_message_send_args;

t/07-test_reconnect_and_leaks.t

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ my $bot = Net::Jabber::Bot->new(
3131
out_messages_per_second => 5,
3232
max_message_size => 800,
3333
max_messages_per_hour => 100,
34+
forum_join_grace => 0,
3435
);
3536

3637
isa_ok( $bot, "Net::Jabber::Bot" );
@@ -57,9 +58,6 @@ ok( defined $process_result, "Process works after reconnect" );
5758
{
5859
my $personal_address = "test_user\@$server/$bot_alias";
5960

60-
# Wait for grace period
61-
sleep 12;
62-
6361
# Simulate sending messages "yesterday" by injecting an old day entry
6462
my $today_yday = ( localtime() )[7];
6563
my $yesterday_yday = $today_yday > 0 ? $today_yday - 1 : 364;

t/lib/MockJabberClient.pm

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,12 @@ sub Process {
3939
return if(!$self->{is_connected}); # Return undef if we're not connected.
4040

4141
foreach my $message (@{$self->{message_queue}}) {
42-
$timeout = 0; # zero out sleep timer;
4342
next if(!defined $self->{message_callback});
4443
$self->{message_callback}->($self->{SESSION}->{id}, $message);
4544
}
46-
47-
45+
4846
@{$self->{message_queue}} = ();
4947

50-
sleep $timeout;
5148
return 1; # undef means we lost connection.
5249
}
5350

0 commit comments

Comments
 (0)