Skip to content

Commit

Permalink
feat(plc4go): add option WithTraceTransportInstance to limit tracing …
Browse files Browse the repository at this point in the history
…of test.TransportInstance
  • Loading branch information
sruehl committed Aug 3, 2023
1 parent 2d0aa26 commit 16ea3d3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/go-platform.yml
Expand Up @@ -64,6 +64,10 @@ on:
description: "sets tracing for executor workers"
required: false
default: 'false'
traceTestTransportInstance:
description: "sets tracing for test transport instance"
required: false
default: 'false'

env:
PLC4X_TEST_HIGH_TEST_LOG_PRECISION: ${{ github.event.inputs.highLogPrecision }}
Expand All @@ -73,6 +77,7 @@ env:
PLC4X_TEST_TRACE_TRANSACTION_MANAGER_TRANSACTIONS: ${{ github.event.inputs.traceTransactionManagerTransactions }}
PLC4X_TEST_TRACE_DEFAULT_MESSAGE_CODEC_WORKER: ${{ github.event.inputs.traceDefaultMessageCodecWorker }}
PLC4X_TEST_TRACE_EXECUTOR_WORKERS: ${{ github.event.inputs.traceExecutorWorkers }}
PLC4X_TEST_TEST_TRANSPORT_INSTANCE: ${{ github.event.inputs.traceTestTransportInstance }}

jobs:
test:
Expand Down
4 changes: 4 additions & 0 deletions plc4go/spi/testutils/TestUtils.go
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/apache/plc4x/plc4go/spi/options"
"github.com/apache/plc4x/plc4go/spi/pool"
"github.com/apache/plc4x/plc4go/spi/transactions"
"github.com/apache/plc4x/plc4go/spi/transports/test"
"github.com/apache/plc4x/plc4go/spi/utils"

"github.com/ajankovic/xdiff"
Expand Down Expand Up @@ -129,6 +130,7 @@ var (
traceTransactionManagerTransactions bool
traceDefaultMessageCodecWorker bool
traceExecutorWorkers bool
traceTestTransportInstance bool
)

func init() {
Expand All @@ -143,6 +145,7 @@ func init() {
getOrLeaveBool("PLC4X_TEST_TRACE_TRANSACTION_MANAGER_TRANSACTIONS", &traceTransactionManagerTransactions)
getOrLeaveBool("PLC4X_TEST_TRACE_DEFAULT_MESSAGE_CODEC_WORKER", &traceDefaultMessageCodecWorker)
getOrLeaveBool("PLC4X_TEST_TRACE_EXECUTOR_WORKERS", &traceExecutorWorkers)
getOrLeaveBool("PLC4X_TEST_TEST_TRANSPORT_INSTANCE", &traceTestTransportInstance)
}

func getOrLeaveBool(key string, setting *bool) {
Expand Down Expand Up @@ -237,6 +240,7 @@ func EnrichOptionsWithOptionsForTesting(t *testing.T, _options ...options.WithOp
options.WithTraceTransactionManagerTransactions(traceTransactionManagerTransactions),
options.WithTraceDefaultMessageCodecWorker(traceDefaultMessageCodecWorker),
options.WithExecutorOptionTracerWorkers(traceExecutorWorkers),
test.WithTraceTransportInstance(traceTestTransportInstance),
)
// We always create a custom executor to ensure shared executor for transaction manager is not used for tests
testSharedExecutorInstance := pool.NewFixedSizeExecutor(
Expand Down
27 changes: 27 additions & 0 deletions plc4go/spi/transports/test/TransportInstance.go
Expand Up @@ -50,6 +50,12 @@ type TransportInstance struct {

func NewTransportInstance(transport *Transport, _options ...options.WithOption) *TransportInstance {
customLogger := options.ExtractCustomLoggerOrDefaultToGlobal(_options...)
shouldTrace, found := ExtractTraceTransportInstance(_options...)
if found && !shouldTrace {
if customLogger.GetLevel() < zerolog.InfoLevel {
customLogger = customLogger.Level(zerolog.InfoLevel)
}
}
return &TransportInstance{
readBuffer: []byte{},
writeBuffer: []byte{},
Expand All @@ -59,6 +65,27 @@ func NewTransportInstance(transport *Transport, _options ...options.WithOption)
}
}

// WithTraceTransportInstance enables tracing of the test transport instance
func WithTraceTransportInstance(trace bool) options.WithOption {
return withTraceTransportInstance{trace: trace}
}

// ExtractTraceTransportInstance to extract the flag indicating that transport instance should be traced
func ExtractTraceTransportInstance(options ...options.WithOption) (trace bool, found bool) {
for _, option := range options {
switch option := option.(type) {
case withTraceTransportInstance:
trace, found = option.trace, true
}
}
return
}

type withTraceTransportInstance struct {
options.Option
trace bool
}

func (m *TransportInstance) Connect() error {
m.stateChangeMutex.Lock()
defer m.stateChangeMutex.Unlock()
Expand Down

0 comments on commit 16ea3d3

Please sign in to comment.