Browse Source

include timer handling in test lib

Change-Id: Ib67a5ea3e6fc1cfa9a34e0a30ee13f7116b2fd73
changes/34/5834/1
Richard Fuchs 10 years ago
parent
commit
d8d5e1bec9
3 changed files with 29 additions and 32 deletions
  1. +14
    -5
      utils/Rtpengine.pm
  2. +15
    -0
      utils/test-basic.pl
  3. +0
    -27
      utils/test3.pl

+ 14
- 5
utils/Rtpengine.pm View File

@ -66,7 +66,7 @@ sub answer {
package Rtpengine::Test;
sub new {
my ($class, $callback) = @_;
my ($class) = @_;
my $self = {};
bless $self, $class;
@ -95,12 +95,11 @@ sub new {
$self->{mux}->set_callback_object($self);
$self->{media_port} = 2000;
$self->{timers} = [];
$self->{rtpe} = Rtpengine->new('localhost', 2223);
$self->{callid} = rand();
$self->{callback} = $callback;
return $self;
};
@ -115,6 +114,12 @@ sub run {
$self->{mux}->loop();
}
sub timer_once {
my ($self, $delay, $sub) = @_;
push(@{$self->{timers}}, { sub => $sub, when => time() + $delay });
@{$self->{timers}} = sort {$a->{when} <=> $b->{when}} @{$self->{timers}};
}
sub mux_input {
my ($self, $mux, $fh, $input) = @_;
@ -124,9 +129,13 @@ sub mux_input {
sub mux_timeout {
my ($self, $mux, $fh) = @_;
$self->{callback}->(time());
$mux->set_timeout($fh, 0.01);
my $now = time();
while (@{$self->{timers}} && $self->{timers}->[0]->{when} <= $now) {
my $t = shift(@{$self->{timers}});
$t->{sub}->();
}
}


+ 15
- 0
utils/test-basic.pl View File

@ -0,0 +1,15 @@
#!/usr/bin/perl
use strict;
use warnings;
use Rtpengine;
my $r = Rtpengine::Test->new();
my $a = $r->client();
my $b = $r->client();
$r->timer_once(3, sub { $b->answer($a) });
$a->offer($b);
$r->run();

+ 0
- 27
utils/test3.pl View File

@ -1,27 +0,0 @@
#!/usr/bin/perl
use strict;
use warnings;
use Rtpengine;
use Time::HiRes qw(time);
my ($r, $a, $b);
my $offer = time();
my $answer = $offer + 3;
my $answer_done = 0;
my $cb = sub {
my ($now) = @_;
if ($now >= $answer && !$answer_done) {
$b->answer($a);
$answer_done = 1;
}
};
$r = Rtpengine::Test->new($cb);
$a = $r->client();
$b = $r->client();
$a->offer($b);
$r->run();

Loading…
Cancel
Save