Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion go/cmd/ftw/ev_ocpp_reconnect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions go/cmd/ftw/ev_send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions go/cmd/sim-ocpp/ocppcp/sim.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
135 changes: 7 additions & 128 deletions go/internal/ocpp/charger_socket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ocpp

import (
"context"
"errors"
"fmt"
"net"
"sync"
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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")
}
}
129 changes: 129 additions & 0 deletions go/internal/ocpp/chargersocket/socket.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading
Loading