update
Some checks failed
Pipeline: Test, Lint, Build / Get version info (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test JS code (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint i18n files (push) Has been cancelled
Pipeline: Test, Lint, Build / Check Docker configuration (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to GHCR (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Has been cancelled
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Has been cancelled
Pipeline: Test, Lint, Build / Build Windows installers (push) Has been cancelled
Pipeline: Test, Lint, Build / Package/Release (push) Has been cancelled
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
POEditor import / update-translations (push) Has been cancelled

This commit is contained in:
2025-12-08 16:16:23 +01:00
commit c251f174ed
1349 changed files with 194301 additions and 0 deletions

34
utils/chrono/meter.go Normal file
View File

@@ -0,0 +1,34 @@
package chrono
import (
"time"
. "github.com/navidrome/navidrome/utils/gg"
)
// Meter is a simple stopwatch
type Meter struct {
elapsed time.Duration
mark *time.Time
}
func (m *Meter) Start() {
m.mark = P(time.Now())
}
func (m *Meter) Stop() time.Duration {
if m.mark == nil {
return m.elapsed
}
m.elapsed += time.Since(*m.mark)
m.mark = nil
return m.elapsed
}
func (m *Meter) Elapsed() time.Duration {
elapsed := m.elapsed
if m.mark != nil {
elapsed += time.Since(*m.mark)
}
return elapsed
}

View File

@@ -0,0 +1,91 @@
package chrono_test
import (
"testing"
"time"
"github.com/navidrome/navidrome/tests"
. "github.com/navidrome/navidrome/utils/chrono"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestChrono(t *testing.T) {
tests.Init(t, false)
RegisterFailHandler(Fail)
RunSpecs(t, "Chrono Suite")
}
// Note: These tests use longer sleep durations and generous tolerances to avoid flakiness
// due to system scheduling delays. For a more elegant approach in the future, consider
// using Go 1.24's experimental testing/synctest package with GOEXPERIMENT=synctest.
var _ = Describe("Meter", func() {
var meter *Meter
BeforeEach(func() {
meter = &Meter{}
})
Describe("Stop", func() {
It("should return the elapsed time", func() {
meter.Start()
time.Sleep(50 * time.Millisecond)
elapsed := meter.Stop()
// Use generous tolerance to account for system scheduling delays
Expect(elapsed).To(BeNumerically(">=", 30*time.Millisecond))
Expect(elapsed).To(BeNumerically("<=", 200*time.Millisecond))
})
It("should accumulate elapsed time on multiple starts and stops", func() {
// First cycle
meter.Start()
time.Sleep(50 * time.Millisecond)
firstElapsed := meter.Stop()
// Second cycle
meter.Start()
time.Sleep(50 * time.Millisecond)
totalElapsed := meter.Stop()
// Test that time accumulates (second measurement should be greater than first)
Expect(totalElapsed).To(BeNumerically(">", firstElapsed))
// Test that accumulated time is reasonable (should be roughly double the first)
Expect(totalElapsed).To(BeNumerically(">=", time.Duration(float64(firstElapsed)*1.5)))
Expect(totalElapsed).To(BeNumerically("<=", firstElapsed*3))
// Sanity check: total should be at least 60ms (allowing for some timing variance)
Expect(totalElapsed).To(BeNumerically(">=", 60*time.Millisecond))
})
})
Describe("Elapsed", func() {
It("should return the total elapsed time", func() {
meter.Start()
time.Sleep(50 * time.Millisecond)
meter.Stop()
// Should not count the time the meter was stopped
time.Sleep(50 * time.Millisecond)
meter.Start()
time.Sleep(50 * time.Millisecond)
meter.Stop()
elapsed := meter.Elapsed()
// Should be roughly 100ms (2 x 50ms), but allow for significant variance
Expect(elapsed).To(BeNumerically(">=", 60*time.Millisecond))
Expect(elapsed).To(BeNumerically("<=", 300*time.Millisecond))
})
It("should include the current running time if started", func() {
meter.Start()
time.Sleep(50 * time.Millisecond)
elapsed := meter.Elapsed()
// Use generous tolerance to account for system scheduling delays
Expect(elapsed).To(BeNumerically(">=", 30*time.Millisecond))
Expect(elapsed).To(BeNumerically("<=", 200*time.Millisecond))
})
})
})