fix: OCPP test race on logger and connector clock (#28460)
This commit is contained in:
parent
f7b214df75
commit
377d3d199c
3 changed files with 30 additions and 16 deletions
|
|
@ -80,6 +80,8 @@ func NewConnector(ctx context.Context, log *util.Logger, id int, cp *CP, idTag s
|
|||
}
|
||||
|
||||
func (conn *Connector) TestClock(clock clock.Clock) {
|
||||
conn.mu.Lock()
|
||||
defer conn.mu.Unlock()
|
||||
conn.clock = clock
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -33,7 +33,8 @@ func TestOcpp(t *testing.T) {
|
|||
|
||||
type ocppTestSuite struct {
|
||||
suite.Suite
|
||||
clock *clock.Mock
|
||||
clock *clock.Mock
|
||||
logger *ocppLogger
|
||||
}
|
||||
|
||||
func (suite *ocppTestSuite) SetupSuite() {
|
||||
|
|
@ -41,12 +42,17 @@ func (suite *ocppTestSuite) SetupSuite() {
|
|||
|
||||
// setup cs so we can overwrite logger afterwards
|
||||
_ = ocpp.Instance()
|
||||
ocppj.SetLogger(&ocppLogger{suite.T()})
|
||||
suite.logger = &ocppLogger{t: suite.T()}
|
||||
ocppj.SetLogger(suite.logger)
|
||||
|
||||
suite.clock = clock.NewMock()
|
||||
suite.NotNil(ocpp.Instance())
|
||||
}
|
||||
|
||||
func (suite *ocppTestSuite) TearDownSuite() {
|
||||
suite.logger.close()
|
||||
}
|
||||
|
||||
func (suite *ocppTestSuite) startChargePoint(id string, connectorId int) (ocpp16.ChargePoint, *ocppj.Client) {
|
||||
// set a handler for all callback functions
|
||||
handler := &ChargePointHandler{
|
||||
|
|
|
|||
|
|
@ -2,27 +2,33 @@ package charger
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ocppLogger struct {
|
||||
t *testing.T
|
||||
mu sync.Mutex
|
||||
t *testing.T
|
||||
}
|
||||
|
||||
func print(t *testing.T, s string) {
|
||||
t.Log((time.Now().Format(time.DateTime)), s)
|
||||
func (l *ocppLogger) close() {
|
||||
l.mu.Lock()
|
||||
l.t = nil
|
||||
l.mu.Unlock()
|
||||
}
|
||||
|
||||
func (l *ocppLogger) Debug(args ...any) { print(l.t, fmt.Sprint(args...)) }
|
||||
func (l *ocppLogger) Debugf(format string, args ...any) {
|
||||
print(l.t, fmt.Sprintf(format, args...))
|
||||
}
|
||||
func (l *ocppLogger) Info(args ...any) { print(l.t, fmt.Sprint(args...)) }
|
||||
func (l *ocppLogger) Infof(format string, args ...any) {
|
||||
print(l.t, fmt.Sprintf(format, args...))
|
||||
}
|
||||
func (l *ocppLogger) Error(args ...any) { print(l.t, fmt.Sprint(args...)) }
|
||||
func (l *ocppLogger) Errorf(format string, args ...any) {
|
||||
print(l.t, fmt.Sprintf(format, args...))
|
||||
func (l *ocppLogger) print(s string) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
if l.t != nil {
|
||||
l.t.Log(time.Now().Format(time.DateTime), s)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ocppLogger) Debug(args ...any) { l.print(fmt.Sprint(args...)) }
|
||||
func (l *ocppLogger) Debugf(format string, args ...any) { l.print(fmt.Sprintf(format, args...)) }
|
||||
func (l *ocppLogger) Info(args ...any) { l.print(fmt.Sprint(args...)) }
|
||||
func (l *ocppLogger) Infof(format string, args ...any) { l.print(fmt.Sprintf(format, args...)) }
|
||||
func (l *ocppLogger) Error(args ...any) { l.print(fmt.Sprint(args...)) }
|
||||
func (l *ocppLogger) Errorf(format string, args ...any) { l.print(fmt.Sprintf(format, args...)) }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue