diff options
author | Andre Hartmann <aha_1980@gmx.de> | 2018-09-09 12:51:30 +0200 |
---|---|---|
committer | Andre Hartmann <aha_1980@gmx.de> | 2019-07-31 09:02:46 +0200 |
commit | fd9b9c73ca60c66d01376ddbd6d36e9ea2dfa95c (patch) | |
tree | 35d6eabd19cff707f34571d1e719edff43456c0f /tests | |
parent | b8dbe5524bd15ea0acc03dc5c5c4bf9f0d912f42 (diff) |
QCanBusDevice: More error codes and messages on failures
More error messages can now be logged with the logging framework
or directly accessed with the errorOccurred() signal.
The new error codes QCanBusDevice::OperationError and
QCanBusDevice::TimeoutError were introduced to signal situations
where the device is not able to perform a specific operation
respectively when an operation timed out.
[ChangeLog][QCanBusDevice] Added the QCanBusDevice::OperationError
and QCanBusDevice::TimeoutError codes to signal wrong operation
respectively timeout errors.
Task-number: QTBUG-70449
Change-Id: Ifa0831bd0947b624579ae8662df10a2a9ce38714
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/qcanbusdevice/tst_qcanbusdevice.cpp | 127 |
1 files changed, 89 insertions, 38 deletions
diff --git a/tests/auto/qcanbusdevice/tst_qcanbusdevice.cpp b/tests/auto/qcanbusdevice/tst_qcanbusdevice.cpp index 24127d9..e0b30f0 100644 --- a/tests/auto/qcanbusdevice/tst_qcanbusdevice.cpp +++ b/tests/auto/qcanbusdevice/tst_qcanbusdevice.cpp @@ -84,8 +84,11 @@ public: bool writeFrame(const QCanBusFrame &data) { - if (state() != QCanBusDevice::ConnectedState) + if (state() != QCanBusDevice::ConnectedState) { + setError(QStringLiteral("Cannot write frame as device is not connected"), + QCanBusDevice::OperationError); return false; + } if (writeBufferUsed) { enqueueOutgoingFrame(data); @@ -174,7 +177,9 @@ void tst_QCanBusDevice::initTestCase() QSignalSpy stateSpy(device.data(), &QCanBusDevice::stateChanged); QVERIFY(!device->connectDevice()); // first connect triggered to fail + QCOMPARE(device->error(), QCanBusDevice::NoError); QVERIFY(device->connectDevice()); + QCOMPARE(device->error(), QCanBusDevice::NoError); QTRY_VERIFY_WITH_TIMEOUT(device->state() == QCanBusDevice::ConnectedState, 5000); QCOMPARE(stateSpy.count(), 4); QCOMPARE(stateSpy.at(0).at(0).value<QCanBusDevice::CanBusDeviceState>(), @@ -236,7 +241,8 @@ void tst_QCanBusDevice::write() stateSpy.clear(); QVERIFY(stateSpy.isEmpty()); - device->writeFrame(frame); + QVERIFY(!device->writeFrame(frame)); + QCOMPARE(device->error(), QCanBusDevice::OperationError); QCOMPARE(spy.count(), 0); device->connectDevice(); @@ -247,7 +253,8 @@ void tst_QCanBusDevice::write() QCOMPARE(stateSpy.at(1).at(0).value<QCanBusDevice::CanBusDeviceState>(), QCanBusDevice::ConnectedState); - device->writeFrame(frame); + QVERIFY(device->writeFrame(frame)); + QCOMPARE(device->error(), QCanBusDevice::NoError); QCOMPARE(spy.count(), 1); } @@ -260,6 +267,7 @@ void tst_QCanBusDevice::read() stateSpy.clear(); const QCanBusFrame frame1 = device->readFrame(); + QCOMPARE(device->error(), QCanBusDevice::OperationError); QVERIFY(device->connectDevice()); QTRY_VERIFY_WITH_TIMEOUT(device->state() == QCanBusDevice::ConnectedState, 5000); @@ -271,6 +279,7 @@ void tst_QCanBusDevice::read() device->triggerNewFrame(); const QCanBusFrame frame2 = device->readFrame(); + QCOMPARE(device->error(), QCanBusDevice::NoError); QVERIFY(!frame1.frameId()); QVERIFY(!frame1.isValid()); QVERIFY(frame2.frameId()); @@ -281,6 +290,12 @@ void tst_QCanBusDevice::readAll() { enum { FrameNumber = 10 }; device->disconnectDevice(); + QTRY_VERIFY_WITH_TIMEOUT(device->state() == QCanBusDevice::UnconnectedState, 5000); + + const QVector<QCanBusFrame> empty = device->readAllFrames(); + QCOMPARE(device->error(), QCanBusDevice::OperationError); + QVERIFY(empty.isEmpty()); + QVERIFY(device->connectDevice()); QTRY_VERIFY_WITH_TIMEOUT(device->state() == QCanBusDevice::ConnectedState, 5000); @@ -288,21 +303,30 @@ void tst_QCanBusDevice::readAll() device->triggerNewFrame(); const QVector<QCanBusFrame> frames = device->readAllFrames(); + QCOMPARE(device->error(), QCanBusDevice::NoError); QCOMPARE(FrameNumber, frames.size()); QVERIFY(!device->framesAvailable()); } void tst_QCanBusDevice::clearInputBuffer() { - if (device->state() != QCanBusDevice::ConnectedState) { - QVERIFY(device->connectDevice()); - QTRY_VERIFY_WITH_TIMEOUT(device->state() == QCanBusDevice::ConnectedState, 5000); - } + device->disconnectDevice(); + QTRY_VERIFY_WITH_TIMEOUT(device->state() == QCanBusDevice::UnconnectedState, 5000); + + device->clear(QCanBusDevice::Input); + QCOMPARE(device->error(), QCanBusDevice::OperationError); + + QVERIFY(device->connectDevice()); + QTRY_VERIFY_WITH_TIMEOUT(device->state() == QCanBusDevice::ConnectedState, 5000); + + device->clear(QCanBusDevice::Input); + QCOMPARE(device->error(), QCanBusDevice::NoError); for (int i = 0; i < 10; ++i) device->triggerNewFrame(); device->clear(QCanBusDevice::Input); + QCOMPARE(device->error(), QCanBusDevice::NoError); QVERIFY(!device->framesAvailable()); } @@ -311,11 +335,17 @@ void tst_QCanBusDevice::clearOutputBuffer() { // this test requires buffered writing device->setWriteBuffered(true); + device->disconnectDevice(); + QTRY_VERIFY_WITH_TIMEOUT(device->state() == QCanBusDevice::UnconnectedState, 5000); - if (device->state() != QCanBusDevice::ConnectedState) { - QVERIFY(device->connectDevice()); - QTRY_VERIFY_WITH_TIMEOUT(device->state() == QCanBusDevice::ConnectedState, 5000); - } + device->clear(QCanBusDevice::Output); + QCOMPARE(device->error(), QCanBusDevice::OperationError); + + QVERIFY(device->connectDevice()); + QTRY_VERIFY_WITH_TIMEOUT(device->state() == QCanBusDevice::ConnectedState, 5000); + + device->clear(QCanBusDevice::Output); + QCOMPARE(device->error(), QCanBusDevice::NoError); // first test buffered writing, frames will be written after some delay QSignalSpy spy(device.data(), &QCanBusDevice::framesWritten); @@ -329,6 +359,7 @@ void tst_QCanBusDevice::clearOutputBuffer() device->writeFrame(QCanBusFrame(0x123, "output")); device->clear(QCanBusDevice::Output); + QCOMPARE(device->error(), QCanBusDevice::NoError); QTRY_VERIFY_WITH_TIMEOUT(spy.count() == 0, 5000); } @@ -522,6 +553,10 @@ void tst_QCanBusDevice::tst_bufferingAttribute() void tst_QCanBusDevice::tst_waitForFramesReceived() { + device->disconnectDevice(); + QVERIFY(!device->waitForFramesReceived(100)); + QCOMPARE(device->error(), QCanBusDevice::OperationError); + if (device->state() != QCanBusDevice::ConnectedState) { QVERIFY(device->connectDevice()); QTRY_VERIFY_WITH_TIMEOUT(device->state() == QCanBusDevice::ConnectedState, 5000); @@ -531,9 +566,10 @@ void tst_QCanBusDevice::tst_waitForFramesReceived() QVERIFY(device->triggerNewFrame()); QVERIFY(device->framesAvailable()); - // frame is available, function blocks and times out - bool result = device->waitForFramesReceived(2000); - QVERIFY(!result); + // frame is already available, but no new frame comes in + // while function blocks, therefore times out + QVERIFY(!device->waitForFramesReceived(2000)); + QCOMPARE(device->error(), QCanBusDevice::TimeoutError); QCanBusFrame frame = device->readFrame(); QVERIFY(frame.isValid()); @@ -543,16 +579,16 @@ void tst_QCanBusDevice::tst_waitForFramesReceived() QElapsedTimer elapsed; elapsed.start(); // no pending frame (should trigger active wait & timeout) - result = device->waitForFramesReceived(5000); + QVERIFY(!device->waitForFramesReceived(5000)); QVERIFY(elapsed.hasExpired(4000)); // should have caused time elapse - QVERIFY(!result); + QCOMPARE(device->error(), QCanBusDevice::TimeoutError); QTimer::singleShot(2000, [&]() { device->triggerNewFrame(); }); elapsed.restart(); // frame will be inserted after 2s - result = device->waitForFramesReceived(8000); + QVERIFY(device->waitForFramesReceived(8000)); + QCOMPARE(device->error(), QCanBusDevice::NoError); QVERIFY(!elapsed.hasExpired(8000)); - QVERIFY(result); frame = device->readFrame(); QVERIFY(frame.isValid()); @@ -564,9 +600,8 @@ void tst_QCanBusDevice::tst_waitForFramesReceived() }); elapsed.restart(); // error will be inserted after 2s - result = device->waitForFramesReceived(8000); + QVERIFY(!device->waitForFramesReceived(8000)); QVERIFY(!elapsed.hasExpired(8000)); - QVERIFY(!result); QCOMPARE(device->errorString(), QStringLiteral("TriggerError")); QCOMPARE(device->error(), QCanBusDevice::ReadError); @@ -580,56 +615,71 @@ void tst_QCanBusDevice::tst_waitForFramesReceived() QObject::connect(device.data(), &QCanBusDevice::framesReceived, [this, &handleCounter]() { handleCounter++; // this should trigger a recursion which we want to catch - device->waitForFramesReceived(5000); + QVERIFY(!device->waitForFramesReceived(5000)); + // Only the first two frames create a recursion, as the outer + // waitForFramesReceived() will immediately exit once at least + // one frame was received. Therefore the third frame here leads + // to TimeoutError, as no further frame is received. + if (handleCounter < 3) { + QCOMPARE(device->error(), QCanBusDevice::OperationError); + } else { + QCOMPARE(device->error(), QCanBusDevice::TimeoutError); + } }); - result = device->waitForFramesReceived(8000); - QVERIFY(result); + QVERIFY(device->waitForFramesReceived(8000)); + QCOMPARE(device->error(), QCanBusDevice::NoError); QTRY_COMPARE_WITH_TIMEOUT(handleCounter, 3, 5000); } void tst_QCanBusDevice::tst_waitForFramesWritten() { + device->disconnectDevice(); + QVERIFY(!device->waitForFramesWritten(100)); + QCOMPARE(device->error(), QCanBusDevice::OperationError); + if (device->state() != QCanBusDevice::ConnectedState) { + QVERIFY(!device->waitForFramesWritten(100)); + QCOMPARE(device->error(), QCanBusDevice::OperationError); + QVERIFY(device->connectDevice()); QTRY_VERIFY_WITH_TIMEOUT(device->state() == QCanBusDevice::ConnectedState, 5000); } device->setWriteBuffered(false); - bool result = device->waitForFramesWritten(1000); - QVERIFY(!result); // no buffer, waiting not possible + QVERIFY(!device->waitForFramesWritten(1000)); // no buffer, waiting not possible + QCOMPARE(device->error(), QCanBusDevice::NoError); device->setWriteBuffered(true); QVERIFY(device->framesToWrite() == 0); - result = device->waitForFramesWritten(1000); - QVERIFY(!result); // nothing in buffer, nothing to wait for + QVERIFY(!device->waitForFramesWritten(1000)); // nothing in buffer, nothing to wait for + QCOMPARE(device->error(), QCanBusDevice::NoError); QCanBusFrame frame; frame.setPayload(QByteArray("testData")); // test error case QTimer::singleShot(500, [&]() { - device->emulateError(QStringLiteral("TriggerWriteError"), QCanBusDevice::ReadError); + device->emulateError(QStringLiteral("TriggerWriteError"), QCanBusDevice::WriteError); }); device->writeFrame(frame); QElapsedTimer elapsed; elapsed.start(); // error will be triggered - result = device->waitForFramesWritten(8000); + QVERIFY(!device->waitForFramesWritten(8000)); QVERIFY(!elapsed.hasExpired(8000)); - QVERIFY(!result); QCOMPARE(device->errorString(), QStringLiteral("TriggerWriteError")); - QCOMPARE(device->error(), QCanBusDevice::ReadError); + QCOMPARE(device->error(), QCanBusDevice::WriteError); // flush remaining frames out to reset the test QTRY_VERIFY_WITH_TIMEOUT(device->framesToWrite() == 0, 10000); // test timeout device->writeFrame(frame); - result = device->waitForFramesWritten(500); + QVERIFY(!device->waitForFramesWritten(500)); + QCOMPARE(device->error(), QCanBusDevice::TimeoutError); QVERIFY(elapsed.hasExpired(500)); - QVERIFY(!result); // flush remaining frames out to reset the test QTRY_VERIFY_WITH_TIMEOUT(device->framesToWrite() == 0, 10000); @@ -637,9 +687,9 @@ void tst_QCanBusDevice::tst_waitForFramesWritten() device->writeFrame(frame); device->writeFrame(frame); elapsed.restart(); - result = device->waitForFramesWritten(8000); + QVERIFY(device->waitForFramesWritten(8000)); + QCOMPARE(device->error(), QCanBusDevice::NoError); QVERIFY(!elapsed.hasExpired(8000)); - QVERIFY(result); // flush remaining frames out to reset the test QTRY_VERIFY_WITH_TIMEOUT(device->framesToWrite() == 0, 10000); @@ -652,10 +702,11 @@ void tst_QCanBusDevice::tst_waitForFramesWritten() QObject::connect(device.data(), &QCanBusDevice::framesWritten, [this, &handleCounter]() { handleCounter++; // this should trigger a recursion which we want to catch - device->waitForFramesWritten(5000); + QVERIFY(!device->waitForFramesWritten(5000)); + QCOMPARE(device->error(), QCanBusDevice::OperationError); }); - result = device->waitForFramesWritten(8000); - QVERIFY(result); + QVERIFY(device->waitForFramesWritten(8000)); + QCOMPARE(device->error(), QCanBusDevice::NoError); QTRY_COMPARE_WITH_TIMEOUT(handleCounter, 3, 5000); device->setWriteBuffered(false); |