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
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:
91
utils/chrono/meter_test.go
Normal file
91
utils/chrono/meter_test.go
Normal 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))
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user