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/02 11:05:15 UTC

[plc4x] branch develop updated: test(plc4go/cbus): properly shutdown discovery at test end

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 30ae32d658 test(plc4go/cbus): properly shutdown discovery at test end
30ae32d658 is described below

commit 30ae32d6586124b433633449819fa47047a04aaa
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Fri Jun 2 13:05:07 2023 +0200

    test(plc4go/cbus): properly shutdown discovery at test end
---
 plc4go/internal/cbus/Discoverer_test.go | 25 +++++++++++++++++++------
 plc4go/internal/cbus/Driver_test.go     | 10 +++++++++-
 2 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/plc4go/internal/cbus/Discoverer_test.go b/plc4go/internal/cbus/Discoverer_test.go
index f005d926dd..5418525b15 100644
--- a/plc4go/internal/cbus/Discoverer_test.go
+++ b/plc4go/internal/cbus/Discoverer_test.go
@@ -73,20 +73,27 @@ func TestDiscoverer_Discover(t *testing.T) {
 		fields   fields
 		args     args
 		wantErr  assert.ErrorAssertionFunc
-		setup    func(t *testing.T, fields *fields) (params []any)
+		setup    func(t *testing.T, fields *fields, args *args) (params []any)
 		teardown func(params []any)
 	}{
 		{
 			name: "discover unknown device",
 			args: args{
-				ctx: context.Background(),
 				callback: func(_ apiModel.PlcDiscoveryItem) {
 				},
 				discoveryOptions: []options.WithDiscoveryOption{
 					options.WithDiscoveryOptionDeviceName("blub"),
 				},
 			},
-			setup: func(t *testing.T, fields *fields) (params []any) {
+			setup: func(t *testing.T, fields *fields, args *args) (params []any) {
+				ctx, cancelFunc := context.WithCancel(context.Background())
+				t.Cleanup(func() {
+					cancelFunc()
+					// We give it on second to settle, so it can stop everything
+					time.Sleep(1 * time.Second)
+				})
+				args.ctx = ctx
+
 				fields.transportInstanceCreationQueue = pool.NewFixedSizeExecutor(50, 100, options.WithCustomLogger(testutils.ProduceTestingLogger(t)))
 				fields.deviceScanningQueue = pool.NewFixedSizeExecutor(50, 100, options.WithCustomLogger(testutils.ProduceTestingLogger(t)))
 				return nil
@@ -96,7 +103,6 @@ func TestDiscoverer_Discover(t *testing.T) {
 		{
 			name: "test with loopback",
 			args: args{
-				ctx: context.Background(),
 				callback: func(_ apiModel.PlcDiscoveryItem) {
 				},
 				discoveryOptions: []options.WithDiscoveryOption{
@@ -104,7 +110,14 @@ func TestDiscoverer_Discover(t *testing.T) {
 				},
 			},
 			wantErr: assert.NoError,
-			setup: func(t *testing.T, fields *fields) (params []any) {
+			setup: func(t *testing.T, fields *fields, args *args) (params []any) {
+				ctx, cancelFunc := context.WithCancel(context.Background())
+				t.Cleanup(func() {
+					cancelFunc()
+					// We give it on second to settle, so it can stop everything
+					time.Sleep(1 * time.Second)
+				})
+				args.ctx = ctx
 				fields.transportInstanceCreationQueue = pool.NewFixedSizeExecutor(50, 100, options.WithCustomLogger(testutils.ProduceTestingLogger(t)))
 				fields.deviceScanningQueue = pool.NewFixedSizeExecutor(50, 100, options.WithCustomLogger(testutils.ProduceTestingLogger(t)))
 				oldaddressProviderRetriever := addressProviderRetriever
@@ -126,7 +139,7 @@ func TestDiscoverer_Discover(t *testing.T) {
 		t.Run(tt.name, func(t *testing.T) {
 			var params []any
 			if tt.setup != nil {
-				params = tt.setup(t, &tt.fields)
+				params = tt.setup(t, &tt.fields, &tt.args)
 			}
 			d := &Discoverer{
 				transportInstanceCreationQueue: tt.fields.transportInstanceCreationQueue,
diff --git a/plc4go/internal/cbus/Driver_test.go b/plc4go/internal/cbus/Driver_test.go
index 878eef1dc3..54d0d6a6f4 100644
--- a/plc4go/internal/cbus/Driver_test.go
+++ b/plc4go/internal/cbus/Driver_test.go
@@ -60,12 +60,20 @@ func TestDriver_DiscoverWithContext(t *testing.T) {
 		{
 			name: "localhost discovery",
 			args: args{
-				ctx: context.Background(),
 				callback: func(event apiModel.PlcDiscoveryItem) {
 					t.Log(event)
 				},
 				discoveryOptions: []options.WithDiscoveryOption{options.WithDiscoveryOptionLocalAddress("localhost")},
 			},
+			setup: func(t *testing.T, fields *fields, args *args) {
+				ctx, cancelFunc := context.WithCancel(context.Background())
+				t.Cleanup(func() {
+					cancelFunc()
+					// We give it on second to settle, so it can stop everything
+					time.Sleep(200 * time.Millisecond)
+				})
+				args.ctx = ctx
+			},
 			wantErr: assert.NoError,
 		},
 	}