All checks were successful
check / check (push) Successful in 4s
Canonical v2-schema `.golangci.yml`, golangci-lint pins bumped to v2.12.2 in `Dockerfile` and `script/bootstrap`, and the tree brought to `0 issues.` under it. Three behaviour deltas: `Cache.StoreVariant` takes a context (cancelled requests skip the accounting row, recovered by reconciliation); `MetadataStorage.Store` no longer leaks `.tmp-*.json` on Write/Close/Rename failure (dead-defer bug fix); the `signing_key` too-short error text gained a `value too short:` prefix. Eviction-loop context cancellation deferred to #102.
48 lines
1.0 KiB
Go
48 lines
1.0 KiB
Go
package imgcache
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"testing"
|
|
)
|
|
|
|
func TestSizePercentSafeWithZeroFetchBytes(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// Simulate the calculation from processAndStore
|
|
fetchBytes := int64(0)
|
|
outputSize := int64(100)
|
|
|
|
var sizePercent float64
|
|
if fetchBytes > 0 {
|
|
sizePercent = float64(outputSize) / float64(fetchBytes) * 100.0
|
|
}
|
|
|
|
// sizePercent should be 0, not Inf or NaN
|
|
if math.IsInf(sizePercent, 0) || math.IsNaN(sizePercent) {
|
|
t.Errorf("sizePercent = %f, should not be Inf/NaN", sizePercent)
|
|
}
|
|
|
|
// Should produce valid log output
|
|
result := fmt.Sprintf("%.1f%%", sizePercent)
|
|
if result != "0.0%" {
|
|
t.Errorf("formatted size ratio = %q, want %q", result, "0.0%")
|
|
}
|
|
}
|
|
|
|
func TestSizePercentNormalCase(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
fetchBytes := int64(1000)
|
|
outputSize := int64(500)
|
|
|
|
var sizePercent float64
|
|
if fetchBytes > 0 {
|
|
sizePercent = float64(outputSize) / float64(fetchBytes) * 100.0
|
|
}
|
|
|
|
if math.Abs(sizePercent-50.0) > 0.001 {
|
|
t.Errorf("sizePercent = %f, want 50.0", sizePercent)
|
|
}
|
|
}
|