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..9e27567dc --- /dev/null +++ b/go/internal/ocpp/chargersocket/socket_test.go @@ -0,0 +1,138 @@ +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) + 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 { + 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 == 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 + } + if !strings.HasSuffix(path, ".go") { + return nil + } + fset := token.NewFileSet() + f, err := parser.ParseFile(fset, path, nil, parser.SkipObjectResolution) + if err != nil { + return nil // the build reports a file that does not parse + } + ctors := map[string]string{} // local package name, "." for a dot import -> constructor + for _, imp := range f.Imports { + p, _ := strconv.Unquote(imp.Path.Value) + c, ok := clients[p] + if !ok { + continue + } + name := c.pkg + if imp.Name != nil { + name = imp.Name.Name + } + 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 { + 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 + }) + 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 + } +}