Skip to content

Commit

Permalink
refactor(plc4go/test): simplify test TransportInstance signature
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Apr 11, 2023
1 parent a6ce77d commit 1ad4fc7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 63 deletions.
53 changes: 13 additions & 40 deletions plc4go/internal/cbus/MessageCodec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,7 @@ func TestMessageCodec_Receive(t *testing.T) {
DefaultCodec: NewMessageCodec(func() transports.TransportInstance {
transport := test.NewTransport()
transportInstance := test.NewTransportInstance(transport)
if err := transportInstance.FillReadBuffer([]byte("!")); err != nil {
t.Error(err)
return nil
}
transportInstance.FillReadBuffer([]byte("!"))
return transportInstance
}()),
requestContext: requestContext,
Expand All @@ -156,10 +153,7 @@ func TestMessageCodec_Receive(t *testing.T) {
DefaultCodec: NewMessageCodec(func() transports.TransportInstance {
transport := test.NewTransport()
transportInstance := test.NewTransportInstance(transport)
if err := transportInstance.FillReadBuffer([]byte("@A62120\r@A62120\r")); err != nil {
t.Error(err)
return nil
}
transportInstance.FillReadBuffer([]byte("@A62120\r@A62120\r"))
return transportInstance
}()),
requestContext: requestContext,
Expand All @@ -178,10 +172,7 @@ func TestMessageCodec_Receive(t *testing.T) {
DefaultCodec: NewMessageCodec(func() transports.TransportInstance {
transport := test.NewTransport()
transportInstance := test.NewTransportInstance(transport)
if err := transportInstance.FillReadBuffer([]byte("what on earth\n\r")); err != nil {
t.Error(err)
return nil
}
transportInstance.FillReadBuffer([]byte("what on earth\n\r"))
return transportInstance
}()),
requestContext: requestContext,
Expand All @@ -200,10 +191,7 @@ func TestMessageCodec_Receive(t *testing.T) {
DefaultCodec: NewMessageCodec(func() transports.TransportInstance {
transport := test.NewTransport()
transportInstance := test.NewTransportInstance(transport)
if err := transportInstance.FillReadBuffer([]byte("AFFE!!!\r")); err != nil {
t.Error(err)
return nil
}
transportInstance.FillReadBuffer([]byte("AFFE!!!\r"))
return transportInstance
}()),
requestContext: requestContext,
Expand All @@ -228,10 +216,7 @@ func TestMessageCodec_Receive(t *testing.T) {
DefaultCodec: NewMessageCodec(func() transports.TransportInstance {
transport := test.NewTransport()
transportInstance := test.NewTransportInstance(transport)
if err := transportInstance.FillReadBuffer([]byte("@1A2001!!!\r")); err != nil {
t.Error(err)
return nil
}
transportInstance.FillReadBuffer([]byte("@1A2001!!!\r"))
return transportInstance
}()),
requestContext: requestContext,
Expand Down Expand Up @@ -269,10 +254,7 @@ func TestMessageCodec_Receive(t *testing.T) {
DefaultCodec: NewMessageCodec(func() transports.TransportInstance {
transport := test.NewTransport()
transportInstance := test.NewTransportInstance(transport)
if err := transportInstance.FillReadBuffer([]byte("86040200F940380001000000000000000008000000000000000000000000FA\r\n")); err != nil {
t.Error(err)
return nil
}
transportInstance.FillReadBuffer([]byte("86040200F940380001000000000000000008000000000000000000000000FA\r\n"))
return transportInstance
}()),
requestContext: requestContext,
Expand Down Expand Up @@ -468,10 +450,7 @@ func TestMessageCodec_Receive(t *testing.T) {
DefaultCodec: NewMessageCodec(func() transports.TransportInstance {
transport := test.NewTransport()
transportInstance := test.NewTransportInstance(transport)
if err := transportInstance.FillReadBuffer([]byte("0531AC0079042F0401430316000011\r\n")); err != nil {
t.Error(err)
return nil
}
transportInstance.FillReadBuffer([]byte("0531AC0079042F0401430316000011\r\n"))
return transportInstance
}()),
requestContext: requestContext,
Expand Down Expand Up @@ -572,17 +551,15 @@ func TestMessageCodec_Receive_Delayed_Response(t *testing.T) {
assert.Error(t, err)
assert.Nil(t, msg)
// Now we add a confirmation
err = transportInstance.FillReadBuffer([]byte("i."))
assert.NoError(t, err)
transportInstance.FillReadBuffer([]byte("i."))

// We should wait for more data, so no error, no message
msg, err = codec.Receive()
assert.NoError(t, err)
assert.Nil(t, msg)

// Now we fill in the payload
err = transportInstance.FillReadBuffer([]byte("86FD0201078900434C495053414C20C2\r\n"))
assert.NoError(t, err)
transportInstance.FillReadBuffer([]byte("86FD0201078900434C495053414C20C2\r\n"))

// We should wait for more data, so no error, no message
msg, err = codec.Receive()
Expand All @@ -605,8 +582,7 @@ func TestMessageCodec_Receive_Delayed_Response(t *testing.T) {
assert.Error(t, err)
assert.Nil(t, msg)
// Now we add a confirmation
err = transportInstance.FillReadBuffer([]byte("i."))
assert.NoError(t, err)
transportInstance.FillReadBuffer([]byte("i."))

for i := 0; i < 8; i++ {
t.Logf("%d try", i+1)
Expand All @@ -617,8 +593,7 @@ func TestMessageCodec_Receive_Delayed_Response(t *testing.T) {
}

// Now we fill in the payload
err = transportInstance.FillReadBuffer([]byte("86FD0201078900434C495053414C20C2\r\n"))
assert.NoError(t, err)
transportInstance.FillReadBuffer([]byte("86FD0201078900434C495053414C20C2\r\n"))

// We should wait for more data, so no error, no message
msg, err = codec.Receive()
Expand All @@ -641,8 +616,7 @@ func TestMessageCodec_Receive_Delayed_Response(t *testing.T) {
assert.Error(t, err)
assert.Nil(t, msg)
// Now we add a confirmation
err = transportInstance.FillReadBuffer([]byte("i."))
assert.NoError(t, err)
transportInstance.FillReadBuffer([]byte("i."))

for i := 0; i < 16; i++ {
t.Logf("%d try", i+1)
Expand All @@ -663,8 +637,7 @@ func TestMessageCodec_Receive_Delayed_Response(t *testing.T) {
}

// Now we fill in the payload
err = transportInstance.FillReadBuffer([]byte("86FD0201078900434C495053414C20C2\r\n"))
assert.NoError(t, err)
transportInstance.FillReadBuffer([]byte("86FD0201078900434C495053414C20C2\r\n"))

// We should wait for more data, so no error, no message
msg, err = codec.Receive()
Expand Down
1 change: 1 addition & 0 deletions plc4go/spi/default/DefaultCodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ func (m *defaultCodec) TimeoutExpectations(now time.Time) {

func (m *defaultCodec) HandleMessages(message spi.Message) bool {
messageHandled := false
log.Trace().Msgf("Current number of expectations: %d", len(m.expectations))
for index, expectation := range m.expectations {
// Check if the current message matches the expectations
// If it does, let it handle the message.
Expand Down
26 changes: 7 additions & 19 deletions plc4go/spi/testutils/DriverTestRunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ func WithRootTypeParser(rootTypeParser func(utils.ReadBufferByteBased) (interfac

type TestTransportInstance interface {
transports.TransportInstance
FillReadBuffer(data []uint8) error
FillReadBuffer(data []byte)
GetNumDrainableBytes() uint32
DrainWriteBuffer(numBytes uint32) ([]uint8, error)
DrainWriteBuffer(numBytes uint32) []byte
}

type withRootTypeParser struct {
Expand Down Expand Up @@ -305,19 +305,16 @@ func (m DriverTestsuite) ExecuteStep(connection plc4go.PlcConnection, testcase *
for testTransportInstance.GetNumDrainableBytes() < expectedRawOutputLength {
if time.Now().Sub(now) > 2*time.Second {
drainableBytes := testTransportInstance.GetNumDrainableBytes()
actualRawOutput, _ := testTransportInstance.DrainWriteBuffer(drainableBytes)
actualRawOutput := testTransportInstance.DrainWriteBuffer(drainableBytes)
return errors.Errorf("error getting bytes from transport. Not enough data available: actual(%d)<expected(%d), \nactual: 0x%X\nexpected: 0x%X\nHexdumps:\n%s",
drainableBytes, expectedRawOutputLength, actualRawOutput, expectedRawOutput, utils.DiffHex(expectedRawOutput, actualRawOutput))
}
time.Sleep(2 * time.Millisecond)
}
actualRawOutput, err := testTransportInstance.DrainWriteBuffer(expectedRawOutputLength)
actualRawOutput := testTransportInstance.DrainWriteBuffer(expectedRawOutputLength)
if testTransportInstance.GetNumDrainableBytes() != 0 {
//panic(fmt.Sprintf("leftover drainable bytes (%d)", testTransportInstance.GetNumDrainableBytes()))
}
if err != nil {
return errors.Wrap(err, "error getting bytes from transport")
}

var bufferFactory func([]byte, ...utils.ReadBufferByteBasedOptions) utils.ReadBufferByteBased
if m.byteOrder == binary.BigEndian {
Expand Down Expand Up @@ -347,10 +344,7 @@ func (m DriverTestsuite) ExecuteStep(connection plc4go.PlcConnection, testcase *
if err != nil {
return errors.Wrap(err, "error decoding hex-encoded byte data")
}
rawInput, err := testTransportInstance.DrainWriteBuffer(uint32(len(expectedRawInput)))
if err != nil {
return errors.Wrap(err, "error getting bytes from transport")
}
rawInput := testTransportInstance.DrainWriteBuffer(uint32(len(expectedRawInput)))

// Compare the bytes read with the ones we expect
log.Trace().Msg("Comparing bytes")
Expand Down Expand Up @@ -390,10 +384,7 @@ func (m DriverTestsuite) ExecuteStep(connection plc4go.PlcConnection, testcase *

// Send these bytes to the transport
log.Trace().Msg("Writing to transport")
err = testTransportInstance.FillReadBuffer(wb.GetBytes())
if err != nil {
return errors.Wrap(err, "error writing data to transport")
}
testTransportInstance.FillReadBuffer(wb.GetBytes())
case StepTypeIncomingPlcBytes:
// Get the raw hex-data.
log.Trace().Msg("Get hex data")
Expand All @@ -404,10 +395,7 @@ func (m DriverTestsuite) ExecuteStep(connection plc4go.PlcConnection, testcase *

// Send these bytes to the transport
log.Trace().Msg("Writing bytes to transport")
err = testTransportInstance.FillReadBuffer(rawInput)
if err != nil {
return errors.Wrap(err, "error writing data to transport")
}
testTransportInstance.FillReadBuffer(rawInput)
case StepTypeDelay:
// Get the number of milliseconds
log.Trace().Msg("Getting millis")
Expand Down
7 changes: 3 additions & 4 deletions plc4go/spi/transports/test/Transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,22 +149,21 @@ func (m *TransportInstance) Write(data []uint8) error {
return nil
}

func (m *TransportInstance) FillReadBuffer(data []uint8) error {
func (m *TransportInstance) FillReadBuffer(data []byte) {
log.Trace().Msgf("FillReadBuffer with 0x%x", data)
m.readBuffer = append(m.readBuffer, data...)
return nil
}

func (m *TransportInstance) GetNumDrainableBytes() uint32 {
log.Trace().Msg("get number of drainable bytes")
return uint32(len(m.writeBuffer))
}

func (m *TransportInstance) DrainWriteBuffer(numBytes uint32) ([]uint8, error) {
func (m *TransportInstance) DrainWriteBuffer(numBytes uint32) []byte {
log.Trace().Msgf("Drain write buffer with number of bytes %d", numBytes)
data := m.writeBuffer[0:int(numBytes)]
m.writeBuffer = m.writeBuffer[int(numBytes):]
return data, nil
return data
}

func (m *TransportInstance) String() string {
Expand Down

0 comments on commit 1ad4fc7

Please sign in to comment.