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 12:52:38 UTC

[plc4x] branch develop updated: test(plc4go/cbus): add some wait time till dispatching mmi segments

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 4b4794311b test(plc4go/cbus): add some wait time till dispatching mmi segments
4b4794311b is described below

commit 4b4794311b2473e40a4966f7f854c7c44ab5ca2b
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Thu Jun 15 14:52:30 2023 +0200

    test(plc4go/cbus): add some wait time till dispatching mmi segments
---
 plc4go/internal/cbus/Browser.go      | 13 +++++++++----
 plc4go/internal/cbus/Browser_test.go | 24 ++++++++++++++++--------
 2 files changed, 25 insertions(+), 12 deletions(-)

diff --git a/plc4go/internal/cbus/Browser.go b/plc4go/internal/cbus/Browser.go
index 85e43e6283..21a26b62df 100644
--- a/plc4go/internal/cbus/Browser.go
+++ b/plc4go/internal/cbus/Browser.go
@@ -180,6 +180,10 @@ func (m Browser) extractAttributes(query *unitInfoQuery) ([]readWriteModel.Attri
 }
 
 func (m Browser) getInstalledUnitAddressBytes(ctx context.Context) (map[byte]any, error) {
+	start := time.Now()
+	defer func() {
+		m.log.Debug().Msgf("Ending unit address acquiring after %s", time.Since(start))
+	}()
 	// We need to pre-subscribe to catch the 2 followup responses
 	subscriptionRequest, err := m.connection.SubscriptionRequestBuilder().
 		AddEventTagAddress("installationMMIMonitor", "mmimonitor/*/NETWORK_CONTROL").
@@ -188,15 +192,16 @@ func (m Browser) getInstalledUnitAddressBytes(ctx context.Context) (map[byte]any
 		return nil, errors.Wrap(err, "Error subscribing to the installation MMI")
 	}
 	subCtx, subCtxCancel := context.WithTimeout(ctx, time.Second*2)
+	defer subCtxCancel()
 	subscriptionResult := <-subscriptionRequest.ExecuteWithContext(subCtx)
-	subCtxCancel()
 	if err := subscriptionResult.GetErr(); err != nil {
 		return nil, errors.Wrap(err, "Error subscribing to the mmi")
 	}
-	if responseCode := subscriptionResult.GetResponse().GetResponseCode("installationMMIMonitor"); responseCode != apiModel.PlcResponseCode_OK {
+	response := subscriptionResult.GetResponse()
+	if responseCode := response.GetResponseCode("installationMMIMonitor"); responseCode != apiModel.PlcResponseCode_OK {
 		return nil, errors.Errorf("Got %s", responseCode)
 	}
-	subscriptionHandle, err := subscriptionResult.GetResponse().GetSubscriptionHandle("installationMMIMonitor")
+	subscriptionHandle, err := response.GetSubscriptionHandle("installationMMIMonitor")
 	if err != nil {
 		return nil, errors.Wrap(err, "Error getting the subscription handle")
 	}
@@ -287,6 +292,7 @@ func (m Browser) getInstalledUnitAddressBytes(ctx context.Context) (map[byte]any
 		return nil, errors.Wrap(err, "Error getting the installation MMI")
 	}
 	readCtx, readCtxCancel := context.WithTimeout(ctx, time.Second*2)
+	defer readCtxCancel()
 	go func() {
 		defer func() {
 			if err := recover(); err != nil {
@@ -377,6 +383,5 @@ func (m Browser) getInstalledUnitAddressBytes(ctx context.Context) (map[byte]any
 			return nil, errors.Wrap(err, "error waiting for other offsets")
 		}
 	}
-	readCtxCancel()
 	return result, nil
 }
diff --git a/plc4go/internal/cbus/Browser_test.go b/plc4go/internal/cbus/Browser_test.go
index bbeca997e8..5f4c0c7529 100644
--- a/plc4go/internal/cbus/Browser_test.go
+++ b/plc4go/internal/cbus/Browser_test.go
@@ -146,10 +146,14 @@ func TestBrowser_BrowseQuery(t *testing.T) {
 						transportInstance.FillReadBuffer([]byte("g.890050435F434E49454422\r\n"))
 						currentState.Store(DONE)
 					case DONE:
-						t.Log("Dispatching 3 MMI segments")
-						transportInstance.FillReadBuffer([]byte("86020200F900FF0094120006000000000000000008000000000000000000CA\r\n"))
-						transportInstance.FillReadBuffer([]byte("86020200F900FF580000000000000000000000000000000000000000000026\r\n"))
-						transportInstance.FillReadBuffer([]byte("86020200F700FFB00000000000000000000000000000000000000000D0\r\n"))
+						t.Log("Connection dance done")
+						go func() {
+							time.Sleep(200 * time.Millisecond)
+							t.Log("Dispatching 3 MMI segments")
+							transportInstance.FillReadBuffer([]byte("86020200F900FF0094120006000000000000000008000000000000000000CA\r\n"))
+							transportInstance.FillReadBuffer([]byte("86020200F900FF580000000000000000000000000000000000000000000026\r\n"))
+							transportInstance.FillReadBuffer([]byte("86020200F700FFB00000000000000000000000000000000000000000D0\r\n"))
+						}()
 					}
 				})
 				err = transport.AddPreregisteredInstances(transportUrl, transportInstance)
@@ -408,10 +412,14 @@ func TestBrowser_getInstalledUnitAddressBytes(t *testing.T) {
 						transportInstance.FillReadBuffer([]byte("3230009E\r\n"))
 						currentState.Store(DONE)
 					case DONE:
-						t.Log("Dispatching 3 MMI segments")
-						transportInstance.FillReadBuffer([]byte("86020200F900FF0094120006000000000000000008000000000000000000CA\r\n"))
-						transportInstance.FillReadBuffer([]byte("86020200F900FF580000000000000000000000000000000000000000000026\r\n"))
-						transportInstance.FillReadBuffer([]byte("86020200F700FFB00000000000000000000000000000000000000000D0\r\n"))
+						t.Log("Connection dance done")
+						go func() {
+							time.Sleep(200 * time.Millisecond)
+							t.Log("Dispatching 3 MMI segments")
+							transportInstance.FillReadBuffer([]byte("86020200F900FF0094120006000000000000000008000000000000000000CA\r\n"))
+							transportInstance.FillReadBuffer([]byte("86020200F900FF580000000000000000000000000000000000000000000026\r\n"))
+							transportInstance.FillReadBuffer([]byte("86020200F700FFB00000000000000000000000000000000000000000D0\r\n"))
+						}()
 					}
 				})
 				err = transport.AddPreregisteredInstances(transportUrl, transportInstance)