MT#16633 launch multiple rateomat instances in tests

Change-Id: I55ee6cf78834e2ad144185c279171d3f12a4cd46
changes/11/3611/5
Rene Krenn 10 years ago
parent f17251442e
commit edacb0e8f2

@ -1,5 +1,7 @@
package Utils::Rateomat;
use threads 'exit' => 'threads_only';
use strict;
use DBI;
use Test::More;
@ -7,10 +9,12 @@ use Test::More;
use Data::Dumper;
use Time::HiRes qw();
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(
run_rateomat
run_rateomat_threads
create_cdrs
get_cdrs
prepare_cdr
@ -74,6 +78,54 @@ sub run_rateomat {
return 1;
}
sub run_rateomat_threads {
my ($num_of_threads,$timeout) = @_;
$timeout //= $rateomat_timeout;
$num_of_threads //= 1;
my @workers = ();
foreach my $i (0..$num_of_threads - 1) {
push(@workers,threads->create(sub {
my $tid = threads->tid();
diag("running rate-o-mat at $rateomat_pl as thread $tid" . ($timeout ? ' for '.$timeout.' secs' : ''));
local $SIG{KILL} = sub {
diag("timeout for rate-o-mat thread $tid");
threads->exit();
} if defined $timeout;
eval {
die($!) if !defined do $rateomat_pl;
return 1;
};
if ($@) {
diag("rate-o-mat thread $tid: " . $@);
return 0;
}
return 1;
}));
}
my @threads = @workers;
push(@threads,threads->create(sub {
my $tid = threads->tid();
#diag("timeout thread $tid started");
#for (my $i = $timeout; $i > 0; $i--) {
# sleep(1);
# diag("timeout thread $tid: $i secs left");
#}
sleep($timeout);
foreach my $w (@workers) {
$w->kill('KILL');
#diag('rate-o-mat thread ' . $w->tid() . ' killed');
}
return 1;
})) if defined $timeout;
my $result = 1;
foreach my $t (@threads) {
my $t_result = $t->join();
$result &= (defined $t_result ? $t_result : 1);
#diag('thread ' . $t->tid() . ' joined');
}
return $result;
}
sub create_cdrs {
my ($cdrs) = @_;
my $is_ary = 'ARRAY' eq ref $cdrs;
@ -443,4 +495,4 @@ sub _rollback_transaction {
}
}
1;
1;

@ -0,0 +1,74 @@
use strict;
use Utils::Api qw();
use Utils::Rateomat qw();
use Test::More;
{
Utils::Rateomat::run_rateomat_threads(3,6);
}
#{
# my $provider = create_provider();
# my $profile = $provider->{profiles}->[0]->{profile};
# my $balance = 5;
# my $caller = Utils::Api::setup_subscriber($provider,$profile,$balance,{ cc => 888, ac => '1<n>', sn => '<t>' });
# my $callee = Utils::Api::setup_subscriber($provider,$profile,$balance,{ cc => 888, ac => '2<n>', sn => '<t>' });
# my $caller_costs = ($provider->{profiles}->[0]->{fees}->[0]->{onpeak_init_rate} *
# $provider->{profiles}->[0]->{fees}->[0]->{onpeak_init_interval} +
# $provider->{profiles}->[0]->{fees}->[0]->{onpeak_follow_rate} *
# $provider->{profiles}->[0]->{fees}->[0]->{onpeak_follow_interval})/100.0;
# my $callee_costs = ($provider->{profiles}->[0]->{fees}->[1]->{onpeak_init_rate} *
# $provider->{profiles}->[0]->{fees}->[1]->{onpeak_init_interval} +
# $provider->{profiles}->[0]->{fees}->[1]->{onpeak_follow_rate} *
# $provider->{profiles}->[0]->{fees}->[1]->{onpeak_follow_interval})/100.0; #negative!
#
# my $now = Utils::Api::get_now();
# my @cdr_ids = map { $_->{id}; } @{ Utils::Rateomat::create_cdrs([
# Utils::Rateomat::prepare_cdr($caller->{subscriber},undef,$caller->{reseller},
# $callee->{subscriber},undef,$callee->{reseller},
# '192.168.0.1',$now->epoch,61),
# ]) };
#
# if (ok((scalar @cdr_ids) > 0 && Utils::Rateomat::run_rateomat(),'rate-o-mat executed')) {
# ok(Utils::Rateomat::check_cdrs('',
# map { $_ => { id => $_, rating_status => 'ok', }; } @cdr_ids
# ),'cdrs were all processed');
# Utils::Api::check_interval_history('negative fees - caller',$caller->{customer}->{id},[
# { cash => $balance - $caller_costs,
# profile => $provider->{profiles}->[0]->{profile}->{id} },
# ]);
# Utils::Api::check_interval_history('negative fees - callee',$callee->{customer}->{id},[
# { cash => $balance - $callee_costs,
# profile => $provider->{profiles}->[0]->{profile}->{id} },
# ]);
# }
#
#}
done_testing();
exit;
sub create_provider {
my $rate_interval = shift;
$rate_interval //= 60;
return Utils::Api::setup_provider('test<n>.com',
[ #rates:
{ #any
onpeak_init_rate => 2,
onpeak_init_interval => $rate_interval,
onpeak_follow_rate => 1,
onpeak_follow_interval => $rate_interval,
offpeak_init_rate => 2,
offpeak_init_interval => $rate_interval,
offpeak_follow_rate => 1,
offpeak_follow_interval => $rate_interval,
},
],
[ #billing networks:
]
);
}
Loading…
Cancel
Save