From 902c8ca2bbf0ccdc77943b53a010f43321863af1 Mon Sep 17 00:00:00 2001 From: Fredrik Ahlgren Date: Sun, 25 Oct 2026 14:19:47 +0100 Subject: [PATCH 1/2] test(ocpp): every simulated charger dials through one safe socket #1836 wrapped ocpp-go's client for internal/ocpp's tests, because it races with itself on close. The OCPP simulator and two cmd/ftw tests still dialed with the plain client. The wrapper moves to its own package, internal/ocpp/chargersocket, with constructors for 1.6 and 2.0.1, and all of them use it. A test fails if any Go file in the module builds a charger on the plain client again. Core is the OCPP server and never dials, so its binary does not change. Co-Authored-By: Claude Opus 5.5 --- go/cmd/ftw/ev_ocpp_reconnect_test.go | 3 +- go/cmd/ftw/ev_send_test.go | 4 +- go/cmd/sim-ocpp/ocppcp/sim.go | 6 +- go/internal/ocpp/charger_socket_test.go | 135 +----------------- go/internal/ocpp/chargersocket/socket.go | 129 +++++++++++++++++ go/internal/ocpp/chargersocket/socket_test.go | 119 +++++++++++++++ 6 files changed, 263 insertions(+), 133 deletions(-) create mode 100644 go/internal/ocpp/chargersocket/socket.go create mode 100644 go/internal/ocpp/chargersocket/socket_test.go diff --git a/go/cmd/ftw/ev_ocpp_reconnect_test.go b/go/cmd/ftw/ev_ocpp_reconnect_test.go index 07f6a3368..37c227040 100644 --- a/go/cmd/ftw/ev_ocpp_reconnect_test.go +++ b/go/cmd/ftw/ev_ocpp_reconnect_test.go @@ -16,6 +16,7 @@ import ( "github.com/srcfl/ftw/go/internal/events" "github.com/srcfl/ftw/go/internal/loadpoint" "github.com/srcfl/ftw/go/internal/ocpp" + "github.com/srcfl/ftw/go/internal/ocpp/chargersocket" "github.com/srcfl/ftw/go/internal/telemetry" ) @@ -56,7 +57,7 @@ func TestOCPPReconnectDoesNotUnplugOrResumeOldStart(t *testing.T) { }) connect := func() ocpp16.ChargePoint { t.Helper() - cp := ocpp16.NewChargePoint("garage", nil, nil) + cp := chargersocket.ChargePoint16("garage", nil) if e := cp.Start("ws://" + addr); e != nil { t.Fatal(e) } diff --git a/go/cmd/ftw/ev_send_test.go b/go/cmd/ftw/ev_send_test.go index bfff5b495..0503e2b71 100644 --- a/go/cmd/ftw/ev_send_test.go +++ b/go/cmd/ftw/ev_send_test.go @@ -10,13 +10,13 @@ import ( "testing" "time" - ocpp16 "github.com/lorenzodonini/ocpp-go/ocpp1.6" "github.com/lorenzodonini/ocpp-go/ocpp1.6/smartcharging" "github.com/lorenzodonini/ocpp-go/ws" "github.com/srcfl/ftw/go/internal/config" "github.com/srcfl/ftw/go/internal/loadpoint" "github.com/srcfl/ftw/go/internal/ocpp" + "github.com/srcfl/ftw/go/internal/ocpp/chargersocket" "github.com/srcfl/ftw/go/internal/telemetry" ) @@ -160,7 +160,7 @@ func connectTestCharger(t *testing.T, port int, id, user, pass string) (*profile if user != "" || pass != "" { client.SetBasicAuth(user, pass) } - cp := ocpp16.NewChargePoint(id, nil, client) + cp := chargersocket.ChargePoint16(id, client) cp.SetSmartChargingHandler(fake) if err := cp.Start(fmt.Sprintf("ws://127.0.0.1:%d", port)); err != nil { t.Fatalf("connect %s: %v", id, err) diff --git a/go/cmd/sim-ocpp/ocppcp/sim.go b/go/cmd/sim-ocpp/ocppcp/sim.go index 59886a807..170bf04da 100644 --- a/go/cmd/sim-ocpp/ocppcp/sim.go +++ b/go/cmd/sim-ocpp/ocppcp/sim.go @@ -15,6 +15,8 @@ import ( "github.com/lorenzodonini/ocpp-go/ocpp2.0.1/transactions" types201 "github.com/lorenzodonini/ocpp-go/ocpp2.0.1/types" "github.com/lorenzodonini/ocpp-go/ws" + + "github.com/srcfl/ftw/go/internal/ocpp/chargersocket" ) // DialOpts is how a simulated charge point reaches FTW. @@ -87,7 +89,7 @@ func (s *Sim) Dial(opts DialOpts) error { if opts.URL201 == "" { return fmt.Errorf("%s needs a 2.0.1 URL", s.Model.ID) } - cs := ocpp201.NewChargingStation(id, nil, client) + cs := chargersocket.ChargingStation201(id, client) h := &handlers201{sim: s} cs.SetProvisioningHandler(h) cs.SetSmartChargingHandler(h) @@ -100,7 +102,7 @@ func (s *Sim) Dial(opts DialOpts) error { if opts.URL16 == "" { return fmt.Errorf("%s needs a 1.6 URL", s.Model.ID) } - cp := ocpp16.NewChargePoint(id, nil, client) + cp := chargersocket.ChargePoint16(id, client) cp.SetCoreHandler(s) cp.SetSmartChargingHandler(s) cp.SetRemoteTriggerHandler(s) diff --git a/go/internal/ocpp/charger_socket_test.go b/go/internal/ocpp/charger_socket_test.go index 538459b43..83884f2f1 100644 --- a/go/internal/ocpp/charger_socket_test.go +++ b/go/internal/ocpp/charger_socket_test.go @@ -2,7 +2,6 @@ package ocpp import ( "context" - "errors" "fmt" "net" "sync" @@ -13,120 +12,19 @@ import ( ocpp201 "github.com/lorenzodonini/ocpp-go/ocpp2.0.1" "github.com/lorenzodonini/ocpp-go/ws" + "github.com/srcfl/ftw/go/internal/ocpp/chargersocket" "github.com/srcfl/ftw/go/internal/telemetry" ) -// chargerSocket is the WebSocket client every test charger dials with. It -// closes three races that ocpp-go v0.19.0's ws.Client has on close, each of -// which has failed CI: -// -// - Write checks the connection, then queues without a lock, while the write -// pump closes that queue on Stop. A server request answered as the charger -// stops sends on a closed channel and panics (#1712). -// - ocppj.Client.Stop installs its own disconnect handler while the write -// pump may be reading the old one after the server closed: a data race. -// - If the pump has already called the old handler, ocppj.Client.Stop waits -// for a call that never comes, and the package hits its timeout. -// -// It serializes Write with Stop, keeps the disconnect handler itself, and -// passes a disconnect the pump has already reported to the handler Stop -// installed. A Write while the server closes the socket can still meet the -// first race; chargers therefore stop before the server, at cleanup. -type chargerSocket struct { - *ws.Client - - mu sync.Mutex - stopping bool - onDown func(error) - onUp func() - // installed counts disconnect handlers; reportedTo is the one that was - // told of the current disconnect, 0 while connected. - installed, reportedTo int -} - -func newChargerSocket(client *ws.Client) *chargerSocket { - s := &chargerSocket{Client: client} - // Set once, before the pumps start, so the library never sees a change. - client.SetDisconnectedHandler(s.disconnected) - client.SetReconnectedHandler(s.reconnected) - return s -} - -func (s *chargerSocket) SetDisconnectedHandler(fn func(error)) { - s.mu.Lock() - defer s.mu.Unlock() - s.onDown = fn - s.installed++ -} - -func (s *chargerSocket) SetReconnectedHandler(fn func()) { - s.mu.Lock() - defer s.mu.Unlock() - s.onUp = fn -} - -func (s *chargerSocket) disconnected(err error) { - s.mu.Lock() - fn := s.onDown - s.reportedTo = s.installed - s.mu.Unlock() - if fn != nil { - fn(err) - } -} - -func (s *chargerSocket) reconnected() { - s.mu.Lock() - fn := s.onUp - s.reportedTo = 0 - s.mu.Unlock() - if fn != nil { - fn() - } -} - -func (s *chargerSocket) Start(url string) error { - s.mu.Lock() - s.stopping, s.reportedTo = false, 0 - s.mu.Unlock() - return s.Client.Start(url) -} - -func (s *chargerSocket) Write(data []byte) error { - s.mu.Lock() - defer s.mu.Unlock() - if s.stopping { - return errors.New("test charger is stopping") - } - return s.Client.Write(data) -} - -func (s *chargerSocket) Stop() { - s.mu.Lock() - s.stopping = true - var missed func(error) - if s.reportedTo != 0 && s.reportedTo != s.installed { - missed, s.reportedTo = s.onDown, s.installed - } - s.mu.Unlock() - s.Client.Stop() - if missed != nil { - missed(nil) - } -} - func wsURL(port int) string { return fmt.Sprintf("ws://127.0.0.1:%d", port) } -// dialChargePoint16 starts a 1.6 charge point on a chargerSocket. setup runs -// before Start, to register handlers; client may carry credentials or TLS. -// The stop it returns runs once and is also registered as cleanup, so a test -// may disconnect on purpose. +// dialChargePoint16 starts a 1.6 charge point on a chargersocket.Socket. +// setup runs before Start, to register handlers; client may carry credentials +// or TLS. The stop it returns runs once and is also registered as cleanup, so +// a test may disconnect on purpose. func dialChargePoint16(t *testing.T, url, id string, client *ws.Client, setup func(ocpp16.ChargePoint)) (ocpp16.ChargePoint, func(), error) { t.Helper() - if client == nil { - client = ws.NewClient() - } - cp := ocpp16.NewChargePoint(id, nil, newChargerSocket(client)) + cp := chargersocket.ChargePoint16(id, client) if setup != nil { setup(cp) } @@ -140,7 +38,7 @@ func dialChargePoint16(t *testing.T, url, id string, client *ws.Client, setup fu // dialChargingStation201 is dialChargePoint16 for OCPP 2.0.1. func dialChargingStation201(t *testing.T, url, id string, setup func(ocpp201.ChargingStation)) (ocpp201.ChargingStation, func(), error) { t.Helper() - cs := ocpp201.NewChargingStation(id, nil, newChargerSocket(ws.NewClient())) + cs := chargersocket.ChargingStation201(id, nil) if setup != nil { setup(cs) } @@ -217,22 +115,3 @@ func listening(t *testing.T, srv *Server) bool { } return true } - -// The write pump reports a server-side close to the handler of the moment. -// When ocppj.Client.Stop installs its own handler after that, Stop must -// still hear of the disconnect, once, or it waits forever. -func TestChargerSocketPassesAMissedDisconnectToStop(t *testing.T) { - s := newChargerSocket(ws.NewClient()) - var first, second int - s.SetDisconnectedHandler(func(error) { first++ }) - s.disconnected(errors.New("server closed")) // as the write pump does - s.SetDisconnectedHandler(func(error) { second++ }) - s.Stop() - s.Stop() - if first != 1 || second != 1 { - t.Fatalf("disconnect reached the first handler %d times and Stop's %d times, want once each", first, second) - } - if err := s.Write([]byte("[]")); err == nil { - t.Fatal("a stopped charger still wrote") - } -} diff --git a/go/internal/ocpp/chargersocket/socket.go b/go/internal/ocpp/chargersocket/socket.go new file mode 100644 index 000000000..d08a36b21 --- /dev/null +++ b/go/internal/ocpp/chargersocket/socket.go @@ -0,0 +1,129 @@ +// Package chargersocket is the WebSocket client every simulated or test +// charger in this module dials Core with. Core itself is the OCPP server and +// never dials. +package chargersocket + +import ( + "errors" + "sync" + + ocpp16 "github.com/lorenzodonini/ocpp-go/ocpp1.6" + ocpp201 "github.com/lorenzodonini/ocpp-go/ocpp2.0.1" + "github.com/lorenzodonini/ocpp-go/ws" +) + +// Socket closes three races that ocpp-go v0.19.0's ws.Client has on close, +// each of which has failed CI: +// +// - Write checks the connection, then queues without a lock, while the write +// pump closes that queue on Stop. A server request answered as the charger +// stops sends on a closed channel and panics (#1712). +// - ocppj.Client.Stop installs its own disconnect handler while the write +// pump may be reading the old one after the server closed: a data race. +// - If the pump has already called the old handler, ocppj.Client.Stop waits +// for a call that never comes, and the caller hangs. +// +// It serializes Write with Stop, keeps the disconnect handler itself, and +// passes a disconnect the pump has already reported to the handler Stop +// installed. A Write while the server closes the socket can still meet the +// first race; tests therefore stop their chargers before the server. +type Socket struct { + *ws.Client + + mu sync.Mutex + stopping bool + onDown func(error) + onUp func() + // installed counts disconnect handlers; reportedTo is the one that was + // told of the current disconnect, 0 while connected. + installed, reportedTo int +} + +// New wraps client, which may carry credentials or TLS. +func New(client *ws.Client) *Socket { + s := &Socket{Client: client} + // Set once, before the pumps start, so the library never sees a change. + client.SetDisconnectedHandler(s.disconnected) + client.SetReconnectedHandler(s.reconnected) + return s +} + +// ChargePoint16 is an OCPP 1.6 charge point on a Socket. A nil client is a +// plain ws.Client. +func ChargePoint16(id string, client *ws.Client) ocpp16.ChargePoint { + if client == nil { + client = ws.NewClient() + } + return ocpp16.NewChargePoint(id, nil, New(client)) +} + +// ChargingStation201 is ChargePoint16 for OCPP 2.0.1. +func ChargingStation201(id string, client *ws.Client) ocpp201.ChargingStation { + if client == nil { + client = ws.NewClient() + } + return ocpp201.NewChargingStation(id, nil, New(client)) +} + +func (s *Socket) SetDisconnectedHandler(fn func(error)) { + s.mu.Lock() + defer s.mu.Unlock() + s.onDown = fn + s.installed++ +} + +func (s *Socket) SetReconnectedHandler(fn func()) { + s.mu.Lock() + defer s.mu.Unlock() + s.onUp = fn +} + +func (s *Socket) disconnected(err error) { + s.mu.Lock() + fn := s.onDown + s.reportedTo = s.installed + s.mu.Unlock() + if fn != nil { + fn(err) + } +} + +func (s *Socket) reconnected() { + s.mu.Lock() + fn := s.onUp + s.reportedTo = 0 + s.mu.Unlock() + if fn != nil { + fn() + } +} + +func (s *Socket) Start(url string) error { + s.mu.Lock() + s.stopping, s.reportedTo = false, 0 + s.mu.Unlock() + return s.Client.Start(url) +} + +func (s *Socket) Write(data []byte) error { + s.mu.Lock() + defer s.mu.Unlock() + if s.stopping { + return errors.New("charger is stopping") + } + return s.Client.Write(data) +} + +func (s *Socket) Stop() { + s.mu.Lock() + s.stopping = true + var missed func(error) + if s.reportedTo != 0 && s.reportedTo != s.installed { + missed, s.reportedTo = s.onDown, s.installed + } + s.mu.Unlock() + s.Client.Stop() + if missed != nil { + missed(nil) + } +} diff --git a/go/internal/ocpp/chargersocket/socket_test.go b/go/internal/ocpp/chargersocket/socket_test.go new file mode 100644 index 000000000..9b86f25e6 --- /dev/null +++ b/go/internal/ocpp/chargersocket/socket_test.go @@ -0,0 +1,119 @@ +package chargersocket + +import ( + "errors" + "go/ast" + "go/parser" + "go/token" + "io/fs" + "os" + "path/filepath" + "strconv" + "strings" + "testing" + + "github.com/lorenzodonini/ocpp-go/ws" +) + +// The write pump reports a server-side close to the handler of the moment. +// When ocppj.Client.Stop installs its own handler after that, Stop must +// still hear of the disconnect, once, or it waits forever. +func TestSocketPassesAMissedDisconnectToStop(t *testing.T) { + s := New(ws.NewClient()) + var first, second int + s.SetDisconnectedHandler(func(error) { first++ }) + s.disconnected(errors.New("server closed")) // as the write pump does + s.SetDisconnectedHandler(func(error) { second++ }) + s.Stop() + s.Stop() + if first != 1 || second != 1 { + t.Fatalf("disconnect reached the first handler %d times and Stop's %d times, want once each", first, second) + } + if err := s.Write([]byte("[]")); err == nil { + t.Fatal("a stopped charger still wrote") + } +} + +// Every charger in the module dials through a Socket. The races above need +// a few instructions of bad luck, so a charger built on the plain client +// passes its own tests and fails CI weeks later; this finds it at once. +func TestEveryChargerDialsThroughASocket(t *testing.T) { + root := moduleRoot(t) + clients := map[string]string{ + "github.com/lorenzodonini/ocpp-go/ocpp1.6": "NewChargePoint", + "github.com/lorenzodonini/ocpp-go/ocpp2.0.1": "NewChargingStation", + } + here, err := filepath.Abs(".") + if err != nil { + t.Fatal(err) + } + var found []string + err = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + if d.IsDir() { + if path == here || d.Name() == "testdata" || d.Name() == "vendor" || (path != root && strings.HasPrefix(d.Name(), ".")) { + return filepath.SkipDir + } + return nil + } + if !strings.HasSuffix(path, ".go") { + return nil + } + fset := token.NewFileSet() + f, err := parser.ParseFile(fset, path, nil, parser.SkipObjectResolution) + if err != nil { + return err + } + names := map[string]string{} // local package name -> constructor + for _, imp := range f.Imports { + p, _ := strconv.Unquote(imp.Path.Value) + ctor, ok := clients[p] + if !ok { + continue + } + name := filepath.Base(p) + if imp.Name != nil { + name = imp.Name.Name + } + names[name] = ctor + } + ast.Inspect(f, func(n ast.Node) bool { + sel, ok := n.(*ast.SelectorExpr) + if !ok { + return true + } + if pkg, ok := sel.X.(*ast.Ident); ok && names[pkg.Name] == sel.Sel.Name { + rel, _ := filepath.Rel(root, path) + found = append(found, rel+":"+strconv.Itoa(fset.Position(sel.Pos()).Line)) + } + return true + }) + return nil + }) + if err != nil { + t.Fatal(err) + } + if len(found) > 0 { + t.Fatalf("chargers built on ocpp-go's plain client, use ChargePoint16 or ChargingStation201:\n%s", strings.Join(found, "\n")) + } +} + +func moduleRoot(t *testing.T) string { + t.Helper() + dir, err := filepath.Abs(".") + if err != nil { + t.Fatal(err) + } + for { + if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil { + return dir + } + parent := filepath.Dir(dir) + if parent == dir { + t.Fatal("no go.mod above this package") + } + dir = parent + } +} From a3d3f5cde14dcc546413bf221640098cc4e7b68c Mon Sep 17 00:00:00 2001 From: Fredrik Ahlgren Date: Sun, 25 Oct 2026 14:49:03 +0100 Subject: [PATCH 2/2] test(ocpp): the charger guard knows ocpp-go's package names Review notes: an import without an alias is named ocpp16 or ocpp2, not after its path, so the guard missed it; it missed a dot import too. It now uses the real names and checks dot imports. It skips what the go tool skips (_ and . directories, testdata, vendor) and the nested modules under go/, and leaves a file that does not parse to the build. Co-Authored-By: Claude Opus 5.5 --- go/internal/ocpp/chargersocket/socket_test.go | 51 +++++++++++++------ 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/go/internal/ocpp/chargersocket/socket_test.go b/go/internal/ocpp/chargersocket/socket_test.go index 9b86f25e6..9e27567dc 100644 --- a/go/internal/ocpp/chargersocket/socket_test.go +++ b/go/internal/ocpp/chargersocket/socket_test.go @@ -39,9 +39,10 @@ func TestSocketPassesAMissedDisconnectToStop(t *testing.T) { // passes its own tests and fails CI weeks later; this finds it at once. func TestEveryChargerDialsThroughASocket(t *testing.T) { root := moduleRoot(t) - clients := map[string]string{ - "github.com/lorenzodonini/ocpp-go/ocpp1.6": "NewChargePoint", - "github.com/lorenzodonini/ocpp-go/ocpp2.0.1": "NewChargingStation", + type client struct{ pkg, ctor string } + clients := map[string]client{ + "github.com/lorenzodonini/ocpp-go/ocpp1.6": {"ocpp16", "NewChargePoint"}, + "github.com/lorenzodonini/ocpp-go/ocpp2.0.1": {"ocpp2", "NewChargingStation"}, } here, err := filepath.Abs(".") if err != nil { @@ -53,7 +54,16 @@ func TestEveryChargerDialsThroughASocket(t *testing.T) { return err } if d.IsDir() { - if path == here || d.Name() == "testdata" || d.Name() == "vendor" || (path != root && strings.HasPrefix(d.Name(), ".")) { + if path == root { + return nil + } + // The go tool skips the same directories, and a directory with + // its own go.mod is another module. + name := d.Name() + if path == here || name == "testdata" || name == "vendor" || strings.HasPrefix(name, ".") || strings.HasPrefix(name, "_") { + return filepath.SkipDir + } + if _, err := os.Stat(filepath.Join(path, "go.mod")); err == nil { return filepath.SkipDir } return nil @@ -64,29 +74,38 @@ func TestEveryChargerDialsThroughASocket(t *testing.T) { fset := token.NewFileSet() f, err := parser.ParseFile(fset, path, nil, parser.SkipObjectResolution) if err != nil { - return err + return nil // the build reports a file that does not parse } - names := map[string]string{} // local package name -> constructor + ctors := map[string]string{} // local package name, "." for a dot import -> constructor for _, imp := range f.Imports { p, _ := strconv.Unquote(imp.Path.Value) - ctor, ok := clients[p] + c, ok := clients[p] if !ok { continue } - name := filepath.Base(p) + name := c.pkg if imp.Name != nil { name = imp.Name.Name } - names[name] = ctor + ctors[name] = c.ctor + } + if len(ctors) == 0 { + return nil + } + report := func(pos token.Pos) { + rel, _ := filepath.Rel(root, path) + found = append(found, rel+":"+strconv.Itoa(fset.Position(pos).Line)) } ast.Inspect(f, func(n ast.Node) bool { - sel, ok := n.(*ast.SelectorExpr) - if !ok { - return true - } - if pkg, ok := sel.X.(*ast.Ident); ok && names[pkg.Name] == sel.Sel.Name { - rel, _ := filepath.Rel(root, path) - found = append(found, rel+":"+strconv.Itoa(fset.Position(sel.Pos()).Line)) + switch n := n.(type) { + case *ast.SelectorExpr: + if pkg, ok := n.X.(*ast.Ident); ok && ctors[pkg.Name] == n.Sel.Name { + report(n.Pos()) + } + case *ast.CallExpr: + if id, ok := n.Fun.(*ast.Ident); ok && id.Name == ctors["."] { + report(n.Pos()) + } } return true })