added test for run_continuation

This commit is contained in:
Matt Simerson 2014-09-16 20:30:36 -07:00
parent 0499317912
commit 9a38850f13
2 changed files with 39 additions and 21 deletions

View File

@ -269,26 +269,28 @@ sub run_hooks {
sub run_hooks_no_respond { sub run_hooks_no_respond {
my ($self, $hook) = (shift, shift); my ($self, $hook) = (shift, shift);
if ($hooks->{$hook}) { return (0, '') if !$hooks->{$hook};
my @r;
for my $code ($self->hooks($hook)) { my @r;
eval { (@r) = $code->{code}->($self, $self->transaction, @_); }; for my $code (@{$hooks->{$hook}}) {
if ($@) { eval { (@r) = $code->{code}->($self, $self->transaction, @_); };
warn("FATAL PLUGIN ERROR [" . $code->{name} . "]: ", $@); if ($@) {
next; warn("FATAL PLUGIN ERROR [" . $code->{name} . "]: ", $@);
} next;
last unless $r[0] == DECLINED;
} }
$r[0] = DECLINED if not defined $r[0]; if ($r[0] == YIELD) {
return @r; die "YIELD not valid from $hook hook";
}
last if $r[0] != DECLINED;
} }
return (0, ''); $r[0] = DECLINED if not defined $r[0];
return @r;
} }
sub run_continuation { sub run_continuation {
my $self = shift; my $self = shift;
die "No continuation in progress" if !$self->{_continuation}; die "No continuation in progress\n" if !$self->{_continuation};
my $todo = $self->{_continuation}; my $todo = $self->{_continuation};
$self->{_continuation} = undef; $self->{_continuation} = undef;
my $hook = shift @$todo or die "No hook in the continuation"; my $hook = shift @$todo or die "No hook in the continuation";
@ -301,7 +303,7 @@ sub run_continuation {
$self->varlog(LOGDEBUG, $hook, $name); $self->varlog(LOGDEBUG, $hook, $name);
my $tran = $self->transaction; my $tran = $self->transaction;
eval { (@r) = $code->{code}->($self, $tran, @$args); }; eval { @r = $code->{code}->($self, $tran, @$args); };
if ($@) { if ($@) {
$self->log(LOGCRIT, "FATAL PLUGIN ERROR [$name]: ", $@); $self->log(LOGCRIT, "FATAL PLUGIN ERROR [$name]: ", $@);
next; next;
@ -313,7 +315,6 @@ sub run_continuation {
next; next;
} }
# note this is wrong as $tran is always true in the current code...
if ($tran) { if ($tran) {
my $tnotes = $tran->notes($name); my $tnotes = $tran->notes($name);
if (!defined $tnotes || ref $tnotes eq 'HASH') { if (!defined $tnotes || ref $tnotes eq 'HASH') {
@ -328,20 +329,20 @@ sub run_continuation {
} }
if ( $r[0] == DENY if ( $r[0] == DENY
or $r[0] == DENYSOFT || $r[0] == DENYSOFT
or $r[0] == DENY_DISCONNECT || $r[0] == DENY_DISCONNECT
or $r[0] == DENYSOFT_DISCONNECT) || $r[0] == DENYSOFT_DISCONNECT)
{ {
$r[1] = '' if !defined $r[1]; $r[1] = '' if !defined $r[1];
$self->log(LOGDEBUG, $log_msg . return_code($r[0]) . ", $r[1]"); $self->log(LOGDEBUG, $log_msg . return_code($r[0]) . ", $r[1]");
if ($hook ne 'deny') { if ($hook ne 'deny') {
$self->run_hooks_no_respond("deny", $name, $r[0], $r[1]) $self->run_hooks_no_respond('deny', $name, $r[0], $r[1]);
}; };
} }
else { else {
$r[1] = '' if not defined $r[1]; $r[1] = '' if not defined $r[1];
$self->log(LOGDEBUG, $log_msg . return_code($r[0]) . ", $r[1]"); $self->log(LOGDEBUG, $log_msg . return_code($r[0]) . ", $r[1]");
$self->run_hooks_no_respond("ok", $name, $r[0], $r[1]) if $hook ne "ok"; $self->run_hooks_no_respond('ok', $name, $r[0], $r[1]) if $hook ne 'ok';
} }
last if $r[0] != DECLINED; last if $r[0] != DECLINED;

View File

@ -26,7 +26,8 @@ __hooks();
__register_hook(); __register_hook();
__hook_responder(); __hook_responder();
#done_testing() and exit; __run_continuation();
done_testing() and exit;
__temp_file(); __temp_file();
__temp_dir(); __temp_dir();
@ -65,6 +66,22 @@ sub __hooks_none {
is_deeply($r, [], 'hooks, empty, specified'); is_deeply($r, [], 'hooks, empty, specified');
} }
sub __run_continuation {
my $r;
eval { $smtpd->run_continuation };
ok($@, "run_continuation w/o continuation: " . $@);
my @local_hooks = @{$Qpsmtpd::hooks->{'connect'}};
$smtpd->{_continuation} = ['connect', [DECLINED, "test mess"], @local_hooks];
eval { $r = $smtpd->run_continuation };
ok(!$@, "run_continuation with a continuation doesn't throw exception");
is($r->[0], 220, "hook_responder, code");
ok($r->[1] =~ /ESMTP qpsmtpd/, "hook_responder, message: ". $r->[1]);
#print Data::Dumper::Dumper(@r);
}
sub __hook_responder { sub __hook_responder {
my ($code, $msg) = $qp->hook_responder('test-hook', ['test code','test mesg']); my ($code, $msg) = $qp->hook_responder('test-hook', ['test code','test mesg']);
is($code, 'test code', "hook_responder, code"); is($code, 'test code', "hook_responder, code");