Skip to content

Commit

Permalink
refactor(plc4go/spi): small cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Apr 26, 2023
1 parent cfbc3a7 commit 0ddb758
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 122 deletions.
8 changes: 4 additions & 4 deletions plc4go/internal/bacnetip/IOCBModule.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ package bacnetip
import (
"container/heap"
"fmt"
"github.com/apache/plc4x/plc4go/spi/plcerrors"
"sync"
"time"

"github.com/apache/plc4x/plc4go/spi/utils"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"sync"
"time"
)

var stateLog = log.Logger
Expand Down Expand Up @@ -245,7 +245,7 @@ func (i *IOCB) SetTimeout(delay time.Duration) {
go func() {
select {
case timeout := <-i.ioTimeout.C:
_ = i.Abort(plcerrors.NewTimeoutError(now.Sub(timeout)))
_ = i.Abort(utils.NewTimeoutError(now.Sub(timeout)))
case <-i.ioTimoutCancel:
}
}()
Expand Down
10 changes: 5 additions & 5 deletions plc4go/internal/eip/Connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/apache/plc4x/plc4go/spi"
"github.com/apache/plc4x/plc4go/spi/default"
internalModel "github.com/apache/plc4x/plc4go/spi/model"
"github.com/apache/plc4x/plc4go/spi/plcerrors"
"github.com/apache/plc4x/plc4go/spi/utils"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)
Expand Down Expand Up @@ -170,7 +170,7 @@ func (m *Connection) setupConnection(ctx context.Context, ch chan plc4go.PlcConn
return nil
}, func(err error) error {
// If this is a timeout, do a check if the connection requires a reconnection
if _, isTimeout := err.(plcerrors.TimeoutError); isTimeout {
if _, isTimeout := err.(utils.TimeoutError); isTimeout {
log.Warn().Msg("Timeout during Connection establishing, closing channel...")
m.Close()
}
Expand Down Expand Up @@ -244,7 +244,7 @@ func (m *Connection) setupConnection(ctx context.Context, ch chan plc4go.PlcConn
return nil
}, func(err error) error {
// If this is a timeout, do a check if the connection requires a reconnection
if _, isTimeout := err.(plcerrors.TimeoutError); isTimeout {
if _, isTimeout := err.(utils.TimeoutError); isTimeout {
log.Warn().Msg("Timeout during Connection establishing, closing channel...")
m.Close()
}
Expand All @@ -257,7 +257,7 @@ func (m *Connection) setupConnection(ctx context.Context, ch chan plc4go.PlcConn
return nil
}, func(err error) error {
// If this is a timeout, do a check if the connection requires a reconnection
if _, isTimeout := err.(plcerrors.TimeoutError); isTimeout {
if _, isTimeout := err.(utils.TimeoutError); isTimeout {
log.Warn().Msg("Timeout during Connection establishing, closing channel...")
m.Close()
}
Expand Down Expand Up @@ -314,7 +314,7 @@ func (m *Connection) setupConnection(ctx context.Context, ch chan plc4go.PlcConn
return nil
}, func(err error) error {
// If this is a timeout, do a check if the connection requires a reconnection
if _, isTimeout := err.(plcerrors.TimeoutError); isTimeout {
if _, isTimeout := err.(utils.TimeoutError); isTimeout {
log.Warn().Msg("Timeout during Connection establishing, closing channel...")
m.Close()
}
Expand Down
29 changes: 14 additions & 15 deletions plc4go/internal/knxnetip/ConnectionInternalOperations.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ package knxnetip

import (
"context"
"github.com/apache/plc4x/plc4go/spi"
"reflect"
"time"

driverModel "github.com/apache/plc4x/plc4go/protocols/knxnetip/readwrite/model"
"github.com/apache/plc4x/plc4go/spi/plcerrors"
"github.com/apache/plc4x/plc4go/spi"
"github.com/apache/plc4x/plc4go/spi/utils"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -69,7 +68,7 @@ func (m *Connection) sendGatewaySearchRequest(ctx context.Context) (driverModel.
},
func(err error) error {
// If this is a timeout, do a check if the connection requires a reconnection
if _, isTimeout := err.(plcerrors.TimeoutError); isTimeout {
if _, isTimeout := err.(utils.TimeoutError); isTimeout {
m.handleTimeout()
}
errorResult <- errors.Wrap(err, "got error processing request")
Expand Down Expand Up @@ -127,7 +126,7 @@ func (m *Connection) sendGatewayConnectionRequest(ctx context.Context) (driverMo
},
func(err error) error {
// If this is a timeout, do a check if the connection requires a reconnection
if _, isTimeout := err.(plcerrors.TimeoutError); isTimeout {
if _, isTimeout := err.(utils.TimeoutError); isTimeout {
m.handleTimeout()
}
errorResult <- errors.Wrap(err, "got error processing request")
Expand Down Expand Up @@ -177,7 +176,7 @@ func (m *Connection) sendGatewayDisconnectionRequest(ctx context.Context) (drive
},
func(err error) error {
// If this is a timeout, do a check if the connection requires a reconnection
if _, isTimeout := err.(plcerrors.TimeoutError); isTimeout {
if _, isTimeout := err.(utils.TimeoutError); isTimeout {
m.handleTimeout()
}
errorResult <- errors.Wrap(err, "got error processing request")
Expand Down Expand Up @@ -225,7 +224,7 @@ func (m *Connection) sendConnectionStateRequest(ctx context.Context) (driverMode
},
func(err error) error {
// If this is a timeout, do a check if the connection requires a reconnection
if _, isTimeout := err.(plcerrors.TimeoutError); isTimeout {
if _, isTimeout := err.(utils.TimeoutError); isTimeout {
m.handleTimeout()
}
errorResult <- errors.Wrap(err, "got error processing request")
Expand Down Expand Up @@ -309,7 +308,7 @@ func (m *Connection) sendGroupAddressReadRequest(ctx context.Context, groupAddre
},
func(err error) error {
// If this is a timeout, do a check if the connection requires a reconnection
if _, isTimeout := err.(plcerrors.TimeoutError); isTimeout {
if _, isTimeout := err.(utils.TimeoutError); isTimeout {
m.handleTimeout()
}
errorResult <- errors.Wrap(err, "got error processing request")
Expand Down Expand Up @@ -399,7 +398,7 @@ func (m *Connection) sendDeviceConnectionRequest(ctx context.Context, targetAddr
},
func(err error) error {
// If this is a timeout, do a check if the connection requires a reconnection
if _, isTimeout := err.(plcerrors.TimeoutError); isTimeout {
if _, isTimeout := err.(utils.TimeoutError); isTimeout {
m.handleTimeout()
}
errorResult <- errors.Wrap(err, "got error processing request")
Expand Down Expand Up @@ -490,7 +489,7 @@ func (m *Connection) sendDeviceDisconnectionRequest(ctx context.Context, targetA
},
func(err error) error {
// If this is a timeout, do a check if the connection requires a reconnection
if _, isTimeout := err.(plcerrors.TimeoutError); isTimeout {
if _, isTimeout := err.(utils.TimeoutError); isTimeout {
m.handleTimeout()
}
errorResult <- errors.Wrap(err, "got error processing request")
Expand Down Expand Up @@ -614,7 +613,7 @@ func (m *Connection) sendDeviceAuthentication(ctx context.Context, targetAddress
return nil
}, func(err error) error {
// If this is a timeout, do a check if the connection requires a reconnection
if _, isTimeout := err.(plcerrors.TimeoutError); isTimeout {
if _, isTimeout := err.(utils.TimeoutError); isTimeout {
m.handleTimeout()
}
errorResult <- errors.Wrap(err, "got error processing request")
Expand Down Expand Up @@ -715,7 +714,7 @@ func (m *Connection) sendDeviceDeviceDescriptorReadRequest(ctx context.Context,
return nil
}, func(err error) error {
// If this is a timeout, do a check if the connection requires a reconnection
if _, isTimeout := err.(plcerrors.TimeoutError); isTimeout {
if _, isTimeout := err.(utils.TimeoutError); isTimeout {
m.handleTimeout()
}
errorResult <- errors.Wrap(err, "got error processing request")
Expand Down Expand Up @@ -831,7 +830,7 @@ func (m *Connection) sendDevicePropertyReadRequest(ctx context.Context, targetAd
},
func(err error) error {
// If this is a timeout, do a check if the connection requires a reconnection
if _, isTimeout := err.(plcerrors.TimeoutError); isTimeout {
if _, isTimeout := err.(utils.TimeoutError); isTimeout {
m.handleTimeout()
}
errorResult <- errors.Wrap(err, "got error processing request")
Expand Down Expand Up @@ -946,7 +945,7 @@ func (m *Connection) sendDevicePropertyDescriptionReadRequest(ctx context.Contex
return nil
}, func(err error) error {
// If this is a timeout, do a check if the connection requires a reconnection
if _, isTimeout := err.(plcerrors.TimeoutError); isTimeout {
if _, isTimeout := err.(utils.TimeoutError); isTimeout {
m.handleTimeout()
}
errorResult <- errors.Wrapf(err, "got error processing request")
Expand Down Expand Up @@ -1057,7 +1056,7 @@ func (m *Connection) sendDeviceMemoryReadRequest(ctx context.Context, targetAddr
},
func(err error) error {
// If this is a timeout, do a check if the connection requires a reconnection
if _, isTimeout := err.(plcerrors.TimeoutError); isTimeout {
if _, isTimeout := err.(utils.TimeoutError); isTimeout {
m.handleTimeout()
}
errorResult <- errors.Wrap(err, "got error processing request")
Expand Down Expand Up @@ -1139,7 +1138,7 @@ func (m *Connection) sendDeviceAck(ctx context.Context, targetAddress driverMode
},
func(err error) error {
// If this is a timeout, do a check if the connection requires a reconnection
if _, isTimeout := err.(plcerrors.TimeoutError); isTimeout {
if _, isTimeout := err.(utils.TimeoutError); isTimeout {
m.handleTimeout()
}
callback(errors.Wrap(err, "got error processing request"))
Expand Down
8 changes: 4 additions & 4 deletions plc4go/internal/s7/Connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"github.com/apache/plc4x/plc4go/spi"
"github.com/apache/plc4x/plc4go/spi/default"
internalModel "github.com/apache/plc4x/plc4go/spi/model"
"github.com/apache/plc4x/plc4go/spi/plcerrors"
"github.com/apache/plc4x/plc4go/spi/utils"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)
Expand Down Expand Up @@ -160,7 +160,7 @@ func (m *Connection) setupConnection(ctx context.Context, ch chan plc4go.PlcConn
return nil
}, func(err error) error {
// If this is a timeout, do a check if the connection requires a reconnection
if _, isTimeout := err.(plcerrors.TimeoutError); isTimeout {
if _, isTimeout := err.(utils.TimeoutError); isTimeout {
log.Warn().Msg("Timeout during Connection establishing, closing channel...")
m.Close()
}
Expand Down Expand Up @@ -201,7 +201,7 @@ func (m *Connection) setupConnection(ctx context.Context, ch chan plc4go.PlcConn
return nil
}, func(err error) error {
// If this is a timeout, do a check if the connection requires a reconnection
if _, isTimeout := err.(plcerrors.TimeoutError); isTimeout {
if _, isTimeout := err.(utils.TimeoutError); isTimeout {
log.Warn().Msg("Timeout during Connection establishing, closing channel...")
m.Close()
}
Expand Down Expand Up @@ -261,7 +261,7 @@ func (m *Connection) setupConnection(ctx context.Context, ch chan plc4go.PlcConn
return nil
}, func(err error) error {
// If this is a timeout, do a check if the connection requires a reconnection
if _, isTimeout := err.(plcerrors.TimeoutError); isTimeout {
if _, isTimeout := err.(utils.TimeoutError); isTimeout {
log.Warn().Msg("Timeout during Connection establishing, closing channel...")
m.Close()
}
Expand Down
3 changes: 1 addition & 2 deletions plc4go/spi/default/DefaultCodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (

"github.com/apache/plc4x/plc4go/pkg/api/config"
"github.com/apache/plc4x/plc4go/spi"
"github.com/apache/plc4x/plc4go/spi/plcerrors"
"github.com/apache/plc4x/plc4go/spi/transports"
"github.com/pkg/errors"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -212,7 +211,7 @@ func (m *defaultCodec) TimeoutExpectations(now time.Time) {
i--
// Call the error handler.
go func(expectation spi.Expectation) {
if err := expectation.GetHandleError()(plcerrors.NewTimeoutError(now.Sub(expectation.GetExpiration()))); err != nil {
if err := expectation.GetHandleError()(utils.NewTimeoutError(now.Sub(expectation.GetExpiration()))); err != nil {
log.Error().Err(err).Msg("Got an error handling error on expectation")
}
}(expectation)
Expand Down
37 changes: 0 additions & 37 deletions plc4go/spi/plcerrors/TimeoutError.go

This file was deleted.

87 changes: 44 additions & 43 deletions plc4go/spi/testutils/TestUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,53 +47,54 @@ func CompareResults(t *testing.T, actualString []byte, referenceString []byte) e
if err != nil {
return errors.Wrap(err, "Error comparing xml trees")
}
if diff != nil {
cleanDiff := make([]xdiff.Delta, 0)
for _, delta := range diff {
if delta.Operation == xdiff.Delete && delta.Subject.Value == nil || delta.Operation == xdiff.Insert && delta.Subject.Value == nil {
log.Info().Msgf("We ignore empty elements which should be deleted %v", delta)
if diff == nil {
// All good
return nil
}
cleanDiff := make([]xdiff.Delta, 0)
for _, delta := range diff {
if delta.Operation == xdiff.Delete && delta.Subject.Value == nil || delta.Operation == xdiff.Insert && delta.Subject.Value == nil {
log.Info().Msgf("We ignore empty elements which should be deleted %v", delta)
continue
}
// Workaround for different precisions on float
if delta.Operation == xdiff.Update &&
string(delta.Subject.Parent.FirstChild.Name) == "dataType" &&
string(delta.Subject.Parent.FirstChild.Value) == "float" &&
string(delta.Object.Parent.FirstChild.Name) == "dataType" &&
string(delta.Object.Parent.FirstChild.Value) == "float" {
if strings.Contains(string(delta.Subject.Value), string(delta.Object.Value)) || strings.Contains(string(delta.Object.Value), string(delta.Subject.Value)) {
log.Info().Msgf("We ignore precision diffs %v", delta)
continue
}
// Workaround for different precisions on float
if delta.Operation == xdiff.Update &&
string(delta.Subject.Parent.FirstChild.Name) == "dataType" &&
string(delta.Subject.Parent.FirstChild.Value) == "float" &&
string(delta.Object.Parent.FirstChild.Name) == "dataType" &&
string(delta.Object.Parent.FirstChild.Value) == "float" {
if strings.Contains(string(delta.Subject.Value), string(delta.Object.Value)) || strings.Contains(string(delta.Object.Value), string(delta.Subject.Value)) {
log.Info().Msgf("We ignore precision diffs %v", delta)
continue
}
}
if delta.Operation == xdiff.Update &&
string(delta.Subject.Parent.FirstChild.Name) == "dataType" &&
string(delta.Subject.Parent.FirstChild.Value) == "string" &&
string(delta.Object.Parent.FirstChild.Name) == "dataType" &&
string(delta.Object.Parent.FirstChild.Value) == "string" {
if diff, err := xdiff.Compare(delta.Subject, delta.Object); diff == nil && err == nil {
log.Info().Msgf("We ignore newline diffs %v", delta)
continue
}
}
if delta.Operation == xdiff.Update &&
string(delta.Subject.Parent.FirstChild.Name) == "dataType" &&
string(delta.Subject.Parent.FirstChild.Value) == "string" &&
string(delta.Object.Parent.FirstChild.Name) == "dataType" &&
string(delta.Object.Parent.FirstChild.Value) == "string" {
if diff, err := xdiff.Compare(delta.Subject, delta.Object); diff == nil && err == nil {
log.Info().Msgf("We ignore newline diffs %v", delta)
continue
}
cleanDiff = append(cleanDiff, delta)
}
cleanDiff = append(cleanDiff, delta)
}

enc := xdiff.NewTextEncoder(os.Stdout)
if err := enc.Encode(diff); err != nil {
return errors.Wrap(err, "Error outputting results")
}
if len(cleanDiff) <= 0 {
log.Warn().Msg("We only found non relevant changes")
return nil
} else {
assert.Equal(t, string(referenceString), string(actualString))
asciiBoxWriter := utils.NewAsciiBoxWriter()
expectedBox := asciiBoxWriter.BoxString("expected", string(referenceString), 0)
gotBox := asciiBoxWriter.BoxString("got", string(actualString), 0)
boxSideBySide := asciiBoxWriter.BoxSideBySide(expectedBox, gotBox)
_ = boxSideBySide // TODO: xml too distorted, we need a don't center option
return errors.New("there were differences: Expected: \n" + string(referenceString) + "\nBut Got: \n" + string(actualString))
}
enc := xdiff.NewTextEncoder(os.Stdout)
if err := enc.Encode(diff); err != nil {
return errors.Wrap(err, "Error outputting results")
}
return nil
if len(cleanDiff) <= 0 {
log.Warn().Msg("We only found non relevant changes")
return nil
}

assert.Equal(t, string(referenceString), string(actualString))
asciiBoxWriter := utils.NewAsciiBoxWriter()
expectedBox := asciiBoxWriter.BoxString("expected", string(referenceString), 0)
gotBox := asciiBoxWriter.BoxString("got", string(actualString), 0)
boxSideBySide := asciiBoxWriter.BoxSideBySide(expectedBox, gotBox)
_ = boxSideBySide // TODO: xml too distorted, we need a don't center option
return errors.New("there were differences: Expected: \n" + string(referenceString) + "\nBut Got: \n" + string(actualString))
}
4 changes: 0 additions & 4 deletions plc4go/spi/utils/ArrayContext.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,3 @@ func GetLastItemFromContext(ctx context.Context) bool {
info := ctx.Value(keyArrayInfo).(arrayInfo)
return info.curItem == (info.numItems - 1)
}

func Dummy() {
// TODO: Just needed to be able to always import this package.
}
Loading

0 comments on commit 0ddb758

Please sign in to comment.