ICMP tests and various fixes (#68)

* various icmp fixes

* improve existing tests
This commit is contained in:
Pat Whittingslow
2026-04-09 12:31:31 -03:00
committed by GitHub
parent 80e4c59ae7
commit 8813ccef82
7 changed files with 212 additions and 43 deletions
+9 -13
View File
@@ -5,7 +5,6 @@ import (
"encoding/binary"
"math"
"math/bits"
"sync"
"time"
"github.com/soypat/lneto"
@@ -265,24 +264,21 @@ func (d Date) Time() (time.Time, error) {
return baseTime.Add(off), nil
}
var (
ntpOnceSystemClock sync.Once
sysPrec int8
)
// CalculateSystemPrecision calculates the NTP system precision for a time source.
// If the time source is nil the default static call to [time.Now] is used.
func CalculateSystemPrecision(now func() time.Time, iters []time.Time) int8 {
// If the time source is nil the default static call to [time.Now]->[time.Time.UnixNano] is used.
func CalculateSystemPrecision(nowNano func() int64, iters []int64) int8 {
maxIter := len(iters)
if now == nil {
if nowNano == nil {
for i := 0; i < maxIter; i++ {
iters[i] = time.Now()
iters[i] = time.Now().UnixNano()
}
} else {
for i := 0; i < maxIter; i++ {
iters[i] = now()
iters[i] = nowNano()
}
}
avg := iters[maxIter-1].Sub(iters[0]) / time.Duration(maxIter)
return int8(math.Log2(avg.Seconds()))
const seconds = 1_000_000_000 // nanoseconds
avg := (iters[maxIter-1] - iters[0]) / int64(maxIter)
avgSeconds := float64(avg) / seconds
return int8(math.Log2(avgSeconds))
}