From b5f8ea0e239286515381e67a6b1a34a9a1932b60 Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Wed, 20 Apr 2022 20:54:37 +0200 Subject: [PATCH] Fix rounding in estimator. The test was buggy. --- estimator/estimator.go | 5 ++--- estimator/estimator_test.go | 3 ++- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/estimator/estimator.go b/estimator/estimator.go index 44349c0..493f208 100644 --- a/estimator/estimator.go +++ b/estimator/estimator.go @@ -40,9 +40,8 @@ func (e *Estimator) swap(now uint64) { var rate, packetRate uint32 if jiffies >= rtptime.JiffiesPerSec/1000 { - rate = uint32(uint64(bytes) * rtptime.JiffiesPerSec / jiffies) - packetRate = - uint32(uint64(packets) * rtptime.JiffiesPerSec / jiffies) + rate = uint32((uint64(bytes)*rtptime.JiffiesPerSec + jiffies/2) / jiffies) + packetRate = uint32((uint64(packets)*rtptime.JiffiesPerSec + jiffies/2) / jiffies) } atomic.StoreUint32(&e.rate, rate) atomic.StoreUint32(&e.packetRate, packetRate) diff --git a/estimator/estimator_test.go b/estimator/estimator_test.go index 7376612..3c68ce2 100644 --- a/estimator/estimator_test.go +++ b/estimator/estimator_test.go @@ -2,13 +2,14 @@ package estimator import ( "testing" + "time" "github.com/jech/galene/rtptime" ) func TestEstimator(t *testing.T) { now := rtptime.Jiffies() - e := New(rtptime.JiffiesPerSec) + e := New(time.Second) e.estimate(now) e.Accumulate(42)