#include #include #include #include #include #include #include #include #ifdef Q_OS_WIN #include #include #endif #include "DecoderOutputFramer.hpp" namespace { char const fixtureOption[] = "--decoder-output-fixture"; int runFixture (QStringList const& arguments) { if (arguments.size () < 3) return 2; auto const payload = QByteArray::fromBase64 (arguments[2].toLatin1 ()); #ifdef Q_OS_WIN if (-1 == _setmode (_fileno (stdout), _O_BINARY)) return 3; #endif QFile output; if (!output.open (stdout, QIODevice::WriteOnly, QFileDevice::DontCloseHandle)) { return 4; } if (output.write (payload) != payload.size () || !output.flush ()) return 5; return arguments.contains ("--hold") ? QCoreApplication::exec () : 0; } } class TestDecoderOutputFramer final : public QObject { Q_OBJECT private Q_SLOTS: void rejectedRecordDoesNotStopDrain (); void staleFramesRemainAssociatedWithTheirGeneration (); void completeStateWaitsForStdoutDrain (); void recordTerminatorsArePreserved (); void malformedFramesAreReported (); void killedFrameCanBeReplaced (); }; void TestDecoderOutputFramer::rejectedRecordDoesNotStopDrain () { QByteArray const output { " gen=7\n" "record rejected by policy\n" " 2 17 123456 gen=7\n"}; DecoderOutputFramer framer; int started {0}; int records {0}; int finished {0}; DecoderOutputFramer::EventHandler handler { [&] (DecoderOutputFramer::Event const& event) { if (DecoderOutputFramer::EventType::Started == event.type) ++started; if (DecoderOutputFramer::EventType::Record == event.type) { ++records; return; } if (DecoderOutputFramer::EventType::Finished == event.type) ++finished; }}; QProcess process; connect (&process, &QProcess::readyReadStandardOutput, this, [&] { framer.drain (process, handler); }); process.start (QCoreApplication::applicationFilePath (), {fixtureOption, QString::fromLatin1 (output.toBase64 ())}); QVERIFY (process.waitForStarted ()); QVERIFY (process.waitForFinished ()); framer.drain (process, handler); QCOMPARE (started, 1); QCOMPARE (records, 1); QCOMPARE (finished, 1); QCOMPARE (framer.currentGeneration (), qint32 {0}); } void TestDecoderOutputFramer::staleFramesRemainAssociatedWithTheirGeneration () { QByteArray const output { " gen=7\n" "stale record\n" " 0 0 0 gen=7\n" " gen=8\n" "current record\n" " 0 1 0 gen=8\n"}; DecoderOutputFramer framer; QVector recordGenerations; QVector acceptedRecords; QVector finishedGenerations; qint32 const activeGeneration {8}; DecoderOutputFramer::EventHandler handler { [&] (DecoderOutputFramer::Event const& event) { if (DecoderOutputFramer::EventType::Record == event.type) { recordGenerations.append (event.generation); if (activeGeneration == event.generation) { acceptedRecords.append (event.rawLine); } } else if (DecoderOutputFramer::EventType::Finished == event.type) { finishedGenerations.append (event.generation); } }}; QProcess process; process.start (QCoreApplication::applicationFilePath (), {fixtureOption, QString::fromLatin1 (output.toBase64 ())}); QVERIFY (process.waitForStarted ()); QVERIFY (process.waitForFinished ()); framer.drain (process, handler); QCOMPARE (recordGenerations, QVector ({7, 8})); QCOMPARE (acceptedRecords, QVector ({"current record\n"})); QCOMPARE (finishedGenerations, QVector ({7, 8})); } void TestDecoderOutputFramer::completeStateWaitsForStdoutDrain () { std::unique_ptr shared {new shared_dec_data_t}; std::unique_ptr payload {new dec_data_t {}}; DecoderIpc::initialize (*shared); QVERIFY (DecoderIpc::publish (*shared, *payload, false, 21)); qint32 generation {0}; QVERIFY (DecoderIpc::claim (*shared, generation)); QVERIFY (DecoderIpc::finish (*shared, generation)); QByteArray output { " gen=21\n" "buffered record\n" " 0 1 0 gen=21\n"}; QBuffer buffer {&output}; QVERIFY (buffer.open (QIODevice::ReadOnly)); QCOMPARE (DecoderIpc::state (*shared), qint32 {DECODER_IPC_COMPLETE}); DecoderOutputFramer framer; int records {0}; bool consumed {false}; framer.drain (buffer, [&] (DecoderOutputFramer::Event const& event) { if (DecoderOutputFramer::EventType::Record == event.type) ++records; if (DecoderOutputFramer::EventType::Finished == event.type) { consumed = DecoderIpc::consume (*shared, event.generation); } }); QCOMPARE (records, 1); QVERIFY (consumed); QCOMPARE (DecoderIpc::state (*shared), qint32 {DECODER_IPC_IDLE}); } void TestDecoderOutputFramer::recordTerminatorsArePreserved () { QByteArray const output { " gen=9\r\n" "first record\r\n" "second record\n" " 0 2 0 gen=9\r\n"}; DecoderOutputFramer framer; QVector records; QProcess process; process.start (QCoreApplication::applicationFilePath (), {fixtureOption, QString::fromLatin1 (output.toBase64 ())}); QVERIFY (process.waitForStarted ()); QVERIFY (process.waitForFinished ()); framer.drain (process, [&] (DecoderOutputFramer::Event const& event) { if (DecoderOutputFramer::EventType::Record == event.type) { records.append (event.rawLine); } }); QCOMPARE (records, QVector ({"first record\r\n", "second record\n"})); } void TestDecoderOutputFramer::malformedFramesAreReported () { QByteArray output { "unframed record\n" " gen=0\n" " gen=14\n" " 0 0 0 gen=15\n" " 0 0 0 gen=14\n"}; QBuffer buffer {&output}; QVERIFY (buffer.open (QIODevice::ReadOnly)); DecoderOutputFramer framer; QVector eventTypes; framer.drain (buffer, [&] (DecoderOutputFramer::Event const& event) { eventTypes.append (event.type); }); QCOMPARE (eventTypes, QVector ({ DecoderOutputFramer::EventType::Malformed, DecoderOutputFramer::EventType::Malformed, DecoderOutputFramer::EventType::Started, DecoderOutputFramer::EventType::Malformed, DecoderOutputFramer::EventType::Finished})); QCOMPARE (framer.currentGeneration (), qint32 {0}); } void TestDecoderOutputFramer::killedFrameCanBeReplaced () { DecoderOutputFramer framer; QVector events; DecoderOutputFramer::EventHandler handler { [&] (DecoderOutputFramer::Event const& event) {events.append (event);}}; QProcess abandoned; connect (&abandoned, &QProcess::readyReadStandardOutput, this, [&] { framer.drain (abandoned, handler); }); QByteArray const abandonedOutput { " gen=11\n" "abandoned record\n"}; abandoned.start (QCoreApplication::applicationFilePath (), {fixtureOption, QString::fromLatin1 (abandonedOutput.toBase64 ()), "--hold"}); QVERIFY (abandoned.waitForStarted ()); QVERIFY (abandoned.waitForReadyRead ()); framer.drain (abandoned, handler); QCOMPARE (framer.currentGeneration (), qint32 {11}); abandoned.kill (); QTRY_COMPARE_WITH_TIMEOUT (abandoned.state (), QProcess::NotRunning, 2000); framer.reset (); events.clear (); QByteArray const replacementOutput { " gen=12\n" "replacement record\n" " 0 1 0 gen=12\n"}; QProcess replacement; connect (&replacement, &QProcess::readyReadStandardOutput, this, [&] { framer.drain (replacement, handler); }); replacement.start (QCoreApplication::applicationFilePath (), {fixtureOption, QString::fromLatin1 (replacementOutput.toBase64 ())}); QVERIFY (replacement.waitForStarted ()); QVERIFY (replacement.waitForFinished ()); framer.drain (replacement, handler); QCOMPARE (events.size (), 3); QCOMPARE (events[0].type, DecoderOutputFramer::EventType::Started); QCOMPARE (events[0].generation, qint32 {12}); QCOMPARE (events[1].type, DecoderOutputFramer::EventType::Record); QCOMPARE (events[1].rawLine, QByteArray {"replacement record\n"}); QCOMPARE (events[2].type, DecoderOutputFramer::EventType::Finished); QCOMPARE (events[2].generation, qint32 {12}); } int main (int argc, char ** argv) { QCoreApplication application {argc, argv}; auto const arguments = application.arguments (); if (arguments.contains (fixtureOption)) return runFixture (arguments); TestDecoderOutputFramer test; return QTest::qExec (&test, argc, argv); } #include "test_decoder_output_framer.moc"