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 2025/03/25 17:56:23 UTC
(plc4x) branch develop updated: fix(plc4go): reverse order for send/expect
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 3cebcc1f85 fix(plc4go): reverse order for send/expect
3cebcc1f85 is described below
commit 3cebcc1f85392e3498ce0de6fdf990a79ee0975e
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Tue Mar 25 18:56:14 2025 +0100
fix(plc4go): reverse order for send/expect
add the expect beforehand helps with devices which responding super fast (and tests)
---
plc4go/internal/bacnetip/ApplicationLayerMessageCodec.go | 2 +-
plc4go/internal/cbus/Reader_test.go | 14 ++++----------
plc4go/spi/MessageCodec.go | 2 +-
plc4go/spi/default/DefaultCodec.go | 12 +++---------
plc4go/spi/default/DefaultCodec_test.go | 12 +++++-------
plc4go/spi/default/mock_DefaultCodec_test.go | 9 ++-------
plc4go/spi/default/mock_MessageCodec_test.go | 9 ++-------
7 files changed, 18 insertions(+), 42 deletions(-)
diff --git a/plc4go/internal/bacnetip/ApplicationLayerMessageCodec.go b/plc4go/internal/bacnetip/ApplicationLayerMessageCodec.go
index c8e0768d46..4ce001a892 100644
--- a/plc4go/internal/bacnetip/ApplicationLayerMessageCodec.go
+++ b/plc4go/internal/bacnetip/ApplicationLayerMessageCodec.go
@@ -152,7 +152,7 @@ func (m *ApplicationLayerMessageCodec) Send(message spi.Message) error {
return nil
}
-func (m *ApplicationLayerMessageCodec) Expect(ctx context.Context, acceptsMessage spi.AcceptsMessage, handleMessage spi.HandleMessage, handleError spi.HandleError, ttl time.Duration) error {
+func (m *ApplicationLayerMessageCodec) Expect(ctx context.Context, acceptsMessage spi.AcceptsMessage, handleMessage spi.HandleMessage, handleError spi.HandleError, ttl time.Duration) {
// TODO: implement me
panic("not yet implemented")
}
diff --git a/plc4go/internal/cbus/Reader_test.go b/plc4go/internal/cbus/Reader_test.go
index 4aadb9e6d5..1107310501 100644
--- a/plc4go/internal/cbus/Reader_test.go
+++ b/plc4go/internal/cbus/Reader_test.go
@@ -288,7 +288,6 @@ func TestReader_readSync(t *testing.T) {
currentState.Store(INITIAL)
stateChangeMutex := sync.Mutex{}
transportInstance.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) {
- time.Sleep(10 * time.Millisecond) // this simulates some network response time // TODO: this should be fixed in DefaultCodec.go where Send is going through before expect
t.Logf("reacting to\n%s", hex.Dump(data))
stateChangeMutex.Lock()
defer stateChangeMutex.Unlock()
@@ -460,7 +459,10 @@ func TestReader_sendMessageOverTheWire(t *testing.T) {
transaction := NewMockRequestTransaction(t)
expect := transaction.EXPECT()
expect.FailRequest(mock.Anything).Return(errors.New("no I say")).Run(func(_ error) {
- close(ch)
+ if ch != nil {
+ close(ch)
+ ch = nil
+ }
})
args.transaction = transaction
@@ -525,7 +527,6 @@ func TestReader_sendMessageOverTheWire(t *testing.T) {
currentState.Store(INITIAL)
stateChangeMutex := sync.Mutex{}
transportInstance.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) {
- time.Sleep(10 * time.Millisecond) // this simulates some network response time // TODO: this should be fixed in DefaultCodec.go where Send is going through before expecttime.Sleep(10 * time.Millisecond) // this simulates some network response time
t.Logf("reacting to\n%s", hex.Dump(data))
stateChangeMutex.Lock()
defer stateChangeMutex.Unlock()
@@ -600,7 +601,6 @@ func TestReader_sendMessageOverTheWire(t *testing.T) {
currentState.Store(INITIAL)
stateChangeMutex := sync.Mutex{}
transportInstance.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) {
- time.Sleep(10 * time.Millisecond) // this simulates some network response time // TODO: this should be fixed in DefaultCodec.go where Send is going through before expecttime.Sleep(10 * time.Millisecond) // this simulates some network response time
t.Logf("reacting to\n%s", hex.Dump(data))
stateChangeMutex.Lock()
defer stateChangeMutex.Unlock()
@@ -691,7 +691,6 @@ func TestReader_sendMessageOverTheWire(t *testing.T) {
currentState.Store(INITIAL)
stateChangeMutex := sync.Mutex{}
transportInstance.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) {
- time.Sleep(10 * time.Millisecond) // this simulates some network response time // TODO: this should be fixed in DefaultCodec.go where Send is going through before expecttime.Sleep(10 * time.Millisecond) // this simulates some network response time
t.Logf("reacting to\n%s", hex.Dump(data))
stateChangeMutex.Lock()
defer stateChangeMutex.Unlock()
@@ -775,7 +774,6 @@ func TestReader_sendMessageOverTheWire(t *testing.T) {
currentState.Store(INITIAL)
stateChangeMutex := sync.Mutex{}
transportInstance.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) {
- time.Sleep(10 * time.Millisecond) // this simulates some network response time // TODO: this should be fixed in DefaultCodec.go where Send is going through before expecttime.Sleep(10 * time.Millisecond) // this simulates some network response time
t.Logf("reacting to\n%s", hex.Dump(data))
stateChangeMutex.Lock()
defer stateChangeMutex.Unlock()
@@ -859,7 +857,6 @@ func TestReader_sendMessageOverTheWire(t *testing.T) {
currentState.Store(INITIAL)
stateChangeMutex := sync.Mutex{}
transportInstance.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) {
- time.Sleep(10 * time.Millisecond) // this simulates some network response time // TODO: this should be fixed in DefaultCodec.go where Send is going through before expecttime.Sleep(10 * time.Millisecond) // this simulates some network response time
t.Logf("reacting to\n%s", hex.Dump(data))
stateChangeMutex.Lock()
defer stateChangeMutex.Unlock()
@@ -943,7 +940,6 @@ func TestReader_sendMessageOverTheWire(t *testing.T) {
currentState.Store(INITIAL)
stateChangeMutex := sync.Mutex{}
transportInstance.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) {
- time.Sleep(10 * time.Millisecond) // this simulates some network response time // TODO: this should be fixed in DefaultCodec.go where Send is going through before expecttime.Sleep(10 * time.Millisecond) // this simulates some network response time
t.Logf("reacting to\n%s", hex.Dump(data))
stateChangeMutex.Lock()
defer stateChangeMutex.Unlock()
@@ -1027,7 +1023,6 @@ func TestReader_sendMessageOverTheWire(t *testing.T) {
currentState.Store(INITIAL)
stateChangeMutex := sync.Mutex{}
transportInstance.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) {
- time.Sleep(10 * time.Millisecond) // this simulates some network response time // TODO: this should be fixed in DefaultCodec.go where Send is going through before expecttime.Sleep(10 * time.Millisecond) // this simulates some network response time
t.Logf("reacting to\n%s", hex.Dump(data))
stateChangeMutex.Lock()
defer stateChangeMutex.Unlock()
@@ -1111,7 +1106,6 @@ func TestReader_sendMessageOverTheWire(t *testing.T) {
currentState.Store(INITIAL)
stateChangeMutex := sync.Mutex{}
transportInstance.(*test.TransportInstance).SetWriteInterceptor(func(transportInstance *test.TransportInstance, data []byte) {
- time.Sleep(10 * time.Millisecond) // this simulates some network response time // TODO: this should be fixed in DefaultCodec.go where Send is going through before expecttime.Sleep(10 * time.Millisecond) // this simulates some network response time
t.Logf("reacting to\n%s", hex.Dump(data))
stateChangeMutex.Lock()
defer stateChangeMutex.Unlock()
diff --git a/plc4go/spi/MessageCodec.go b/plc4go/spi/MessageCodec.go
index 4127c4c688..3adf83e416 100644
--- a/plc4go/spi/MessageCodec.go
+++ b/plc4go/spi/MessageCodec.go
@@ -60,7 +60,7 @@ type MessageCodec interface {
Send(message Message) error
// Expect Wait for a given timespan for a message to come in, which returns 'true' for 'acceptMessage'
// and is then forwarded to the 'handleMessage' function
- Expect(ctx context.Context, acceptsMessage AcceptsMessage, handleMessage HandleMessage, handleError HandleError, ttl time.Duration) error
+ Expect(ctx context.Context, acceptsMessage AcceptsMessage, handleMessage HandleMessage, handleError HandleError, ttl time.Duration)
// SendRequest A combination that sends a message first and then waits for a response. !!!Important note: the callbacks are blocking calls
SendRequest(ctx context.Context, message Message, acceptsMessage AcceptsMessage, handleMessage HandleMessage, handleError HandleError, ttl time.Duration) error
diff --git a/plc4go/spi/default/DefaultCodec.go b/plc4go/spi/default/DefaultCodec.go
index 1367721a98..82fa7ed35e 100644
--- a/plc4go/spi/default/DefaultCodec.go
+++ b/plc4go/spi/default/DefaultCodec.go
@@ -188,27 +188,21 @@ func (m *defaultCodec) IsRunning() bool {
return m.running.Load()
}
-func (m *defaultCodec) Expect(ctx context.Context, acceptsMessage spi.AcceptsMessage, handleMessage spi.HandleMessage, handleError spi.HandleError, ttl time.Duration) error {
+func (m *defaultCodec) Expect(ctx context.Context, acceptsMessage spi.AcceptsMessage, handleMessage spi.HandleMessage, handleError spi.HandleError, ttl time.Duration) {
m.expectationsChangeMutex.Lock()
defer m.expectationsChangeMutex.Unlock()
expectation := newDefaultExpectation(ctx, ttl, acceptsMessage, handleMessage, handleError)
m.expectations = append(m.expectations, expectation)
m.log.Debug().Stringer("expectation", expectation).Msg("Added expectation")
- return nil
}
func (m *defaultCodec) SendRequest(ctx context.Context, message spi.Message, acceptsMessage spi.AcceptsMessage, handleMessage spi.HandleMessage, handleError spi.HandleError, ttl time.Duration) error {
if err := ctx.Err(); err != nil {
return errors.Wrap(err, "Not sending message as context is aborted")
}
+ m.Expect(ctx, acceptsMessage, handleMessage, handleError, ttl) // We register the expectation first to avoid getting a response between sending and adding the expect
m.log.Trace().Msg("Sending request")
- // Send the actual message
- // TODO: this can create a race condition when it is super fast (send goes through and expect no yet registered)
- err := m.Send(message)
- if err != nil {
- return errors.Wrap(err, "Error sending the request")
- }
- return m.Expect(ctx, acceptsMessage, handleMessage, handleError, ttl)
+ return m.Send(message)
}
func (m *defaultCodec) TimeoutExpectations(now time.Time) {
diff --git a/plc4go/spi/default/DefaultCodec_test.go b/plc4go/spi/default/DefaultCodec_test.go
index f7437fa7f9..c0ba8ea0c7 100644
--- a/plc4go/spi/default/DefaultCodec_test.go
+++ b/plc4go/spi/default/DefaultCodec_test.go
@@ -513,18 +513,16 @@ func Test_defaultCodec_Expect(t *testing.T) {
ttl time.Duration
}
tests := []struct {
- name string
- fields fields
- args args
- setup func(t *testing.T, fields *fields, args *args)
- wantErr assert.ErrorAssertionFunc
+ name string
+ fields fields
+ args args
+ setup func(t *testing.T, fields *fields, args *args)
}{
{
name: "expect it",
setup: func(t *testing.T, fields *fields, args *args) {
args.ctx = testutils.TestContext(t)
},
- wantErr: assert.NoError,
},
}
for _, tt := range tests {
@@ -540,7 +538,7 @@ func Test_defaultCodec_Expect(t *testing.T) {
customMessageHandling: tt.fields.customMessageHandling,
log: testutils.ProduceTestingLogger(t),
}
- tt.wantErr(t, m.Expect(tt.args.ctx, tt.args.acceptsMessage, tt.args.handleMessage, tt.args.handleError, tt.args.ttl), fmt.Sprintf("Expect(%v, func(), func(), func(), %v)", tt.args.ctx, tt.args.ttl))
+ m.Expect(tt.args.ctx, tt.args.acceptsMessage, tt.args.handleMessage, tt.args.handleError, tt.args.ttl)
})
}
}
diff --git a/plc4go/spi/default/mock_DefaultCodec_test.go b/plc4go/spi/default/mock_DefaultCodec_test.go
index fb75e21960..01bcbdad3e 100644
--- a/plc4go/spi/default/mock_DefaultCodec_test.go
+++ b/plc4go/spi/default/mock_DefaultCodec_test.go
@@ -184,21 +184,16 @@ func (_c *MockDefaultCodec_Disconnect_Call) RunAndReturn(run func() error) *Mock
}
// Expect provides a mock function with given fields: ctx, acceptsMessage, handleMessage, handleError, ttl
-func (_m *MockDefaultCodec) Expect(ctx context.Context, acceptsMessage spi.AcceptsMessage, handleMessage spi.HandleMessage, handleError spi.HandleError, ttl time.Duration) error {
+func (_m *MockDefaultCodec) Expect(ctx context.Context, acceptsMessage spi.AcceptsMessage, handleMessage spi.HandleMessage, handleError spi.HandleError, ttl time.Duration) {
ret := _m.Called(ctx, acceptsMessage, handleMessage, handleError, ttl)
if len(ret) == 0 {
panic("no return value specified for Expect")
}
- var r0 error
if rf, ok := ret.Get(0).(func(context.Context, spi.AcceptsMessage, spi.HandleMessage, spi.HandleError, time.Duration) error); ok {
- r0 = rf(ctx, acceptsMessage, handleMessage, handleError, ttl)
- } else {
- r0 = ret.Error(0)
+ rf(ctx, acceptsMessage, handleMessage, handleError, ttl)
}
-
- return r0
}
// MockDefaultCodec_Expect_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Expect'
diff --git a/plc4go/spi/default/mock_MessageCodec_test.go b/plc4go/spi/default/mock_MessageCodec_test.go
index a8950eeb30..777a820159 100644
--- a/plc4go/spi/default/mock_MessageCodec_test.go
+++ b/plc4go/spi/default/mock_MessageCodec_test.go
@@ -182,21 +182,16 @@ func (_c *MockMessageCodec_Disconnect_Call) RunAndReturn(run func() error) *Mock
}
// Expect provides a mock function with given fields: ctx, acceptsMessage, handleMessage, handleError, ttl
-func (_m *MockMessageCodec) Expect(ctx context.Context, acceptsMessage spi.AcceptsMessage, handleMessage spi.HandleMessage, handleError spi.HandleError, ttl time.Duration) error {
+func (_m *MockMessageCodec) Expect(ctx context.Context, acceptsMessage spi.AcceptsMessage, handleMessage spi.HandleMessage, handleError spi.HandleError, ttl time.Duration) {
ret := _m.Called(ctx, acceptsMessage, handleMessage, handleError, ttl)
if len(ret) == 0 {
panic("no return value specified for Expect")
}
- var r0 error
if rf, ok := ret.Get(0).(func(context.Context, spi.AcceptsMessage, spi.HandleMessage, spi.HandleError, time.Duration) error); ok {
- r0 = rf(ctx, acceptsMessage, handleMessage, handleError, ttl)
- } else {
- r0 = ret.Error(0)
+ rf(ctx, acceptsMessage, handleMessage, handleError, ttl)
}
-
- return r0
}
// MockMessageCodec_Expect_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Expect'