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 cf0b38f651e2d7127c17fd3b967b4e6ca96b03da Author: Sebastian Rühl <[email protected]> AuthorDate: Wed Apr 12 11:03:57 2023 +0200 test(plc4go/cbus): added more tests for setup connection --- plc4go/internal/cbus/Connection_test.go | 310 ++++++++++++++++++++++++++++++++ 1 file changed, 310 insertions(+) diff --git a/plc4go/internal/cbus/Connection_test.go b/plc4go/internal/cbus/Connection_test.go index 62e651ca41..339ceedd89 100644 --- a/plc4go/internal/cbus/Connection_test.go +++ b/plc4go/internal/cbus/Connection_test.go @@ -30,6 +30,7 @@ import ( "github.com/apache/plc4x/plc4go/spi/transports/test" "github.com/stretchr/testify/assert" "net/url" + "sync/atomic" "testing" "time" ) @@ -1296,6 +1297,315 @@ func TestConnection_setupConnection(t *testing.T) { ch: make(chan plc4go.PlcConnectionConnectResult, 1), }, }, + { + name: "setup connection (failing after reset)", + fields: fields{ + DefaultConnection: _default.NewDefaultConnection(nil), + messageCodec: func() *MessageCodec { + transport := test.NewTransport() + transportUrl := url.URL{Scheme: "test"} + transportInstance, err := transport.CreateTransportInstance(transportUrl, nil) + if err != nil { + t.Error(err) + t.FailNow() + return nil + } + type MockState uint8 + const ( + RESET MockState = iota + DONE + ) + currentState := atomic.Value{} + currentState.Store(RESET) + transportInstance.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) { + switch currentState.Load().(MockState) { + case RESET: + t.Log("Dispatching reset echo") + transportInstance.FillReadBuffer([]byte("~~~\r")) + currentState.Store(DONE) + case DONE: + t.Log("Done") + } + }) + codec := NewMessageCodec(transportInstance) + err = codec.Connect() + if err != nil { + t.Error(err) + t.FailNow() + return nil + } + return codec + }(), + }, + args: args{ + ctx: context.Background(), + ch: make(chan plc4go.PlcConnectionConnectResult, 1), + }, + }, + { + name: "setup connection (failing after app filters)", + fields: fields{ + DefaultConnection: _default.NewDefaultConnection(nil), + messageCodec: func() *MessageCodec { + transport := test.NewTransport() + transportUrl := url.URL{Scheme: "test"} + transportInstance, err := transport.CreateTransportInstance(transportUrl, nil) + if err != nil { + t.Error(err) + t.FailNow() + return nil + } + type MockState uint8 + const ( + RESET MockState = iota + APPLICATION_FILTER_1 + APPLICATION_FILTER_2 + DONE + ) + currentState := atomic.Value{} + currentState.Store(RESET) + transportInstance.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) { + switch currentState.Load().(MockState) { + case RESET: + t.Log("Dispatching reset echo") + transportInstance.FillReadBuffer([]byte("~~~\r")) + currentState.Store(APPLICATION_FILTER_1) + case APPLICATION_FILTER_1: + t.Log("Dispatching app1 echo and confirm") + transportInstance.FillReadBuffer([]byte("@A32100FF\r")) + transportInstance.FillReadBuffer([]byte("322100AD\r\n")) + currentState.Store(APPLICATION_FILTER_2) + case APPLICATION_FILTER_2: + t.Log("Dispatching app2 echo and confirm") + transportInstance.FillReadBuffer([]byte("@A32200FF\r")) + transportInstance.FillReadBuffer([]byte("322200AC\r\n")) + currentState.Store(DONE) + case DONE: + t.Log("Done") + } + }) + codec := NewMessageCodec(transportInstance) + err = codec.Connect() + if err != nil { + t.Error(err) + t.FailNow() + return nil + } + return codec + }(), + }, + args: args{ + ctx: context.Background(), + ch: make(chan plc4go.PlcConnectionConnectResult, 1), + }, + }, + { + name: "setup connection (failing after interface options 3", + fields: fields{ + DefaultConnection: _default.NewDefaultConnection(nil), + messageCodec: func() *MessageCodec { + transport := test.NewTransport() + transportUrl := url.URL{Scheme: "test"} + transportInstance, err := transport.CreateTransportInstance(transportUrl, nil) + if err != nil { + t.Error(err) + t.FailNow() + return nil + } + type MockState uint8 + const ( + RESET MockState = iota + APPLICATION_FILTER_1 + APPLICATION_FILTER_2 + INTERFACE_OPTIONS_3 + DONE + ) + currentState := atomic.Value{} + currentState.Store(RESET) + transportInstance.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) { + switch currentState.Load().(MockState) { + case RESET: + t.Log("Dispatching reset echo") + transportInstance.FillReadBuffer([]byte("~~~\r")) + currentState.Store(APPLICATION_FILTER_1) + case APPLICATION_FILTER_1: + t.Log("Dispatching app1 echo and confirm") + transportInstance.FillReadBuffer([]byte("@A32100FF\r")) + transportInstance.FillReadBuffer([]byte("322100AD\r\n")) + currentState.Store(APPLICATION_FILTER_2) + case APPLICATION_FILTER_2: + t.Log("Dispatching app2 echo and confirm") + transportInstance.FillReadBuffer([]byte("@A32200FF\r")) + transportInstance.FillReadBuffer([]byte("322200AC\r\n")) + currentState.Store(INTERFACE_OPTIONS_3) + case INTERFACE_OPTIONS_3: + t.Log("Dispatching interface 3 echo and confirm") + transportInstance.FillReadBuffer([]byte("@A342000A\r")) + transportInstance.FillReadBuffer([]byte("3242008C\r\n")) + currentState.Store(DONE) + case DONE: + t.Log("Done") + } + }) + codec := NewMessageCodec(transportInstance) + err = codec.Connect() + if err != nil { + t.Error(err) + t.FailNow() + return nil + } + return codec + }(), + }, + args: args{ + ctx: context.Background(), + ch: make(chan plc4go.PlcConnectionConnectResult, 1), + }, + }, + { + name: "setup connection (failing after interface options 1 pun)", + fields: fields{ + DefaultConnection: _default.NewDefaultConnection(nil), + messageCodec: func() *MessageCodec { + transport := test.NewTransport() + transportUrl := url.URL{Scheme: "test"} + transportInstance, err := transport.CreateTransportInstance(transportUrl, nil) + if err != nil { + t.Error(err) + t.FailNow() + return nil + } + type MockState uint8 + const ( + RESET MockState = iota + APPLICATION_FILTER_1 + APPLICATION_FILTER_2 + INTERFACE_OPTIONS_3 + INTERFACE_OPTIONS_1_PUN + DONE + ) + currentState := atomic.Value{} + currentState.Store(RESET) + transportInstance.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) { + switch currentState.Load().(MockState) { + case RESET: + t.Log("Dispatching reset echo") + transportInstance.FillReadBuffer([]byte("~~~\r")) + currentState.Store(APPLICATION_FILTER_1) + case APPLICATION_FILTER_1: + t.Log("Dispatching app1 echo and confirm") + transportInstance.FillReadBuffer([]byte("@A32100FF\r")) + transportInstance.FillReadBuffer([]byte("322100AD\r\n")) + currentState.Store(APPLICATION_FILTER_2) + case APPLICATION_FILTER_2: + t.Log("Dispatching app2 echo and confirm") + transportInstance.FillReadBuffer([]byte("@A32200FF\r")) + transportInstance.FillReadBuffer([]byte("322200AC\r\n")) + currentState.Store(INTERFACE_OPTIONS_3) + case INTERFACE_OPTIONS_3: + t.Log("Dispatching interface 3 echo and confirm") + transportInstance.FillReadBuffer([]byte("@A342000A\r")) + transportInstance.FillReadBuffer([]byte("3242008C\r\n")) + currentState.Store(INTERFACE_OPTIONS_1_PUN) + case INTERFACE_OPTIONS_1_PUN: + t.Log("Dispatching interface 1 PUN echo and confirm???") + transportInstance.FillReadBuffer([]byte("@A3410079\r")) + transportInstance.FillReadBuffer([]byte("3241008D\r\n")) + currentState.Store(DONE) + case DONE: + t.Log("Done") + } + }) + codec := NewMessageCodec(transportInstance) + err = codec.Connect() + if err != nil { + t.Error(err) + t.FailNow() + return nil + } + return codec + }(), + }, + args: args{ + ctx: context.Background(), + ch: make(chan plc4go.PlcConnectionConnectResult, 1), + }, + }, + { + name: "setup connection", + fields: fields{ + DefaultConnection: _default.NewDefaultConnection(nil), + messageCodec: func() *MessageCodec { + transport := test.NewTransport() + transportUrl := url.URL{Scheme: "test"} + transportInstance, err := transport.CreateTransportInstance(transportUrl, nil) + if err != nil { + t.Error(err) + t.FailNow() + return nil + } + type MockState uint8 + const ( + RESET MockState = iota + APPLICATION_FILTER_1 + APPLICATION_FILTER_2 + INTERFACE_OPTIONS_3 + INTERFACE_OPTIONS_1_PUN + INTERFACE_OPTIONS_1 + DONE + ) + currentState := atomic.Value{} + currentState.Store(RESET) + transportInstance.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) { + switch currentState.Load().(MockState) { + case RESET: + t.Log("Dispatching reset echo") + transportInstance.FillReadBuffer([]byte("~~~\r")) + currentState.Store(APPLICATION_FILTER_1) + case APPLICATION_FILTER_1: + t.Log("Dispatching app1 echo and confirm") + transportInstance.FillReadBuffer([]byte("@A32100FF\r")) + transportInstance.FillReadBuffer([]byte("322100AD\r\n")) + currentState.Store(APPLICATION_FILTER_2) + case APPLICATION_FILTER_2: + t.Log("Dispatching app2 echo and confirm") + transportInstance.FillReadBuffer([]byte("@A32200FF\r")) + transportInstance.FillReadBuffer([]byte("322200AC\r\n")) + currentState.Store(INTERFACE_OPTIONS_3) + case INTERFACE_OPTIONS_3: + t.Log("Dispatching interface 3 echo and confirm") + transportInstance.FillReadBuffer([]byte("@A342000A\r")) + transportInstance.FillReadBuffer([]byte("3242008C\r\n")) + currentState.Store(INTERFACE_OPTIONS_1_PUN) + case INTERFACE_OPTIONS_1_PUN: + t.Log("Dispatching interface 1 PUN echo and confirm???") + transportInstance.FillReadBuffer([]byte("@A3410079\r")) + transportInstance.FillReadBuffer([]byte("3241008D\r\n")) + currentState.Store(INTERFACE_OPTIONS_1) + case INTERFACE_OPTIONS_1: + t.Log("Dispatching interface 1 echo and confirm???") + transportInstance.FillReadBuffer([]byte("@A3300079\r")) + transportInstance.FillReadBuffer([]byte("3230009E\r\n")) + currentState.Store(DONE) + case DONE: + t.Log("Done") + } + }) + codec := NewMessageCodec(transportInstance) + err = codec.Connect() + if err != nil { + t.Error(err) + t.FailNow() + return nil + } + return codec + }(), + }, + args: args{ + ctx: context.Background(), + ch: make(chan plc4go.PlcConnectionConnectResult, 1), + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {
