go-watchdog/watermarks_test.go
Raúl Kripalani 4558d98653 major rewrite of go-watchdog.
This commit introduces a major rewrite of go-watchdog.

* HeapDriven and SystemDriven are now distinct run modes.
* WIP ProcessDriven that uses cgroups.
* Policies are now stateless, pure and greatly simplified.
* Policies now return the next utilization at which GC
  should run. The watchdog enforces that value differently
  depending on the run mode.
* The heap-driven run mode adjusts GOGC dynamically. This
  places the responsibility on the Go runtime to honour the
  trigger point, and results in more robust logic that is not
  vulnerable to very quick bursts within sampling periods.
* The heap-driven run mode is no longer polling (interval-driven).
  Instead, it relies entirely on GC signals.
* The Silence and Emergency features of the watermark policy
  have been removed. If utilization is above the last watermark,
  the policy will request immediate GC.
* Races removed.
2020-12-08 14:19:04 +00:00

62 lines
1.8 KiB
Go

package watchdog
import (
"testing"
"github.com/raulk/clock"
"github.com/stretchr/testify/require"
)
var (
watermarks = []float64{0.50, 0.75, 0.80}
thresholds = func() []uint64 {
var ret []uint64
for _, w := range watermarks {
ret = append(ret, uint64(float64(limit)*w))
}
return ret
}()
)
func TestProgressiveWatermarks(t *testing.T) {
clk := clock.NewMock()
Clock = clk
p, err := NewWatermarkPolicy(watermarks...)(limit)
require.NoError(t, err)
// at zero
next, immediate := p.Evaluate(UtilizationSystem, uint64(0))
require.False(t, immediate)
require.EqualValues(t, thresholds[0], next)
// before the watermark.
next, immediate = p.Evaluate(UtilizationSystem, uint64(float64(limit)*watermarks[0])-1)
require.False(t, immediate)
require.EqualValues(t, thresholds[0], next)
// exactly at the watermark; gives us the next watermark, as the watchdodg would've
// taken care of triggering the first watermark.
next, immediate = p.Evaluate(UtilizationSystem, uint64(float64(limit)*watermarks[0]))
require.False(t, immediate)
require.EqualValues(t, thresholds[1], next)
// after the watermark gives us the next watermark.
next, immediate = p.Evaluate(UtilizationSystem, uint64(float64(limit)*watermarks[0])+1)
require.False(t, immediate)
require.EqualValues(t, thresholds[1], next)
// last watermark; always triggers.
next, immediate = p.Evaluate(UtilizationSystem, uint64(float64(limit)*watermarks[2]))
require.True(t, immediate)
require.EqualValues(t, uint64(float64(limit)*watermarks[2]), next)
next, immediate = p.Evaluate(UtilizationSystem, uint64(float64(limit)*watermarks[2]+1))
require.True(t, immediate)
require.EqualValues(t, uint64(float64(limit)*watermarks[2])+1, next)
next, immediate = p.Evaluate(UtilizationSystem, limit)
require.True(t, immediate)
require.EqualValues(t, limit, next)
}