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 7ae962c4fae1a334280a5232e947f85428498a8c Author: Sebastian Rühl <[email protected]> AuthorDate: Tue Nov 11 12:13:52 2025 +0100 test(plc4go): fix wobbly test --- plc4go/spi/options/Option.go | 4 +- plc4go/spi/pool/WorkerPool.go | 22 ++++++---- plc4go/spi/pool/dynamicExecutor.go | 4 +- plc4go/spi/pool/executor.go | 17 +++++++- .../transactions/RequestTransactionManager_test.go | 49 ++++++++++++---------- 5 files changed, 60 insertions(+), 36 deletions(-) diff --git a/plc4go/spi/options/Option.go b/plc4go/spi/options/Option.go index 09bc7d08f9..fde92b278d 100644 --- a/plc4go/spi/options/Option.go +++ b/plc4go/spi/options/Option.go @@ -168,11 +168,11 @@ func WithExecutorOptionName(name string) WithOption { } // ExtractExecutorName returns the value from WithExecutorOptionName -func ExtractExecutorName(_options ...WithOption) (executorName string) { +func ExtractExecutorName(_options ...WithOption) (executorName string, found bool) { for _, option := range _options { switch option := option.(type) { case *withExecutorNameOption: - executorName = option.name + executorName, found = option.name, true } } return diff --git a/plc4go/spi/pool/WorkerPool.go b/plc4go/spi/pool/WorkerPool.go index 15a65f8b52..dae882721e 100644 --- a/plc4go/spi/pool/WorkerPool.go +++ b/plc4go/spi/pool/WorkerPool.go @@ -45,21 +45,27 @@ type Executor interface { func NewFixedSizeExecutor(numberOfWorkers, queueDepth int, _options ...options.WithOption) Executor { customLogger := options.ExtractCustomLoggerOrDefaultToGlobal(_options...) - _executor := newExecutor(queueDepth, numberOfWorkers, customLogger) - _executor.traceWorkers, _ = options.ExtractTracerWorkers(_options...) - if name := options.ExtractExecutorName(_options...); name != "" { - _executor.name = name + var executorOpts []func(*executor) + if name, found := options.ExtractExecutorName(_options...); found && name != "" { + executorOpts = append(executorOpts, withExecutorName(name)) } + if traceWorkers, found := options.ExtractTracerWorkers(_options...); found { + executorOpts = append(executorOpts, withTraceWorkers(traceWorkers)) + } + _executor := newExecutor(queueDepth, numberOfWorkers, customLogger, executorOpts...) return _executor } func NewDynamicExecutor(maxNumberOfWorkers, queueDepth int, _options ...options.WithOption) Executor { customLogger := options.ExtractCustomLoggerOrDefaultToGlobal(_options...) - _executor := newDynamicExecutor(queueDepth, maxNumberOfWorkers, customLogger) - _executor.traceWorkers, _ = options.ExtractTracerWorkers(_options...) - if name := options.ExtractExecutorName(_options...); name != "" { - _executor.name = name + var executorOpts []func(*executor) + if name, found := options.ExtractExecutorName(_options...); found && name != "" { + executorOpts = append(executorOpts, withExecutorName(name)) + } + if traceWorkers, found := options.ExtractTracerWorkers(_options...); found { + executorOpts = append(executorOpts, withTraceWorkers(traceWorkers)) } + _executor := newDynamicExecutor(queueDepth, maxNumberOfWorkers, customLogger, executorOpts...) // We spawn one initial worker workerId := fmt.Sprintf("%s-worker-%d", _executor.name, 0) w := newWorker(customLogger, workerId, _executor) diff --git a/plc4go/spi/pool/dynamicExecutor.go b/plc4go/spi/pool/dynamicExecutor.go index 6c61f1afa6..e9266a54f3 100644 --- a/plc4go/spi/pool/dynamicExecutor.go +++ b/plc4go/spi/pool/dynamicExecutor.go @@ -47,9 +47,9 @@ type dynamicExecutor struct { wg sync.WaitGroup // use to track spawned go routines } -func newDynamicExecutor(queueDepth, maxNumberOfWorkers int, log zerolog.Logger) *dynamicExecutor { +func newDynamicExecutor(queueDepth, maxNumberOfWorkers int, log zerolog.Logger, opts ...func(*executor)) *dynamicExecutor { return &dynamicExecutor{ - executor: newExecutor(queueDepth, 0, log), + executor: newExecutor(queueDepth, 0, log, opts...), maxNumberOfWorkers: maxNumberOfWorkers, } } diff --git a/plc4go/spi/pool/executor.go b/plc4go/spi/pool/executor.go index 10061c12c6..ad17b97b5b 100644 --- a/plc4go/spi/pool/executor.go +++ b/plc4go/spi/pool/executor.go @@ -55,13 +55,16 @@ type executor struct { log zerolog.Logger } -func newExecutor(queueDepth int, numberOfInitialWorkers int, customLogger zerolog.Logger) *executor { +func newExecutor(queueDepth int, numberOfInitialWorkers int, customLogger zerolog.Logger, opts ...func(*executor)) *executor { e := &executor{ name: fmt.Sprintf("executor-%d", defaultExecutorNameUsage.Add(1)), workItems: make(chan workItem, queueDepth), log: customLogger, } e.ctx, e.ctxCancel = context.WithCancel(context.Background()) + for _, opt := range opts { + opt(e) + } workers := make([]*worker, numberOfInitialWorkers) for i := 0; i < numberOfInitialWorkers; i++ { w := newWorker(customLogger, fmt.Sprintf("%s-worker-%d", e.name, i), e) @@ -71,6 +74,18 @@ func newExecutor(queueDepth int, numberOfInitialWorkers int, customLogger zerolo return e } +func withExecutorName(name string) func(*executor) { + return func(e *executor) { + e.name = name + } +} + +func withTraceWorkers(traceWorkers bool) func(*executor) { + return func(e *executor) { + e.traceWorkers = traceWorkers + } +} + func (e *executor) isTraceWorkers() bool { return e.traceWorkers } diff --git a/plc4go/spi/transactions/RequestTransactionManager_test.go b/plc4go/spi/transactions/RequestTransactionManager_test.go index 7972fb2d56..d3800ec88b 100644 --- a/plc4go/spi/transactions/RequestTransactionManager_test.go +++ b/plc4go/spi/transactions/RequestTransactionManager_test.go @@ -664,29 +664,32 @@ func Test_requestTransactionManager_String(t *testing.T) { traceTransactionManagerTransactions: true, }, want: ` -╔═requestTransactionManager══════════════════════════════════════════════════════════════════════════════════════════╗ -║╔═runningRequests/value/requestTransaction╗╔═numberOfConcurrentRequests╗╔═currentTransactionId╗ ║ -║║ ╔═transactionId╗╔═completed╗ ║║ 0x0000000000000003 3 ║║ 0x00000004 4 ║ ║ -║║ ║ 0x00000002 2 ║║ b0 false ║ ║╚═══════════════════════════╝╚═════════════════════╝ ║ -║║ ╚══════════════╝╚══════════╝ ║ ║ -║╚═════════════════════════════════════════╝ ║ -║╔═executor/executor═════════════════════════════════════════════════════════════════════════════════════╗╔═shutdown╗║ -║║╔═name════════════════════════════════╗╔═running╗╔═shutdown╗ ║║b0 false ║║ -║║║Test_requestTransactionManager_String║║b0 false║║b0 false ║ ║╚═════════╝║ -║║╚═════════════════════════════════════╝╚════════╝╚═════════╝ ║ ║ -║║╔═worker/value/worker═════════════════════════════════════════════════════════════════════════════════╗║ ║ -║║║╔═id════════════════╗╔═lastReceived════════════════╗╔═running╗╔═shutdown╗╔═interrupted╗╔═interrupter╗║║ ║ -║║║║executor-2-worker-0║║0001-01-01 00:00:00 +0000 UTC║║b0 false║║b0 false ║║ b0 false ║║0 element(s)║║║ ║ -║║║╚═══════════════════╝╚═════════════════════════════╝╚════════╝╚═════════╝╚════════════╝╚════════════╝║║ ║ -║║╚═════════════════════════════════════════════════════════════════════════════════════════════════════╝║ ║ -║║╔═workerNumber╗╔═workItems══╗╔═traceWorkers╗╔═ctx═════════════════════════╗ ║ ║ -║║║0x00000000 0 ║║0 element(s)║║ b0 false ║║context.Background.WithCancel║ ║ ║ -║║╚═════════════╝╚════════════╝╚═════════════╝╚═════════════════════════════╝ ║ ║ -║╚═══════════════════════════════════════════════════════════════════════════════════════════════════════╝ ║ -║╔═traceTransactionManagerTransactions╗ ║ -║║ b1 true ║ ║ -║╚════════════════════════════════════╝ ║ -╚════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝`[1:], +╔═requestTransactionManager═════════════════════════════════════════════════════════════════════════════════════════╗ +║╔═runningRequests/value/requestTransaction╗╔═numberOfConcurrentRequests╗╔═currentTransactionId╗ ║ +║║ ╔═transactionId╗╔═completed╗ ║║ 0x0000000000000003 3 ║║ 0x00000004 4 ║ ║ +║║ ║ 0x00000002 2 ║║ b0 false ║ ║╚═══════════════════════════╝╚═════════════════════╝ ║ +║║ ╚══════════════╝╚══════════╝ ║ ║ +║╚═════════════════════════════════════════╝ ║ +║╔═executor/executor════════════════════════════════════════════════════════════════════════════════════╗╔═shutdown╗║ +║║╔═name════════════════════════════════╗╔═running╗╔═shutdown╗ ║║b0 false ║║ +║║║Test_requestTransactionManager_String║║b0 false║║b0 false ║ ║╚═════════╝║ +║║╚═════════════════════════════════════╝╚════════╝╚═════════╝ ║ ║ +║║╔═worker/value/worker════════════════════════════════════════════════════════════════════════════════╗║ ║ +║║║╔═id═══════════════════════════════════════════╗╔═lastReceived════════════════╗╔═running╗╔═shutdown╗║║ ║ +║║║║Test_requestTransactionManager_String-worker-0║║0001-01-01 00:00:00 +0000 UTC║║b0 false║║b0 false ║║║ ║ +║║║╚══════════════════════════════════════════════╝╚═════════════════════════════╝╚════════╝╚═════════╝║║ ║ +║║║╔═interrupted╗╔═interrupter╗ ║║ ║ +║║║║ b0 false ║║0 element(s)║ ║║ ║ +║║║╚════════════╝╚════════════╝ ║║ ║ +║║╚════════════════════════════════════════════════════════════════════════════════════════════════════╝║ ║ +║║╔═workerNumber╗╔═workItems══╗╔═traceWorkers╗╔═ctx═════════════════════════╗ ║ ║ +║║║0x00000000 0 ║║0 element(s)║║ b0 false ║║context.Background.WithCancel║ ║ ║ +║║╚═════════════╝╚════════════╝╚═════════════╝╚═════════════════════════════╝ ║ ║ +║╚══════════════════════════════════════════════════════════════════════════════════════════════════════╝ ║ +║╔═traceTransactionManagerTransactions╗ ║ +║║ b1 true ║ ║ +║╚════════════════════════════════════╝ ║ +╚═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝`[1:], }, } for _, tt := range tests {
