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/14 11:56:55 UTC

[plc4x] branch develop updated: test(plc4go/cbus): sync state change for state machine

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 634607d135 test(plc4go/cbus): sync state change for state machine
634607d135 is described below

commit 634607d13577a213c558e5ce889d02c404a9f72f
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Wed Jun 14 13:56:46 2023 +0200

    test(plc4go/cbus): sync state change for state machine
---
 plc4go/internal/cbus/Browser_test.go    |  7 +++++++
 plc4go/internal/cbus/Connection_test.go | 16 ++++++++++++++++
 plc4go/internal/cbus/Reader_test.go     | 28 ++++++++++++++++++++++++++++
 plc4go/spi/transports/test/Transport.go |  1 +
 4 files changed, 52 insertions(+)

diff --git a/plc4go/internal/cbus/Browser_test.go b/plc4go/internal/cbus/Browser_test.go
index e150f04f8f..56046131bc 100644
--- a/plc4go/internal/cbus/Browser_test.go
+++ b/plc4go/internal/cbus/Browser_test.go
@@ -24,6 +24,7 @@ import (
 	"fmt"
 	"github.com/apache/plc4x/plc4go/spi/pool"
 	"net/url"
+	"sync"
 	"sync/atomic"
 	"testing"
 	"time"
@@ -118,7 +119,10 @@ func TestBrowser_BrowseQuery(t *testing.T) {
 				)
 				currentState := atomic.Value{}
 				currentState.Store(RESET)
+				stateChangeMutex := sync.Mutex{}
 				transportInstance.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) {
+					stateChangeMutex.Lock()
+					defer stateChangeMutex.Unlock()
 					switch currentState.Load().(MockState) {
 					case RESET:
 						t.Log("Dispatching reset echo")
@@ -395,7 +399,10 @@ func TestBrowser_getInstalledUnitAddressBytes(t *testing.T) {
 				)
 				currentState := atomic.Value{}
 				currentState.Store(RESET)
+				stateChangeMutex := sync.Mutex{}
 				transportInstance.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) {
+					stateChangeMutex.Lock()
+					defer stateChangeMutex.Unlock()
 					switch currentState.Load().(MockState) {
 					case RESET:
 						t.Log("Dispatching reset echo")
diff --git a/plc4go/internal/cbus/Connection_test.go b/plc4go/internal/cbus/Connection_test.go
index 991ef458d2..9dea7b71b9 100644
--- a/plc4go/internal/cbus/Connection_test.go
+++ b/plc4go/internal/cbus/Connection_test.go
@@ -36,6 +36,7 @@ import (
 	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/require"
 	"net/url"
+	"sync"
 	"sync/atomic"
 	"testing"
 	"time"
@@ -1529,7 +1530,10 @@ func TestConnection_setupConnection(t *testing.T) {
 				)
 				currentState := atomic.Value{}
 				currentState.Store(RESET)
+				stateChangeMutex := sync.Mutex{}
 				ti.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) {
+					stateChangeMutex.Lock()
+					defer stateChangeMutex.Unlock()
 					switch currentState.Load().(MockState) {
 					case RESET:
 						t.Log("Dispatching reset echo")
@@ -1582,7 +1586,10 @@ func TestConnection_setupConnection(t *testing.T) {
 				)
 				currentState := atomic.Value{}
 				currentState.Store(RESET)
+				stateChangeMutex := sync.Mutex{}
 				ti.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) {
+					stateChangeMutex.Lock()
+					defer stateChangeMutex.Unlock()
 					switch currentState.Load().(MockState) {
 					case RESET:
 						t.Log("Dispatching reset echo")
@@ -1646,7 +1653,10 @@ func TestConnection_setupConnection(t *testing.T) {
 				)
 				currentState := atomic.Value{}
 				currentState.Store(RESET)
+				stateChangeMutex := sync.Mutex{}
 				ti.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) {
+					stateChangeMutex.Lock()
+					defer stateChangeMutex.Unlock()
 					switch currentState.Load().(MockState) {
 					case RESET:
 						t.Log("Dispatching reset echo")
@@ -1716,7 +1726,10 @@ func TestConnection_setupConnection(t *testing.T) {
 				)
 				currentState := atomic.Value{}
 				currentState.Store(RESET)
+				stateChangeMutex := sync.Mutex{}
 				ti.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) {
+					stateChangeMutex.Lock()
+					defer stateChangeMutex.Unlock()
 					switch currentState.Load().(MockState) {
 					case RESET:
 						t.Log("Dispatching reset echo")
@@ -1789,7 +1802,10 @@ func TestConnection_setupConnection(t *testing.T) {
 					)
 					currentState := atomic.Value{}
 					currentState.Store(RESET)
+					stateChangeMutex := sync.Mutex{}
 					ti.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) {
+						stateChangeMutex.Lock()
+						defer stateChangeMutex.Unlock()
 						switch currentState.Load().(MockState) {
 						case RESET:
 							t.Log("Dispatching reset echo")
diff --git a/plc4go/internal/cbus/Reader_test.go b/plc4go/internal/cbus/Reader_test.go
index 0d7f54a647..7b2a047232 100644
--- a/plc4go/internal/cbus/Reader_test.go
+++ b/plc4go/internal/cbus/Reader_test.go
@@ -36,6 +36,7 @@ import (
 	"github.com/stretchr/testify/require"
 	"net/url"
 	"strings"
+	"sync"
 	"sync/atomic"
 	"testing"
 	"time"
@@ -278,7 +279,10 @@ func TestReader_readSync(t *testing.T) {
 				)
 				currentState := atomic.Value{}
 				currentState.Store(INITIAL)
+				stateChangeMutex := sync.Mutex{}
 				transportInstance.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) {
+					stateChangeMutex.Lock()
+					defer stateChangeMutex.Unlock()
 					switch currentState.Load().(MockState) {
 					case INITIAL:
 						t.Log("Dispatching read response")
@@ -527,7 +531,10 @@ func TestReader_sendMessageOverTheWire(t *testing.T) {
 				)
 				currentState := atomic.Value{}
 				currentState.Store(INITIAL)
+				stateChangeMutex := sync.Mutex{}
 				transportInstance.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) {
+					stateChangeMutex.Lock()
+					defer stateChangeMutex.Unlock()
 					switch currentState.Load().(MockState) {
 					case INITIAL:
 						t.Log("Dispatching read response")
@@ -570,7 +577,10 @@ func TestReader_sendMessageOverTheWire(t *testing.T) {
 					)
 					currentState := atomic.Value{}
 					currentState.Store(INITIAL)
+					stateChangeMutex := sync.Mutex{}
 					transportInstance.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) {
+						stateChangeMutex.Lock()
+						defer stateChangeMutex.Unlock()
 						switch currentState.Load().(MockState) {
 						case INITIAL:
 							t.Log("Dispatching read response")
@@ -707,7 +717,10 @@ func TestReader_sendMessageOverTheWire(t *testing.T) {
 				)
 				currentState := atomic.Value{}
 				currentState.Store(INITIAL)
+				stateChangeMutex := sync.Mutex{}
 				transportInstance.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) {
+					stateChangeMutex.Lock()
+					defer stateChangeMutex.Unlock()
 					switch currentState.Load().(MockState) {
 					case INITIAL:
 						t.Log("Dispatching read response")
@@ -797,7 +810,10 @@ func TestReader_sendMessageOverTheWire(t *testing.T) {
 				)
 				currentState := atomic.Value{}
 				currentState.Store(INITIAL)
+				stateChangeMutex := sync.Mutex{}
 				transportInstance.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) {
+					stateChangeMutex.Lock()
+					defer stateChangeMutex.Unlock()
 					switch currentState.Load().(MockState) {
 					case INITIAL:
 						t.Log("Dispatching read response")
@@ -887,7 +903,10 @@ func TestReader_sendMessageOverTheWire(t *testing.T) {
 				)
 				currentState := atomic.Value{}
 				currentState.Store(INITIAL)
+				stateChangeMutex := sync.Mutex{}
 				transportInstance.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) {
+					stateChangeMutex.Lock()
+					defer stateChangeMutex.Unlock()
 					switch currentState.Load().(MockState) {
 					case INITIAL:
 						t.Log("Dispatching read response")
@@ -977,7 +996,10 @@ func TestReader_sendMessageOverTheWire(t *testing.T) {
 				)
 				currentState := atomic.Value{}
 				currentState.Store(INITIAL)
+				stateChangeMutex := sync.Mutex{}
 				transportInstance.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) {
+					stateChangeMutex.Lock()
+					defer stateChangeMutex.Unlock()
 					switch currentState.Load().(MockState) {
 					case INITIAL:
 						t.Log("Dispatching read response")
@@ -1067,7 +1089,10 @@ func TestReader_sendMessageOverTheWire(t *testing.T) {
 				)
 				currentState := atomic.Value{}
 				currentState.Store(INITIAL)
+				stateChangeMutex := sync.Mutex{}
 				transportInstance.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) {
+					stateChangeMutex.Lock()
+					defer stateChangeMutex.Unlock()
 					switch currentState.Load().(MockState) {
 					case INITIAL:
 						t.Log("Dispatching read response")
@@ -1157,7 +1182,10 @@ func TestReader_sendMessageOverTheWire(t *testing.T) {
 				)
 				currentState := atomic.Value{}
 				currentState.Store(INITIAL)
+				stateChangeMutex := sync.Mutex{}
 				transportInstance.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) {
+					stateChangeMutex.Lock()
+					defer stateChangeMutex.Unlock()
 					switch currentState.Load().(MockState) {
 					case INITIAL:
 						t.Log("Dispatching read response")
diff --git a/plc4go/spi/transports/test/Transport.go b/plc4go/spi/transports/test/Transport.go
index d7d55eea8e..20bca09acd 100644
--- a/plc4go/spi/transports/test/Transport.go
+++ b/plc4go/spi/transports/test/Transport.go
@@ -171,6 +171,7 @@ func (m *TransportInstance) SetWriteInterceptor(writeInterceptor func(transportI
 
 func (m *TransportInstance) Write(data []byte) error {
 	if m.writeInterceptor != nil {
+		m.log.Trace().Msgf("Passing data to write interceptor\n%s", hex.Dump(data))
 		m.writeInterceptor(m, data)
 	}
 	m.log.Trace().Msgf("Write data\n%s", hex.Dump(data))