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/20 06:47:29 UTC
[plc4x] 01/03: test(plc4go/spi): ensure that we always use a custom executor when using testing
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 08b13f2d10cdc44b512d0207ca9f2a51121c8f72
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Tue Jun 20 08:40:23 2023 +0200
test(plc4go/spi): ensure that we always use a custom executor when using testing
---
plc4go/spi/testutils/TestUtils.go | 23 +++++++--
.../transactions/RequestTransactionManager_test.go | 60 +++++++++++++++++-----
plc4go/spi/transactions/RequestTransaction_test.go | 8 ++-
plc4go/spi/transactions/common_test.go | 40 +++++++++++++++
4 files changed, 109 insertions(+), 22 deletions(-)
diff --git a/plc4go/spi/testutils/TestUtils.go b/plc4go/spi/testutils/TestUtils.go
index a13ac3100d..2f5dfc5a31 100644
--- a/plc4go/spi/testutils/TestUtils.go
+++ b/plc4go/spi/testutils/TestUtils.go
@@ -21,14 +21,18 @@ package testutils
import (
"context"
- "github.com/apache/plc4x/plc4go/spi/options"
- "github.com/apache/plc4x/plc4go/spi/utils"
"os"
+ "runtime"
"runtime/debug"
"strings"
"testing"
"time"
+ "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/utils"
+
"github.com/ajankovic/xdiff"
"github.com/ajankovic/xdiff/parser"
"github.com/pkg/errors"
@@ -163,12 +167,23 @@ func EnrichOptionsWithOptionsForTesting(t *testing.T, _options ...options.WithOp
if extractedTraceWorkers, found := options.ExtractTracerWorkers(_options...); found {
traceExecutorWorkers = extractedTraceWorkers
}
- // TODO: apply to other options like above
- return append(_options,
+ _options = append(_options,
options.WithCustomLogger(ProduceTestingLogger(t)),
options.WithPassLoggerToModel(true),
options.WithExecutorOptionTracerWorkers(traceExecutorWorkers),
)
+ // We always create a custom executor to ensure shared executor for transaction manager is not used for tests
+ testSharedExecutorInstance := pool.NewFixedSizeExecutor(
+ runtime.NumCPU(),
+ 100,
+ _options...,
+ )
+ testSharedExecutorInstance.Start()
+ t.Cleanup(testSharedExecutorInstance.Stop)
+ _options = append(_options,
+ transactions.WithCustomExecutor(testSharedExecutorInstance),
+ )
+ return _options
}
type _explodingGlobalLogger struct {
diff --git a/plc4go/spi/transactions/RequestTransactionManager_test.go b/plc4go/spi/transactions/RequestTransactionManager_test.go
index b80299c202..ca59db46a2 100644
--- a/plc4go/spi/transactions/RequestTransactionManager_test.go
+++ b/plc4go/spi/transactions/RequestTransactionManager_test.go
@@ -25,7 +25,6 @@ import (
"fmt"
"github.com/apache/plc4x/plc4go/spi/options"
"github.com/apache/plc4x/plc4go/spi/pool"
- "github.com/apache/plc4x/plc4go/spi/testutils"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"testing"
@@ -162,6 +161,9 @@ func Test_requestTransactionManager_StartTransaction(t *testing.T) {
}{
{
name: "start one",
+ setup: func(t *testing.T, fields *fields) {
+ fields.executor = pool.NewFixedSizeExecutor(10, 10, options.WithCustomLogger(produceTestingLogger(t)))
+ },
wantAssert: func(t *testing.T, requestTransaction RequestTransaction) bool {
assert.False(t, requestTransaction.IsCompleted())
return true
@@ -169,6 +171,9 @@ func Test_requestTransactionManager_StartTransaction(t *testing.T) {
},
{
name: "start one in shutdown",
+ setup: func(t *testing.T, fields *fields) {
+ fields.executor = pool.NewFixedSizeExecutor(10, 10, options.WithCustomLogger(produceTestingLogger(t)))
+ },
manipulator: func(t *testing.T, manager *requestTransactionManager) {
manager.shutdown.Store(true)
},
@@ -191,7 +196,7 @@ func Test_requestTransactionManager_StartTransaction(t *testing.T) {
workLog: tt.fields.workLog,
executor: tt.fields.executor,
traceTransactionManagerTransactions: tt.fields.traceTransactionManagerTransactions,
- log: testutils.ProduceTestingLogger(t),
+ log: produceTestingLogger(t),
}
if tt.manipulator != nil {
tt.manipulator(t, r)
@@ -218,6 +223,7 @@ func Test_requestTransactionManager_endRequest(t *testing.T) {
name string
fields fields
args args
+ setup func(t *testing.T, fields *fields, args *args)
wantErr bool
}{
{
@@ -225,6 +231,9 @@ func Test_requestTransactionManager_endRequest(t *testing.T) {
args: args{
transaction: &requestTransaction{},
},
+ setup: func(t *testing.T, fields *fields, args *args) {
+ fields.executor = pool.NewFixedSizeExecutor(10, 10, options.WithCustomLogger(produceTestingLogger(t)))
+ },
wantErr: true,
},
{
@@ -232,6 +241,9 @@ func Test_requestTransactionManager_endRequest(t *testing.T) {
args: args{
transaction: &requestTransaction{},
},
+ setup: func(t *testing.T, fields *fields, args *args) {
+ fields.executor = pool.NewFixedSizeExecutor(10, 10, options.WithCustomLogger(produceTestingLogger(t)))
+ },
fields: fields{
runningRequests: []*requestTransaction{
{},
@@ -241,6 +253,9 @@ func Test_requestTransactionManager_endRequest(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
+ if tt.setup != nil {
+ tt.setup(t, &tt.fields, &tt.args)
+ }
r := &requestTransactionManager{
runningRequests: tt.fields.runningRequests,
numberOfConcurrentRequests: tt.fields.numberOfConcurrentRequests,
@@ -268,18 +283,20 @@ func Test_requestTransactionManager_failRequest(t *testing.T) {
err error
}
tests := []struct {
- name string
- fields fields
- args args
- mockSetup func(t *testing.T, fields *fields, args *args)
- wantErr bool
+ name string
+ fields fields
+ args args
+ setup func(t *testing.T, fields *fields, args *args)
+ wantErr bool
}{
{
name: "fail a request",
args: args{
transaction: &requestTransaction{},
},
- mockSetup: func(t *testing.T, fields *fields, args *args) {
+ setup: func(t *testing.T, fields *fields, args *args) {
+ fields.executor = pool.NewFixedSizeExecutor(10, 10, options.WithCustomLogger(produceTestingLogger(t)))
+
completionFutureMock := NewMockCompletionFuture(t)
expect := completionFutureMock.EXPECT()
expect.Cancel(true, nil).Return()
@@ -291,8 +308,8 @@ func Test_requestTransactionManager_failRequest(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- if tt.mockSetup != nil {
- tt.mockSetup(t, &tt.fields, &tt.args)
+ if tt.setup != nil {
+ tt.setup(t, &tt.fields, &tt.args)
}
r := &requestTransactionManager{
runningRequests: tt.fields.runningRequests,
@@ -300,7 +317,7 @@ func Test_requestTransactionManager_failRequest(t *testing.T) {
currentTransactionId: tt.fields.currentTransactionId,
workLog: tt.fields.workLog,
executor: tt.fields.executor,
- log: testutils.ProduceTestingLogger(t),
+ log: produceTestingLogger(t),
}
if err := r.failRequest(tt.args.transaction, tt.args.err); (err != nil) != tt.wantErr {
t.Errorf("failRequest() error = %v, wantErr %v", err, tt.wantErr)
@@ -353,6 +370,7 @@ func Test_requestTransactionManager_processWorklog(t *testing.T) {
tests := []struct {
name string
fields fields
+ setup func(t *testing.T, fields *fields)
}{
{
name: "process nothing",
@@ -368,6 +386,9 @@ func Test_requestTransactionManager_processWorklog(t *testing.T) {
}(),
executor: sharedExecutorInstance,
},
+ setup: func(t *testing.T, fields *fields) {
+ fields.executor = pool.NewFixedSizeExecutor(10, 10, options.WithCustomLogger(produceTestingLogger(t)))
+ },
},
{
name: "process two",
@@ -390,10 +411,16 @@ func Test_requestTransactionManager_processWorklog(t *testing.T) {
}(),
executor: sharedExecutorInstance,
},
+ setup: func(t *testing.T, fields *fields) {
+ fields.executor = pool.NewFixedSizeExecutor(10, 10, options.WithCustomLogger(produceTestingLogger(t)))
+ },
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
+ if tt.setup != nil {
+ tt.setup(t, &tt.fields)
+ }
r := &requestTransactionManager{
runningRequests: tt.fields.runningRequests,
numberOfConcurrentRequests: tt.fields.numberOfConcurrentRequests,
@@ -421,6 +448,7 @@ func Test_requestTransactionManager_submitTransaction(t *testing.T) {
name string
fields fields
args args
+ setup func(t *testing.T, fields *fields, args *args)
}{
{
name: "submit it",
@@ -431,10 +459,16 @@ func Test_requestTransactionManager_submitTransaction(t *testing.T) {
},
},
},
+ setup: func(t *testing.T, fields *fields, args *args) {
+ fields.executor = pool.NewFixedSizeExecutor(10, 10, options.WithCustomLogger(produceTestingLogger(t)))
+ },
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
+ if tt.setup != nil {
+ tt.setup(t, &tt.fields, &tt.args)
+ }
r := &requestTransactionManager{
runningRequests: tt.fields.runningRequests,
numberOfConcurrentRequests: tt.fields.numberOfConcurrentRequests,
@@ -484,7 +518,7 @@ func Test_requestTransactionManager_Close(t *testing.T) {
workLog: tt.fields.workLog,
executor: tt.fields.executor,
traceTransactionManagerTransactions: tt.fields.traceTransactionManagerTransactions,
- log: testutils.ProduceTestingLogger(t),
+ log: produceTestingLogger(t),
}
tt.wantErr(t, r.Close(), fmt.Sprintf("Close()"))
})
@@ -636,7 +670,7 @@ func Test_requestTransactionManager_String(t *testing.T) {
workLog: tt.fields.workLog,
executor: tt.fields.executor,
traceTransactionManagerTransactions: tt.fields.traceTransactionManagerTransactions,
- log: testutils.ProduceTestingLogger(t),
+ log: produceTestingLogger(t),
}
assert.Equalf(t, tt.want, r.String(), "String()")
})
diff --git a/plc4go/spi/transactions/RequestTransaction_test.go b/plc4go/spi/transactions/RequestTransaction_test.go
index 00c68fe1f9..666fde714f 100644
--- a/plc4go/spi/transactions/RequestTransaction_test.go
+++ b/plc4go/spi/transactions/RequestTransaction_test.go
@@ -25,8 +25,6 @@ import (
"time"
"github.com/apache/plc4x/plc4go/spi/pool"
- "github.com/apache/plc4x/plc4go/spi/testutils"
-
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
@@ -67,7 +65,7 @@ func Test_requestTransaction_EndRequest(t1 *testing.T) {
parent: tt.fields.parent,
transactionId: tt.fields.transactionId,
operation: tt.fields.operation,
- transactionLog: testutils.ProduceTestingLogger(t1),
+ transactionLog: produceTestingLogger(t1),
completed: tt.fields.completed,
}
if err := t.EndRequest(); (err != nil) != tt.wantErr {
@@ -173,7 +171,7 @@ func Test_requestTransaction_String(t *testing.T) {
parent: tt.fields.parent,
transactionId: tt.fields.transactionId,
operation: tt.fields.operation,
- transactionLog: testutils.ProduceTestingLogger(t1),
+ transactionLog: produceTestingLogger(t1),
}
if tt.manipulator != nil {
tt.manipulator(t, _t)
@@ -313,7 +311,7 @@ func Test_requestTransaction_AwaitCompletion(t1 *testing.T) {
parent: tt.fields.parent,
transactionId: tt.fields.transactionId,
operation: tt.fields.operation,
- transactionLog: testutils.ProduceTestingLogger(t1),
+ transactionLog: produceTestingLogger(t1),
}
if tt.manipulator != nil {
tt.manipulator(t1, t)
diff --git a/plc4go/spi/transactions/common_test.go b/plc4go/spi/transactions/common_test.go
new file mode 100644
index 0000000000..f86018626a
--- /dev/null
+++ b/plc4go/spi/transactions/common_test.go
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package transactions
+
+import (
+ "github.com/rs/zerolog"
+ "os"
+ "testing"
+)
+
+// note: we can't use testutils here due to import cycle
+func produceTestingLogger(t *testing.T) zerolog.Logger {
+ return zerolog.New(zerolog.NewConsoleWriter(zerolog.ConsoleTestWriter(t),
+ func(w *zerolog.ConsoleWriter) {
+ // TODO: this is really an issue with go-junit-report not sanitizing output before dumping into xml...
+ onJenkins := os.Getenv("JENKINS_URL") != ""
+ onGithubAction := os.Getenv("GITHUB_ACTIONS") != ""
+ onCI := os.Getenv("CI") != ""
+ if onJenkins || onGithubAction || onCI {
+ w.NoColor = true
+ }
+ }))
+}