From e2a5f4485afb970fba7610965b7d8b2b2de5b850 Mon Sep 17 00:00:00 2001 From: andig Date: Sat, 14 Sep 2024 14:34:21 +0200 Subject: [PATCH] Ocpp: refactor testcase (#16103) --- charger/ocpp_test.go | 44 ++++++++++++++++++++++-------------- charger/ocpp_test_handler.go | 7 ------ go.mod | 2 +- go.sum | 4 ++-- 4 files changed, 30 insertions(+), 27 deletions(-) diff --git a/charger/ocpp_test.go b/charger/ocpp_test.go index 6aef283c8..2339c388f 100644 --- a/charger/ocpp_test.go +++ b/charger/ocpp_test.go @@ -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) } diff --git a/charger/ocpp_test_handler.go b/charger/ocpp_test_handler.go index 0ef2abb97..1069dedb0 100644 --- a/charger/ocpp_test_handler.go +++ b/charger/ocpp_test_handler.go @@ -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 } diff --git a/go.mod b/go.mod index a1af48da5..be24457ed 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index fe28c7b0f..2c9deba1d 100644 --- a/go.sum +++ b/go.sum @@ -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=