From 6365e3a66e1a1048dad607acc0a92438f3d4a491 Mon Sep 17 00:00:00 2001 From: Robert Date: Sat, 14 Mar 2009 00:31:18 -0700 Subject: [PATCH] Updates to the random_error sample plugin from David Nicol Signed-off-by: Robert --- plugins/random_error | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/plugins/random_error b/plugins/random_error index 56660bf..7585ed1 100644 --- a/plugins/random_error +++ b/plugins/random_error @@ -30,16 +30,31 @@ sub register { sub NEXT() { DECLINED } sub random_fail { - my $self = shift; - my $fpct = $self->qp->connection->notes('random_fail_%'); - rand(100) > ($fpct / 6) and return NEXT; + my $fpct = $_[0]->qp->connection->notes('random_fail_%'); + +=head calculating the probability of failure + +There are six tests a message must pass to reach the queueing stage, and we wish to +provide random failure for each one, with the combined probability being out +configuration argument. So we want to solve this equation: + + (1-x) ** 6 = ( 1 - input_number ) + +or + + x = 1 - ( (1 - input_number ) ** (1/6) ) + +=cut + my $successp = 1 - ($fpct / 100); + $_[0]->log(LOGINFO, "to fail, rand(1) must be more than ". ($successp ** (1 / 6)) ); + rand(1) < ($successp ** (1 / 6)) and return NEXT; rand(5) < 2 and return (DENYSOFT_DISCONNECT, "random failure"); return (DENYSOFT, "random failure"); } sub hook_connect { - $self->qp->connection->notes('random_fail_%', $self->{__PACKAGE__.'_how'}); + $_[0]->qp->connection->notes('random_fail_%', $_[0]->{__PACKAGE__.'_how'}); goto &random_fail }