Skip to content

Commit

Permalink
fix(plc4go/spi): increase sleep times of DriverTestRunner
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Jun 21, 2023
1 parent 12fc12c commit 77453bf
Showing 1 changed file with 23 additions and 27 deletions.
50 changes: 23 additions & 27 deletions plc4go/spi/testutils/DriverTestRunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ type TestTransportInstance interface {
}

func (m DriverTestsuite) Run(t *testing.T, driverManager plc4go.PlcDriverManager, testcase DriverTestcase) error {
var options []string
var driverParameters []string
for key, value := range m.driverParameters {
options = append(options, fmt.Sprintf("%s=%s", key, value))
driverParameters = append(driverParameters, fmt.Sprintf("%s=%s", key, value))
}
optionsString := ""
if len(options) > 0 {
optionsString = "?" + strings.Join(options, "&")
if len(driverParameters) > 0 {
optionsString = "?" + strings.Join(driverParameters, "&")
}
// Get a connection
connectionChan := driverManager.GetConnection(m.driverName + ":test://hurz" + optionsString)
Expand All @@ -119,8 +119,9 @@ func (m DriverTestsuite) Run(t *testing.T, driverManager plc4go.PlcDriverManager
return errors.Wrap(err, "error in setup step "+testStep.name)
}
// We sleep a bit to not run too fast into the post setup steps and give connections a bit time to settle built up
time.Sleep(10 * time.Millisecond)
time.Sleep(100 * time.Millisecond) // TODO: this is really bad as on CI sometimes those sleeps are not enough...
}
t.Log("setup done")

// Run the actual scenario steps
t.Logf("\n-------------------------------------------------------\nRunning testcases for: %s \n-------------------------------------------------------\n", testcase.name)
Expand All @@ -129,8 +130,9 @@ func (m DriverTestsuite) Run(t *testing.T, driverManager plc4go.PlcDriverManager
if err != nil {
return errors.Wrap(err, "error in step "+testStep.name)
}
time.Sleep(10 * time.Millisecond)
time.Sleep(100 * time.Millisecond) // TODO: this is really bad as on CI sometimes those sleeps are not enough...
}
t.Log("test steps done")

// Run the teardown steps
t.Logf("\n-------------------------------------------------------\nPerforming teardown for: %s \n-------------------------------------------------------\n", testcase.name)
Expand All @@ -139,8 +141,9 @@ func (m DriverTestsuite) Run(t *testing.T, driverManager plc4go.PlcDriverManager
if err != nil {
return errors.Wrap(err, "error in teardown step "+testStep.name)
}
time.Sleep(10 * time.Millisecond)
time.Sleep(100 * time.Millisecond) // TODO: this is really bad as on CI sometimes those sleeps are not enough...
}
t.Log("tear down done")

t.Logf("\n-------------------------------------------------------\nDone\n-------------------------------------------------------\n")
return nil
Expand Down Expand Up @@ -176,8 +179,7 @@ func (m DriverTestsuite) ExecuteStep(t *testing.T, connection plc4go.PlcConnecti
}

// Execute the read-request and store the response-channel in the testcase.
t.Logf("Execute read request (%T)", readRequest)
t.Logf("\n%s", readRequest)
t.Logf("Execute read request (%T)\n%[1]s", readRequest)
if testcase.readRequestResultChannel != nil {
return errors.New("testcase read-request result channel already occupied")
}
Expand Down Expand Up @@ -213,8 +215,7 @@ func (m DriverTestsuite) ExecuteStep(t *testing.T, connection plc4go.PlcConnecti
if err != nil {
return errors.Wrap(err, "Error creating write-request")
}
t.Logf("Execute write request (%T)", writeRequest)
t.Logf("\n%s", writeRequest)
t.Logf("Execute write request (%T)\n%[1]s", writeRequest)
if testcase.writeRequestResultChannel != nil {
return errors.New("testcase write-request result channel already occupied")
}
Expand All @@ -235,8 +236,7 @@ func (m DriverTestsuite) ExecuteStep(t *testing.T, connection plc4go.PlcConnecti
// Serialize the response to XML
xmlWriteBuffer := utils.NewXmlWriteBuffer()
response := readRequestResult.GetResponse()
t.Logf("Got response (%T)", response)
t.Logf("\n%s", response)
t.Logf("Got response (%T)\n%[1]s", response)
err := response.(utils.Serializable).SerializeWithWriteBuffer(context.Background(), xmlWriteBuffer)
if err != nil {
return errors.Wrap(err, "error serializing response")
Expand All @@ -263,8 +263,7 @@ func (m DriverTestsuite) ExecuteStep(t *testing.T, connection plc4go.PlcConnecti
// Serialize the response to XML
xmlWriteBuffer := utils.NewXmlWriteBuffer()
response := writeResponseResult.GetResponse()
t.Logf("Got response (%T)", response)
t.Logf("\n%s", response)
t.Logf("Got response (%T)\n%[1]s", response)
err := response.(utils.Serializable).SerializeWithWriteBuffer(context.Background(), xmlWriteBuffer)
if err != nil {
return errors.Wrap(err, "error serializing response")
Expand All @@ -290,8 +289,7 @@ func (m DriverTestsuite) ExecuteStep(t *testing.T, connection plc4go.PlcConnecti
if err != nil {
return errors.Wrap(err, "Error parsing message")
}
t.Logf("Parsed message (%T)", expectedMessage)
t.Logf("\n%s", expectedMessage)
t.Logf("Parsed message (%T)\n%[1]s", expectedMessage)

// Serialize the model into bytes
t.Log("Write to bytes")
Expand All @@ -312,11 +310,11 @@ func (m DriverTestsuite) ExecuteStep(t *testing.T, connection plc4go.PlcConnecti
expectedRawOutput := expectedWriteBuffer.GetBytes()
expectedRawOutputLength := uint32(len(expectedRawOutput))

now := time.Now()
startTransportPolling := time.Now()
// Read exactly this amount of bytes from the transport
t.Logf("Reading bytes (expectedRawOutputLength %d)", expectedRawOutputLength)
t.Logf("Reading bytes from transport instance (expectedRawOutputLength %d)", expectedRawOutputLength)
for testTransportInstance.GetNumDrainableBytes() < expectedRawOutputLength {
if time.Now().Sub(now) > 2*time.Second {
if time.Since(startTransportPolling) > 2*time.Second {
drainableBytes := testTransportInstance.GetNumDrainableBytes()
actualRawOutput := testTransportInstance.DrainWriteBuffer(drainableBytes)
return errors.Errorf("error getting bytes from transport. Not enough data available: actual(%d)<expected(%d), \nactual: %#X\nexpected: %#X\nHexdumps:\n%s",
Expand Down Expand Up @@ -349,6 +347,7 @@ func (m DriverTestsuite) ExecuteStep(t *testing.T, connection plc4go.PlcConnecti
return errors.Errorf("actual output doesn't match expected output:\nactual: %#X\nexpected: %#X\nHexdumps:\n%s", actualRawOutput, expectedRawOutput, utils.DiffHex(expectedRawOutput, actualRawOutput))
}
}
t.Log("outputs are matching")
// If there's a difference, parse the input and display it to simplify debugging
case StepTypeOutgoingPlcBytes:
// Read exactly this amount of bytes from the transport
Expand Down Expand Up @@ -378,8 +377,7 @@ func (m DriverTestsuite) ExecuteStep(t *testing.T, connection plc4go.PlcConnecti
if err != nil {
return errors.Wrap(err, "error parsing message")
}
t.Logf("Parsed message (%T)", expectedMessage)
t.Logf("\n%s", expectedMessage)
t.Logf("Parsed message (%T)\n%[1]s", expectedMessage)

// Serialize the model into bytes
t.Log("Serializing bytes")
Expand All @@ -399,9 +397,8 @@ func (m DriverTestsuite) ExecuteStep(t *testing.T, connection plc4go.PlcConnecti
}

// Send these bytes to the transport
t.Log("Writing to transport")
_bytes := wb.GetBytes()
t.Logf("\n%s", hex.Dump(_bytes))
t.Logf("Writing to transport\n%s", hex.Dump(_bytes))
testTransportInstance.FillReadBuffer(_bytes)
case StepTypeIncomingPlcBytes:
// Get the raw hex-data.
Expand All @@ -412,8 +409,7 @@ func (m DriverTestsuite) ExecuteStep(t *testing.T, connection plc4go.PlcConnecti
}

// Send these bytes to the transport
t.Log("Writing bytes to transport")
t.Logf("\n%s", hex.Dump(rawInput))
t.Logf("Writing bytes to transport\n%[1]s", hex.Dump(rawInput))
testTransportInstance.FillReadBuffer(rawInput)
case StepTypeDelay:
// Get the number of milliseconds
Expand All @@ -433,7 +429,7 @@ func (m DriverTestsuite) ExecuteStep(t *testing.T, connection plc4go.PlcConnecti
return errors.Wrap(err, "error closing transport")
}
}
t.Logf("\n-------------------------------------------------------\n - Finished step: %s after %vms \n-------------------------------------------------------", step.name, time.Now().Sub(start).Milliseconds())
t.Logf("\n-------------------------------------------------------\n - Finished step: %s after %sms \n-------------------------------------------------------", step.name, time.Since(start))
return nil
}

Expand Down

0 comments on commit 77453bf

Please sign in to comment.