fermentord/internal/controllers/hysteresis_test.go

52 lines
1.1 KiB
Go

package controllers
import (
"testing"
"time"
)
func discard(h *Hysteresis) {
for {
<-h.C
}
}
func TestHysteresisNormalCooling(t *testing.T) {
c := &Config{
FermentationTemperature: 20,
MaxWortDelta: 0.5,
MinChamberTemperature: 2,
MinCoolerRuntimeSecs: 300,
MaxCoolerRuntimeSecs: 86400,
MinCoolerCooldownSecs: 300,
}
h := NewHysteresis("test")
h.config = c
h.ambientTemperature = 25
h.chamberTemperature = 22
h.wortTemperature = 20
go discard(h)
h.update()
assert := func(expectedState bool) {
actualState := h.GetCoolerState()
if actualState != expectedState {
t.Errorf("Expected cooler state %v, but got %v", expectedState, actualState)
}
}
assert(false)
h.lastCoolerStateChange = h.lastCoolerStateChange.Add(-1 * time.Hour)
h.UpdateWortTemperature(20.5)
assert(false)
h.lastCoolerStateChange = h.lastCoolerStateChange.Add(-1 * time.Hour)
h.UpdateWortTemperature(20.6)
assert(true)
h.lastCoolerStateChange = h.lastCoolerStateChange.Add(-1 * time.Hour)
h.UpdateWortTemperature(20)
assert(false)
}