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/03/28 12:19:44 UTC

[plc4x] branch develop updated (f66dd42324 -> 0bb3c38f9b)

This is an automated email from the ASF dual-hosted git repository.

sruehl pushed a change to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


    from f66dd42324 feat(plc4go/cbus): add more tracing, increase receive timeout
     new 17efa3969c test(plc4go): fix peek panic on to small buffer for test transport
     new 0bb3c38f9b test(plc4go/cbus): add test for message codec to check message stitching

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 plc4go/internal/cbus/MessageCodec_test.go          | 282 +++++++++++++++++++++
 plc4go/spi/transports/test/Transport.go            |  11 +-
 .../apache/plc4x/java/cbus/RandomPackagesTest.java |  12 +
 3 files changed, 304 insertions(+), 1 deletion(-)
 create mode 100644 plc4go/internal/cbus/MessageCodec_test.go


[plc4x] 02/02: test(plc4go/cbus): add test for message codec to check message stitching

Posted by sr...@apache.org.
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 0bb3c38f9b11b7275894b9d3e83a3053840c04ee
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Tue Mar 28 14:19:35 2023 +0200

    test(plc4go/cbus): add test for message codec to check message stitching
---
 plc4go/internal/cbus/MessageCodec_test.go          | 282 +++++++++++++++++++++
 .../apache/plc4x/java/cbus/RandomPackagesTest.java |  12 +
 2 files changed, 294 insertions(+)

