From 9bef1b25cdfe36af8ee29406740aa8f7f703f337 Mon Sep 17 00:00:00 2001 From: Rene Krenn Date: Mon, 2 Jul 2018 13:06:42 +0200 Subject: [PATCH] TT#39612 fix balance lookup for 23:59:59.xxx start/end time Change-Id: I0985a28c1617439016c8b039806ddb0ad2854b25 --- rate-o-mat.pl | 13 ++-- t/Utils/Env.pm | 6 +- t/rateomat-20-basic-onnet.t | 1 + t/rateomat-30-negative-fees.t | 1 + t/rateomat-34-freecash.t | 1 + t/rateomat-42-catchup-discard.t | 1 + t/rateomat-44-last-second.t | 111 ++++++++++++++++++++++++++++++++ t/rateomat-45-split-cdr.t | 1 + 8 files changed, 127 insertions(+), 8 deletions(-) create mode 100644 t/rateomat-44-last-second.t diff --git a/rate-o-mat.pl b/rate-o-mat.pl index f7689d9..be314cf 100755 --- a/rate-o-mat.pl +++ b/rate-o-mat.pl @@ -52,7 +52,7 @@ my $update_prepaid_preference = 1; my $use_customer_real_cost = 0; my $use_provider_real_cost = 0; # don't update balance of prepaid contracts, if no prepaid_costs record is found (re-rating): -my $prepaid_update_balance = 0; +my $prepaid_update_balance = ((defined $ENV{RATEOMAT_PREPAID_UPDATE_BALANCE} && $ENV{RATEOMAT_PREPAID_UPDATE_BALANCE}) ? int $ENV{RATEOMAT_PREPAID_UPDATE_BALANCE} : 0); # control writing cdr relation data: # disable it for now until this will be limited to prepaid contracts, @@ -139,7 +139,7 @@ my $cps_info = { t => 0.0, t_old => 0.0, - dt => 0.0, + dt => 0.0, delay => 0.0, cps => 0.0, @@ -1471,10 +1471,10 @@ sub get_contract_balances { my $start_time = $cdr->{start_time}; my $duration = $cdr->{duration}; - catchup_contract_balance($start_time,$start_time + $duration,$contract_id,$r_package_info); + catchup_contract_balance(int($start_time),int($start_time + $duration),$contract_id,$r_package_info); my $sth = $sth_get_cbalances; - $sth->execute($contract_id, $start_time) + $sth->execute($contract_id, int($start_time)) or FATAL "Error executing get contract balance statement: ".$sth->errstr; my $res = $sth->fetchall_arrayref({}); $sth->finish; @@ -2958,7 +2958,7 @@ sub main { } INFO "Up and running.\n"; - + while (!$shutdown) { $log_fatal = 1; @@ -3090,6 +3090,9 @@ _update_cps(1); # unless ($rated_batch % 5); _update_cps(0); _cps_delay(); } + if ($debug && $split_peak_parts && (scalar @cdrs) < 5) { + sleep $loop_interval; #split peak parts testcase + } $shutdown and last; diff --git a/t/Utils/Env.pm b/t/Utils/Env.pm index f376b59..e5e5550 100644 --- a/t/Utils/Env.pm +++ b/t/Utils/Env.pm @@ -13,9 +13,9 @@ our @EXPORT_OK = qw( ); ## no critic (Variables::RequireLocalizedPunctuationVars) -#$ENV{RATEOMAT_BILLING_DB_HOST} = '192.168.0.84'; -#$ENV{RATEOMAT_PROVISIONING_DB_HOST} = '192.168.0.84'; -#$ENV{RATEOMAT_ACCOUNTING_DB_HOST} = '192.168.0.84'; +#$ENV{RATEOMAT_BILLING_DB_HOST} = '192.168.0.29'; +#$ENV{RATEOMAT_PROVISIONING_DB_HOST} = '192.168.0.29'; +#$ENV{RATEOMAT_ACCOUNTING_DB_HOST} = '192.168.0.29'; $ENV{CATALYST_SERVER} = 'https://127.0.0.1:1443'; diff --git a/t/rateomat-20-basic-onnet.t b/t/rateomat-20-basic-onnet.t index 9e0c75d..406754b 100644 --- a/t/rateomat-20-basic-onnet.t +++ b/t/rateomat-20-basic-onnet.t @@ -18,6 +18,7 @@ use Test::More; ### and cash balance interval values. local $ENV{RATEOMAT_WRITE_CDR_RELATION_DATA} = 1; +local $ENV{RATEOMAT_PREPAID_UPDATE_BALANCE} = 1; my $init_secs = 60; my $follow_secs = 30; diff --git a/t/rateomat-30-negative-fees.t b/t/rateomat-30-negative-fees.t index f187ada..da72965 100644 --- a/t/rateomat-30-negative-fees.t +++ b/t/rateomat-30-negative-fees.t @@ -19,6 +19,7 @@ use Test::More; ### balance. local $ENV{RATEOMAT_WRITE_CDR_RELATION_DATA} = 1; +local $ENV{RATEOMAT_PREPAID_UPDATE_BALANCE} = 1; use Text::Table; use Text::Wrap; diff --git a/t/rateomat-34-freecash.t b/t/rateomat-34-freecash.t index 31c2ae5..ecb476e 100644 --- a/t/rateomat-34-freecash.t +++ b/t/rateomat-34-freecash.t @@ -18,6 +18,7 @@ use Test::More; ### for both prepaid and postpaid (new). local $ENV{RATEOMAT_WRITE_CDR_RELATION_DATA} = 1; +local $ENV{RATEOMAT_PREPAID_UPDATE_BALANCE} = 1; Utils::Env::set_local_timezone('UTC','+00:00'); #'UTC'; #vagrant SYSTEM timezone is "Etc/UTC" my $init_secs = 50; diff --git a/t/rateomat-42-catchup-discard.t b/t/rateomat-42-catchup-discard.t index 5361e34..4b926b0 100644 --- a/t/rateomat-42-catchup-discard.t +++ b/t/rateomat-42-catchup-discard.t @@ -21,6 +21,7 @@ use Storable qw(); ### note: this tests takes longer time to complete local $ENV{RATEOMAT_WRITE_CDR_RELATION_DATA} = 1; +local $ENV{RATEOMAT_PREPAID_UPDATE_BALANCE} = 1; Utils::Api::set_time(Utils::Api::get_now->subtract(months => 5)); #provider contract needs to be created in the past as well: diff --git a/t/rateomat-44-last-second.t b/t/rateomat-44-last-second.t new file mode 100644 index 0000000..3a7c1e4 --- /dev/null +++ b/t/rateomat-44-last-second.t @@ -0,0 +1,111 @@ + +use strict; +use warnings; + +use File::Basename; +use Cwd; +use lib Cwd::abs_path(File::Basename::dirname(__FILE__)); + +use DateTime qw(); +use Utils::Api qw(); +use Utils::Rateomat qw(); +use Test::More; + +### testcase outline: +### test calls starting/ending at 23:59:59.xxx + +my $provider = create_provider(); +{ + + my $callee = Utils::Api::setup_subscriber($provider,$provider->{subscriber_fees}->[0]->{profile},undef,{ cc => 888, ac => '2', sn => '' }); + my $caller = Utils::Api::setup_subscriber($provider,$provider->{subscriber_fees}->[0]->{profile},undef,{ cc => 888, ac => '1', sn => '' }); + my $costs_initial = $provider->{subscriber_fees}->[0]->{fee}->{onpeak_init_rate} * + $provider->{subscriber_fees}->[0]->{fee}->{onpeak_init_interval}; + + my $now = Utils::Api::get_now(); + my $start = $now->clone->add(months => 1)->truncate(to => 'month')->subtract(seconds => 2)->epoch + 0.567; + my $duration = 1.0; + + 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',$start,$duration), + ]) }; + + if (ok((scalar @cdr_ids) > 0 && Utils::Rateomat::run_rateomat_threads(),'rate-o-mat executed')) { + ok(Utils::Rateomat::check_cdrs('', + map { $_ => { + id => $_, + rating_status => 'ok', + source_customer_cost => Utils::Rateomat::decimal_to_string($costs_initial) }; + } @cdr_ids + ),'cdrs were all processed'); + my $label = 'cdr with end time at '.Utils::Api::datetime_to_string(DateTime->from_epoch(epoch => ($start + $duration))).': '; + Utils::Api::check_interval_history($label,$caller->{customer}->{id},[ + { start => Utils::Api::datetime_to_string($now->clone->truncate(to => 'month')), + stop => Utils::Api::datetime_to_string($now->clone->add(months => 1)->truncate(to => 'month')->subtract(seconds => 1)), + }, + ]); + } +} + +{ + + my $amount = 5; + my $callee = Utils::Api::setup_subscriber($provider,$provider->{subscriber_fees}->[0]->{profile},undef,{ cc => 888, ac => '2', sn => '' }); + my $caller = Utils::Api::setup_subscriber($provider,$provider->{subscriber_fees}->[0]->{profile},$amount,{ cc => 888, ac => '1', sn => '' }); + my $costs_initial = ($provider->{subscriber_fees}->[0]->{fee}->{onpeak_init_rate} * + $provider->{subscriber_fees}->[0]->{fee}->{onpeak_init_interval}) / 100.0; + + my $now = Utils::Api::get_now(); + my $start = $now->clone->add(months => 1)->truncate(to => 'month')->subtract(seconds => 1)->epoch + 0.567; + + 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',$start,1), + ]) }; + + if (ok((scalar @cdr_ids) > 0 && Utils::Rateomat::run_rateomat_threads(),'rate-o-mat executed')) { + ok(Utils::Rateomat::check_cdrs('', + map { $_ => { + id => $_, + rating_status => 'ok', }; + } @cdr_ids + ),'cdrs were all processed'); + my $label = 'cdr with start time at '.Utils::Api::datetime_to_string(DateTime->from_epoch(epoch => $start)).': '; + Utils::Api::check_interval_history($label,$caller->{customer}->{id},[ + { start => Utils::Api::datetime_to_string($now->clone->truncate(to => 'month')), + stop => Utils::Api::datetime_to_string($now->clone->add(months => 1)->truncate(to => 'month')->subtract(seconds => 1)), + cash => $amount - $costs_initial, + }, + { start => Utils::Api::datetime_to_string($now->clone->add(months => 1)->truncate(to => 'month')), + stop => Utils::Api::datetime_to_string($now->clone->add(months => 2)->truncate(to => 'month')->subtract(seconds => 1)), + cash => $amount - $costs_initial, + }, + ]); + } + +} + +done_testing(); +exit; + +sub create_provider { + return Utils::Api::setup_provider('test.com', + [ #rates: + { #initial: + onpeak_init_rate => 2, + onpeak_init_interval => 60, + onpeak_follow_rate => 1, + onpeak_follow_interval => 30, + offpeak_init_rate => 2, + offpeak_init_interval => 60, + offpeak_follow_rate => 1, + offpeak_follow_interval => 30, + }, + ], + [ #billing networks: + ] + ); +} diff --git a/t/rateomat-45-split-cdr.t b/t/rateomat-45-split-cdr.t index 1455e87..fc01e2b 100644 --- a/t/rateomat-45-split-cdr.t +++ b/t/rateomat-45-split-cdr.t @@ -22,6 +22,7 @@ use List::Util qw(); local $ENV{RATEOMAT_WRITE_CDR_RELATION_DATA} = 1; local $ENV{RATEOMAT_SPLIT_PEAK_PARTS} = 1; +local $ENV{RATEOMAT_PREPAID_UPDATE_BALANCE} = 1; local $ENV{RATEOMAT_BATCH_SIZE} = 1; #use Text::Table;