You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2023/06/01 08:58:20 UTC
[plc4x] 02/02: test(plc4go): avoid global logging in connection cache
This is an automated email from the ASF dual-hosted git repository.
sruehl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git
commit aa260692f3568461191fced8f8539c6fbb1bdd4c
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Thu Jun 1 10:58:10 2023 +0200
test(plc4go): avoid global logging in connection cache
---
plc4go/internal/ads/Connection.go | 7 +-
plc4go/internal/bacnetip/Connection.go | 7 +-
plc4go/internal/cbus/Connection.go | 7 +-
plc4go/internal/cbus/Connection_test.go | 51 ++++----
plc4go/internal/cbus/Reader_test.go | 45 ++++---
plc4go/internal/eip/Connection.go | 5 +-
plc4go/internal/knxnetip/Connection.go | 7 +-
plc4go/internal/modbus/Connection.go | 7 +-
plc4go/internal/s7/Connection.go | 7 +-
plc4go/internal/simulated/Connection.go | 7 +-
plc4go/internal/simulated/Reader.go | 6 +-
plc4go/internal/simulated/Subscriber.go | 6 +-
plc4go/internal/simulated/Writer.go | 6 +-
plc4go/pkg/api/cache/PlcConnectionCache.go | 49 ++++----
plc4go/pkg/api/cache/PlcConnectionCache_test.go | 43 ++++---
plc4go/pkg/api/cache/common.go | 22 +++-
plc4go/pkg/api/cache/connectionContainer.go | 132 +++++++++++----------
plc4go/pkg/api/cache/connectionContainer_test.go | 127 +++++++++++++-------
.../pkg/api/cache/mock_tracedPlcConnection_test.go | 3 +-
...{tracedPlcConnection.go => noGlobalLog_test.go} | 14 +--
plc4go/pkg/api/cache/plcConnectionLease.go | 6 +-
plc4go/pkg/api/cache/tracedPlcConnection.go | 4 +-
plc4go/spi/default/DefaultConnection.go | 9 +-
plc4go/spi/default/DefaultConnection_test.go | 11 +-
.../mock_DefaultPlcConnectionCloseResult_test.go | 3 +-
plc4go/spi/{ => tracer}/Tracer.go | 20 +++-
plc4go/spi/{ => tracer}/Tracer_test.go | 2 +-
.../spi/{ => tracer}/mock_TracerProvider_test.go | 6 +-
plc4go/spi/utils/IdGenerator.go | 5 +-
plc4go/spi/utils/IdGenerator_test.go | 3 +-
30 files changed, 355 insertions(+), 272 deletions(-)
diff --git a/plc4go/internal/ads/Connection.go b/plc4go/internal/ads/Connection.go
index e1ba92bb86..3e08d7ad8d 100644
--- a/plc4go/internal/ads/Connection.go
+++ b/plc4go/internal/ads/Connection.go
@@ -24,6 +24,7 @@ import (
"encoding/binary"
"fmt"
"github.com/apache/plc4x/plc4go/spi/options"
+ "github.com/apache/plc4x/plc4go/spi/tracer"
"strconv"
"strings"
@@ -50,7 +51,7 @@ type Connection struct {
requestInterceptor interceptors.RequestInterceptor
configuration model.Configuration
driverContext *DriverContext
- tracer *spi.Tracer
+ tracer *tracer.Tracer
subscriptions map[uint32]apiModel.PlcSubscriptionHandle
}
@@ -69,7 +70,7 @@ func NewConnection(messageCodec spi.MessageCodec, configuration model.Configurat
if traceEnabledOption, ok := options["traceEnabled"]; ok {
if len(traceEnabledOption) == 1 {
// TODO: Connection Id is probably "" all the time.
- connection.tracer = spi.NewTracer(driverContext.connectionId, _options...)
+ connection.tracer = tracer.NewTracer(driverContext.connectionId, _options...)
}
}
tagHandler := NewTagHandlerWithDriverContext(driverContext)
@@ -89,7 +90,7 @@ func (m *Connection) IsTraceEnabled() bool {
return m.tracer != nil
}
-func (m *Connection) GetTracer() *spi.Tracer {
+func (m *Connection) GetTracer() *tracer.Tracer {
return m.tracer
}
diff --git a/plc4go/internal/bacnetip/Connection.go b/plc4go/internal/bacnetip/Connection.go
index 9d26f22ef4..97791c09e2 100644
--- a/plc4go/internal/bacnetip/Connection.go
+++ b/plc4go/internal/bacnetip/Connection.go
@@ -23,6 +23,7 @@ import (
"context"
"fmt"
"github.com/apache/plc4x/plc4go/spi/options"
+ "github.com/apache/plc4x/plc4go/spi/tracer"
"github.com/apache/plc4x/plc4go/spi/transactions"
"github.com/apache/plc4x/plc4go/spi/utils"
"github.com/pkg/errors"
@@ -45,7 +46,7 @@ type Connection struct {
tm transactions.RequestTransactionManager
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
}
func NewConnection(messageCodec spi.MessageCodec, tagHandler spi.PlcTagHandler, tm transactions.RequestTransactionManager, options map[string][]string, _options ...options.WithOption) *Connection {
@@ -56,7 +57,7 @@ func NewConnection(messageCodec spi.MessageCodec, tagHandler spi.PlcTagHandler,
}
if traceEnabledOption, ok := options["traceEnabled"]; ok {
if len(traceEnabledOption) == 1 {
- connection.tracer = spi.NewTracer(connection.connectionId, _options...)
+ connection.tracer = tracer.NewTracer(connection.connectionId, _options...)
}
}
connection.DefaultConnection = _default.NewDefaultConnection(connection,
@@ -74,7 +75,7 @@ func (c *Connection) IsTraceEnabled() bool {
return c.tracer != nil
}
-func (c *Connection) GetTracer() *spi.Tracer {
+func (c *Connection) GetTracer() *tracer.Tracer {
return c.tracer
}
diff --git a/plc4go/internal/cbus/Connection.go b/plc4go/internal/cbus/Connection.go
index 24e0ba0616..ec500374f8 100644
--- a/plc4go/internal/cbus/Connection.go
+++ b/plc4go/internal/cbus/Connection.go
@@ -23,6 +23,7 @@ import (
"context"
"fmt"
"github.com/apache/plc4x/plc4go/spi/options"
+ "github.com/apache/plc4x/plc4go/spi/tracer"
"github.com/apache/plc4x/plc4go/spi/transactions"
"github.com/rs/zerolog"
"sync"
@@ -66,7 +67,7 @@ type Connection struct {
driverContext DriverContext
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
log zerolog.Logger
}
@@ -83,7 +84,7 @@ func NewConnection(messageCodec *MessageCodec, configuration Configuration, driv
}
if traceEnabledOption, ok := connectionOptions["traceEnabled"]; ok {
if len(traceEnabledOption) == 1 {
- connection.tracer = spi.NewTracer(connection.connectionId, _options...)
+ connection.tracer = tracer.NewTracer(connection.connectionId, _options...)
}
}
connection.DefaultConnection = _default.NewDefaultConnection(
@@ -104,7 +105,7 @@ func (c *Connection) IsTraceEnabled() bool {
return c.tracer != nil
}
-func (c *Connection) GetTracer() *spi.Tracer {
+func (c *Connection) GetTracer() *tracer.Tracer {
return c.tracer
}
diff --git a/plc4go/internal/cbus/Connection_test.go b/plc4go/internal/cbus/Connection_test.go
index 80d0e04a71..657d30a0f8 100644
--- a/plc4go/internal/cbus/Connection_test.go
+++ b/plc4go/internal/cbus/Connection_test.go
@@ -28,6 +28,7 @@ import (
_default "github.com/apache/plc4x/plc4go/spi/default"
"github.com/apache/plc4x/plc4go/spi/options"
"github.com/apache/plc4x/plc4go/spi/testutils"
+ "github.com/apache/plc4x/plc4go/spi/tracer"
"github.com/apache/plc4x/plc4go/spi/transactions"
"github.com/apache/plc4x/plc4go/spi/transports"
"github.com/apache/plc4x/plc4go/spi/transports/test"
@@ -93,7 +94,7 @@ func TestConnection_BrowseRequestBuilder(t *testing.T) {
configuration Configuration
driverContext DriverContext
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
log zerolog.Logger
}
tests := []struct {
@@ -138,7 +139,7 @@ func TestConnection_ConnectWithContext(t *testing.T) {
configuration Configuration
driverContext DriverContext
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
log zerolog.Logger
}
type args struct {
@@ -240,7 +241,7 @@ func TestConnection_GetConnection(t *testing.T) {
configuration Configuration
driverContext DriverContext
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
log zerolog.Logger
}
tests := []struct {
@@ -282,7 +283,7 @@ func TestConnection_GetConnectionId(t *testing.T) {
configuration Configuration
driverContext DriverContext
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
log zerolog.Logger
}
tests := []struct {
@@ -321,7 +322,7 @@ func TestConnection_GetMessageCodec(t *testing.T) {
configuration Configuration
driverContext DriverContext
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
log zerolog.Logger
}
tests := []struct {
@@ -364,7 +365,7 @@ func TestConnection_GetMetadata(t *testing.T) {
configuration Configuration
driverContext DriverContext
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
log zerolog.Logger
}
tests := []struct {
@@ -410,13 +411,13 @@ func TestConnection_GetTracer(t *testing.T) {
configuration Configuration
driverContext DriverContext
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
log zerolog.Logger
}
tests := []struct {
name string
fields fields
- want *spi.Tracer
+ want *tracer.Tracer
}{
{
name: "just nil",
@@ -449,7 +450,7 @@ func TestConnection_IsTraceEnabled(t *testing.T) {
configuration Configuration
driverContext DriverContext
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
log zerolog.Logger
}
tests := []struct {
@@ -488,7 +489,7 @@ func TestConnection_ReadRequestBuilder(t *testing.T) {
configuration Configuration
driverContext DriverContext
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
log zerolog.Logger
}
tests := []struct {
@@ -533,7 +534,7 @@ func TestConnection_String(t *testing.T) {
configuration Configuration
driverContext DriverContext
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
log zerolog.Logger
}
tests := []struct {
@@ -573,7 +574,7 @@ func TestConnection_SubscriptionRequestBuilder(t *testing.T) {
configuration Configuration
driverContext DriverContext
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
log zerolog.Logger
}
tests := []struct {
@@ -618,7 +619,7 @@ func TestConnection_UnsubscriptionRequestBuilder(t *testing.T) {
configuration Configuration
driverContext DriverContext
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
log zerolog.Logger
}
tests := []struct {
@@ -657,7 +658,7 @@ func TestConnection_WriteRequestBuilder(t *testing.T) {
configuration Configuration
driverContext DriverContext
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
log zerolog.Logger
}
tests := []struct {
@@ -702,7 +703,7 @@ func TestConnection_addSubscriber(t *testing.T) {
configuration Configuration
driverContext DriverContext
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
log zerolog.Logger
}
type args struct {
@@ -759,7 +760,7 @@ func TestConnection_fireConnected(t *testing.T) {
configuration Configuration
driverContext DriverContext
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
log zerolog.Logger
}
type args struct {
@@ -823,7 +824,7 @@ func TestConnection_fireConnectionError(t *testing.T) {
configuration Configuration
driverContext DriverContext
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
log zerolog.Logger
}
type args struct {
@@ -936,7 +937,7 @@ func TestConnection_sendCalDataWrite(t *testing.T) {
configuration Configuration
driverContext DriverContext
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
log zerolog.Logger
}
type args struct {
@@ -1028,7 +1029,7 @@ func TestConnection_sendReset(t *testing.T) {
configuration Configuration
driverContext DriverContext
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
log zerolog.Logger
}
type args struct {
@@ -1117,7 +1118,7 @@ func TestConnection_setApplicationFilter(t *testing.T) {
configuration Configuration
driverContext DriverContext
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
log zerolog.Logger
}
type args struct {
@@ -1206,7 +1207,7 @@ func TestConnection_setInterface1PowerUpSettings(t *testing.T) {
configuration Configuration
driverContext DriverContext
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
log zerolog.Logger
}
type args struct {
@@ -1295,7 +1296,7 @@ func TestConnection_setInterfaceOptions1(t *testing.T) {
configuration Configuration
driverContext DriverContext
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
log zerolog.Logger
}
type args struct {
@@ -1384,7 +1385,7 @@ func TestConnection_setInterfaceOptions3(t *testing.T) {
configuration Configuration
driverContext DriverContext
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
log zerolog.Logger
}
type args struct {
@@ -1473,7 +1474,7 @@ func TestConnection_setupConnection(t *testing.T) {
configuration Configuration
driverContext DriverContext
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
log zerolog.Logger
}
type args struct {
@@ -1938,7 +1939,7 @@ func TestConnection_startSubscriptionHandler(t *testing.T) {
configuration Configuration
driverContext DriverContext
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
log zerolog.Logger
}
tests := []struct {
diff --git a/plc4go/internal/cbus/Reader_test.go b/plc4go/internal/cbus/Reader_test.go
index c9a5cb4903..646e5e04a2 100644
--- a/plc4go/internal/cbus/Reader_test.go
+++ b/plc4go/internal/cbus/Reader_test.go
@@ -420,29 +420,6 @@ func TestReader_sendMessageOverTheWire(t *testing.T) {
name: "Send message empty message",
fields: fields{
alphaGenerator: &AlphaGenerator{currentAlpha: 'g'},
- messageCodec: func() *MessageCodec {
- transport := test.NewTransport()
- transportUrl := url.URL{Scheme: "test"}
- transportInstance, err := transport.CreateTransportInstance(transportUrl, nil)
- if err != nil {
- t.Error(err)
- t.FailNow()
- return nil
- }
- codec := NewMessageCodec(transportInstance)
- t.Cleanup(func() {
- if err := codec.Disconnect(); err != nil {
- t.Error(err)
- }
- })
- err = codec.Connect()
- if err != nil {
- t.Error(err)
- t.FailNow()
- return nil
- }
- return codec
- }(),
},
args: args{
ctx: func() context.Context {
@@ -468,6 +445,28 @@ func TestReader_sendMessageOverTheWire(t *testing.T) {
},
},
setup: func(t *testing.T, fields *fields, args *args) {
+ loggerOption := options.WithCustomLogger(testutils.ProduceTestingLogger(t))
+
+ transport := test.NewTransport(loggerOption)
+ transportUrl := url.URL{Scheme: "test"}
+ transportInstance, err := transport.CreateTransportInstance(transportUrl, nil, loggerOption)
+ if err != nil {
+ t.Error(err)
+ t.FailNow()
+ }
+ codec := NewMessageCodec(transportInstance, loggerOption)
+ t.Cleanup(func() {
+ if err := codec.Disconnect(); err != nil {
+ t.Error(err)
+ }
+ })
+ err = codec.Connect()
+ if err != nil {
+ t.Error(err)
+ t.FailNow()
+ }
+ fields.messageCodec = codec
+
transaction := NewMockRequestTransaction(t)
expect := transaction.EXPECT()
expect.FailRequest(mock.Anything).Return(errors.New("no I say"))
diff --git a/plc4go/internal/eip/Connection.go b/plc4go/internal/eip/Connection.go
index 2524e333db..418922e563 100644
--- a/plc4go/internal/eip/Connection.go
+++ b/plc4go/internal/eip/Connection.go
@@ -22,6 +22,7 @@ package eip
import (
"context"
"fmt"
+ "github.com/apache/plc4x/plc4go/spi/tracer"
"github.com/apache/plc4x/plc4go/spi/transactions"
"github.com/apache/plc4x/plc4go/pkg/api"
@@ -56,7 +57,7 @@ type Connection struct {
useMessageRouter bool
useConnectionManager bool
routingAddress []readWriteModel.PathSegment
- tracer *spi.Tracer
+ tracer *tracer.Tracer
}
func NewConnection(messageCodec spi.MessageCodec, configuration Configuration, driverContext DriverContext, tagHandler spi.PlcTagHandler, tm transactions.RequestTransactionManager, options map[string][]string) *Connection {
@@ -91,7 +92,7 @@ func (m *Connection) IsTraceEnabled() bool {
return m.tracer != nil
}
-func (m *Connection) GetTracer() *spi.Tracer {
+func (m *Connection) GetTracer() *tracer.Tracer {
return m.tracer
}
diff --git a/plc4go/internal/knxnetip/Connection.go b/plc4go/internal/knxnetip/Connection.go
index e9d32e70a8..9646c73672 100644
--- a/plc4go/internal/knxnetip/Connection.go
+++ b/plc4go/internal/knxnetip/Connection.go
@@ -25,6 +25,7 @@ import (
"encoding/hex"
"fmt"
"github.com/apache/plc4x/plc4go/spi/options"
+ "github.com/apache/plc4x/plc4go/spi/tracer"
"strconv"
"strings"
"sync"
@@ -134,7 +135,7 @@ type Connection struct {
handleTunnelingRequests bool
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
}
func (m *Connection) String() string {
@@ -190,7 +191,7 @@ func NewConnection(transportInstance transports.TransportInstance, options map[s
if traceEnabledOption, ok := options["traceEnabled"]; ok {
if len(traceEnabledOption) == 1 {
- connection.tracer = spi.NewTracer(connection.connectionId, _options...)
+ connection.tracer = tracer.NewTracer(connection.connectionId, _options...)
}
}
// If a building key was provided, save that in a dedicated variable
@@ -212,7 +213,7 @@ func (m *Connection) IsTraceEnabled() bool {
return m.tracer != nil
}
-func (m *Connection) GetTracer() *spi.Tracer {
+func (m *Connection) GetTracer() *tracer.Tracer {
return m.tracer
}
diff --git a/plc4go/internal/modbus/Connection.go b/plc4go/internal/modbus/Connection.go
index a2366dc59d..55a93f5120 100644
--- a/plc4go/internal/modbus/Connection.go
+++ b/plc4go/internal/modbus/Connection.go
@@ -23,6 +23,7 @@ import (
"context"
"fmt"
"github.com/apache/plc4x/plc4go/spi/options"
+ "github.com/apache/plc4x/plc4go/spi/tracer"
"time"
"github.com/apache/plc4x/plc4go/pkg/api"
@@ -45,7 +46,7 @@ type Connection struct {
requestInterceptor interceptors.RequestInterceptor
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
}
func NewConnection(unitIdentifier uint8, messageCodec spi.MessageCodec, options map[string][]string, tagHandler spi.PlcTagHandler, _options ...options.WithOption) *Connection {
@@ -63,7 +64,7 @@ func NewConnection(unitIdentifier uint8, messageCodec spi.MessageCodec, options
}
if traceEnabledOption, ok := options["traceEnabled"]; ok {
if len(traceEnabledOption) == 1 {
- connection.tracer = spi.NewTracer(connection.connectionId, _options...)
+ connection.tracer = tracer.NewTracer(connection.connectionId, _options...)
}
}
connection.DefaultConnection = _default.NewDefaultConnection(connection,
@@ -82,7 +83,7 @@ func (m *Connection) IsTraceEnabled() bool {
return m.tracer != nil
}
-func (m *Connection) GetTracer() *spi.Tracer {
+func (m *Connection) GetTracer() *tracer.Tracer {
return m.tracer
}
diff --git a/plc4go/internal/s7/Connection.go b/plc4go/internal/s7/Connection.go
index 579beaccad..29e9f92232 100644
--- a/plc4go/internal/s7/Connection.go
+++ b/plc4go/internal/s7/Connection.go
@@ -23,6 +23,7 @@ import (
"context"
"fmt"
"github.com/apache/plc4x/plc4go/spi/options"
+ "github.com/apache/plc4x/plc4go/spi/tracer"
"github.com/apache/plc4x/plc4go/spi/transactions"
"reflect"
"strings"
@@ -65,7 +66,7 @@ type Connection struct {
tm transactions.RequestTransactionManager
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
}
func NewConnection(messageCodec spi.MessageCodec, configuration Configuration, driverContext DriverContext, tagHandler spi.PlcTagHandler, tm transactions.RequestTransactionManager, options map[string][]string, _options ...options.WithOption) *Connection {
@@ -78,7 +79,7 @@ func NewConnection(messageCodec spi.MessageCodec, configuration Configuration, d
}
if traceEnabledOption, ok := options["traceEnabled"]; ok {
if len(traceEnabledOption) == 1 {
- connection.tracer = spi.NewTracer(connection.connectionId, _options...)
+ connection.tracer = tracer.NewTracer(connection.connectionId, _options...)
}
}
connection.DefaultConnection = _default.NewDefaultConnection(connection,
@@ -96,7 +97,7 @@ func (m *Connection) IsTraceEnabled() bool {
return m.tracer != nil
}
-func (m *Connection) GetTracer() *spi.Tracer {
+func (m *Connection) GetTracer() *tracer.Tracer {
return m.tracer
}
diff --git a/plc4go/internal/simulated/Connection.go b/plc4go/internal/simulated/Connection.go
index df2139d066..f429056a1a 100644
--- a/plc4go/internal/simulated/Connection.go
+++ b/plc4go/internal/simulated/Connection.go
@@ -21,6 +21,7 @@ package simulated
import (
"context"
+ "github.com/apache/plc4x/plc4go/spi/tracer"
"strconv"
"time"
@@ -43,7 +44,7 @@ type Connection struct {
options map[string][]string
connected bool
connectionId string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
log zerolog.Logger
}
@@ -62,7 +63,7 @@ func NewConnection(device *Device, tagHandler spi.PlcTagHandler, valueHandler sp
}
if traceEnabledOption, ok := connectionOptions["traceEnabled"]; ok {
if len(traceEnabledOption) == 1 {
- connection.tracer = spi.NewTracer(connection.connectionId, _options...)
+ connection.tracer = tracer.NewTracer(connection.connectionId, _options...)
}
}
return connection
@@ -76,7 +77,7 @@ func (c *Connection) IsTraceEnabled() bool {
return c.tracer != nil
}
-func (c *Connection) GetTracer() *spi.Tracer {
+func (c *Connection) GetTracer() *tracer.Tracer {
return c.tracer
}
diff --git a/plc4go/internal/simulated/Reader.go b/plc4go/internal/simulated/Reader.go
index 4be8926393..3958fc2ebc 100644
--- a/plc4go/internal/simulated/Reader.go
+++ b/plc4go/internal/simulated/Reader.go
@@ -21,23 +21,23 @@ package simulated
import (
"context"
+ "github.com/apache/plc4x/plc4go/spi/tracer"
"github.com/pkg/errors"
"strconv"
"time"
apiModel "github.com/apache/plc4x/plc4go/pkg/api/model"
apiValues "github.com/apache/plc4x/plc4go/pkg/api/values"
- "github.com/apache/plc4x/plc4go/spi"
spiModel "github.com/apache/plc4x/plc4go/spi/model"
)
type Reader struct {
device *Device
options map[string][]string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
}
-func NewReader(device *Device, options map[string][]string, tracer *spi.Tracer) *Reader {
+func NewReader(device *Device, options map[string][]string, tracer *tracer.Tracer) *Reader {
return &Reader{
device: device,
options: options,
diff --git a/plc4go/internal/simulated/Subscriber.go b/plc4go/internal/simulated/Subscriber.go
index 7bda6bad18..30f7b6a196 100644
--- a/plc4go/internal/simulated/Subscriber.go
+++ b/plc4go/internal/simulated/Subscriber.go
@@ -22,18 +22,18 @@ package simulated
import (
"context"
apiModel "github.com/apache/plc4x/plc4go/pkg/api/model"
- "github.com/apache/plc4x/plc4go/spi"
spiModel "github.com/apache/plc4x/plc4go/spi/model"
+ "github.com/apache/plc4x/plc4go/spi/tracer"
"github.com/pkg/errors"
)
type Subscriber struct {
device *Device
options map[string][]string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
}
-func NewSubscriber(device *Device, options map[string][]string, tracer *spi.Tracer) *Subscriber {
+func NewSubscriber(device *Device, options map[string][]string, tracer *tracer.Tracer) *Subscriber {
return &Subscriber{
device: device,
options: options,
diff --git a/plc4go/internal/simulated/Writer.go b/plc4go/internal/simulated/Writer.go
index 08dfa93836..44f39a86cb 100644
--- a/plc4go/internal/simulated/Writer.go
+++ b/plc4go/internal/simulated/Writer.go
@@ -21,22 +21,22 @@ package simulated
import (
"context"
+ "github.com/apache/plc4x/plc4go/spi/tracer"
"github.com/pkg/errors"
"strconv"
"time"
apiModel "github.com/apache/plc4x/plc4go/pkg/api/model"
- "github.com/apache/plc4x/plc4go/spi"
spiModel "github.com/apache/plc4x/plc4go/spi/model"
)
type Writer struct {
device *Device
options map[string][]string
- tracer *spi.Tracer
+ tracer *tracer.Tracer
}
-func NewWriter(device *Device, options map[string][]string, tracer *spi.Tracer) *Writer {
+func NewWriter(device *Device, options map[string][]string, tracer *tracer.Tracer) *Writer {
return &Writer{
device: device,
options: options,
diff --git a/plc4go/pkg/api/cache/PlcConnectionCache.go b/plc4go/pkg/api/cache/PlcConnectionCache.go
index 5033028639..ec17b955b7 100644
--- a/plc4go/pkg/api/cache/PlcConnectionCache.go
+++ b/plc4go/pkg/api/cache/PlcConnectionCache.go
@@ -23,12 +23,12 @@ import (
"fmt"
"github.com/apache/plc4x/plc4go/pkg/api"
"github.com/apache/plc4x/plc4go/pkg/api/config"
- "github.com/apache/plc4x/plc4go/spi"
_default "github.com/apache/plc4x/plc4go/spi/default"
+ "github.com/apache/plc4x/plc4go/spi/options"
+ "github.com/apache/plc4x/plc4go/spi/tracer"
"github.com/apache/plc4x/plc4go/spi/utils"
"github.com/pkg/errors"
"github.com/rs/zerolog"
- "github.com/rs/zerolog/log"
"github.com/viney-shih/go-lock"
"time"
)
@@ -39,13 +39,13 @@ type PlcConnectionCache interface {
}
func NewPlcConnectionCache(driverManager plc4go.PlcDriverManager, withConnectionCacheOptions ...WithConnectionCacheOption) PlcConnectionCache {
- cacheLog := log.Logger
+ var log zerolog.Logger
if !config.TraceConnectionCache {
- cacheLog = zerolog.Nop()
+ log = zerolog.Nop()
}
maxLeaseTime := time.Second * 5
cc := &plcConnectionCache{
- cacheLog: cacheLog,
+ log: log,
driverManager: driverManager,
maxLeaseTime: maxLeaseTime,
maxWaitTime: maxLeaseTime * 5,
@@ -79,9 +79,14 @@ func WithTracer() WithConnectionCacheOption {
}
}
+// Deprecated: use WithCustomLogger
func WithLogger(logger zerolog.Logger) WithConnectionCacheOption {
+ return WithCustomLogger(logger)
+}
+
+func WithCustomLogger(logger zerolog.Logger) WithConnectionCacheOption {
return func(plcConnectionCache *plcConnectionCache) {
- plcConnectionCache.cacheLog = logger
+ plcConnectionCache.log = logger
}
}
@@ -92,8 +97,6 @@ func WithLogger(logger zerolog.Logger) WithConnectionCacheOption {
//
type plcConnectionCache struct {
- cacheLog zerolog.Logger
-
driverManager plc4go.PlcDriverManager
// Maximum duration a connection can be used per lease.
@@ -103,7 +106,9 @@ type plcConnectionCache struct {
cacheLock lock.RWMutex
connections map[string]*connectionContainer
- tracer *spi.Tracer
+ tracer *tracer.Tracer
+
+ log zerolog.Logger
}
func (t *plcConnectionCache) onConnectionEvent(event connectionEvent) {
@@ -112,7 +117,7 @@ func (t *plcConnectionCache) onConnectionEvent(event connectionEvent) {
if t.tracer != nil {
t.tracer.AddTrace("destroy-connection", errorEvent.getError().Error())
}
- t.cacheLog.Debug().Str("connectionString", connectionContainerInstance.connectionString)
+ t.log.Debug().Str("connectionString", connectionContainerInstance.connectionString)
}
}
@@ -123,10 +128,10 @@ func (t *plcConnectionCache) onConnectionEvent(event connectionEvent) {
///////////////////////////////////////
func (t *plcConnectionCache) EnableTracer() {
- t.tracer = spi.NewTracer("cache")
+ t.tracer = tracer.NewTracer("cache", options.WithCustomLogger(t.log))
}
-func (t *plcConnectionCache) GetTracer() *spi.Tracer {
+func (t *plcConnectionCache) GetTracer() *tracer.Tracer {
return t.tracer
}
@@ -142,9 +147,9 @@ func (t *plcConnectionCache) GetConnection(connectionString string) <-chan plc4g
if t.tracer != nil {
t.tracer.AddTrace("get-connection", "create new cached connection")
}
- t.cacheLog.Debug().Str("connectionString", connectionString).Msg("Create new cached connection")
+ t.log.Debug().Str("connectionString", connectionString).Msg("Create new cached connection")
// Create a new connection container.
- cc := newConnectionContainer(&t.cacheLog, t.driverManager, connectionString)
+ cc := newConnectionContainer(t.log, t.driverManager, connectionString)
// Register for connection events (Like connection closed or error).
cc.addListener(t)
// Store the new connection container in the cache of connections.
@@ -172,7 +177,7 @@ func (t *plcConnectionCache) GetConnection(connectionString string) <-chan plc4g
select {
// Wait till we get a lease.
case connectionResponse := <-leaseChan:
- t.cacheLog.Debug().Str("connectionString", connectionString).Msg("Successfully got lease to connection")
+ t.log.Debug().Str("connectionString", connectionString).Msg("Successfully got lease to connection")
responseTimeout := time.NewTimer(10 * time.Millisecond)
defer utils.CleanupTimer(responseTimeout)
select {
@@ -186,7 +191,7 @@ func (t *plcConnectionCache) GetConnection(connectionString string) <-chan plc4g
t.tracer.AddTransactionalTrace(txId, "get-connection", "client given up")
}
close(ch)
- t.cacheLog.Debug().Str("connectionString", connectionString).Msg("Client not available returning connection to cache.")
+ t.log.Debug().Str("connectionString", connectionString).Msg("Client not available returning connection to cache.")
// Return the connection to give another connection the chance to use it.
if connectionResponse.GetConnection() != nil {
connectionResponse.GetConnection().Close()
@@ -203,7 +208,7 @@ func (t *plcConnectionCache) GetConnection(connectionString string) <-chan plc4g
if t.tracer != nil {
t.tracer.AddTransactionalTrace(txId, "get-connection", "timeout")
}
- t.cacheLog.Debug().Str("connectionString", connectionString).Msg("Timeout while waiting for connection.")
+ t.log.Debug().Str("connectionString", connectionString).Msg("Timeout while waiting for connection.")
ch <- _default.NewDefaultPlcConnectionCloseResult(nil, errors.New("timeout while waiting for connection"))
}
}()
@@ -212,7 +217,7 @@ func (t *plcConnectionCache) GetConnection(connectionString string) <-chan plc4g
}
func (t *plcConnectionCache) Close() <-chan PlcConnectionCacheCloseResult {
- t.cacheLog.Debug().Msg("Closing connection cache started.")
+ t.log.Debug().Msg("Closing connection cache started.")
ch := make(chan PlcConnectionCacheCloseResult)
go func() {
@@ -226,7 +231,7 @@ func (t *plcConnectionCache) Close() <-chan PlcConnectionCacheCloseResult {
case ch <- newDefaultPlcConnectionCacheCloseResult(t, nil):
case <-responseDeliveryTimeout.C:
}
- t.cacheLog.Debug().Msg("Closing connection cache finished.")
+ t.log.Debug().Msg("Closing connection cache finished.")
return
}
@@ -244,14 +249,14 @@ func (t *plcConnectionCache) Close() <-chan PlcConnectionCacheCloseResult {
// We also really don't care if it worked, or not ... it's just an attempt of being
// nice.
case _ = <-leaseResults:
- t.cacheLog.Debug().Str("connectionString", container.connectionString).Msg("Gracefully closing connection ...")
+ t.log.Debug().Str("connectionString", container.connectionString).Msg("Gracefully closing connection ...")
// Give back the connection.
if container.connection != nil {
container.connection.Close()
}
// If we're timing out brutally kill the connection.
case <-closeTimeout.C:
- t.cacheLog.Debug().Str("connectionString", container.connectionString).Msg("Forcefully closing connection ...")
+ t.log.Debug().Str("connectionString", container.connectionString).Msg("Forcefully closing connection ...")
// Forcefully close this connection.
if container.connection != nil {
container.connection.Close()
@@ -264,7 +269,7 @@ func (t *plcConnectionCache) Close() <-chan PlcConnectionCacheCloseResult {
case ch <- newDefaultPlcConnectionCacheCloseResult(t, nil):
case <-responseDeliveryTimeout.C:
}
- t.cacheLog.Debug().Msg("Closing connection cache finished.")
+ t.log.Debug().Msg("Closing connection cache finished.")
}(cc)
}
}()
diff --git a/plc4go/pkg/api/cache/PlcConnectionCache_test.go b/plc4go/pkg/api/cache/PlcConnectionCache_test.go
index 8dd79ce4ab..efb73750c2 100644
--- a/plc4go/pkg/api/cache/PlcConnectionCache_test.go
+++ b/plc4go/pkg/api/cache/PlcConnectionCache_test.go
@@ -20,12 +20,15 @@
package cache
import (
+ "github.com/apache/plc4x/plc4go/pkg/api/config"
+ "github.com/apache/plc4x/plc4go/spi/options"
+ "github.com/apache/plc4x/plc4go/spi/testutils"
+ "github.com/apache/plc4x/plc4go/spi/tracer"
"testing"
"time"
"github.com/apache/plc4x/plc4go/internal/simulated"
"github.com/apache/plc4x/plc4go/pkg/api"
- "github.com/apache/plc4x/plc4go/spi"
_default "github.com/apache/plc4x/plc4go/spi/default"
"github.com/stretchr/testify/assert"
"github.com/viney-shih/go-lock"
@@ -44,41 +47,45 @@ func TestPlcConnectionCache_GetConnection(t *testing.T) {
name string
fields fields
args args
+ setup func(t *testing.T, fields *fields, args *args)
wantErr bool
wantTimeout bool
}{
{
name: "simple",
- fields: fields{
- driverManager: func() plc4go.PlcDriverManager {
- driverManager := plc4go.NewPlcDriverManager()
- driverManager.RegisterDriver(simulated.NewDriver())
- return driverManager
- }(),
- }, args: args{
+ args: args{
connectionString: "simulated://1.2.3.4:42",
},
+ setup: func(t *testing.T, fields *fields, args *args) {
+ logger := testutils.ProduceTestingLogger(t)
+ driverManager := plc4go.NewPlcDriverManager(config.WithCustomLogger(logger))
+ driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
+ fields.driverManager = driverManager
+ },
wantErr: false,
wantTimeout: false,
},
{
name: "simpleWithTimeout",
- fields: fields{
- driverManager: func() plc4go.PlcDriverManager {
- driverManager := plc4go.NewPlcDriverManager()
- driverManager.RegisterDriver(simulated.NewDriver())
- return driverManager
- }(),
- }, args: args{
+ args: args{
connectionString: "simulated://1.2.3.4:42?connectionDelay=5",
},
+ setup: func(t *testing.T, fields *fields, args *args) {
+ logger := testutils.ProduceTestingLogger(t)
+ driverManager := plc4go.NewPlcDriverManager(config.WithCustomLogger(logger))
+ driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
+ fields.driverManager = driverManager
+ },
wantErr: false,
wantTimeout: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- cc := NewPlcConnectionCache(tt.fields.driverManager)
+ if tt.setup != nil {
+ tt.setup(t, &tt.fields, &tt.args)
+ }
+ cc := NewPlcConnectionCache(tt.fields.driverManager, WithCustomLogger(testutils.ProduceTestingLogger(t)))
got := cc.GetConnection(tt.args.connectionString)
select {
case connectResult := <-got:
@@ -182,8 +189,8 @@ func TestPlcConnectionCache_Close(t *testing.T) {
}
}
-func readFromPlc(t *testing.T, cache plcConnectionCache, connectionString string, resourceString string) <-chan []spi.TraceEntry {
- ch := make(chan []spi.TraceEntry)
+func readFromPlc(t *testing.T, cache plcConnectionCache, connectionString string, resourceString string) <-chan []tracer.TraceEntry {
+ ch := make(chan []tracer.TraceEntry)
// Get a connection
connectionResultChan := cache.GetConnection(connectionString)
diff --git a/plc4go/pkg/api/cache/common.go b/plc4go/pkg/api/cache/common.go
index 906f15778c..9bd3b584d8 100644
--- a/plc4go/pkg/api/cache/common.go
+++ b/plc4go/pkg/api/cache/common.go
@@ -19,6 +19,17 @@
package cache
+type PlcConnectionCacheCloseResult interface {
+ GetConnectionCache() PlcConnectionCache
+ GetErr() error
+}
+
+///////////////////////////////////////
+///////////////////////////////////////
+//
+// Internal section
+//
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cachedPlcConnectionState
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -77,11 +88,6 @@ func (c connectionErrorEvent) getError() error {
// PlcConnectionCacheCloseResult / plcConnectionCacheCloseResult
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-type PlcConnectionCacheCloseResult interface {
- GetConnectionCache() PlcConnectionCache
- GetErr() error
-}
-
type plcConnectionCacheCloseResult struct {
connectionCache PlcConnectionCache
err error
@@ -101,3 +107,9 @@ func (p plcConnectionCacheCloseResult) GetConnectionCache() PlcConnectionCache {
func (p plcConnectionCacheCloseResult) GetErr() error {
return p.err
}
+
+//
+// Internal section
+//
+///////////////////////////////////////
+///////////////////////////////////////
diff --git a/plc4go/pkg/api/cache/connectionContainer.go b/plc4go/pkg/api/cache/connectionContainer.go
index faca8d3606..18987eaafa 100644
--- a/plc4go/pkg/api/cache/connectionContainer.go
+++ b/plc4go/pkg/api/cache/connectionContainer.go
@@ -29,7 +29,6 @@ import (
)
type connectionContainer struct {
- cacheLog *zerolog.Logger
lock lock.RWMutex
connectionString string
driverManager plc4go.PlcDriverManager
@@ -43,11 +42,12 @@ type connectionContainer struct {
queue []chan plc4go.PlcConnectionConnectResult
// Listeners for connection events.
listeners []connectionListener
+
+ log zerolog.Logger
}
-func newConnectionContainer(cacheLog *zerolog.Logger, driverManager plc4go.PlcDriverManager, connectionString string) *connectionContainer {
+func newConnectionContainer(log zerolog.Logger, driverManager plc4go.PlcDriverManager, connectionString string) *connectionContainer {
return &connectionContainer{
- cacheLog: cacheLog,
driverManager: driverManager,
connectionString: connectionString,
lock: lock.NewCASMutex(),
@@ -55,13 +55,15 @@ func newConnectionContainer(cacheLog *zerolog.Logger, driverManager plc4go.PlcDr
closed: false,
state: StateInitialized,
queue: []chan plc4go.PlcConnectionConnectResult{},
+
+ log: log,
}
}
-func (t *connectionContainer) connect() {
- t.cacheLog.Debug().Str("connectionString", t.connectionString).Msg("Connecting new cached connection ...")
+func (c *connectionContainer) connect() {
+ c.log.Debug().Str("connectionString", c.connectionString).Msg("Connecting new cached connection ...")
// Initialize the new connection.
- connectionResultChan := t.driverManager.GetConnection(t.connectionString)
+ connectionResultChan := c.driverManager.GetConnection(c.connectionString)
// Allow us to finish this function and return the lock quickly
// Wait for the connection to be established.
@@ -69,85 +71,85 @@ func (t *connectionContainer) connect() {
connectionResult := <-connectionResultChan
// Get the lock.
- t.lock.Lock()
- defer t.lock.Unlock()
+ c.lock.Lock()
+ defer c.lock.Unlock()
// If the connection was successful, pass the active connection into the container.
// If something went wrong, we have to remove the connection from the cache and return the error.
if err := connectionResult.GetErr(); err != nil {
- t.cacheLog.Debug().Str("connectionString", t.connectionString).
+ c.log.Debug().Str("connectionString", c.connectionString).
Err(err).
Msg("Error connecting new cached connection.")
// Tell the connection cache that the connection is no longer available.
- if t.listeners != nil {
+ if c.listeners != nil {
event := connectionErrorEvent{
- conn: *t,
+ conn: *c,
err: err,
}
- for _, listener := range t.listeners {
+ for _, listener := range c.listeners {
listener.onConnectionEvent(event)
}
}
// Send a failure to all waiting clients.
- if len(t.queue) > 0 {
- for _, waitingClient := range t.queue {
+ if len(c.queue) > 0 {
+ for _, waitingClient := range c.queue {
waitingClient <- _default.NewDefaultPlcConnectionConnectResult(nil, err)
}
- t.queue = nil
+ c.queue = nil
}
return
}
- t.cacheLog.Debug().Str("connectionString", t.connectionString).Msg("Successfully connected new cached connection.")
+ c.log.Debug().Str("connectionString", c.connectionString).Msg("Successfully connected new cached connection.")
// Inject the real connection into the container.
if connection, ok := connectionResult.GetConnection().(tracedPlcConnection); !ok {
- panic("Return connection doesn't implement the cache.tracedPlcConnection interface")
+ panic("Return connection doesn'c implement the cache.tracedPlcConnection interface")
} else {
- t.connection = connection
+ c.connection = connection
}
- t.tracerEnabled = t.connection.IsTraceEnabled()
+ c.tracerEnabled = c.connection.IsTraceEnabled()
// Mark the connection as idle for now.
- t.state = StateIdle
+ c.state = StateIdle
// If there is a request in the queue, hand out the connection to that.
- if len(t.queue) > 0 {
+ if len(c.queue) > 0 {
// Get the first in the queue.
- queueHead := t.queue[0]
- t.queue = t.queue[1:]
+ queueHead := c.queue[0]
+ c.queue = c.queue[1:]
// Mark the connection as being used.
- t.state = StateInUse
+ c.state = StateInUse
// Return the lease to the caller.
- connection := newPlcConnectionLease(t, t.leaseCounter, t.connection)
- // In this case we don't need to check for blocks
+ connection := newPlcConnectionLease(c, c.leaseCounter, c.connection)
+ // In this case we don'c need to check for blocks
// as the getConnection function of the connection cache
// is definitely eagerly waiting for input.
queueHead <- _default.NewDefaultPlcConnectionConnectResult(connection, nil)
}
}
-func (t *connectionContainer) addListener(listener connectionListener) {
+func (c *connectionContainer) addListener(listener connectionListener) {
// Get the lock.
- t.lock.Lock()
- defer t.lock.Unlock()
+ c.lock.Lock()
+ defer c.lock.Unlock()
// Add the listener to the queue
- t.listeners = append(t.listeners, listener)
+ c.listeners = append(c.listeners, listener)
}
-func (t *connectionContainer) lease() <-chan plc4go.PlcConnectionConnectResult {
- t.lock.Lock()
- defer t.lock.Unlock()
+func (c *connectionContainer) lease() <-chan plc4go.PlcConnectionConnectResult {
+ c.lock.Lock()
+ defer c.lock.Unlock()
ch := make(chan plc4go.PlcConnectionConnectResult)
// Check if the connection is available.
- switch t.state {
+ switch c.state {
case StateIdle:
- t.leaseCounter++
- connection := newPlcConnectionLease(t, t.leaseCounter, t.connection)
- t.state = StateInUse
- // In this case we don't need to check for blocks
+ c.leaseCounter++
+ connection := newPlcConnectionLease(c, c.leaseCounter, c.connection)
+ c.state = StateInUse
+ // In this case we don'c need to check for blocks
// as the getConnection function of the connection cache
// is definitely eagerly waiting for input.
- t.cacheLog.Debug().Str("connectionString", t.connectionString).
+ c.log.Debug().Str("connectionString", c.connectionString).
Msg("Got lease instantly as connection was idle.")
go func() {
ch <- _default.NewDefaultPlcConnectionConnectResult(connection, nil)
@@ -155,17 +157,17 @@ func (t *connectionContainer) lease() <-chan plc4go.PlcConnectionConnectResult {
case StateInUse, StateInitialized:
// If the connection is currently busy or not finished initializing,
// add the new channel to the queue for this connection.
- t.queue = append(t.queue, ch)
- t.cacheLog.Debug().Str("connectionString", t.connectionString).
- Int("waiting-queue-size", len(t.queue)).
+ c.queue = append(c.queue, ch)
+ c.log.Debug().Str("connectionString", c.connectionString).
+ Int("waiting-queue-size", len(c.queue)).
Msg("Added lease-request to queue.")
case StateInvalid:
- t.cacheLog.Debug().Str("connectionString", t.connectionString).Msg("No lease because invalid")
+ c.log.Debug().Str("connectionString", c.connectionString).Msg("No lease because invalid")
}
return ch
}
-func (t *connectionContainer) returnConnection(newState cachedPlcConnectionState) error {
+func (c *connectionContainer) returnConnection(newState cachedPlcConnectionState) error {
// Intentionally not locking anything, as there are two cases, where the connection is returned:
// 1) The connection failed to get established (No connection has a lock anyway)
// 2) The connection is returned, then the one returning it already has a lock on it.
@@ -173,47 +175,47 @@ func (t *connectionContainer) returnConnection(newState cachedPlcConnectionState
switch newState {
case StateInitialized, StateInvalid:
// TODO: Perhaps do a maximum number of retries and then call failConnection()
- t.cacheLog.Debug().Str("connectionString", t.connectionString).
+ c.log.Debug().Str("connectionString", c.connectionString).
Msgf("Client returned a %s connection, reconnecting.", newState)
- t.connect()
+ c.connect()
default:
- t.cacheLog.Debug().Str("connectionString", t.connectionString).Msg("Client returned valid connection.")
+ c.log.Debug().Str("connectionString", c.connectionString).Msg("Client returned valid connection.")
}
- t.lock.Lock()
- defer t.lock.Unlock()
- if t.connection == nil {
- t.state = StateInvalid
- return errors.New("Can't return a broken connection")
+ c.lock.Lock()
+ defer c.lock.Unlock()
+ if c.connection == nil {
+ c.state = StateInvalid
+ return errors.New("Can'c return a broken connection")
}
// Check how many others are waiting for this connection.
- if len(t.queue) > 0 {
+ if len(c.queue) > 0 {
// There are waiting clients, give the connection to the next client in the line.
- next := t.queue[0]
- t.queue = t.queue[1:]
- t.leaseCounter++
- connection := newPlcConnectionLease(t, t.leaseCounter, t.connection)
+ next := c.queue[0]
+ c.queue = c.queue[1:]
+ c.leaseCounter++
+ connection := newPlcConnectionLease(c, c.leaseCounter, c.connection)
// Send asynchronously as the receiver might have given up waiting,
- // and we don't want anything to block here. 1ms should be enough for
+ // and we don'c want anything to block here. 1ms should be enough for
// the calling process to reach the blocking read.
go func() {
- // In this case we don't need to check for blocks
+ // In this case we don'c need to check for blocks
// as the getConnection function of the connection cache
// is definitely eagerly waiting for input.
next <- _default.NewDefaultPlcConnectionConnectResult(connection, nil)
- t.cacheLog.Debug().Str("connectionString", t.connectionString).
- Int("waiting-queue-size", len(t.queue)).
+ c.log.Debug().Str("connectionString", c.connectionString).
+ Int("waiting-queue-size", len(c.queue)).
Msg("Returned connection to the next client waiting.")
}()
} else {
// Otherwise, just mark the connection as idle.
- t.cacheLog.Debug().Str("connectionString", t.connectionString).
+ c.log.Debug().Str("connectionString", c.connectionString).
Msg("Connection set to 'idle'.")
- t.state = StateIdle
+ c.state = StateIdle
}
return nil
}
-func (t *connectionContainer) String() string {
- return fmt.Sprintf("connectionContainer{%s:%s, leaseCounter: %d, closed: %t, state: %s}", t.connectionString, t.connection, t.leaseCounter, t.closed, t.state)
+func (c *connectionContainer) String() string {
+ return fmt.Sprintf("connectionContainer{%s:%s, leaseCounter: %d, closed: %t, state: %s}", c.connectionString, c.connection, c.leaseCounter, c.closed, c.state)
}
diff --git a/plc4go/pkg/api/cache/connectionContainer_test.go b/plc4go/pkg/api/cache/connectionContainer_test.go
index 1cb1246b0b..44bb7f2295 100644
--- a/plc4go/pkg/api/cache/connectionContainer_test.go
+++ b/plc4go/pkg/api/cache/connectionContainer_test.go
@@ -23,8 +23,10 @@ import (
"fmt"
"github.com/apache/plc4x/plc4go/internal/simulated"
plc4go "github.com/apache/plc4x/plc4go/pkg/api"
+ "github.com/apache/plc4x/plc4go/pkg/api/config"
+ "github.com/apache/plc4x/plc4go/spi/options"
+ "github.com/apache/plc4x/plc4go/spi/testutils"
"github.com/rs/zerolog"
- "github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
"github.com/viney-shih/go-lock"
"testing"
@@ -32,7 +34,7 @@ import (
func Test_connectionContainer_String(t1 *testing.T) {
type fields struct {
- cacheLog *zerolog.Logger
+ log zerolog.Logger
lock lock.RWMutex
connectionString string
driverManager plc4go.PlcDriverManager
@@ -47,14 +49,24 @@ func Test_connectionContainer_String(t1 *testing.T) {
tests := []struct {
name string
fields fields
+ setup func(t *testing.T, fields *fields)
want string
}{
- // TODO: Add test cases.
+ {
+ name: "string it",
+ want: "connectionContainer{:%!s(<nil>), leaseCounter: 0, closed: false, state: StateInitialized}",
+ setup: func(t *testing.T, fields *fields) {
+ fields.log = testutils.ProduceTestingLogger(t)
+ },
+ },
}
for _, tt := range tests {
- t1.Run(tt.name, func(t1 *testing.T) {
- t := &connectionContainer{
- cacheLog: tt.fields.cacheLog,
+ t1.Run(tt.name, func(t *testing.T) {
+ if tt.setup != nil {
+ tt.setup(t, &tt.fields)
+ }
+ c := &connectionContainer{
+ log: tt.fields.log,
lock: tt.fields.lock,
connectionString: tt.fields.connectionString,
driverManager: tt.fields.driverManager,
@@ -66,14 +78,14 @@ func Test_connectionContainer_String(t1 *testing.T) {
queue: tt.fields.queue,
listeners: tt.fields.listeners,
}
- assert.Equalf(t1, tt.want, t.String(), "String()")
+ assert.Equalf(t1, tt.want, c.String(), "String()")
})
}
}
func Test_connectionContainer_addListener(t1 *testing.T) {
type fields struct {
- cacheLog *zerolog.Logger
+ log zerolog.Logger
lock lock.RWMutex
connectionString string
driverManager plc4go.PlcDriverManager
@@ -98,7 +110,7 @@ func Test_connectionContainer_addListener(t1 *testing.T) {
for _, tt := range tests {
t1.Run(tt.name, func(t1 *testing.T) {
t := &connectionContainer{
- cacheLog: tt.fields.cacheLog,
+ log: tt.fields.log,
lock: tt.fields.lock,
connectionString: tt.fields.connectionString,
driverManager: tt.fields.driverManager,
@@ -117,7 +129,7 @@ func Test_connectionContainer_addListener(t1 *testing.T) {
func Test_connectionContainer_connect(t1 *testing.T) {
type fields struct {
- cacheLog *zerolog.Logger
+ log zerolog.Logger
lock lock.RWMutex
connectionString string
driverManager plc4go.PlcDriverManager
@@ -132,26 +144,33 @@ func Test_connectionContainer_connect(t1 *testing.T) {
tests := []struct {
name string
fields fields
+ setup func(t *testing.T, fields *fields)
}{
{
name: "connect fresh",
fields: fields{
- cacheLog: &log.Logger,
- driverManager: func() plc4go.PlcDriverManager {
- driverManager := plc4go.NewPlcDriverManager()
- driverManager.RegisterDriver(simulated.NewDriver())
- return driverManager
- }(),
connectionString: "simulated://1.2.3.4:42",
lock: lock.NewCASMutex(),
queue: []chan plc4go.PlcConnectionConnectResult{},
},
+ setup: func(t *testing.T, fields *fields) {
+ logger := testutils.ProduceTestingLogger(t)
+
+ fields.log = logger
+
+ driverManager := plc4go.NewPlcDriverManager(config.WithCustomLogger(logger))
+ driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
+ fields.driverManager = driverManager
+ },
},
}
for _, tt := range tests {
- t1.Run(tt.name, func(t1 *testing.T) {
- t := &connectionContainer{
- cacheLog: tt.fields.cacheLog,
+ t1.Run(tt.name, func(t *testing.T) {
+ if tt.setup != nil {
+ tt.setup(t, &tt.fields)
+ }
+ c := &connectionContainer{
+ log: tt.fields.log,
lock: tt.fields.lock,
connectionString: tt.fields.connectionString,
driverManager: tt.fields.driverManager,
@@ -163,14 +182,14 @@ func Test_connectionContainer_connect(t1 *testing.T) {
queue: tt.fields.queue,
listeners: tt.fields.listeners,
}
- t.connect()
+ c.connect()
})
}
}
func Test_connectionContainer_lease(t1 *testing.T) {
type fields struct {
- cacheLog *zerolog.Logger
+ log zerolog.Logger
lock lock.RWMutex
connectionString string
driverManager plc4go.PlcDriverManager
@@ -185,12 +204,12 @@ func Test_connectionContainer_lease(t1 *testing.T) {
tests := []struct {
name string
fields fields
+ setup func(t *testing.T, fields *fields)
wantNotNil bool
}{
{
name: "lease fresh",
fields: fields{
- cacheLog: &log.Logger,
driverManager: func() plc4go.PlcDriverManager {
driverManager := plc4go.NewPlcDriverManager()
driverManager.RegisterDriver(simulated.NewDriver())
@@ -200,13 +219,25 @@ func Test_connectionContainer_lease(t1 *testing.T) {
lock: lock.NewCASMutex(),
queue: []chan plc4go.PlcConnectionConnectResult{},
},
+ setup: func(t *testing.T, fields *fields) {
+ logger := testutils.ProduceTestingLogger(t)
+
+ fields.log = logger
+
+ driverManager := plc4go.NewPlcDriverManager(config.WithCustomLogger(logger))
+ driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
+ fields.driverManager = driverManager
+ },
wantNotNil: true,
},
}
for _, tt := range tests {
- t1.Run(tt.name, func(t1 *testing.T) {
- t := &connectionContainer{
- cacheLog: tt.fields.cacheLog,
+ t1.Run(tt.name, func(t *testing.T) {
+ if tt.setup != nil {
+ tt.setup(t, &tt.fields)
+ }
+ c := &connectionContainer{
+ log: tt.fields.log,
lock: tt.fields.lock,
connectionString: tt.fields.connectionString,
driverManager: tt.fields.driverManager,
@@ -218,14 +249,14 @@ func Test_connectionContainer_lease(t1 *testing.T) {
queue: tt.fields.queue,
listeners: tt.fields.listeners,
}
- assert.True(t1, tt.wantNotNil, t.lease(), "lease()")
+ assert.True(t1, tt.wantNotNil, c.lease(), "lease()")
})
}
}
func Test_connectionContainer_returnConnection(t1 *testing.T) {
type fields struct {
- cacheLog *zerolog.Logger
+ log zerolog.Logger
lock lock.RWMutex
connectionString string
driverManager plc4go.PlcDriverManager
@@ -244,17 +275,12 @@ func Test_connectionContainer_returnConnection(t1 *testing.T) {
name string
fields fields
args args
+ setup func(t *testing.T, fields *fields, args *args)
wantErr assert.ErrorAssertionFunc
}{
{
name: "return connection fresh",
fields: fields{
- cacheLog: &log.Logger,
- driverManager: func() plc4go.PlcDriverManager {
- driverManager := plc4go.NewPlcDriverManager()
- driverManager.RegisterDriver(simulated.NewDriver())
- return driverManager
- }(),
connectionString: "simulated://1.2.3.4:42",
lock: lock.NewCASMutex(),
queue: []chan plc4go.PlcConnectionConnectResult{},
@@ -262,17 +288,20 @@ func Test_connectionContainer_returnConnection(t1 *testing.T) {
args: args{
state: StateInitialized,
},
+ setup: func(t *testing.T, fields *fields, args *args) {
+ logger := testutils.ProduceTestingLogger(t)
+
+ fields.log = logger
+
+ driverManager := plc4go.NewPlcDriverManager(config.WithCustomLogger(logger))
+ driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
+ fields.driverManager = driverManager
+ },
wantErr: assert.NoError,
},
{
name: "return unconnected connection",
fields: fields{
- cacheLog: &log.Logger,
- driverManager: func() plc4go.PlcDriverManager {
- driverManager := plc4go.NewPlcDriverManager()
- driverManager.RegisterDriver(simulated.NewDriver())
- return driverManager
- }(),
connectionString: "simulated://1.2.3.4:42",
lock: lock.NewCASMutex(),
queue: []chan plc4go.PlcConnectionConnectResult{},
@@ -280,13 +309,25 @@ func Test_connectionContainer_returnConnection(t1 *testing.T) {
args: args{
state: StateInUse,
},
+ setup: func(t *testing.T, fields *fields, args *args) {
+ logger := testutils.ProduceTestingLogger(t)
+
+ fields.log = logger
+
+ driverManager := plc4go.NewPlcDriverManager(config.WithCustomLogger(logger))
+ driverManager.RegisterDriver(simulated.NewDriver(options.WithCustomLogger(logger)))
+ fields.driverManager = driverManager
+ },
wantErr: assert.Error,
},
}
for _, tt := range tests {
- t1.Run(tt.name, func(t1 *testing.T) {
- t := &connectionContainer{
- cacheLog: tt.fields.cacheLog,
+ t1.Run(tt.name, func(t *testing.T) {
+ if tt.setup != nil {
+ tt.setup(t, &tt.fields, &tt.args)
+ }
+ c := &connectionContainer{
+ log: tt.fields.log,
lock: tt.fields.lock,
connectionString: tt.fields.connectionString,
driverManager: tt.fields.driverManager,
@@ -298,7 +339,7 @@ func Test_connectionContainer_returnConnection(t1 *testing.T) {
queue: tt.fields.queue,
listeners: tt.fields.listeners,
}
- tt.wantErr(t1, t.returnConnection(tt.args.state), fmt.Sprintf("returnConnection(%v)", tt.args.state))
+ tt.wantErr(t1, c.returnConnection(tt.args.state), fmt.Sprintf("returnConnection(%v)", tt.args.state))
})
}
}
diff --git a/plc4go/pkg/api/cache/mock_tracedPlcConnection_test.go b/plc4go/pkg/api/cache/mock_tracedPlcConnection_test.go
index 2efa3c7de8..39011fc323 100644
--- a/plc4go/pkg/api/cache/mock_tracedPlcConnection_test.go
+++ b/plc4go/pkg/api/cache/mock_tracedPlcConnection_test.go
@@ -23,13 +23,12 @@ package cache
import (
context "context"
+ spi "github.com/apache/plc4x/plc4go/spi/tracer"
model "github.com/apache/plc4x/plc4go/pkg/api/model"
mock "github.com/stretchr/testify/mock"
plc4go "github.com/apache/plc4x/plc4go/pkg/api"
-
- spi "github.com/apache/plc4x/plc4go/spi"
)
// mockTracedPlcConnection is an autogenerated mock type for the tracedPlcConnection type
diff --git a/plc4go/pkg/api/cache/tracedPlcConnection.go b/plc4go/pkg/api/cache/noGlobalLog_test.go
similarity index 78%
copy from plc4go/pkg/api/cache/tracedPlcConnection.go
copy to plc4go/pkg/api/cache/noGlobalLog_test.go
index 104e466721..9d0fe095b8 100644
--- a/plc4go/pkg/api/cache/tracedPlcConnection.go
+++ b/plc4go/pkg/api/cache/noGlobalLog_test.go
@@ -19,15 +19,9 @@
package cache
-import (
- "github.com/apache/plc4x/plc4go/pkg/api"
- "github.com/apache/plc4x/plc4go/spi"
-)
+import "github.com/apache/plc4x/plc4go/spi/testutils"
-type tracedPlcConnection interface {
- plc4go.PlcConnection
-
- GetConnectionId() string
- IsTraceEnabled() bool
- GetTracer() *spi.Tracer
+// This ensures that we don't global log
+func init() {
+ testutils.ExplodingGlobalLogger(true)
}
diff --git a/plc4go/pkg/api/cache/plcConnectionLease.go b/plc4go/pkg/api/cache/plcConnectionLease.go
index 8ba3a71fda..41f6b0a69a 100644
--- a/plc4go/pkg/api/cache/plcConnectionLease.go
+++ b/plc4go/pkg/api/cache/plcConnectionLease.go
@@ -22,11 +22,11 @@ package cache
import (
"context"
"fmt"
+ "github.com/apache/plc4x/plc4go/spi/tracer"
"time"
plc4go "github.com/apache/plc4x/plc4go/pkg/api"
apiModel "github.com/apache/plc4x/plc4go/pkg/api/model"
- "github.com/apache/plc4x/plc4go/spi"
_default "github.com/apache/plc4x/plc4go/spi/default"
)
@@ -58,7 +58,7 @@ func (t *plcConnectionLease) IsTraceEnabled() bool {
return t.connection.IsTraceEnabled()
}
-func (t *plcConnectionLease) GetTracer() *spi.Tracer {
+func (t *plcConnectionLease) GetTracer() *tracer.Tracer {
if t.connection == nil {
panic("Called 'GetTracer' on a closed cached connection")
}
@@ -119,7 +119,7 @@ func (t *plcConnectionLease) Close() <-chan plc4go.PlcConnectionCloseResult {
}
// Extract the trace entries from the connection.
- var traces []spi.TraceEntry
+ var traces []tracer.TraceEntry
if t.IsTraceEnabled() {
tracer := t.GetTracer()
// Save all traces.
diff --git a/plc4go/pkg/api/cache/tracedPlcConnection.go b/plc4go/pkg/api/cache/tracedPlcConnection.go
index 104e466721..7b10061169 100644
--- a/plc4go/pkg/api/cache/tracedPlcConnection.go
+++ b/plc4go/pkg/api/cache/tracedPlcConnection.go
@@ -21,7 +21,7 @@ package cache
import (
"github.com/apache/plc4x/plc4go/pkg/api"
- "github.com/apache/plc4x/plc4go/spi"
+ "github.com/apache/plc4x/plc4go/spi/tracer"
)
type tracedPlcConnection interface {
@@ -29,5 +29,5 @@ type tracedPlcConnection interface {
GetConnectionId() string
IsTraceEnabled() bool
- GetTracer() *spi.Tracer
+ GetTracer() *tracer.Tracer
}
diff --git a/plc4go/spi/default/DefaultConnection.go b/plc4go/spi/default/DefaultConnection.go
index 17f8715daa..ebbde588cc 100644
--- a/plc4go/spi/default/DefaultConnection.go
+++ b/plc4go/spi/default/DefaultConnection.go
@@ -21,6 +21,7 @@ package _default
import (
"context"
+ "github.com/apache/plc4x/plc4go/spi/tracer"
"github.com/rs/zerolog"
"time"
@@ -92,7 +93,7 @@ func NewDefaultPlcConnectionConnectResult(connection plc4go.PlcConnection, err e
type DefaultPlcConnectionCloseResult interface {
plc4go.PlcConnectionCloseResult
- GetTraces() []spi.TraceEntry
+ GetTraces() []tracer.TraceEntry
}
func NewDefaultPlcConnectionCloseResult(connection plc4go.PlcConnection, err error) plc4go.PlcConnectionCloseResult {
@@ -103,7 +104,7 @@ func NewDefaultPlcConnectionCloseResult(connection plc4go.PlcConnection, err err
}
}
-func NewDefaultPlcConnectionCloseResultWithTraces(connection plc4go.PlcConnection, err error, traces []spi.TraceEntry) plc4go.PlcConnectionCloseResult {
+func NewDefaultPlcConnectionCloseResultWithTraces(connection plc4go.PlcConnection, err error, traces []tracer.TraceEntry) plc4go.PlcConnectionCloseResult {
return &plcConnectionCloseResult{
connection: connection,
err: err,
@@ -198,7 +199,7 @@ func (d *plcConnectionConnectResult) GetErr() error {
type plcConnectionCloseResult struct {
connection plc4go.PlcConnection
err error
- traces []spi.TraceEntry
+ traces []tracer.TraceEntry
}
func (d *plcConnectionCloseResult) GetConnection() plc4go.PlcConnection {
@@ -209,7 +210,7 @@ func (d *plcConnectionCloseResult) GetErr() error {
return d.err
}
-func (d *plcConnectionCloseResult) GetTraces() []spi.TraceEntry {
+func (d *plcConnectionCloseResult) GetTraces() []tracer.TraceEntry {
return d.traces
}
diff --git a/plc4go/spi/default/DefaultConnection_test.go b/plc4go/spi/default/DefaultConnection_test.go
index 4965e5eda6..c013b0690e 100644
--- a/plc4go/spi/default/DefaultConnection_test.go
+++ b/plc4go/spi/default/DefaultConnection_test.go
@@ -22,6 +22,7 @@ package _default
import (
"context"
"fmt"
+ "github.com/apache/plc4x/plc4go/spi/tracer"
"testing"
"time"
@@ -241,7 +242,7 @@ func TestNewDefaultPlcConnectionCloseResultWithTraces(t *testing.T) {
type args struct {
connection plc4go.PlcConnection
err error
- traces []spi.TraceEntry
+ traces []tracer.TraceEntry
}
tests := []struct {
name string
@@ -1152,7 +1153,7 @@ func Test_plcConnectionCloseResult_GetConnection(t *testing.T) {
type fields struct {
connection plc4go.PlcConnection
err error
- traces []spi.TraceEntry
+ traces []tracer.TraceEntry
}
tests := []struct {
name string
@@ -1179,7 +1180,7 @@ func Test_plcConnectionCloseResult_GetErr(t *testing.T) {
type fields struct {
connection plc4go.PlcConnection
err error
- traces []spi.TraceEntry
+ traces []tracer.TraceEntry
}
tests := []struct {
name string
@@ -1214,12 +1215,12 @@ func Test_plcConnectionCloseResult_GetTraces(t *testing.T) {
type fields struct {
connection plc4go.PlcConnection
err error
- traces []spi.TraceEntry
+ traces []tracer.TraceEntry
}
tests := []struct {
name string
fields fields
- want []spi.TraceEntry
+ want []tracer.TraceEntry
}{
{
name: "get it",
diff --git a/plc4go/spi/default/mock_DefaultPlcConnectionCloseResult_test.go b/plc4go/spi/default/mock_DefaultPlcConnectionCloseResult_test.go
index 4d39f0dc21..5ae4cd4380 100644
--- a/plc4go/spi/default/mock_DefaultPlcConnectionCloseResult_test.go
+++ b/plc4go/spi/default/mock_DefaultPlcConnectionCloseResult_test.go
@@ -23,9 +23,8 @@ package _default
import (
plc4go "github.com/apache/plc4x/plc4go/pkg/api"
+ spi "github.com/apache/plc4x/plc4go/spi/tracer"
mock "github.com/stretchr/testify/mock"
-
- spi "github.com/apache/plc4x/plc4go/spi"
)
// MockDefaultPlcConnectionCloseResult is an autogenerated mock type for the DefaultPlcConnectionCloseResult type
diff --git a/plc4go/spi/Tracer.go b/plc4go/spi/tracer/Tracer.go
similarity index 88%
rename from plc4go/spi/Tracer.go
rename to plc4go/spi/tracer/Tracer.go
index f1ea2f696b..9a78bc7360 100644
--- a/plc4go/spi/Tracer.go
+++ b/plc4go/spi/tracer/Tracer.go
@@ -17,11 +17,16 @@
* under the License.
*/
-package spi
+package tracer
import (
- "github.com/apache/plc4x/plc4go/spi/utils"
+ "fmt"
"time"
+
+ "github.com/apache/plc4x/plc4go/spi/options"
+ "github.com/apache/plc4x/plc4go/spi/utils"
+
+ "github.com/rs/zerolog"
)
type TraceEntry struct {
@@ -40,12 +45,15 @@ type TracerProvider interface {
type Tracer struct {
connectionId string
traceEntries []TraceEntry
+
+ log zerolog.Logger
}
-func NewTracer(connectionId string) *Tracer {
+func NewTracer(connectionId string, _options ...options.WithOption) *Tracer {
return &Tracer{
connectionId: connectionId,
traceEntries: []TraceEntry{},
+ log: options.ExtractCustomLogger(_options...),
}
}
@@ -76,7 +84,7 @@ func (t *Tracer) AddTrace(operation string, message string) {
}
func (t *Tracer) AddTransactionalStartTrace(operation string, message string) string {
- transactionId := utils.GenerateId(4)
+ transactionId := utils.GenerateId(t.log, 4)
t.traceEntries = append(t.traceEntries, TraceEntry{
Timestamp: time.Now(),
ConnectionId: t.connectionId,
@@ -117,3 +125,7 @@ traceFiltering:
}
return result
}
+
+func (t *Tracer) String() string {
+ return fmt.Sprintf("Tracer for %s", t.connectionId)
+}
diff --git a/plc4go/spi/Tracer_test.go b/plc4go/spi/tracer/Tracer_test.go
similarity index 99%
rename from plc4go/spi/Tracer_test.go
rename to plc4go/spi/tracer/Tracer_test.go
index ae2d10f265..1c4008185b 100644
--- a/plc4go/spi/Tracer_test.go
+++ b/plc4go/spi/tracer/Tracer_test.go
@@ -17,7 +17,7 @@
* under the License.
*/
-package spi
+package tracer
import (
"github.com/stretchr/testify/assert"
diff --git a/plc4go/spi/mock_TracerProvider_test.go b/plc4go/spi/tracer/mock_TracerProvider_test.go
similarity index 98%
rename from plc4go/spi/mock_TracerProvider_test.go
rename to plc4go/spi/tracer/mock_TracerProvider_test.go
index 1c98e5c1cd..1c5b011293 100644
--- a/plc4go/spi/mock_TracerProvider_test.go
+++ b/plc4go/spi/tracer/mock_TracerProvider_test.go
@@ -19,9 +19,11 @@
// Code generated by mockery v2.28.1. DO NOT EDIT.
-package spi
+package tracer
-import mock "github.com/stretchr/testify/mock"
+import (
+ mock "github.com/stretchr/testify/mock"
+)
// MockTracerProvider is an autogenerated mock type for the TracerProvider type
type MockTracerProvider struct {
diff --git a/plc4go/spi/utils/IdGenerator.go b/plc4go/spi/utils/IdGenerator.go
index a27f4b0373..a44e2f97df 100644
--- a/plc4go/spi/utils/IdGenerator.go
+++ b/plc4go/spi/utils/IdGenerator.go
@@ -21,14 +21,13 @@ package utils
import (
"encoding/hex"
+ "github.com/rs/zerolog"
"math/rand"
-
- "github.com/rs/zerolog/log"
)
var randomByteFiller = rand.Read
-func GenerateId(numBytes int) string {
+func GenerateId(log zerolog.Logger, numBytes int) string {
transactionIdBytes := make([]byte, numBytes)
n, err := randomByteFiller(transactionIdBytes)
log.Trace().Err(err).Msgf("Read %d bytes", n)
diff --git a/plc4go/spi/utils/IdGenerator_test.go b/plc4go/spi/utils/IdGenerator_test.go
index 514d930dee..abf104417c 100644
--- a/plc4go/spi/utils/IdGenerator_test.go
+++ b/plc4go/spi/utils/IdGenerator_test.go
@@ -20,6 +20,7 @@
package utils
import (
+ "github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"testing"
)
@@ -46,7 +47,7 @@ func TestGenerateId(t *testing.T) {
randomByteFiller = func(_ []byte) (n int, err error) {
return 0, nil
}
- assert.Equalf(t, tt.want, GenerateId(tt.args.numBytes), "GenerateId(%v)", tt.args.numBytes)
+ assert.Equalf(t, tt.want, GenerateId(zerolog.New(zerolog.NewConsoleWriter(zerolog.ConsoleTestWriter(t))), tt.args.numBytes), "GenerateId(%v)", tt.args.numBytes)
})
}
}