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/15 10:55:41 UTC
[plc4x] branch develop updated: test(plc4go): always use a test context
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
The following commit(s) were added to refs/heads/develop by this push:
new 11d8b9ff26 test(plc4go): always use a test context
11d8b9ff26 is described below
commit 11d8b9ff268123894c01d704f8f55f59f224fb91
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Thu Jun 15 12:55:33 2023 +0200
test(plc4go): always use a test context
---
plc4go/internal/cbus/Browser_test.go | 4 +--
plc4go/internal/cbus/Discoverer_test.go | 2 +-
plc4go/internal/cbus/Driver_test.go | 8 +++---
plc4go/internal/cbus/Writer_test.go | 5 ++--
plc4go/pkg/api/driverManager_test.go | 32 ++++++++++++++--------
plc4go/spi/default/DefaultCodec_test.go | 7 +++--
.../SingleItemRequestInterceptor_test.go | 13 +++++----
plc4go/spi/utils/ArrayContext_test.go | 32 +++++++++++++++++++---
8 files changed, 69 insertions(+), 34 deletions(-)
diff --git a/plc4go/internal/cbus/Browser_test.go b/plc4go/internal/cbus/Browser_test.go
index 377e5b4462..bbeca997e8 100644
--- a/plc4go/internal/cbus/Browser_test.go
+++ b/plc4go/internal/cbus/Browser_test.go
@@ -76,7 +76,7 @@ func TestBrowser_BrowseQuery(t *testing.T) {
{
name: "non responding browse",
args: args{
- ctx: context.Background(),
+ ctx: testutils.TestContext(t),
interceptor: func(result apiModel.PlcBrowseItem) bool {
// No-OP
return true
@@ -349,7 +349,7 @@ func TestBrowser_getInstalledUnitAddressBytes(t *testing.T) {
{
name: "get units",
args: args{
- ctx: context.Background(),
+ ctx: testutils.TestContext(t),
},
setup: func(t *testing.T, fields *fields) {
_options := testutils.EnrichOptionsWithOptionsForTesting(t)
diff --git a/plc4go/internal/cbus/Discoverer_test.go b/plc4go/internal/cbus/Discoverer_test.go
index 3088d4aa65..4d2a1a3269 100644
--- a/plc4go/internal/cbus/Discoverer_test.go
+++ b/plc4go/internal/cbus/Discoverer_test.go
@@ -251,7 +251,7 @@ func TestDiscoverer_createTransportInstanceDispatcher(t *testing.T) {
{
name: "create a dispatcher",
args: args{
- ctx: context.Background(),
+ ctx: testutils.TestContext(t),
wg: func() *sync.WaitGroup {
var wg sync.WaitGroup
return &wg
diff --git a/plc4go/internal/cbus/Driver_test.go b/plc4go/internal/cbus/Driver_test.go
index cee718f5ff..c70b61b431 100644
--- a/plc4go/internal/cbus/Driver_test.go
+++ b/plc4go/internal/cbus/Driver_test.go
@@ -122,7 +122,7 @@ func TestDriver_GetConnectionWithContext(t *testing.T) {
awaitDisconnectComplete: false,
},
args: args{
- ctx: context.Background(),
+ ctx: testutils.TestContext(t),
transportUrl: url.URL{
Scheme: "test",
},
@@ -151,7 +151,7 @@ func TestDriver_GetConnectionWithContext(t *testing.T) {
awaitDisconnectComplete: false,
},
args: args{
- ctx: context.Background(),
+ ctx: testutils.TestContext(t),
transportUrl: url.URL{
Scheme: "test",
},
@@ -185,7 +185,7 @@ func TestDriver_GetConnectionWithContext(t *testing.T) {
awaitDisconnectComplete: false,
},
args: args{
- ctx: context.Background(),
+ ctx: testutils.TestContext(t),
transportUrl: url.URL{
Scheme: "test",
},
@@ -218,7 +218,7 @@ func TestDriver_GetConnectionWithContext(t *testing.T) {
awaitDisconnectComplete: false,
},
args: args{
- ctx: context.Background(),
+ ctx: testutils.TestContext(t),
transportUrl: url.URL{
Scheme: "test",
},
diff --git a/plc4go/internal/cbus/Writer_test.go b/plc4go/internal/cbus/Writer_test.go
index 3af6f00013..5cc0f315bb 100644
--- a/plc4go/internal/cbus/Writer_test.go
+++ b/plc4go/internal/cbus/Writer_test.go
@@ -22,6 +22,7 @@ package cbus
import (
"context"
spiModel "github.com/apache/plc4x/plc4go/spi/model"
+ "github.com/apache/plc4x/plc4go/spi/testutils"
"github.com/apache/plc4x/plc4go/spi/transactions"
"github.com/apache/plc4x/plc4go/spi/utils"
"github.com/stretchr/testify/assert"
@@ -97,7 +98,7 @@ func TestWriter_Write(t *testing.T) {
{
name: "too many tags",
args: args{
- ctx: context.Background(),
+ ctx: testutils.TestContext(t),
writeRequest: spiModel.NewDefaultPlcWriteRequest(nil, func() []string {
return strings.Split(strings.Repeat("asd,", 30), ",")
}(), nil, nil, nil),
@@ -135,7 +136,7 @@ func TestWriter_Write(t *testing.T) {
tm: spi.NewRequestTransactionManager(10),
},
args: args{
- ctx: context.Background(),
+ ctx: testutils.TestContext(t),
writeRequest: spiModel.NewDefaultPlcWriteRequest(
map[string]apiModel.PlcTag{
"asd": &statusTag{},
diff --git a/plc4go/pkg/api/driverManager_test.go b/plc4go/pkg/api/driverManager_test.go
index 1ea81c977b..044b386a2e 100644
--- a/plc4go/pkg/api/driverManager_test.go
+++ b/plc4go/pkg/api/driverManager_test.go
@@ -333,7 +333,7 @@ func Test_plcDriverManger_Discover(t *testing.T) {
drivers: tt.fields.drivers,
transports: tt.fields.transports,
}
- m.log = produceTestLogger(t)
+ m.log = produceTestingLogger(t)
if err := m.Discover(tt.args.callback, tt.args.discoveryOptions...); (err != nil) != tt.wantErr {
t.Errorf("Discover() error = %v, wantErr %v", err, tt.wantErr)
}
@@ -364,7 +364,7 @@ func Test_plcDriverManger_DiscoverWithContext(t *testing.T) {
drivers: map[string]PlcDriver{},
},
args: args{
- ctx: context.Background(),
+ ctx: testContext(t),
callback: func(event model.PlcDiscoveryItem) {
// No-op
},
@@ -388,7 +388,7 @@ func Test_plcDriverManger_DiscoverWithContext(t *testing.T) {
drivers: map[string]PlcDriver{},
},
args: args{
- ctx: context.Background(),
+ ctx: testContext(t),
callback: func(event model.PlcDiscoveryItem) {
// No-op
},
@@ -417,7 +417,7 @@ func Test_plcDriverManger_DiscoverWithContext(t *testing.T) {
drivers: tt.fields.drivers,
transports: tt.fields.transports,
}
- m.log = produceTestLogger(t)
+ m.log = produceTestingLogger(t)
if err := m.DiscoverWithContext(tt.args.ctx, tt.args.callback, tt.args.discoveryOptions...); (err != nil) != tt.wantErr {
t.Errorf("DiscoverWithContext() error = %v, wantErr %v", err, tt.wantErr)
}
@@ -526,7 +526,7 @@ func Test_plcDriverManger_GetConnection(t *testing.T) {
drivers: tt.fields.drivers,
transports: tt.fields.transports,
}
- m.log = produceTestLogger(t)
+ m.log = produceTestingLogger(t)
if got := m.GetConnection(tt.args.connectionString); !tt.wantVerifier(t, got) {
t.Errorf("GetConnection() = %v", got)
}
@@ -568,7 +568,7 @@ func Test_plcDriverManger_GetDriver(t *testing.T) {
drivers: tt.fields.drivers,
transports: tt.fields.transports,
}
- m.log = produceTestLogger(t)
+ m.log = produceTestingLogger(t)
got, err := m.GetDriver(tt.args.driverName)
if (err != nil) != tt.wantErr {
t.Errorf("GetDriver() error = %v, wantErr %v", err, tt.wantErr)
@@ -617,7 +617,7 @@ func Test_plcDriverManger_GetTransport(t *testing.T) {
drivers: tt.fields.drivers,
transports: tt.fields.transports,
}
- m.log = produceTestLogger(t)
+ m.log = produceTestingLogger(t)
got, err := m.GetTransport(tt.args.transportName, tt.args.in1, tt.args.in2)
if (err != nil) != tt.wantErr {
t.Errorf("GetTransport() error = %v, wantErr %v", err, tt.wantErr)
@@ -659,7 +659,7 @@ func Test_plcDriverManger_ListDriverNames(t *testing.T) {
drivers: tt.fields.drivers,
transports: tt.fields.transports,
}
- m.log = produceTestLogger(t)
+ m.log = produceTestingLogger(t)
if got := m.ListDriverNames(); !assert.Equal(t, got, tt.want) {
t.Errorf("ListDriverNames() = %v, want %v", got, tt.want)
}
@@ -696,7 +696,7 @@ func Test_plcDriverManger_ListTransportNames(t *testing.T) {
drivers: tt.fields.drivers,
transports: tt.fields.transports,
}
- m.log = produceTestLogger(t)
+ m.log = produceTestingLogger(t)
if got := m.ListTransportNames(); !assert.Equal(t, got, tt.want) {
t.Errorf("ListTransportNames() = %v, want %v", got, tt.want)
}
@@ -756,7 +756,7 @@ func Test_plcDriverManger_RegisterDriver(t *testing.T) {
drivers: tt.fields.drivers,
transports: tt.fields.transports,
}
- m.log = produceTestLogger(t)
+ m.log = produceTestingLogger(t)
m.RegisterDriver(tt.args.driver)
})
}
@@ -812,7 +812,7 @@ func Test_plcDriverManger_RegisterTransport(t *testing.T) {
drivers: tt.fields.drivers,
transports: tt.fields.transports,
}
- m.log = produceTestLogger(t)
+ m.log = produceTestingLogger(t)
m.RegisterTransport(tt.args.transport)
})
}
@@ -845,7 +845,7 @@ func Test_withDiscoveryOption_isDiscoveryOption(t *testing.T) {
}
// note: we can't use testutils here due to import cycle
-func produceTestLogger(t *testing.T) zerolog.Logger {
+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...
@@ -857,3 +857,11 @@ func produceTestLogger(t *testing.T) zerolog.Logger {
}
}))
}
+
+// note: we can't use testutils here due to import cycle
+func testContext(t *testing.T) context.Context {
+ ctx, cancel := context.WithCancel(context.Background())
+ t.Cleanup(cancel)
+ ctx = produceTestingLogger(t).WithContext(ctx)
+ return ctx
+}
diff --git a/plc4go/spi/default/DefaultCodec_test.go b/plc4go/spi/default/DefaultCodec_test.go
index a081221d4c..c6829c64a5 100644
--- a/plc4go/spi/default/DefaultCodec_test.go
+++ b/plc4go/spi/default/DefaultCodec_test.go
@@ -22,6 +22,7 @@ package _default
import (
"context"
"fmt"
+ "github.com/apache/plc4x/plc4go/spi/testutils"
"testing"
"time"
@@ -508,7 +509,7 @@ func Test_defaultCodec_Expect(t *testing.T) {
{
name: "expect it",
args: args{
- ctx: context.Background(),
+ ctx: testutils.TestContext(t),
},
wantErr: assert.NoError,
},
@@ -745,7 +746,7 @@ func Test_defaultCodec_SendRequest(t *testing.T) {
{
name: "send it",
args: args{
- ctx: context.Background(),
+ ctx: testutils.TestContext(t),
},
mockSetup: func(t *testing.T, fields *fields, args *args) {
requirements := NewMockDefaultCodecRequirements(t)
@@ -771,7 +772,7 @@ func Test_defaultCodec_SendRequest(t *testing.T) {
{
name: "send it errors",
args: args{
- ctx: context.Background(),
+ ctx: testutils.TestContext(t),
},
mockSetup: func(t *testing.T, fields *fields, args *args) {
requirements := NewMockDefaultCodecRequirements(t)
diff --git a/plc4go/spi/interceptors/SingleItemRequestInterceptor_test.go b/plc4go/spi/interceptors/SingleItemRequestInterceptor_test.go
index 7c60fd0a84..9406f90af6 100644
--- a/plc4go/spi/interceptors/SingleItemRequestInterceptor_test.go
+++ b/plc4go/spi/interceptors/SingleItemRequestInterceptor_test.go
@@ -24,6 +24,7 @@ import (
"errors"
"github.com/apache/plc4x/plc4go/pkg/api/values"
"github.com/apache/plc4x/plc4go/spi"
+ "github.com/apache/plc4x/plc4go/spi/testutils"
"github.com/apache/plc4x/plc4go/spi/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
@@ -114,7 +115,7 @@ func TestSingleItemRequestInterceptor_InterceptReadRequest(t *testing.T) {
},
},
args: args{
- ctx: context.Background(),
+ ctx: testutils.TestContext(t),
},
mockSetup: func(t *testing.T, fields *fields, args *args) {
plcReadRequest := NewMockPlcReadRequest(t)
@@ -267,7 +268,7 @@ func TestSingleItemRequestInterceptor_InterceptWriteRequest(t *testing.T) {
},
},
args: args{
- ctx: context.Background(),
+ ctx: testutils.TestContext(t),
},
mockSetup: func(t *testing.T, fields *fields, args *args) {
plcWriteRequest := NewMockPlcWriteRequest(t)
@@ -414,7 +415,7 @@ func TestSingleItemRequestInterceptor_ProcessReadResponses(t *testing.T) {
{
name: "two result (bit empty)",
args: args{
- ctx: context.Background(),
+ ctx: testutils.TestContext(t),
},
fields: fields{
readResponseFactory: func(t *testing.T) readResponseFactory {
@@ -444,7 +445,7 @@ func TestSingleItemRequestInterceptor_ProcessReadResponses(t *testing.T) {
{
name: "two result",
args: args{
- ctx: context.Background(),
+ ctx: testutils.TestContext(t),
},
fields: fields{
readResponseFactory: func(t *testing.T) readResponseFactory {
@@ -598,7 +599,7 @@ func TestSingleItemRequestInterceptor_ProcessWriteResponses(t *testing.T) {
},
},
args: args{
- ctx: context.Background(),
+ ctx: testutils.TestContext(t),
},
mockSetup: func(t *testing.T, fields *fields, args *args) {
result1 := NewMockPlcWriteRequestResult(t)
@@ -621,7 +622,7 @@ func TestSingleItemRequestInterceptor_ProcessWriteResponses(t *testing.T) {
{
name: "two result",
args: args{
- ctx: context.Background(),
+ ctx: testutils.TestContext(t),
},
fields: fields{
writeResponseFactory: func(request apiModel.PlcWriteRequest, responseCodes map[string]apiModel.PlcResponseCode) apiModel.PlcWriteResponse {
diff --git a/plc4go/spi/utils/ArrayContext_test.go b/plc4go/spi/utils/ArrayContext_test.go
index 6fd913fbd2..6f009d3173 100644
--- a/plc4go/spi/utils/ArrayContext_test.go
+++ b/plc4go/spi/utils/ArrayContext_test.go
@@ -21,7 +21,9 @@ package utils
import (
"context"
+ "github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
+ "os"
"testing"
)
@@ -39,7 +41,7 @@ func TestCreateArrayContext(t *testing.T) {
{
name: "Create one",
args: args{
- ctx: context.Background(),
+ ctx: testContext(t),
},
wantVerifier: func(t *testing.T, ctx context.Context) bool {
_arrayInfo := ctx.Value(keyArrayInfo)
@@ -72,7 +74,7 @@ func TestGetCurItemFromContext(t *testing.T) {
{
name: "key not set",
args: args{
- ctx: context.Background(),
+ ctx: testContext(t),
},
wantPanic: true,
},
@@ -121,7 +123,7 @@ func TestGetLastItemFromContext(t *testing.T) {
{
name: "key not set",
args: args{
- ctx: context.Background(),
+ ctx: testContext(t),
},
wantPanic: true,
},
@@ -170,7 +172,7 @@ func TestGetNumItemsFromContext(t *testing.T) {
{
name: "key not set",
args: args{
- ctx: context.Background(),
+ ctx: testContext(t),
},
wantPanic: true,
},
@@ -205,3 +207,25 @@ func TestGetNumItemsFromContext(t *testing.T) {
})
}
}
+
+// 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
+ }
+ }))
+}
+
+// note: we can't use testutils here due to import cycle
+func testContext(t *testing.T) context.Context {
+ ctx, cancel := context.WithCancel(context.Background())
+ t.Cleanup(cancel)
+ ctx = produceTestingLogger(t).WithContext(ctx)
+ return ctx
+}