Ocpp: refactor testcase (#16103)

This commit is contained in:
andig 2024-09-14 14:34:21 +02:00 • committed by GitHub
parent 1273ef9ab6
commit e2a5f4485a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 30 additions and 27 deletions

View file

@ -8,11 +8,17 @@ import (
"github.com/benbjohnson/clock"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/charger/ocpp"
ocppapi "github.com/lorenzodonini/ocpp-go/ocpp"
ocpp16 "github.com/lorenzodonini/ocpp-go/ocpp1.6"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/core"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/firmware"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/localauth"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/remotetrigger"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/reservation"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/smartcharging"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/types"
"github.com/lorenzodonini/ocpp-go/ocppj"
"github.com/lorenzodonini/ocpp-go/ws"
"github.com/stretchr/testify/suite"
)
@ -41,19 +47,20 @@ func (suite *ocppTestSuite) SetupSuite() {
suite.NotNil(ocpp.Instance())
}
func (suite *ocppTestSuite) SetupTest() {
// default delays
ocppDelays = make(map[string]time.Duration)
}
func (suite *ocppTestSuite) startChargePoint(id string, connectorId int) ocpp16.ChargePoint {
func (suite *ocppTestSuite) startChargePoint(id string, connectorId int) (ocpp16.ChargePoint, *ocppj.Client) {
// set a handler for all callback functions
handler := &ChargePointHandler{
triggerC: make(chan remotetrigger.MessageTrigger, 1),
}
// ocppj endpoint with handler
client := ws.NewClient()
client.SetRequestedSubProtocol(types.V16Subprotocol)
dispatcher := ocppj.NewDefaultClientDispatcher(ocppj.NewFIFOClientQueue(0))
endpoint := ocppj.NewClient(id, client, dispatcher, nil, core.Profile, localauth.Profile, firmware.Profile, reservation.Profile, remotetrigger.Profile, smartcharging.Profile)
// create charge point with handler
cp := ocpp16.NewChargePoint(id, nil, nil)
cp := ocpp16.NewChargePoint(id, endpoint, client)
cp.SetCoreHandler(handler)
cp.SetRemoteTriggerHandler(handler)
cp.SetSmartChargingHandler(handler)
@ -65,7 +72,7 @@ func (suite *ocppTestSuite) startChargePoint(id string, connectorId int) ocpp16.
}
}()
return cp
return cp, endpoint
}
func (suite *ocppTestSuite) handleTrigger(cp ocpp16.ChargePoint, connectorId int, msg remotetrigger.MessageTrigger) {
@ -100,7 +107,7 @@ func (suite *ocppTestSuite) handleTrigger(cp ocpp16.ChargePoint, connectorId int
func (suite *ocppTestSuite) TestConnect() {
// 1st charge point- remote
cp1 := suite.startChargePoint("test-1", 1)
cp1, _ := suite.startChargePoint("test-1", 1)
suite.Require().NoError(cp1.Start(ocppTestUrl))
suite.Require().True(cp1.IsConnected())
@ -149,7 +156,7 @@ func (suite *ocppTestSuite) TestConnect() {
}
// 2nd charge point - remote
cp2 := suite.startChargePoint("test-2", 1)
cp2, _ := suite.startChargePoint("test-2", 1)
suite.Require().NoError(cp2.Start(ocppTestUrl))
suite.Require().True(cp2.IsConnected())
@ -167,7 +174,7 @@ func (suite *ocppTestSuite) TestConnect() {
}
// error on unconfigured 2nd charge point
cp3 := suite.startChargePoint("unconfigured", 1)
cp3, _ := suite.startChargePoint("unconfigured", 1)
_, err = cp3.BootNotification("model", "vendor")
suite.Require().Error(err)
@ -191,7 +198,7 @@ WAIT_DISCONNECT:
func (suite *ocppTestSuite) TestAutoStart() {
// 1st charge point- remote
cp1 := suite.startChargePoint("test-3", 1)
cp1, _ := suite.startChargePoint("test-3", 1)
suite.Require().NoError(cp1.Start(ocppTestUrl))
suite.Require().True(cp1.IsConnected())
@ -231,16 +238,19 @@ func (suite *ocppTestSuite) TestAutoStart() {
func (suite *ocppTestSuite) TestTimeout() {
// 1st charge point- remote
cp1 := suite.startChargePoint("test-4", 1)
cp1, ocppjClient := suite.startChargePoint("test-4", 1)
suite.Require().NoError(cp1.Start(ocppTestUrl))
suite.Require().True(cp1.IsConnected())
// timeout change availability request
ocppDelays[core.ChangeAvailabilityFeatureName] = time.Minute
handler := ocppjClient.GetRequestHandler()
ocppjClient.SetRequestHandler(func(request ocppapi.Request, requestId string, action string) {
if action != core.ChangeAvailabilityFeatureName {
handler(request, requestId, action)
}
})
// 1st charge point- local
_, err := NewOCPP("test-4", 1, "", "", 0, false, false, ocppTestConnectTimeout)
// TODO fix test - this should NOT error
suite.Require().Error(err)
suite.Require().NoError(err)
}

View file

@ -1,8 +1,6 @@
package charger
import (
"time"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/core"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/remotetrigger"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/smartcharging"
@ -13,15 +11,10 @@ type ChargePointHandler struct {
triggerC chan remotetrigger.MessageTrigger
}
var ocppDelays = make(map[string]time.Duration)
// core
func (handler *ChargePointHandler) OnChangeAvailability(request *core.ChangeAvailabilityRequest) (confirmation *core.ChangeAvailabilityConfirmation, err error) {
defer func() { handler.triggerC <- core.ChangeAvailabilityFeatureName }()
if d, ok := ocppDelays[core.ChangeAvailabilityFeatureName]; ok {
time.Sleep(d)
}
return core.NewChangeAvailabilityConfirmation(core.AvailabilityStatusAccepted), nil
}

2
go.mod
View file

@ -202,7 +202,7 @@ replace gopkg.in/yaml.v3 => github.com/andig/yaml v0.0.0-20240531135838-1ff5761a
replace github.com/grid-x/modbus => github.com/evcc-io/modbus v0.0.0-20240911180928-7b1464a53285
replace github.com/lorenzodonini/ocpp-go => github.com/evcc-io/ocpp-go v0.0.0-20240730071053-d69e53b0fce9
replace github.com/lorenzodonini/ocpp-go => github.com/evcc-io/ocpp-go v0.0.0-20240914122959-0d109e403130
replace github.com/enbility/ship-go => github.com/enbility/ship-go v0.0.0-20240909200111-0d37cebbfc21

4
go.sum
View file

@ -142,8 +142,8 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/evcc-io/modbus v0.0.0-20240911180928-7b1464a53285 h1:+zUVVXpybAsLXRY4KuTiMye/v5jCOxKG0heTSTHLW2Y=
github.com/evcc-io/modbus v0.0.0-20240911180928-7b1464a53285/go.mod h1:WpbUAyptAAi0VAriSRopZa6uhiJOJCTz7KFvgGtNRXc=
github.com/evcc-io/ocpp-go v0.0.0-20240730071053-d69e53b0fce9 h1:FLv1vmLnfc8DanI5U1qOTe1Zr0OgZ/tuOvHALtZ2sOI=
github.com/evcc-io/ocpp-go v0.0.0-20240730071053-d69e53b0fce9/go.mod h1:ZynYDWGw6CslG3vyPuucLsy6AyE+h3XXYlr39jhNiQY=
github.com/evcc-io/ocpp-go v0.0.0-20240914122959-0d109e403130 h1:U4xSZBknKsMDFQtLU3gdkDUylfN9kRUj01offtqs8lY=
github.com/evcc-io/ocpp-go v0.0.0-20240914122959-0d109e403130/go.mod h1:ZynYDWGw6CslG3vyPuucLsy6AyE+h3XXYlr39jhNiQY=
github.com/evcc-io/tesla-proxy-client v0.0.0-20240221194046-4168b3759701 h1:3JplY3KS6KMDVDNAU+3+KWmSWmoHIU34qwuIpW6SiHk=
github.com/evcc-io/tesla-proxy-client v0.0.0-20240221194046-4168b3759701/go.mod h1:zWtAweBqXJTk3HSrPSecz3Q3a2hAUQ4vOE6paJfn03I=
github.com/fatih/camelcase v1.0.0/go.mod h1:yN2Sb0lFhZJUdVvtELVWefmrXpuZESvPmqwoZc+/fpc=