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/16 16:46:20 UTC
[plc4x] 03/07: fix(plc4go/spi): fix timeout output using the wrong duration
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 9db3034ea5d4d1e27819accac73aab4b48afcedf
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Fri Jun 16 16:51:57 2023 +0200
fix(plc4go/spi): fix timeout output using the wrong duration
---
plc4go/spi/MessageCodec.go | 1 +
plc4go/spi/default/DefaultCodec.go | 38 ++-----------
plc4go/spi/default/DefaultCodec_test.go | 82 ++++++++++++++---------------
plc4go/spi/default/defaultExpectation.go | 65 +++++++++++++++++++++++
plc4go/spi/default/mock_Expectation_test.go | 41 +++++++++++++++
plc4go/spi/mock_Expectation_test.go | 41 +++++++++++++++
plc4go/spi/utils/Errors.go | 2 +-
7 files changed, 193 insertions(+), 77 deletions(-)
diff --git a/plc4go/spi/MessageCodec.go b/plc4go/spi/MessageCodec.go
index 94826da578..4127c4c688 100644
--- a/plc4go/spi/MessageCodec.go
+++ b/plc4go/spi/MessageCodec.go
@@ -28,6 +28,7 @@ import (
type Expectation interface {
fmt.Stringer
GetContext() context.Context
+ GetCreationTime() time.Time
GetExpiration() time.Time
GetAcceptsMessage() AcceptsMessage
GetHandleMessage() HandleMessage
diff --git a/plc4go/spi/default/DefaultCodec.go b/plc4go/spi/default/DefaultCodec.go
index e365c5b095..91c48d0ce9 100644
--- a/plc4go/spi/default/DefaultCodec.go
+++ b/plc4go/spi/default/DefaultCodec.go
@@ -21,7 +21,6 @@ package _default
import (
"context"
- "fmt"
"runtime/debug"
"sync"
"sync/atomic"
@@ -56,14 +55,6 @@ func NewDefaultCodec(requirements DefaultCodecRequirements, transportInstance tr
return buildDefaultCodec(requirements, transportInstance, options...)
}
-type DefaultExpectation struct {
- Context context.Context
- Expiration time.Time
- AcceptsMessage spi.AcceptsMessage
- HandleMessage spi.HandleMessage
- HandleError spi.HandleError
-}
-
type CustomMessageHandler func(codec DefaultCodecRequirements, message spi.Message) bool
func WithCustomMessageHandler(customMessageHandler CustomMessageHandler) options.WithOption {
@@ -126,30 +117,6 @@ func buildDefaultCodec(defaultCodecRequirements DefaultCodecRequirements, transp
///////////////////////////////////////
///////////////////////////////////////
-func (d *DefaultExpectation) GetContext() context.Context {
- return d.Context
-}
-
-func (d *DefaultExpectation) GetExpiration() time.Time {
- return d.Expiration
-}
-
-func (d *DefaultExpectation) GetAcceptsMessage() spi.AcceptsMessage {
- return d.AcceptsMessage
-}
-
-func (d *DefaultExpectation) GetHandleMessage() spi.HandleMessage {
- return d.HandleMessage
-}
-
-func (d *DefaultExpectation) GetHandleError() spi.HandleError {
- return d.HandleError
-}
-
-func (d *DefaultExpectation) String() string {
- return fmt.Sprintf("Expectation(expires at %v)", d.Expiration)
-}
-
func (m *defaultCodec) GetTransportInstance() transports.TransportInstance {
return m.transportInstance
}
@@ -211,8 +178,9 @@ func (m *defaultCodec) IsRunning() bool {
func (m *defaultCodec) Expect(ctx context.Context, acceptsMessage spi.AcceptsMessage, handleMessage spi.HandleMessage, handleError spi.HandleError, ttl time.Duration) error {
m.expectationsChangeMutex.Lock()
defer m.expectationsChangeMutex.Unlock()
- expectation := &DefaultExpectation{
+ expectation := &defaultExpectation{
Context: ctx,
+ CreationTime: time.Now(),
Expiration: time.Now().Add(ttl),
AcceptsMessage: acceptsMessage,
HandleMessage: handleMessage,
@@ -247,7 +215,7 @@ func (m *defaultCodec) TimeoutExpectations(now time.Time) {
i--
// Call the error handler.
go func(expectation spi.Expectation) {
- if err := expectation.GetHandleError()(utils.NewTimeoutError(now.Sub(expectation.GetExpiration()))); err != nil {
+ if err := expectation.GetHandleError()(utils.NewTimeoutError(expectation.GetExpiration().Sub(expectation.GetCreationTime()))); err != nil {
m.log.Error().Err(err).Msg("Got an error handling error on expectation")
}
}(expectation)
diff --git a/plc4go/spi/default/DefaultCodec_test.go b/plc4go/spi/default/DefaultCodec_test.go
index c6829c64a5..19bde057ac 100644
--- a/plc4go/spi/default/DefaultCodec_test.go
+++ b/plc4go/spi/default/DefaultCodec_test.go
@@ -59,7 +59,7 @@ func TestDefaultExpectation_GetAcceptsMessage(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- m := &DefaultExpectation{
+ m := &defaultExpectation{
Context: tt.fields.Context,
Expiration: tt.fields.Expiration,
AcceptsMessage: tt.fields.AcceptsMessage,
@@ -90,7 +90,7 @@ func TestDefaultExpectation_GetContext(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- m := &DefaultExpectation{
+ m := &defaultExpectation{
Context: tt.fields.Context,
Expiration: tt.fields.Expiration,
AcceptsMessage: tt.fields.AcceptsMessage,
@@ -121,7 +121,7 @@ func TestDefaultExpectation_GetExpiration(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- m := &DefaultExpectation{
+ m := &defaultExpectation{
Context: tt.fields.Context,
Expiration: tt.fields.Expiration,
AcceptsMessage: tt.fields.AcceptsMessage,
@@ -156,7 +156,7 @@ func TestDefaultExpectation_GetHandleError(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- m := &DefaultExpectation{
+ m := &defaultExpectation{
Context: tt.fields.Context,
Expiration: tt.fields.Expiration,
AcceptsMessage: tt.fields.AcceptsMessage,
@@ -191,7 +191,7 @@ func TestDefaultExpectation_GetHandleMessage(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- m := &DefaultExpectation{
+ m := &defaultExpectation{
Context: tt.fields.Context,
Expiration: tt.fields.Expiration,
AcceptsMessage: tt.fields.AcceptsMessage,
@@ -223,7 +223,7 @@ func TestDefaultExpectation_String(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- m := &DefaultExpectation{
+ m := &defaultExpectation{
Context: tt.fields.Context,
Expiration: tt.fields.Expiration,
AcceptsMessage: tt.fields.AcceptsMessage,
@@ -617,12 +617,12 @@ func Test_defaultCodec_HandleMessages(t *testing.T) {
name: "handle some",
fields: fields{
expectations: []spi.Expectation{
- &DefaultExpectation{ // doesn't accept
+ &defaultExpectation{ // doesn't accept
AcceptsMessage: func(_ spi.Message) bool {
return false
},
},
- &DefaultExpectation{ // accepts but fails
+ &defaultExpectation{ // accepts but fails
AcceptsMessage: func(_ spi.Message) bool {
return true
},
@@ -633,7 +633,7 @@ func Test_defaultCodec_HandleMessages(t *testing.T) {
return nil
},
},
- &DefaultExpectation{ // accepts but fails and fails to handle the error
+ &defaultExpectation{ // accepts but fails and fails to handle the error
AcceptsMessage: func(_ spi.Message) bool {
return true
},
@@ -644,7 +644,7 @@ func Test_defaultCodec_HandleMessages(t *testing.T) {
return errors.New("I failed completely")
},
},
- &DefaultExpectation{ // accepts
+ &defaultExpectation{ // accepts
AcceptsMessage: func(_ spi.Message) bool {
return true
},
@@ -652,7 +652,7 @@ func Test_defaultCodec_HandleMessages(t *testing.T) {
return nil
},
},
- &DefaultExpectation{ // accepts
+ &defaultExpectation{ // accepts
AcceptsMessage: func(_ spi.Message) bool {
return true
},
@@ -660,7 +660,7 @@ func Test_defaultCodec_HandleMessages(t *testing.T) {
return nil
},
},
- &DefaultExpectation{ // accepts
+ &defaultExpectation{ // accepts
AcceptsMessage: func(_ spi.Message) bool {
return true
},
@@ -823,26 +823,26 @@ func Test_defaultCodec_TimeoutExpectations(t *testing.T) {
name: "timeout some",
fields: fields{
expectations: []spi.Expectation{
- &DefaultExpectation{ // Expired
+ &defaultExpectation{ // Expired
Context: context.Background(),
HandleError: func(err error) error {
return nil
},
},
- &DefaultExpectation{ // Expired errors
+ &defaultExpectation{ // Expired errors
Context: context.Background(),
HandleError: func(err error) error {
return errors.New("yep")
},
},
- &DefaultExpectation{ // Fine
+ &defaultExpectation{ // Fine
Context: context.Background(),
HandleError: func(err error) error {
return errors.New("yep")
},
Expiration: time.Time{}.Add(3 * time.Hour),
},
- &DefaultExpectation{ // Context error
+ &defaultExpectation{ // Context error
Context: func() context.Context {
ctx, cancelFunc := context.WithCancel(context.Background())
cancelFunc() // Cancel it instantly
@@ -902,26 +902,26 @@ func Test_defaultCodec_Work(t *testing.T) {
name: "work hard (panics everywhere)",
fields: fields{
expectations: []spi.Expectation{
- &DefaultExpectation{ // Expired
+ &defaultExpectation{ // Expired
Context: context.Background(),
HandleError: func(err error) error {
return nil
},
},
- &DefaultExpectation{ // Expired errors
+ &defaultExpectation{ // Expired errors
Context: context.Background(),
HandleError: func(err error) error {
return errors.New("yep")
},
},
- &DefaultExpectation{ // Fine
+ &defaultExpectation{ // Fine
Context: context.Background(),
HandleError: func(err error) error {
return errors.New("yep")
},
Expiration: time.Time{}.Add(3 * time.Hour),
},
- &DefaultExpectation{ // Context error
+ &defaultExpectation{ // Context error
Context: func() context.Context {
ctx, cancelFunc := context.WithCancel(context.Background())
cancelFunc() // Cancel it instantly
@@ -943,26 +943,26 @@ func Test_defaultCodec_Work(t *testing.T) {
name: "work harder (nil message)",
fields: fields{
expectations: []spi.Expectation{
- &DefaultExpectation{ // Expired
+ &defaultExpectation{ // Expired
Context: context.Background(),
HandleError: func(err error) error {
return nil
},
},
- &DefaultExpectation{ // Expired errors
+ &defaultExpectation{ // Expired errors
Context: context.Background(),
HandleError: func(err error) error {
return errors.New("yep")
},
},
- &DefaultExpectation{ // Fine
+ &defaultExpectation{ // Fine
Context: context.Background(),
HandleError: func(err error) error {
return errors.New("yep")
},
Expiration: time.Time{}.Add(3 * time.Hour),
},
- &DefaultExpectation{ // Context error
+ &defaultExpectation{ // Context error
Context: func() context.Context {
ctx, cancelFunc := context.WithCancel(context.Background())
cancelFunc() // Cancel it instantly
@@ -989,26 +989,26 @@ func Test_defaultCodec_Work(t *testing.T) {
name: "work harder (message)",
fields: fields{
expectations: []spi.Expectation{
- &DefaultExpectation{ // Expired
+ &defaultExpectation{ // Expired
Context: context.Background(),
HandleError: func(err error) error {
return nil
},
},
- &DefaultExpectation{ // Expired errors
+ &defaultExpectation{ // Expired errors
Context: context.Background(),
HandleError: func(err error) error {
return errors.New("yep")
},
},
- &DefaultExpectation{ // Fine
+ &defaultExpectation{ // Fine
Context: context.Background(),
HandleError: func(err error) error {
return errors.New("yep")
},
Expiration: time.Time{}.Add(3 * time.Hour),
},
- &DefaultExpectation{ // Context error
+ &defaultExpectation{ // Context error
Context: func() context.Context {
ctx, cancelFunc := context.WithCancel(context.Background())
cancelFunc() // Cancel it instantly
@@ -1036,7 +1036,7 @@ func Test_defaultCodec_Work(t *testing.T) {
fields: fields{
defaultIncomingMessageChannel: make(chan spi.Message, 1),
expectations: []spi.Expectation{
- &DefaultExpectation{ // Fine
+ &defaultExpectation{ // Fine
Context: context.Background(),
HandleError: func(err error) error {
return errors.New("yep")
@@ -1059,26 +1059,26 @@ func Test_defaultCodec_Work(t *testing.T) {
name: "work harder (message receive error)",
fields: fields{
expectations: []spi.Expectation{
- &DefaultExpectation{ // Expired
+ &defaultExpectation{ // Expired
Context: context.Background(),
HandleError: func(err error) error {
return nil
},
},
- &DefaultExpectation{ // Expired errors
+ &defaultExpectation{ // Expired errors
Context: context.Background(),
HandleError: func(err error) error {
return errors.New("yep")
},
},
- &DefaultExpectation{ // Fine
+ &defaultExpectation{ // Fine
Context: context.Background(),
HandleError: func(err error) error {
return errors.New("yep")
},
Expiration: time.Time{}.Add(3 * time.Hour),
},
- &DefaultExpectation{ // Context error
+ &defaultExpectation{ // Context error
Context: func() context.Context {
ctx, cancelFunc := context.WithCancel(context.Background())
cancelFunc() // Cancel it instantly
@@ -1108,26 +1108,26 @@ func Test_defaultCodec_Work(t *testing.T) {
return false
},
expectations: []spi.Expectation{
- &DefaultExpectation{ // Expired
+ &defaultExpectation{ // Expired
Context: context.Background(),
HandleError: func(err error) error {
return nil
},
},
- &DefaultExpectation{ // Expired errors
+ &defaultExpectation{ // Expired errors
Context: context.Background(),
HandleError: func(err error) error {
return errors.New("yep")
},
},
- &DefaultExpectation{ // Fine
+ &defaultExpectation{ // Fine
Context: context.Background(),
HandleError: func(err error) error {
return errors.New("yep")
},
Expiration: time.Time{}.Add(3 * time.Hour),
},
- &DefaultExpectation{ // Context error
+ &defaultExpectation{ // Context error
Context: func() context.Context {
ctx, cancelFunc := context.WithCancel(context.Background())
cancelFunc() // Cancel it instantly
@@ -1157,26 +1157,26 @@ func Test_defaultCodec_Work(t *testing.T) {
return true
},
expectations: []spi.Expectation{
- &DefaultExpectation{ // Expired
+ &defaultExpectation{ // Expired
Context: context.Background(),
HandleError: func(err error) error {
return nil
},
},
- &DefaultExpectation{ // Expired errors
+ &defaultExpectation{ // Expired errors
Context: context.Background(),
HandleError: func(err error) error {
return errors.New("yep")
},
},
- &DefaultExpectation{ // Fine
+ &defaultExpectation{ // Fine
Context: context.Background(),
HandleError: func(err error) error {
return errors.New("yep")
},
Expiration: time.Time{}.Add(3 * time.Hour),
},
- &DefaultExpectation{ // Context error
+ &defaultExpectation{ // Context error
Context: func() context.Context {
ctx, cancelFunc := context.WithCancel(context.Background())
cancelFunc() // Cancel it instantly
diff --git a/plc4go/spi/default/defaultExpectation.go b/plc4go/spi/default/defaultExpectation.go
new file mode 100644
index 0000000000..fe11e0e081
--- /dev/null
+++ b/plc4go/spi/default/defaultExpectation.go
@@ -0,0 +1,65 @@
+/*
+ * 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 _default
+
+import (
+ "context"
+ "fmt"
+ "time"
+
+ "github.com/apache/plc4x/plc4go/spi"
+)
+
+type defaultExpectation struct {
+ Context context.Context
+ CreationTime time.Time
+ Expiration time.Time
+ AcceptsMessage spi.AcceptsMessage
+ HandleMessage spi.HandleMessage
+ HandleError spi.HandleError
+}
+
+func (d *defaultExpectation) GetContext() context.Context {
+ return d.Context
+}
+
+func (d *defaultExpectation) GetCreationTime() time.Time {
+ return d.CreationTime
+}
+
+func (d *defaultExpectation) GetExpiration() time.Time {
+ return d.Expiration
+}
+
+func (d *defaultExpectation) GetAcceptsMessage() spi.AcceptsMessage {
+ return d.AcceptsMessage
+}
+
+func (d *defaultExpectation) GetHandleMessage() spi.HandleMessage {
+ return d.HandleMessage
+}
+
+func (d *defaultExpectation) GetHandleError() spi.HandleError {
+ return d.HandleError
+}
+
+func (d *defaultExpectation) String() string {
+ return fmt.Sprintf("Expectation(expires at %v)", d.Expiration)
+}
diff --git a/plc4go/spi/default/mock_Expectation_test.go b/plc4go/spi/default/mock_Expectation_test.go
index 0ffc04e950..638d330a84 100644
--- a/plc4go/spi/default/mock_Expectation_test.go
+++ b/plc4go/spi/default/mock_Expectation_test.go
@@ -129,6 +129,47 @@ func (_c *MockExpectation_GetContext_Call) RunAndReturn(run func() context.Conte
return _c
}
+// GetCreationTime provides a mock function with given fields:
+func (_m *MockExpectation) GetCreationTime() time.Time {
+ ret := _m.Called()
+
+ var r0 time.Time
+ if rf, ok := ret.Get(0).(func() time.Time); ok {
+ r0 = rf()
+ } else {
+ r0 = ret.Get(0).(time.Time)
+ }
+
+ return r0
+}
+
+// MockExpectation_GetCreationTime_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetCreationTime'
+type MockExpectation_GetCreationTime_Call struct {
+ *mock.Call
+}
+
+// GetCreationTime is a helper method to define mock.On call
+func (_e *MockExpectation_Expecter) GetCreationTime() *MockExpectation_GetCreationTime_Call {
+ return &MockExpectation_GetCreationTime_Call{Call: _e.mock.On("GetCreationTime")}
+}
+
+func (_c *MockExpectation_GetCreationTime_Call) Run(run func()) *MockExpectation_GetCreationTime_Call {
+ _c.Call.Run(func(args mock.Arguments) {
+ run()
+ })
+ return _c
+}
+
+func (_c *MockExpectation_GetCreationTime_Call) Return(_a0 time.Time) *MockExpectation_GetCreationTime_Call {
+ _c.Call.Return(_a0)
+ return _c
+}
+
+func (_c *MockExpectation_GetCreationTime_Call) RunAndReturn(run func() time.Time) *MockExpectation_GetCreationTime_Call {
+ _c.Call.Return(run)
+ return _c
+}
+
// GetExpiration provides a mock function with given fields:
func (_m *MockExpectation) GetExpiration() time.Time {
ret := _m.Called()
diff --git a/plc4go/spi/mock_Expectation_test.go b/plc4go/spi/mock_Expectation_test.go
index c2f5369299..ede1ebbe16 100644
--- a/plc4go/spi/mock_Expectation_test.go
+++ b/plc4go/spi/mock_Expectation_test.go
@@ -127,6 +127,47 @@ func (_c *MockExpectation_GetContext_Call) RunAndReturn(run func() context.Conte
return _c
}
+// GetCreationTime provides a mock function with given fields:
+func (_m *MockExpectation) GetCreationTime() time.Time {
+ ret := _m.Called()
+
+ var r0 time.Time
+ if rf, ok := ret.Get(0).(func() time.Time); ok {
+ r0 = rf()
+ } else {
+ r0 = ret.Get(0).(time.Time)
+ }
+
+ return r0
+}
+
+// MockExpectation_GetCreationTime_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetCreationTime'
+type MockExpectation_GetCreationTime_Call struct {
+ *mock.Call
+}
+
+// GetCreationTime is a helper method to define mock.On call
+func (_e *MockExpectation_Expecter) GetCreationTime() *MockExpectation_GetCreationTime_Call {
+ return &MockExpectation_GetCreationTime_Call{Call: _e.mock.On("GetCreationTime")}
+}
+
+func (_c *MockExpectation_GetCreationTime_Call) Run(run func()) *MockExpectation_GetCreationTime_Call {
+ _c.Call.Run(func(args mock.Arguments) {
+ run()
+ })
+ return _c
+}
+
+func (_c *MockExpectation_GetCreationTime_Call) Return(_a0 time.Time) *MockExpectation_GetCreationTime_Call {
+ _c.Call.Return(_a0)
+ return _c
+}
+
+func (_c *MockExpectation_GetCreationTime_Call) RunAndReturn(run func() time.Time) *MockExpectation_GetCreationTime_Call {
+ _c.Call.Return(run)
+ return _c
+}
+
// GetExpiration provides a mock function with given fields:
func (_m *MockExpectation) GetExpiration() time.Time {
ret := _m.Called()
diff --git a/plc4go/spi/utils/Errors.go b/plc4go/spi/utils/Errors.go
index 339fb4b3c8..697dffaeee 100644
--- a/plc4go/spi/utils/Errors.go
+++ b/plc4go/spi/utils/Errors.go
@@ -91,7 +91,7 @@ func NewTimeoutError(timeout time.Duration) TimeoutError {
}
func (t TimeoutError) Error() string {
- return fmt.Sprintf("got timeout after %v", t.timeout)
+ return fmt.Sprintf("got timeout after %s", t.timeout)
}
func (t TimeoutError) Is(target error) bool {