OCPP: clear failed charge point registration on init error (#31652)

This commit is contained in:
andig 2026-07-10 16:40:24 +02:00 • committed by GitHub
parent 6d2ed7d2ac
commit c52b4a79ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 55 additions and 1 deletions

View file

@ -175,7 +175,17 @@ func (cs *CS) RegisterChargepoint(id string, newfun func() *CP, init func(*CP) e
cp.onTransportConnect()
}
return cp, init(cp)
err := init(cp)
if err != nil {
// allow retry on next call instead of permanently caching a failed setup
cs.mu.Lock()
if reg.cp == cp {
reg.cp = nil
}
cs.mu.Unlock()
}
return cp, err
}
// NewChargePoint implements ocpp16.ChargePointConnectionHandler

44
charger/ocpp/cs_test.go Normal file
View file

@ -0,0 +1,44 @@
package ocpp
import (
"errors"
"testing"
"github.com/evcc-io/evcc/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRegisterChargepointClearsCacheOnInitFailure(t *testing.T) {
cs := &CS{
log: util.NewLogger("foo"),
regs: make(map[string]*registration),
}
newfun := func() *CP { return NewChargePoint(util.NewLogger("foo"), cs, "test") }
// first attempt fails- must not leave a stale cp cached
_, err := cs.RegisterChargepoint("test", newfun, func(*CP) error {
return errors.New("boom")
})
require.Error(t, err)
assert.Nil(t, cs.regs["test"].cp, "failed init must not cache the charge point")
// retry must create a fresh charge point and succeed
var initialised *CP
cp, err := cs.RegisterChargepoint("test", newfun, func(cp *CP) error {
initialised = cp
return nil
})
require.NoError(t, err)
assert.Same(t, initialised, cp)
assert.Same(t, cp, cs.regs["test"].cp, "successful init must cache the charge point")
// further calls must return the cached charge point without re-running init
cp2, err := cs.RegisterChargepoint("test", newfun, func(*CP) error {
t.Fatal("init must not be called again for an already registered charge point")
return nil
})
require.NoError(t, err)
assert.Same(t, cp, cp2)
}