Skip to content

Commit

Permalink
test(plc4go/opcua): driver test suite test works now
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Aug 3, 2023
1 parent 333b72b commit 5716a86
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 18 deletions.
10 changes: 8 additions & 2 deletions plc4go/internal/opcua/Connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,10 @@ func (c *Connection) fireConnectionError(err error, ch chan<- plc4go.PlcConnecti
c.log.Debug().Err(err).Msg("Error disconnecting message codec on connection error")
}
c.SetConnected(false)
close(c.disconnectEvent)
select {
case c.disconnectEvent <- struct{}{}:
default:
}
}

func (c *Connection) fireConnected(ch chan<- plc4go.PlcConnectionConnectResult) {
Expand All @@ -247,5 +250,8 @@ func (c *Connection) fireConnected(ch chan<- plc4go.PlcConnectionConnectResult)
c.log.Info().Msg("Successfully connected")
}
c.SetConnected(true)
close(c.connectEvent)
select {
case c.connectEvent <- struct{}{}:
default:
}
}
16 changes: 9 additions & 7 deletions plc4go/internal/opcua/MessageCodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/apache/plc4x/plc4go/spi/default"
"github.com/apache/plc4x/plc4go/spi/options"
"github.com/apache/plc4x/plc4go/spi/transports"
"github.com/apache/plc4x/plc4go/spi/utils"
"sync"

"github.com/pkg/errors"
Expand Down Expand Up @@ -71,14 +72,14 @@ func (m *MessageCodec) Send(message spi.Message) error {
}

// Serialize the request
theBytes, err := messagePdu.Serialize()
if err != nil {
wbbb := utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.LittleEndian))
if err := messagePdu.SerializeWithWriteBuffer(context.Background(), wbbb); err != nil {
return errors.Wrap(err, "error serializing request")
}
theBytes := wbbb.GetBytes()

// Send it to the PLC
err = m.GetTransportInstance().Write(theBytes)
if err != nil {
if err := m.GetTransportInstance().Write(theBytes); err != nil {
return errors.Wrap(err, "error sending request")
}
return nil
Expand All @@ -102,21 +103,22 @@ func (m *MessageCodec) Receive() (spi.Message, error) {
m.log.Trace().Uint32("numBytesAvailable", numBytesAvailable).Msg("check available bytes < 8")
return numBytesAvailable < 8
}); err != nil {
m.log.Warn().Err(err).Msg("error filling buffer")
m.log.Debug().Err(err).Msg("error filling buffer")
}

data, err := ti.PeekReadableBytes(8)
if err != nil {
m.log.Debug().Err(err).Msg("error peeking")
return nil, nil
}
numberOfBytesToRead := binary.LittleEndian.Uint32(data[:4])
numberOfBytesToRead := binary.LittleEndian.Uint32(data[4:8])
readBytes, err := ti.Read(numberOfBytesToRead)
if err != nil {
return nil, errors.Wrapf(err, "could not read %d bytes", readBytes)
}
ctxForModel := options.GetLoggerContextForModel(context.Background(), m.log, options.WithPassLoggerToModel(m.passLogToModel))
messagePdu, err := readWriteModel.MessagePDUParse(ctxForModel, readBytes, true)
rbbb := utils.NewReadBufferByteBased(readBytes, utils.WithByteOrderForReadBufferByteBased(binary.LittleEndian))
messagePdu, err := readWriteModel.MessagePDUParseWithBuffer(ctxForModel, rbbb, true)
if err != nil {
return nil, errors.New("Could not parse pdu")
}
Expand Down
2 changes: 1 addition & 1 deletion plc4go/spi/default/DefaultCodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func (m *defaultCodec) HandleMessages(message spi.Message) bool {
expectationLog.Trace().Msg("doesn't accept message")
}
}
m.log.Trace().Bool("messageHandled", messageHandled).Msg("handled message")
m.log.Trace().Bool("messageHandled", messageHandled).Msg("finished message handling")
return messageHandled
}

Expand Down
5 changes: 1 addition & 4 deletions plc4go/spi/testutils/DriverTestRunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (m DriverTestsuite) Run(t *testing.T, driverManager plc4go.PlcDriverManager
t.Log("no testcase steps")
}

if len(testcase.steps) > 0 {
if len(m.teardownSteps) > 0 {
// Run the teardown steps
m.LogDelimiterSection(t, "-", "Performing teardown for: %s", testcase.name)
for _, testStep := range m.teardownSteps {
Expand Down Expand Up @@ -561,9 +561,6 @@ func RunDriverTestsuite(t *testing.T, driver plc4go.PlcDriver, testPath string,
}
})
}
t.Logf("Done running %d testcases", len(testsuite.testcases))
// Execute the tests in the testsuite
t.Log(testsuite.name)
}

type ConnectionConnectAwaiter interface {
Expand Down
2 changes: 1 addition & 1 deletion plc4go/tests/drivers/tests/opcua_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
)

func TestOPCUADriver(t *testing.T) {
t.Skip("Not yet finished")
//t.Skip("Not yet finished")
parser := func(readBufferByteBased utils.ReadBufferByteBased) (any, error) {
return readWriteModel.MessagePDUParseWithBuffer(context.Background(), readBufferByteBased, false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@
<messageType dataType="string" bitLength="24" encoding="UTF-8">HEL</messageType>
<OpcuaHelloRequest>
<chunk dataType="string" bitLength="8" encoding="UTF-8">F</chunk>
<messageSize dataType="int" bitLength="32">52</messageSize>
<messageSize dataType="int" bitLength="32">47</messageSize>
<version dataType="int" bitLength="32">0</version>
<receiveBufferSize dataType="int" bitLength="32">65535</receiveBufferSize>
<sendBufferSize dataType="int" bitLength="32">65535</sendBufferSize>
<maxMessageSize dataType="int" bitLength="32">2097152</maxMessageSize>
<maxChunkCount dataType="int" bitLength="32">64</maxChunkCount>
<endpoint>
<PascalString>
<sLength dataType="int" bitLength="32">20</sLength>
<stringValue dataType="string" bitLength="160" encoding="UTF-8">opc.test://hurz:null</stringValue>
<sLength dataType="int" bitLength="32">15</sLength>
<stringValue dataType="string" bitLength="120" encoding="UTF-8">opc.test://hurz</stringValue>
</PascalString>
</endpoint>
</OpcuaHelloRequest>
Expand Down

0 comments on commit 5716a86

Please sign in to comment.