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 2026/10/12 10:47:07 UTC

(plc4x) 02/05: fix(plc4go/spi): keep the codes of a sub-read that failed when merging reads

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 ed5878ff936dda7475f4ef62dbdbeb82ae045602
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Mon Oct 12 12:14:45 2026 +0200

    fix(plc4go/spi): keep the codes of a sub-read that failed when merging reads
    
    ProcessReadResponses dropped the response of any sub-read that also carried
    an error, so its tags were missing from the merged response altogether - the
    same defect ProcessWriteResponses had. The error is still aggregated; the
    codes and values the sub-read did report are now kept next to it.
---
 .../interceptors/SingleItemRequestInterceptor.go   |  5 ++-
 .../SingleItemRequestInterceptor_test.go           | 48 ++++++++++++++++++++++
 2 files changed, 52 insertions(+), 1 deletion(-)

diff --git a/plc4go/spi/interceptors/SingleItemRequestInterceptor.go b/plc4go/spi/interceptors/SingleItemRequestInterceptor.go
index 047701f1f3..93b0065e0f 100644
--- a/plc4go/spi/interceptors/SingleItemRequestInterceptor.go
+++ b/plc4go/spi/interceptors/SingleItemRequestInterceptor.go
@@ -189,7 +189,10 @@ func (m SingleItemRequestInterceptor) ProcessReadResponses(ctx context.Context,
 		if err := readResult.GetErr(); err != nil {
 			m.log.Debug().Err(err).Msg("Error during read")
 			collectedErrors = append(collectedErrors, err)
-		} else if response := readResult.GetResponse(); response != nil {
+		}
+		// A sub-read can fail and still say how it went for its tag - a read the device never
+		// answered comes back as REQUEST_TIMEOUT next to its error - so its codes are kept too.
+		if response := readResult.GetResponse(); response != nil {
 			request := response.GetRequest()
 			if len(request.GetTagNames()) > 1 {
 				m.log.Error().Int("numberOfTags", len(request.GetTagNames())).Msg("We should only get 1")
diff --git a/plc4go/spi/interceptors/SingleItemRequestInterceptor_test.go b/plc4go/spi/interceptors/SingleItemRequestInterceptor_test.go
index 25b143787a..59fbc6d1f3 100644
--- a/plc4go/spi/interceptors/SingleItemRequestInterceptor_test.go
+++ b/plc4go/spi/interceptors/SingleItemRequestInterceptor_test.go
@@ -33,6 +33,7 @@ import (
 	"github.com/apache/plc4x/plc4go/spi"
 	"github.com/apache/plc4x/plc4go/spi/errors"
 	"github.com/apache/plc4x/plc4go/spi/testutils"
+	spiValues "github.com/apache/plc4x/plc4go/spi/values"
 )
 
 func TestNewSingleItemRequestInterceptor(t *testing.T) {
@@ -465,6 +466,7 @@ func TestSingleItemRequestInterceptor_ProcessReadResponses(t *testing.T) {
 				result1 := NewMockPlcReadRequestResult(t)
 				result1Expect := result1.EXPECT()
 				result1Expect.GetErr().Return(errors.New("asd"))
+				result1Expect.GetResponse().Return(nil)
 				result2 := NewMockPlcReadRequestResult(t)
 				result2Expect := result2.EXPECT()
 				result2Expect.GetResponse().Return(nil)
@@ -485,6 +487,42 @@ func TestSingleItemRequestInterceptor_ProcessReadResponses(t *testing.T) {
 				return true
 			},
 		},
+		{
+			name: "two result (one failed with a response)",
+			setup: func(t *testing.T, fields *fields, args *args) {
+				var mergedCodes map[string]apiModel.PlcResponseCode
+				var mergedValues map[string]apiValues.PlcValue
+				fields.readResponseFactory = func(t *testing.T) readResponseFactory {
+					return func(request apiModel.PlcReadRequest, responseCodes map[string]apiModel.PlcResponseCode, values map[string]apiValues.PlcValue) apiModel.PlcReadResponse {
+						mergedCodes = responseCodes
+						mergedValues = values
+						return nil
+					}
+				}
+				// A sub-read that failed, but still reports a code for its tag (a timeout, say).
+				failed := NewMockPlcReadRequestResult(t)
+				failed.EXPECT().GetErr().Return(errors.New("timed out"))
+				failed.EXPECT().GetResponse().Return(readResponseWith(t, "tag1", apiModel.PlcResponseCode_REQUEST_TIMEOUT, spiValues.NewPlcNULL()))
+				succeeded := NewMockPlcReadRequestResult(t)
+				succeeded.EXPECT().GetErr().Return(nil)
+				succeeded.EXPECT().GetResponse().Return(readResponseWith(t, "tag2", apiModel.PlcResponseCode_OK, spiValues.NewPlcBOOL(true)))
+				args.readResults = []apiModel.PlcReadRequestResult{failed, succeeded}
+				args.ctx = testutils.TestContext(t)
+				t.Cleanup(func() {
+					assert.Equal(t, map[string]apiModel.PlcResponseCode{
+						"tag1": apiModel.PlcResponseCode_REQUEST_TIMEOUT,
+						"tag2": apiModel.PlcResponseCode_OK,
+					}, mergedCodes)
+					assert.Equal(t, map[string]apiValues.PlcValue{
+						"tag1": spiValues.NewPlcNULL(),
+						"tag2": spiValues.NewPlcBOOL(true),
+					}, mergedValues)
+				})
+			},
+			wantAssert: func(t *testing.T, args args, got apiModel.PlcReadRequestResult) bool {
+				return assert.Error(t, got.GetErr())
+			},
+		},
 		{
 			name: "two result (canceled)",
 			args: args{
@@ -739,6 +777,16 @@ func TestSingleItemRequestInterceptor_ProcessWriteResponses(t *testing.T) {
 }
 
 // writeResponseWith is a sub-write's response: one tag, and the code it was written with.
+func readResponseWith(t *testing.T, tagName string, code apiModel.PlcResponseCode, value apiValues.PlcValue) apiModel.PlcReadResponse {
+	request := NewMockPlcReadRequest(t)
+	request.EXPECT().GetTagNames().Return([]string{tagName})
+	response := NewMockPlcReadResponse(t)
+	response.EXPECT().GetRequest().Return(request)
+	response.EXPECT().GetResponseCode(tagName).Return(code)
+	response.EXPECT().GetValue(tagName).Return(value)
+	return response
+}
+
 func writeResponseWith(t *testing.T, tagName string, code apiModel.PlcResponseCode) apiModel.PlcWriteResponse {
 	request := NewMockPlcWriteRequest(t)
 	request.EXPECT().GetTagNames().Return([]string{tagName})