Skip to content

Commit 45862ee

Browse files
toddr-botclaude
andcommitted
fix: eliminate unnecessary sleeps from mock test suite
The test suite took ~120s primarily because five test files each sleep(12) to wait past the forum_join_grace default, and MockJabberClient::Process slept for the full timeout when the message queue was empty. Three changes cut runtime to ~60s: 1. Add NonNegNum type and use it for forum_join_grace, allowing 0 2. Set forum_join_grace => 0 in all mock tests, remove sleep(12) calls 3. Remove the sleep($timeout) from MockJabberClient::Process — a mock has no server to wait for Adds t/07-forum_join_grace.t to explicitly test grace period behavior with a short (2s) value, covering both the "ignored during grace" and "processed after grace" cases, plus forum_join_grace => 0. The remaining ~60s is legitimate rate-limiting (flood protection tests). Closes #32 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a7258c4 commit 45862ee

File tree

8 files changed

+103
-23
lines changed

8 files changed

+103
-23
lines changed

lib/Net/Jabber/Bot.pm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package Net::Jabber::Bot;
22

33
use Moose;
4-
use MooseX::Types -declare => [qw( JabberClientObject PosInt PosNum HundredInt )];
4+
use MooseX::Types -declare => [qw( JabberClientObject PosInt PosNum NonNegNum HundredInt )];
55

66
# import builtin types
77
use MooseX::Types::Moose qw/Int HashRef Str Maybe ArrayRef Bool CodeRef Object Num/;
@@ -20,6 +20,7 @@ subtype JabberClientObject, as Object, where { $_->isa('Net::Jabber::Client') };
2020

2121
subtype PosInt, as Int, where { $_ > 0 };
2222
subtype PosNum, as Num, where { $_ > 0 };
23+
subtype NonNegNum, as Num, where { $_ >= 0 };
2324
subtype HundredInt, as Num, where { $_ > 100 };
2425

2526
has jabber_client => (
@@ -32,7 +33,7 @@ has jabber_client => (
3233

3334
has 'client_session_id' => ( isa => Str, is => 'rw' );
3435
has 'connect_time' => ( isa => PosInt, is => 'rw', default => 9_999_999_999 );
35-
has 'forum_join_grace' => ( isa => PosNum, is => 'rw', default => 10 );
36+
has 'forum_join_grace' => ( isa => NonNegNum, is => 'rw', default => 10 );
3637
has 'server_host' => ( isa => Str, is => 'rw', lazy => 1, default => sub { shift->server } );
3738
has 'server' => ( isa => Str, is => 'rw' );
3839
has 'port' => ( isa => PosInt, is => 'rw', default => 5222 );

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)