chore: fix flaky OCPP test bind on fixed port 8887 (#30590)

This commit is contained in:
andig 2026-06-07 12:09:24 +02:00 • committed by GitHub
parent 2f7126b005
commit 9e9a07221b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 39 additions and 4 deletions

View file

@ -28,9 +28,18 @@ var (
once sync.Once
instance *CS
port = 8887
boundPort int
externalUrl string
)
// Port returns the TCP port the central system is bound to. With the default
// configuration this equals the configured port; when port 0 is configured
// (as in tests) it is the OS-assigned ephemeral port. It returns 0 while the
// server is not bound.
func Port() int {
return boundPort
}
// GetStatus returns the OCPP runtime status
func GetStatus() Status {
if instance == nil {
@ -111,6 +120,8 @@ func Instance() *CS {
return
}
}
boundPort = server.Addr().Port
})
return instance

View file

@ -1,9 +1,18 @@
package ocpp
import (
"os"
"testing"
)
func TestMain(m *testing.M) {
// bind the central system to an ephemeral port so this test binary does not
// contend with the charger package test binary for the fixed default port
// when both run in parallel under `go test ./...`
Init(Config{Port: 0}, "")
os.Exit(m.Run())
}
func TestExternalUrl(t *testing.T) {
tests := []struct{ input, expected string }{
{"", ""},

View file

@ -2,6 +2,8 @@ package charger
import (
"errors"
"fmt"
"os"
"testing"
"time"
@ -22,10 +24,20 @@ import (
"github.com/stretchr/testify/suite"
)
const (
ocppTestUrl = "ws://localhost:8887"
ocppTestConnectTimeout = 10 * time.Second
)
const ocppTestConnectTimeout = 10 * time.Second
// ocppTestUrl is derived from the actual bound port in SetupSuite: the central
// system binds an ephemeral port to avoid bind failures when the fixed default
// port is already in use on the CI runner.
var ocppTestUrl string
func TestMain(m *testing.M) {
// bind the OCPP central system to an ephemeral port so this test binary
// does not contend with the charger/ocpp package test binary for the fixed
// default port when both run in parallel under `go test ./...`
ocpp.Init(ocpp.Config{Port: 0}, "")
os.Exit(m.Run())
}
func TestOcpp(t *testing.T) {
suite.Run(t, new(ocppTestSuite))
@ -49,6 +61,9 @@ func (suite *ocppTestSuite) SetupSuite() {
suite.logger = &ocppLogger{t: suite.T()}
ocppj.SetLogger(suite.logger)
suite.Require().NotZero(ocpp.Port(), "central system did not bind")
ocppTestUrl = fmt.Sprintf("ws://localhost:%d", ocpp.Port())
suite.clock = clock.NewMock()
suite.NotNil(ocpp.Instance())
}