Config: probe device check capabilities in parallel (#31118)

This commit is contained in:
andig 2026-06-22 13:04:28 +02:00 • committed by GitHub
parent 8b2c540741
commit 6d03b3e79c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 86 additions and 3 deletions

View file

@ -273,25 +273,32 @@ func testInstance(ctx context.Context, instance any) map[string]testResult {
resMu.Unlock()
}
done := make(chan struct{})
go func() {
defer close(done)
var wg sync.WaitGroup
// probes run concurrently so a responsive getter still returns when another
// blocks; slow getters are abandoned once ctx expires (see below)
wg.Go(func() {
if dev, ok := api.Cap[api.Meter](instance); ok {
val, err := dev.CurrentPower()
makeResult("power", val, err)
}
})
wg.Go(func() {
if dev, ok := api.Cap[api.MeterEnergy](instance); ok {
val, err := dev.TotalEnergy()
makeResult("energy", val, err)
}
})
wg.Go(func() {
if dev, ok := api.Cap[api.MeterReturnEnergy](instance); ok {
val, err := dev.ReturnEnergy()
makeResult("returnEnergy", val, err)
}
})
wg.Go(func() {
if dev, ok := api.Cap[api.Battery](instance); ok {
val, err := dev.Soc()
key := "soc"
@ -300,80 +307,114 @@ func testInstance(ctx context.Context, instance any) map[string]testResult {
}
makeResult(key, val, err)
}
})
wg.Go(func() {
if api.HasCap[api.BatteryController](instance) {
makeResult("controllable", true, nil)
}
})
wg.Go(func() {
if dev, ok := api.Cap[api.VehicleOdometer](instance); ok {
val, err := dev.Odometer()
makeResult("odometer", val, err)
}
})
wg.Go(func() {
if dev, ok := api.Cap[api.BatteryCapacity](instance); ok {
val := dev.Capacity()
makeResult("capacity", val, nil)
}
})
wg.Go(func() {
if dev, ok := api.Cap[api.PhaseCurrents](instance); ok {
i1, i2, i3, err := dev.Currents()
makeResult("phaseCurrents", []float64{i1, i2, i3}, err)
}
})
wg.Go(func() {
if dev, ok := api.Cap[api.PhaseVoltages](instance); ok {
u1, u2, u3, err := dev.Voltages()
makeResult("phaseVoltages", []float64{u1, u2, u3}, err)
}
})
wg.Go(func() {
if dev, ok := api.Cap[api.PhasePowers](instance); ok {
p1, p2, p3, err := dev.Powers()
makeResult("phasePowers", []float64{p1, p2, p3}, err)
}
})
wg.Go(func() {
if dev, ok := api.Cap[api.ChargeState](instance); ok {
val, err := dev.Status()
makeResult("chargeStatus", val, err)
}
})
wg.Go(func() {
if dev, ok := api.Cap[api.Charger](instance); ok {
val, err := dev.Enabled()
makeResult("enabled", val, err)
}
})
wg.Go(func() {
if dev, ok := api.Cap[api.ChargeRater](instance); ok {
val, err := dev.ChargedEnergy()
makeResult("chargedEnergy", val, err)
}
})
wg.Go(func() {
if api.HasCap[api.PhaseSwitcher](instance) {
makeResult("phases1p3p", true, nil)
}
})
wg.Go(func() {
if hasFeature(instance, api.Heating) {
makeResult("heating", true, nil)
}
})
wg.Go(func() {
if hasFeature(instance, api.IntegratedDevice) {
makeResult("integratedDevice", true, nil)
}
})
wg.Go(func() {
if hasFeature(instance, api.SwitchDevice) {
makeResult("switchDevice", true, nil)
}
})
wg.Go(func() {
if dev, ok := api.Cap[api.IconDescriber](instance); ok && dev.Icon() != "" {
makeResult("icon", dev.Icon(), nil)
}
})
wg.Go(func() {
if cc, ok := api.Cap[api.PhaseDescriber](instance); ok && cc.Phases() == 1 {
makeResult("singlePhase", true, nil)
}
})
wg.Go(func() {
if dev, ok := api.Cap[api.VehicleRange](instance); ok {
val, err := dev.Range()
makeResult("range", val, err)
}
})
wg.Go(func() {
if dev, ok := api.Cap[api.SocLimiter](instance); ok {
val, err := dev.GetLimitSoc()
key := "vehicleLimitSoc"
@ -382,24 +423,32 @@ func testInstance(ctx context.Context, instance any) map[string]testResult {
}
makeResult(key, val, err)
}
})
wg.Go(func() {
if dev, ok := api.Cap[api.Dimmer](instance); ok {
val, err := dev.Dimmed()
makeResult("dimmed", val, err)
}
})
wg.Go(func() {
if dev, ok := api.Cap[api.Curtailer](instance); ok {
makeResult("curtailable", true, nil)
if val, err := dev.Curtailed(); err != nil || val {
makeResult("curtailed", true, err)
}
}
})
wg.Go(func() {
if dev, ok := api.Cap[api.Identifier](instance); ok {
val, err := dev.Identify()
makeResult("identifier", val, err)
}
})
wg.Go(func() {
if dev, ok := api.Cap[api.Tariff](instance); ok {
rates, err := dev.Rates()
@ -430,6 +479,12 @@ func testInstance(ctx context.Context, instance any) map[string]testResult {
}
}
}
})
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
// bound the probe phase: on ctx.Done return collected results so far. A leaked

View file

@ -38,6 +38,34 @@ func TestInstanceBoundedByContext(t *testing.T) {
require.Less(t, time.Since(start), 500*time.Millisecond, "testInstance must not wait for blocking getter")
}
// slowPowerMeter blocks on CurrentPower but returns TotalEnergy immediately.
type slowPowerMeter struct {
done chan struct{}
}
func (m slowPowerMeter) CurrentPower() (float64, error) {
<-m.done
return 0, nil
}
func (m slowPowerMeter) TotalEnergy() (float64, error) {
return 42, nil
}
// TestInstanceParallelProbes ensures a responsive getter returns even while a
// sibling blocks; sequential probing would starve energy behind power.
func TestInstanceParallelProbes(t *testing.T) {
done := make(chan struct{})
defer close(done)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
res := testInstance(ctx, slowPowerMeter{done: done})
require.Contains(t, res, "energy", "fast getter must return despite a blocking sibling")
require.NotContains(t, res, "power", "blocking getter must be abandoned")
}
func TestConfigReqUnmarshal(t *testing.T) {
var req configReq
require.NoError(t, json.Unmarshal([]byte(`{