diff --git a/plc4go/internal/cbus/MessageCodec_test.go b/plc4go/internal/cbus/MessageCodec_test.go
new file mode 100644
index 0000000000..6d07340b16
--- /dev/null
+++ b/plc4go/internal/cbus/MessageCodec_test.go
@@ -0,0 +1,282 @@
+/*
+ * 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 cbus
+
+import (
+	"fmt"
+	readWriteModel "github.com/apache/plc4x/plc4go/protocols/cbus/readwrite/model"
+	"github.com/apache/plc4x/plc4go/spi"
+	_default "github.com/apache/plc4x/plc4go/spi/default"
+	"github.com/apache/plc4x/plc4go/spi/transports"
+	"github.com/apache/plc4x/plc4go/spi/transports/test"
+	"github.com/stretchr/testify/assert"
+	"testing"
+)
+
+func TestMessageCodec_Send(t *testing.T) {
+	type fields struct {
+		DefaultCodec                  _default.DefaultCodec
+		requestContext                readWriteModel.RequestContext
+		cbusOptions                   readWriteModel.CBusOptions
+		monitoredMMIs                 chan readWriteModel.CALReply
+		monitoredSALs                 chan readWriteModel.MonitoredSAL
+		lastPackageHash               uint32
+		hashEncountered               uint
+		currentlyReportedServerErrors uint
+	}
+	type args struct {
+		message spi.Message
+	}
+	tests := []struct {
+		name    string
+		fields  fields
+		args    args
+		wantErr assert.ErrorAssertionFunc
+	}{
+		// TODO: Add test cases.
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			m := &MessageCodec{
+				DefaultCodec:                  tt.fields.DefaultCodec,
+				requestContext:                tt.fields.requestContext,
+				cbusOptions:                   tt.fields.cbusOptions,
+				monitoredMMIs:                 tt.fields.monitoredMMIs,
+				monitoredSALs:                 tt.fields.monitoredSALs,
+				lastPackageHash:               tt.fields.lastPackageHash,
+				hashEncountered:               tt.fields.hashEncountered,
+				currentlyReportedServerErrors: tt.fields.currentlyReportedServerErrors,
+			}
+			tt.wantErr(t, m.Send(tt.args.message), fmt.Sprintf("Send(%v)", tt.args.message))
+		})
+	}
+}
+
+func TestMessageCodec_Receive(t *testing.T) {
+	type fields struct {
+		DefaultCodec                  _default.DefaultCodec
+		requestContext                readWriteModel.RequestContext
+		cbusOptions                   readWriteModel.CBusOptions
+		monitoredMMIs                 chan readWriteModel.CALReply
+		monitoredSALs                 chan readWriteModel.MonitoredSAL
+		lastPackageHash               uint32
+		hashEncountered               uint
+		currentlyReportedServerErrors uint
+	}
+	tests := []struct {
+		name    string
+		fields  fields
+		want    spi.Message
+		wantErr assert.ErrorAssertionFunc
+	}{
+		{
+			name: "No data",
+			fields: fields{
+				DefaultCodec: NewMessageCodec(func() transports.TransportInstance {
+					transport := test.NewTransport()
+					transportInstance := test.NewTransportInstance(transport)
+					return transportInstance
+				}()),
+				requestContext:                nil,
+				cbusOptions:                   nil,
+				monitoredMMIs:                 nil,
+				monitoredSALs:                 nil,
+				lastPackageHash:               0,
+				hashEncountered:               0,
+				currentlyReportedServerErrors: 0,
+			},
+			wantErr: assert.Error,
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			m := &MessageCodec{
+				DefaultCodec:                  tt.fields.DefaultCodec,
+				requestContext:                tt.fields.requestContext,
+				cbusOptions:                   tt.fields.cbusOptions,
+				monitoredMMIs:                 tt.fields.monitoredMMIs,
+				monitoredSALs:                 tt.fields.monitoredSALs,
+				lastPackageHash:               tt.fields.lastPackageHash,
+				hashEncountered:               tt.fields.hashEncountered,
+				currentlyReportedServerErrors: tt.fields.currentlyReportedServerErrors,
+			}
+			got, err := m.Receive()
+			if !tt.wantErr(t, err, fmt.Sprintf("Receive()")) {
+				return
+			}
+			assert.Equalf(t, tt.want, got, "Receive()")
+		})
+	}
+}
+
+func TestMessageCodec_Receive_Delayed_Response(t *testing.T) {
+	t.Run("instant data", func(t *testing.T) {
+		transport := test.NewTransport()
+		transportInstance := test.NewTransportInstance(transport)
+		codec := NewMessageCodec(transportInstance)
+		codec.requestContext = readWriteModel.NewRequestContext(true)
+
+		var msg spi.Message
+		var err error
+		msg, err = codec.Receive()
+		// No data yet so this should error
+		assert.Error(t, err)
+		assert.Nil(t, msg)
+		// Now we add a confirmation
+		err = transportInstance.FillReadBuffer([]byte("i."))
+		assert.NoError(t, err)
+
+		// We should wait for more data, so no error, no message
+		msg, err = codec.Receive()
+		assert.NoError(t, err)
+		assert.Nil(t, msg)
+
+		// Now we fill in the payload
+		err = transportInstance.FillReadBuffer([]byte("86FD0201078900434C495053414C20C2\r\n"))
+		assert.NoError(t, err)
+
+		// We should wait for more data, so no error, no message
+		msg, err = codec.Receive()
+		assert.NoError(t, err)
+		assert.NotNil(t, msg)
+
+		// The message should have a confirmation with an alpha
+		assert.True(t, msg.(readWriteModel.CBusMessageToClient).GetReply().GetIsAlpha())
+	})
+	t.Run("data after 6 times", func(t *testing.T) {
+		transport := test.NewTransport()
+		transportInstance := test.NewTransportInstance(transport)
+		codec := NewMessageCodec(transportInstance)
+		codec.requestContext = readWriteModel.NewRequestContext(true)
+
+		var msg spi.Message
+		var err error
+		msg, err = codec.Receive()
+		// No data yet so this should error
+		assert.Error(t, err)
+		assert.Nil(t, msg)
+		// Now we add a confirmation
+		err = transportInstance.FillReadBuffer([]byte("i."))
+		assert.NoError(t, err)
+
+		for i := 0; i < 8; i++ {
+			t.Logf("%d try", i+1)
+			// We should wait for more data, so no error, no message
+			msg, err = codec.Receive()
+			assert.NoError(t, err)
+			assert.Nil(t, msg)
+		}
+
+		// Now we fill in the payload
+		err = transportInstance.FillReadBuffer([]byte("86FD0201078900434C495053414C20C2\r\n"))
+		assert.NoError(t, err)
+
+		// We should wait for more data, so no error, no message
+		msg, err = codec.Receive()
+		assert.NoError(t, err)
+		assert.NotNil(t, msg)
+
+		// The message should have a confirmation with an alpha
+		assert.True(t, msg.(readWriteModel.CBusMessageToClient).GetReply().GetIsAlpha())
+	})
+	t.Run("data after 16 times", func(t *testing.T) {
+		transport := test.NewTransport()
+		transportInstance := test.NewTransportInstance(transport)
+		codec := NewMessageCodec(transportInstance)
+		codec.requestContext = readWriteModel.NewRequestContext(true)
+
+		var msg spi.Message
+		var err error
+		msg, err = codec.Receive()
+		// No data yet so this should error
+		assert.Error(t, err)
+		assert.Nil(t, msg)
+		// Now we add a confirmation
+		err = transportInstance.FillReadBuffer([]byte("i."))
+		assert.NoError(t, err)
+
+		for i := 0; i < 16; i++ {
+			t.Logf("%d try", i+1)
+			// We should wait for more data, so no error, no message
+			msg, err = codec.Receive()
+			if i == 15 {
+				assert.NoError(t, err)
+				assert.NotNil(t, msg)
+				// This should be the confirmation only ...
+				reply := msg.(readWriteModel.CBusMessageToClient).GetReply()
+				assert.True(t, reply.GetIsAlpha())
+				// ... and no content
+				assert.Nil(t, reply.(readWriteModel.ReplyOrConfirmationConfirmation).GetEmbeddedReply())
+			} else {
+				assert.NoError(t, err)
+				assert.Nil(t, msg)
+			}
+		}
+
+		// Now we fill in the payload
+		err = transportInstance.FillReadBuffer([]byte("86FD0201078900434C495053414C20C2\r\n"))
+		assert.NoError(t, err)
+
+		// We should wait for more data, so no error, no message
+		msg, err = codec.Receive()
+		assert.NoError(t, err)
+		assert.NotNil(t, msg)
+
+		// The message should have a confirmation without an alpha
+		assert.False(t, msg.(readWriteModel.CBusMessageToClient).GetReply().GetIsAlpha())
+	})
+}
+
+func TestNewMessageCodec(t *testing.T) {
+	type args struct {
+		transportInstance transports.TransportInstance
+	}
+	tests := []struct {
+		name string
+		args args
+		want *MessageCodec
+	}{
+		// TODO: Add test cases.
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			assert.Equalf(t, tt.want, NewMessageCodec(tt.args.transportInstance), "NewMessageCodec(%v)", tt.args.transportInstance)
+		})
+	}
+}
+
+func Test_extractMMIAndSAL(t *testing.T) {
+	type args struct {
+		codec   _default.DefaultCodecRequirements
+		message spi.Message
+	}
+	tests := []struct {
+		name string
+		args args
+		want bool
+	}{
+		// TODO: Add test cases.
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			assert.Equalf(t, tt.want, extractMMIAndSAL(tt.args.codec, tt.args.message), "extractMMIAndSAL(%v, %v)", tt.args.codec, tt.args.message)
+		})
+	}
+}
diff --git a/plc4j/drivers/c-bus/src/test/java/org/apache/plc4x/java/cbus/RandomPackagesTest.java b/plc4j/drivers/c-bus/src/test/java/org/apache/plc4x/java/cbus/RandomPackagesTest.java
index a50c1059f8..e2c437464a 100644
--- a/plc4j/drivers/c-bus/src/test/java/org/apache/plc4x/java/cbus/RandomPackagesTest.java
+++ b/plc4j/drivers/c-bus/src/test/java/org/apache/plc4x/java/cbus/RandomPackagesTest.java
@@ -482,6 +482,18 @@ public class RandomPackagesTest {
             assertThat(msg).isNotNull();
             System.out.println(msg);
 
+            assertMessageMatches(bytes, msg);
+        }
+        @Test
+        void BridgedIdentifyResponse2() throws Exception {
+            byte[] bytes = ("86FD0201078900434C495053414C20C2\r\n").getBytes(StandardCharsets.UTF_8);
+            ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+            cBusOptions = C_BUS_OPTIONS_WITH_SRCHK;
+            requestContext = new RequestContext(true);
+            CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, true, requestContext, cBusOptions);
+            assertThat(msg).isNotNull();
+            System.out.println(msg);
+
             assertMessageMatches(bytes, msg);
         }
     }


[plc4x] 01/02: test(plc4go): fix peek panic on to small buffer for test transport

Posted by sr...@apache.org.
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 17efa3969c7ea8aa14bd8d433f223eb8b294c7b7
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Tue Mar 28 14:18:59 2023 +0200

    test(plc4go): fix peek panic on to small buffer for test transport
---
 plc4go/spi/transports/test/Transport.go | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/plc4go/spi/transports/test/Transport.go b/plc4go/spi/transports/test/Transport.go
index 51a2b952ed..44a5ecc55e 100644
--- a/plc4go/spi/transports/test/Transport.go
+++ b/plc4go/spi/transports/test/Transport.go
@@ -26,6 +26,7 @@ import (
 	"github.com/apache/plc4x/plc4go/spi/transports"
 	"github.com/pkg/errors"
 	"github.com/rs/zerolog/log"
+	"math"
 	"net/url"
 )
 
@@ -111,7 +112,15 @@ func (m *TransportInstance) FillBuffer(until func(pos uint, currentByte byte, re
 
 func (m *TransportInstance) PeekReadableBytes(numBytes uint32) ([]uint8, error) {
 	log.Trace().Msgf("Peek %d readable bytes", numBytes)
-	return m.readBuffer[0:numBytes], nil
+	availableBytes := uint32(math.Min(float64(numBytes), float64(len(m.readBuffer))))
+	var err error
+	if availableBytes != numBytes {
+		err = errors.New("not enough bytes available")
+	}
+	if availableBytes == 0 {
+		return nil, err
+	}
+	return m.readBuffer[0:availableBytes], nil
 }
 
 func (m *TransportInstance) Read(numBytes uint32) ([]uint8, error) {