summaryrefslogtreecommitdiff
path: root/tests/unit/decoder_ipc
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unit/decoder_ipc')
-rw-r--r--tests/unit/decoder_ipc/CMakeLists.txt42
-rw-r--r--tests/unit/decoder_ipc/decoder_ipc_fortran_probe.f9023
-rw-r--r--tests/unit/decoder_ipc/test_decode_operating_context.cpp152
-rw-r--r--tests/unit/decoder_ipc/test_decoder_ipc_protocol.cpp396
-rw-r--r--tests/unit/decoder_ipc/test_decoder_output_framer.cpp281
5 files changed, 894 insertions, 0 deletions
diff --git a/tests/unit/decoder_ipc/CMakeLists.txt b/tests/unit/decoder_ipc/CMakeLists.txt
new file mode 100644
index 00000000..0d8c0083
--- /dev/null
+++ b/tests/unit/decoder_ipc/CMakeLists.txt
@@ -0,0 +1,42 @@
+if (APPLE)
+ add_library (test_decoder_ipc_fortran_probe STATIC decoder_ipc_fortran_probe.f90)
+ target_include_directories (test_decoder_ipc_fortran_probe PRIVATE ${CMAKE_SOURCE_DIR}/lib)
+ set (decoder_ipc_fortran_probe_sources)
+else ()
+ set (decoder_ipc_fortran_probe_sources decoder_ipc_fortran_probe.f90)
+endif ()
+
+add_executable (test_decoder_ipc_protocol
+ test_decoder_ipc_protocol.cpp
+ ${decoder_ipc_fortran_probe_sources}
+ ${CMAKE_SOURCE_DIR}/DecoderIpc.cpp
+ ${CMAKE_SOURCE_DIR}/lib/decoder_ipc_control.cpp
+)
+set_target_properties (test_decoder_ipc_protocol PROPERTIES LINKER_LANGUAGE CXX)
+target_include_directories (test_decoder_ipc_protocol PRIVATE ${CMAKE_SOURCE_DIR}/lib)
+if (APPLE)
+ target_link_libraries (test_decoder_ipc_protocol Qt5::Test test_decoder_ipc_fortran_probe)
+else ()
+ target_link_libraries (test_decoder_ipc_protocol Qt5::Test)
+endif ()
+add_test (NAME test_decoder_ipc_protocol COMMAND test_decoder_ipc_protocol)
+
+add_executable (test_decoder_output_framer
+ test_decoder_output_framer.cpp
+ ${CMAKE_SOURCE_DIR}/DecoderOutputFramer.cpp
+ ${CMAKE_SOURCE_DIR}/DecoderIpc.cpp
+ ${CMAKE_SOURCE_DIR}/lib/decoder_ipc_control.cpp
+)
+target_include_directories (test_decoder_output_framer PRIVATE
+ ${CMAKE_SOURCE_DIR}
+ ${CMAKE_SOURCE_DIR}/lib
+)
+target_link_libraries (test_decoder_output_framer Qt5::Test)
+add_test (NAME test_decoder_output_framer COMMAND test_decoder_output_framer)
+
+add_executable (test_decode_operating_context
+ test_decode_operating_context.cpp
+)
+target_include_directories (test_decode_operating_context PRIVATE ${CMAKE_SOURCE_DIR})
+target_link_libraries (test_decode_operating_context Qt5::Test)
+add_test (NAME test_decode_operating_context COMMAND test_decode_operating_context)
diff --git a/tests/unit/decoder_ipc/decoder_ipc_fortran_probe.f90 b/tests/unit/decoder_ipc/decoder_ipc_fortran_probe.f90
new file mode 100644
index 00000000..c1304ee7
--- /dev/null
+++ b/tests/unit/decoder_ipc/decoder_ipc_fortran_probe.f90
@@ -0,0 +1,23 @@
+subroutine decoder_ipc_fortran_probe(address, values) bind(C)
+ use, intrinsic :: iso_c_binding, only: c_ptr, c_f_pointer, c_int, c_sizeof
+ include 'jt9com.f90'
+
+ type(c_ptr), value :: address
+ integer(c_int), intent(out) :: values(10)
+ type(shared_dec_data), pointer :: shared
+
+ call c_f_pointer(address, shared)
+ values(1) = c_sizeof(shared%control)
+ values(2) = c_sizeof(shared%payload%params)
+ values(3) = c_sizeof(shared%payload)
+ values(4) = c_sizeof(shared)
+ values(5) = DECODER_IPC_VERSION
+ values(6) = DECODER_IPC_IDLE
+ values(7) = DECODER_IPC_READY
+ values(8) = DECODER_IPC_DECODING
+ values(9) = DECODER_IPC_COMPLETE
+ values(10) = DECODER_IPC_SHUTDOWN
+
+ shared%control%generation = shared%control%generation + 1
+ shared%payload%id2(1) = 1234
+end subroutine decoder_ipc_fortran_probe
diff --git a/tests/unit/decoder_ipc/test_decode_operating_context.cpp b/tests/unit/decoder_ipc/test_decode_operating_context.cpp
new file mode 100644
index 00000000..0380434d
--- /dev/null
+++ b/tests/unit/decoder_ipc/test_decode_operating_context.cpp
@@ -0,0 +1,152 @@
+#include <QtTest>
+
+#include "DecodeOperatingContext.hpp"
+
+class TestDecodeOperatingContext final : public QObject
+{
+ Q_OBJECT
+
+private Q_SLOTS:
+ void identityMatchesEquivalentContext ();
+ void sequenceStartIsAttributionOnly ();
+ void identityChangeMakesContextObsolete_data ();
+ void identityChangeMakesContextObsolete ();
+ void ft8PendingIdentityChangeMakesSnapshotObsolete_data ();
+ void ft8PendingIdentityChangeMakesSnapshotObsolete ();
+ void ft8PendingIdentityRejectsOtherModes ();
+};
+
+namespace
+{
+ DecodeOperatingContext context ()
+ {
+ DecodeOperatingContext result;
+ result.mode = "FT8";
+ result.specOp = SpecialOperatingActivity::NONE;
+ result.periodFrequency = 14074000;
+ result.band = "20m";
+ result.sequenceStart = QDateTime::fromString ("2026-07-26T12:00:00Z", Qt::ISODate);
+ result.trPeriod = 15.0;
+ result.submode = 0;
+ result.diskData = false;
+ result.multithreadFt8 = true;
+ result.ft8DecoderStart = 1;
+ result.ft8ThreadCount = 4;
+ result.decodeDepth = 3;
+ result.ft8Cycles = 3;
+ result.ft8Sensitivity = 3;
+ result.ft8RxFrequencySensitivity = 3;
+ result.decodeLowFrequency = 200;
+ result.decodeHighFrequency = 3000;
+ result.ft8WideDxCallSearch = true;
+ result.hideFt8DuplicateMessages = true;
+ result.ft8ApEnabled = true;
+ result.superFox = false;
+ result.myCall = "K1ABC";
+ return result;
+ }
+}
+
+void TestDecodeOperatingContext::identityMatchesEquivalentContext ()
+{
+ auto const captured = context ();
+ QVERIFY (captured.hasSameDecodeIdentity (context ()));
+}
+
+void TestDecodeOperatingContext::sequenceStartIsAttributionOnly ()
+{
+ auto captured = context ();
+ auto current = context ();
+ current.sequenceStart = current.sequenceStart.addSecs (15);
+
+ QVERIFY (captured.hasSameDecodeIdentity (current));
+}
+
+void TestDecodeOperatingContext::identityChangeMakesContextObsolete_data ()
+{
+ QTest::addColumn<QString> ("field");
+ QTest::newRow ("mode") << QString {"mode"};
+ QTest::newRow ("special operation") << QString {"special operation"};
+ QTest::newRow ("frequency") << QString {"frequency"};
+ QTest::newRow ("band") << QString {"band"};
+ QTest::newRow ("TR period") << QString {"TR period"};
+ QTest::newRow ("submode") << QString {"submode"};
+ QTest::newRow ("disk data") << QString {"disk data"};
+ QTest::newRow ("multithread FT8") << QString {"multithread FT8"};
+ QTest::newRow ("FT8 start") << QString {"FT8 start"};
+ QTest::newRow ("SuperFox") << QString {"SuperFox"};
+ QTest::newRow ("callsign") << QString {"callsign"};
+}
+
+void TestDecodeOperatingContext::identityChangeMakesContextObsolete ()
+{
+ QFETCH (QString, field);
+ auto captured = context ();
+ auto current = context ();
+
+ if (field == "mode") current.mode = "FT4";
+ else if (field == "special operation") current.specOp = SpecialOperatingActivity::HOUND;
+ else if (field == "frequency") ++current.periodFrequency;
+ else if (field == "band") current.band = "17m";
+ else if (field == "TR period") current.trPeriod = 30.0;
+ else if (field == "submode") ++current.submode;
+ else if (field == "disk data") current.diskData = true;
+ else if (field == "multithread FT8") current.multithreadFt8 = false;
+ else if (field == "FT8 start") ++current.ft8DecoderStart;
+ else if (field == "SuperFox") current.superFox = true;
+ else if (field == "callsign") current.myCall = "K1XYZ";
+
+ QVERIFY (!captured.hasSameDecodeIdentity (current));
+}
+
+void TestDecodeOperatingContext::ft8PendingIdentityChangeMakesSnapshotObsolete_data ()
+{
+ QTest::addColumn<QString> ("field");
+ QTest::newRow ("FT8 threads") << QString {"FT8 threads"};
+ QTest::newRow ("decode depth") << QString {"decode depth"};
+ QTest::newRow ("FT8 cycles") << QString {"FT8 cycles"};
+ QTest::newRow ("FT8 sensitivity") << QString {"FT8 sensitivity"};
+ QTest::newRow ("FT8 RX sensitivity") << QString {"FT8 RX sensitivity"};
+ QTest::newRow ("decode low frequency") << QString {"decode low frequency"};
+ QTest::newRow ("decode high frequency") << QString {"decode high frequency"};
+ QTest::newRow ("FT8 wide DX search") << QString {"FT8 wide DX search"};
+ QTest::newRow ("hide FT8 duplicates") << QString {"hide FT8 duplicates"};
+ QTest::newRow ("FT8 AP") << QString {"FT8 AP"};
+}
+
+void TestDecodeOperatingContext::ft8PendingIdentityChangeMakesSnapshotObsolete ()
+{
+ QFETCH (QString, field);
+ auto captured = context ();
+ auto current = context ();
+
+ if (field == "FT8 threads") ++current.ft8ThreadCount;
+ else if (field == "decode depth") ++current.decodeDepth;
+ else if (field == "FT8 cycles") ++current.ft8Cycles;
+ else if (field == "FT8 sensitivity") ++current.ft8Sensitivity;
+ else if (field == "FT8 RX sensitivity") ++current.ft8RxFrequencySensitivity;
+ else if (field == "decode low frequency") ++current.decodeLowFrequency;
+ else if (field == "decode high frequency") ++current.decodeHighFrequency;
+ else if (field == "FT8 wide DX search") current.ft8WideDxCallSearch = false;
+ else if (field == "hide FT8 duplicates") current.hideFt8DuplicateMessages = false;
+ else if (field == "FT8 AP") current.ft8ApEnabled = false;
+
+ QVERIFY (captured.hasSameDecodeIdentity (current));
+ QVERIFY (!captured.hasSameFt8PendingIdentity (current));
+}
+
+void TestDecodeOperatingContext::ft8PendingIdentityRejectsOtherModes ()
+{
+ auto captured = context ();
+ auto current = context ();
+ captured.mode = "Q65";
+ current.mode = "Q65";
+ ++current.ft8ThreadCount;
+
+ QVERIFY (captured.hasSameDecodeIdentity (current));
+ QVERIFY (!captured.hasSameFt8PendingIdentity (current));
+}
+
+QTEST_GUILESS_MAIN (TestDecodeOperatingContext)
+
+#include "test_decode_operating_context.moc"
diff --git a/tests/unit/decoder_ipc/test_decoder_ipc_protocol.cpp b/tests/unit/decoder_ipc/test_decoder_ipc_protocol.cpp
new file mode 100644
index 00000000..85a414fa
--- /dev/null
+++ b/tests/unit/decoder_ipc/test_decoder_ipc_protocol.cpp
@@ -0,0 +1,396 @@
+#include <QtTest>
+
+#include <cstring>
+#include <limits>
+#include <memory>
+#include <thread>
+#include <vector>
+
+#include "DecoderIpc.hpp"
+#include "lib/decoder_ipc_control.h"
+
+extern "C"
+{
+ void decoder_ipc_fortran_probe (void *, int[10]);
+}
+
+class TestDecoderIpcProtocol final : public QObject
+{
+ Q_OBJECT
+
+private Q_SLOTS:
+ void layoutMatchesFortran ();
+ void sharedMemorySizeAllowsPlatformRounding ();
+ void publicationAndClaimValidateControl ();
+ void compactFt8MtdPublicationCopiesRequiredPayload ();
+ void compactFt8MtdPublicationRejectsInvalidRequests ();
+ void transitionsRequireMatchingGeneration ();
+ void sharedProgressTracksActiveGeneration ();
+ void delayedCompletionCannotClobberNextRequest ();
+ void compatibleShutdownReleasesWorkers ();
+ void shutdownReplacesAnyState_data ();
+ void shutdownReplacesAnyState ();
+ void generationWrapsWithoutUsingZero ();
+ void startMarkerParsing ();
+ void startMarkerRejections_data ();
+ void startMarkerRejections ();
+ void completionMarkerParsing ();
+ void completionMarkerRejections_data ();
+ void completionMarkerRejections ();
+};
+
+void TestDecoderIpcProtocol::layoutMatchesFortran ()
+{
+ struct CompatibleSharedData
+ {
+ int ipc[4];
+ dec_data_t payload;
+ };
+
+ QCOMPARE (sizeof (decoder_ipc_control_t), size_t {16});
+ QCOMPARE (offsetof (decoder_ipc_control_t, state), size_t {4});
+ QCOMPARE (offsetof (shared_dec_data_t, payload), size_t {16});
+ QCOMPARE (sizeof (shared_dec_data_t),
+ sizeof (decoder_ipc_control_t) + sizeof (dec_data_t));
+ QCOMPARE (sizeof (shared_dec_data_t), sizeof (CompatibleSharedData));
+ QCOMPARE (offsetof (CompatibleSharedData, ipc) + sizeof (int),
+ offsetof (decoder_ipc_control_t, state));
+ QCOMPARE (offsetof (CompatibleSharedData, payload),
+ offsetof (shared_dec_data_t, payload));
+
+ std::unique_ptr<shared_dec_data_t> shared {new shared_dec_data_t};
+ shared->control.generation = 41;
+ int values[10] {};
+ decoder_ipc_fortran_probe (shared.get (), values);
+
+ QCOMPARE (values[0], static_cast<int> (sizeof (decoder_ipc_control_t)));
+ QCOMPARE (values[1], static_cast<int> (sizeof (decoder_params_t)));
+ QCOMPARE (values[2], static_cast<int> (sizeof (dec_data_t)));
+ QCOMPARE (values[3], static_cast<int> (sizeof (shared_dec_data_t)));
+ QCOMPARE (values[4], int {DECODER_IPC_VERSION});
+ QCOMPARE (values[5], int {DECODER_IPC_IDLE});
+ QCOMPARE (values[6], int {DECODER_IPC_READY});
+ QCOMPARE (values[7], int {DECODER_IPC_DECODING});
+ QCOMPARE (values[8], int {DECODER_IPC_COMPLETE});
+ QCOMPARE (values[9], int {DECODER_IPC_SHUTDOWN});
+ QCOMPARE (shared->control.generation, 42);
+ QCOMPARE (shared->payload.d2[0], short {1234});
+}
+
+void TestDecoderIpcProtocol::sharedMemorySizeAllowsPlatformRounding ()
+{
+ auto const required = static_cast<qint64> (sizeof (shared_dec_data_t));
+ auto const shutdownRequired = static_cast<qint64> (
+ offsetof (decoder_ipc_control_t, progress));
+ QVERIFY (!DecoderIpc::hasShutdownControlSize (shutdownRequired - 1));
+ QVERIFY (DecoderIpc::hasShutdownControlSize (shutdownRequired));
+ QVERIFY (!DecoderIpc::hasUsableSize (required - 1));
+ QVERIFY (DecoderIpc::hasUsableSize (required));
+ QVERIFY (DecoderIpc::hasUsableSize (required + 80));
+
+ int compatibleControl[3] {17, DECODER_IPC_DECODING, 0};
+ DecoderIpc::shutdownControl (compatibleControl);
+ QCOMPARE (compatibleControl[0], 17);
+ QCOMPARE (compatibleControl[1], int {DECODER_IPC_SHUTDOWN});
+ QCOMPARE (compatibleControl[2], 1);
+}
+
+void TestDecoderIpcProtocol::publicationAndClaimValidateControl ()
+{
+ std::unique_ptr<shared_dec_data_t> shared {new shared_dec_data_t};
+ std::unique_ptr<dec_data_t> payload {new dec_data_t {}};
+ DecoderIpc::initialize (*shared);
+
+ QVERIFY (!DecoderIpc::publish (*shared, *payload, false, 0));
+ QCOMPARE (shared->control.state, int {DECODER_IPC_IDLE});
+
+ shared->control.version = DECODER_IPC_VERSION + 1;
+ QVERIFY (!DecoderIpc::publish (*shared, *payload, false, 1));
+ QCOMPARE (shared->control.state, int {DECODER_IPC_IDLE});
+
+ shared->control.version = DECODER_IPC_VERSION;
+ shared->control.generation = 0;
+ shared->control.state = DECODER_IPC_READY;
+ qint32 generation {0};
+ QVERIFY (!DecoderIpc::claim (*shared, generation));
+ QCOMPARE (shared->control.state, int {DECODER_IPC_READY});
+}
+
+void TestDecoderIpcProtocol::compactFt8MtdPublicationCopiesRequiredPayload ()
+{
+ std::unique_ptr<shared_dec_data_t> shared {new shared_dec_data_t};
+ std::unique_ptr<DecoderIpc::Ft8MtdPayload> payload {new DecoderIpc::Ft8MtdPayload};
+ DecoderIpc::initialize (*shared);
+ shared->payload.ss[0] = 3.5F;
+ shared->payload.savg[0] = 4.5F;
+ shared->payload.sred[0] = 5.5F;
+ shared->payload.d2[DecoderIpc::Ft8SampleCount] = 61;
+ shared->payload.d2[NTMAX * RX_SAMPLE_RATE - 1] = 62;
+ payload->params.nmode = 8;
+ payload->params.lmultift8 = true;
+ payload->params.nutc = 123456;
+ payload->samples.front () = 17;
+ payload->samples.back () = 29;
+
+ QVERIFY (DecoderIpc::publishFt8Mtd (*shared, *payload, 7));
+
+ QCOMPARE (shared->control.state, int {DECODER_IPC_READY});
+ QCOMPARE (shared->control.generation, 7);
+ QCOMPARE (shared->payload.params.nutc, 123456);
+ QCOMPARE (shared->payload.d2[0], short {17});
+ QCOMPARE (shared->payload.d2[DecoderIpc::Ft8SampleCount - 1], short {29});
+ QCOMPARE (shared->payload.d2[DecoderIpc::Ft8SampleCount], short {61});
+ QCOMPARE (shared->payload.d2[NTMAX * RX_SAMPLE_RATE - 1], short {62});
+ QCOMPARE (shared->payload.ss[0], 3.5F);
+ QCOMPARE (shared->payload.savg[0], 4.5F);
+ QCOMPARE (shared->payload.sred[0], 5.5F);
+}
+
+void TestDecoderIpcProtocol::compactFt8MtdPublicationRejectsInvalidRequests ()
+{
+ std::unique_ptr<shared_dec_data_t> shared {new shared_dec_data_t};
+ std::unique_ptr<DecoderIpc::Ft8MtdPayload> payload {new DecoderIpc::Ft8MtdPayload};
+ DecoderIpc::initialize (*shared);
+ shared->payload.params.nutc = 111111;
+ shared->payload.d2[0] = 11;
+ payload->params.nmode = 9;
+ payload->params.lmultift8 = true;
+ payload->params.nutc = 222222;
+ payload->samples[0] = 22;
+
+ QVERIFY (!DecoderIpc::publishFt8Mtd (*shared, *payload, 1));
+ QCOMPARE (shared->control.state, int {DECODER_IPC_IDLE});
+ QCOMPARE (shared->payload.params.nutc, 111111);
+ QCOMPARE (shared->payload.d2[0], short {11});
+
+ payload->params.nmode = 8;
+ payload->params.lmultift8 = false;
+ QVERIFY (!DecoderIpc::publishFt8Mtd (*shared, *payload, 1));
+ QCOMPARE (shared->control.state, int {DECODER_IPC_IDLE});
+ QCOMPARE (shared->payload.params.nutc, 111111);
+ QCOMPARE (shared->payload.d2[0], short {11});
+
+ payload->params.lmultift8 = true;
+ QVERIFY (!DecoderIpc::publishFt8Mtd (*shared, *payload, 0));
+ shared->control.version = DECODER_IPC_VERSION + 1;
+ QVERIFY (!DecoderIpc::publishFt8Mtd (*shared, *payload, 1));
+ shared->control.version = DECODER_IPC_VERSION;
+ QVERIFY (DecoderIpc::publishFt8Mtd (*shared, *payload, 1));
+ payload->params.nutc = 333333;
+ payload->samples[0] = 33;
+ QVERIFY (!DecoderIpc::publishFt8Mtd (*shared, *payload, 2));
+ QCOMPARE (shared->payload.params.nutc, 222222);
+ QCOMPARE (shared->payload.d2[0], short {22});
+
+ qint32 generation {0};
+ QVERIFY (DecoderIpc::claim (*shared, generation));
+ QVERIFY (!DecoderIpc::publishFt8Mtd (*shared, *payload, 2));
+ QCOMPARE (shared->payload.params.nutc, 222222);
+ QCOMPARE (shared->payload.d2[0], short {22});
+}
+
+void TestDecoderIpcProtocol::transitionsRequireMatchingGeneration ()
+{
+ std::unique_ptr<shared_dec_data_t> shared {new shared_dec_data_t};
+ std::unique_ptr<dec_data_t> payload {new dec_data_t {}};
+ DecoderIpc::initialize (*shared);
+ payload->params.nutc = 123456;
+ payload->d2[0] = 17;
+
+ QVERIFY (DecoderIpc::publish (*shared, *payload, true, 7));
+ QCOMPARE (shared->control.state, int {DECODER_IPC_READY});
+ QCOMPARE (shared->control.generation, 7);
+ QCOMPARE (shared->payload.params.nutc, 123456);
+ QCOMPARE (shared->payload.d2[0], short {17});
+
+ payload->params.nutc = 654321;
+ payload->d2[0] = 99;
+ QVERIFY (!DecoderIpc::publish (*shared, *payload, true, 8));
+ QCOMPARE (shared->payload.params.nutc, 123456);
+ QCOMPARE (shared->payload.d2[0], short {17});
+
+ qint32 claimed {0};
+ QVERIFY (DecoderIpc::claim (*shared, claimed));
+ QCOMPARE (claimed, qint32 {7});
+ QVERIFY (!DecoderIpc::publish (*shared, *payload, true, 8));
+ QCOMPARE (shared->payload.params.nutc, 123456);
+ QCOMPARE (shared->payload.d2[0], short {17});
+ QVERIFY (!DecoderIpc::consume (*shared, 6));
+ QCOMPARE (shared->control.state, int {DECODER_IPC_DECODING});
+ QVERIFY (!DecoderIpc::consume (*shared, 7));
+ QCOMPARE (shared->control.state, int {DECODER_IPC_DECODING});
+ QVERIFY (DecoderIpc::finish (*shared, 7));
+ QVERIFY (DecoderIpc::consume (*shared, 7));
+ QCOMPARE (shared->control.state, int {DECODER_IPC_IDLE});
+ QVERIFY (!DecoderIpc::consume (*shared, 7));
+
+ QVERIFY (DecoderIpc::publish (*shared, *payload, false, 8));
+ QCOMPARE (shared->payload.params.nutc, 654321);
+ QCOMPARE (shared->control.generation, 8);
+ QCOMPARE (shared->payload.d2[0], short {17});
+ QVERIFY (!DecoderIpc::consume (*shared, 8));
+ DecoderIpc::shutdown (*shared);
+}
+
+void TestDecoderIpcProtocol::sharedProgressTracksActiveGeneration ()
+{
+ std::unique_ptr<shared_dec_data_t> shared {new shared_dec_data_t};
+ std::unique_ptr<dec_data_t> payload {new dec_data_t {}};
+ DecoderIpc::initialize (*shared);
+
+ QVERIFY (DecoderIpc::publish (*shared, *payload, false, 7));
+ qint32 claimed {0};
+ QVERIFY (DecoderIpc::claim (*shared, claimed));
+ QCOMPARE (DecoderIpc::progress (*shared), qint32 {0});
+
+ decoder_ipc_progress_bind (&shared->control.generation,
+ &shared->control.state,
+ &shared->control.version,
+ &shared->control.progress);
+ decoder_ipc_progress_report (6);
+ QCOMPARE (DecoderIpc::progress (*shared), qint32 {0});
+ std::vector<std::thread> workers;
+ for (int worker = 0; worker < 4; ++worker)
+ {
+ workers.emplace_back ([] {
+ for (int report = 0; report < 1000; ++report)
+ {
+ decoder_ipc_progress_report (7);
+ }
+ });
+ }
+ for (auto& worker: workers) worker.join ();
+ QCOMPARE (DecoderIpc::progress (*shared), qint32 {4000});
+
+ QVERIFY (DecoderIpc::finish (*shared, 7));
+ decoder_ipc_progress_report (7);
+ QCOMPARE (DecoderIpc::progress (*shared), qint32 {4000});
+ QVERIFY (DecoderIpc::consume (*shared, 7));
+ QVERIFY (DecoderIpc::publish (*shared, *payload, false, 8));
+ QCOMPARE (DecoderIpc::progress (*shared), qint32 {0});
+
+ decoder_ipc_progress_unbind ();
+ decoder_ipc_progress_report (8);
+ QCOMPARE (DecoderIpc::progress (*shared), qint32 {0});
+}
+
+void TestDecoderIpcProtocol::delayedCompletionCannotClobberNextRequest ()
+{
+ std::unique_ptr<shared_dec_data_t> shared {new shared_dec_data_t};
+ std::unique_ptr<dec_data_t> payload {new dec_data_t};
+ DecoderIpc::initialize (*shared);
+ payload->params = {};
+
+ QVERIFY (DecoderIpc::publish (*shared, *payload, false, 10));
+ qint32 claimed {0};
+ QVERIFY (DecoderIpc::claim (*shared, claimed));
+ QVERIFY (DecoderIpc::finish (*shared, 10));
+ QVERIFY (DecoderIpc::consume (*shared, 10));
+ QVERIFY (DecoderIpc::publish (*shared, *payload, false, 11));
+
+ QVERIFY (!DecoderIpc::finish (*shared, 10));
+ QCOMPARE (shared->control.generation, 11);
+ QCOMPARE (shared->control.state, int {DECODER_IPC_READY});
+
+ DecoderIpc::shutdown (*shared);
+ QCOMPARE (shared->control.state, int {DECODER_IPC_SHUTDOWN});
+}
+
+void TestDecoderIpcProtocol::compatibleShutdownReleasesWorkers ()
+{
+ decoder_ipc_control_t control {17, 0, DECODER_IPC_VERSION, 4};
+ DecoderIpc::shutdown (control);
+
+ QCOMPARE (control.generation, 17);
+ QCOMPARE (control.state, int {DECODER_IPC_SHUTDOWN});
+ QCOMPARE (control.version, 1);
+ QCOMPARE (control.progress, 4);
+}
+
+void TestDecoderIpcProtocol::shutdownReplacesAnyState_data ()
+{
+ QTest::addColumn<int> ("state");
+ QTest::newRow ("idle") << int {DECODER_IPC_IDLE};
+ QTest::newRow ("ready") << int {DECODER_IPC_READY};
+ QTest::newRow ("decoding") << int {DECODER_IPC_DECODING};
+ QTest::newRow ("complete") << int {DECODER_IPC_COMPLETE};
+}
+
+void TestDecoderIpcProtocol::shutdownReplacesAnyState ()
+{
+ QFETCH (int, state);
+ decoder_ipc_control_t control {17, state, DECODER_IPC_VERSION, 0};
+
+ DecoderIpc::shutdown (control);
+
+ QCOMPARE (control.generation, 17);
+ QCOMPARE (control.state, int {DECODER_IPC_SHUTDOWN});
+ QCOMPARE (control.version, 1);
+}
+
+void TestDecoderIpcProtocol::generationWrapsWithoutUsingZero ()
+{
+ QCOMPARE (DecoderIpc::nextGeneration (0), qint32 {1});
+ QCOMPARE (DecoderIpc::nextGeneration (-1), qint32 {1});
+ QCOMPARE (DecoderIpc::nextGeneration (41), qint32 {42});
+ QCOMPARE (DecoderIpc::nextGeneration (std::numeric_limits<qint32>::max ()), qint32 {1});
+}
+
+void TestDecoderIpcProtocol::startMarkerParsing ()
+{
+ qint32 generation {0};
+ QVERIFY (DecoderIpc::parseStart ("<DecodeStarted> gen=42\r\n", &generation));
+ QCOMPARE (generation, qint32 {42});
+}
+
+void TestDecoderIpcProtocol::startMarkerRejections_data ()
+{
+ QTest::addColumn<QByteArray> ("line");
+ QTest::newRow ("embedded") << QByteArray {"noise <DecodeStarted> gen=42"};
+ QTest::newRow ("missing generation") << QByteArray {"<DecodeStarted>"};
+ QTest::newRow ("zero generation") << QByteArray {"<DecodeStarted> gen=0"};
+ QTest::newRow ("negative generation") << QByteArray {"<DecodeStarted> gen=-1"};
+ QTest::newRow ("overflow generation") << QByteArray {"<DecodeStarted> gen=2147483648"};
+ QTest::newRow ("trailing garbage") << QByteArray {"<DecodeStarted> gen=42 extra"};
+}
+
+void TestDecoderIpcProtocol::startMarkerRejections ()
+{
+ QFETCH (QByteArray, line);
+ qint32 generation {0};
+ QVERIFY (!DecoderIpc::parseStart (line, &generation));
+}
+
+void TestDecoderIpcProtocol::completionMarkerParsing ()
+{
+ DecoderIpc::Completion completion {};
+ QVERIFY (DecoderIpc::parseCompletion (
+ "<DecodeFinished> 2 17 123456 gen=42\r\n", &completion));
+ QCOMPARE (completion.synchronized, qint32 {2});
+ QCOMPARE (completion.decoded, qint32 {17});
+ QCOMPARE (completion.average, qint32 {123456});
+ QCOMPARE (completion.generation, qint32 {42});
+}
+
+void TestDecoderIpcProtocol::completionMarkerRejections_data ()
+{
+ QTest::addColumn<QByteArray> ("line");
+ QTest::newRow ("embedded") << QByteArray {"noise <DecodeFinished> 2 17 123456 gen=42"};
+ QTest::newRow ("legacy") << QByteArray {"<DecodeFinished> 2 17 123456"};
+ QTest::newRow ("zero generation") << QByteArray {"<DecodeFinished> 2 17 123456 gen=0"};
+ QTest::newRow ("negative generation") << QByteArray {"<DecodeFinished> 2 17 123456 gen=-1"};
+ QTest::newRow ("overflow generation") << QByteArray {"<DecodeFinished> 2 17 123456 gen=2147483648"};
+ QTest::newRow ("overflow field") << QByteArray {"<DecodeFinished>**** 17 123456 gen=42"};
+ QTest::newRow ("trailing garbage") << QByteArray {"<DecodeFinished> 2 17 123456 gen=42 extra"};
+}
+
+void TestDecoderIpcProtocol::completionMarkerRejections ()
+{
+ QFETCH (QByteArray, line);
+ DecoderIpc::Completion completion {};
+ QVERIFY (!DecoderIpc::parseCompletion (line, &completion));
+}
+
+QTEST_GUILESS_MAIN (TestDecoderIpcProtocol)
+
+#include "test_decoder_ipc_protocol.moc"
diff --git a/tests/unit/decoder_ipc/test_decoder_output_framer.cpp b/tests/unit/decoder_ipc/test_decoder_output_framer.cpp
new file mode 100644
index 00000000..5fbb2698
--- /dev/null
+++ b/tests/unit/decoder_ipc/test_decoder_output_framer.cpp
@@ -0,0 +1,281 @@
+#include <QtTest>
+
+#include <QCoreApplication>
+#include <QBuffer>
+#include <QFile>
+#include <QProcess>
+#include <QVector>
+
+#include <cstdio>
+#include <memory>
+
+#ifdef Q_OS_WIN
+#include <fcntl.h>
+#include <io.h>
+#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 {
+ "<DecodeStarted> gen=7\n"
+ "record rejected by policy\n"
+ "<DecodeFinished> 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 {
+ "<DecodeStarted> gen=7\n"
+ "stale record\n"
+ "<DecodeFinished> 0 0 0 gen=7\n"
+ "<DecodeStarted> gen=8\n"
+ "current record\n"
+ "<DecodeFinished> 0 1 0 gen=8\n"};
+ DecoderOutputFramer framer;
+ QVector<qint32> recordGenerations;
+ QVector<QByteArray> acceptedRecords;
+ QVector<qint32> 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<qint32> ({7, 8}));
+ QCOMPARE (acceptedRecords, QVector<QByteArray> ({"current record\n"}));
+ QCOMPARE (finishedGenerations, QVector<qint32> ({7, 8}));
+}
+
+void TestDecoderOutputFramer::completeStateWaitsForStdoutDrain ()
+{
+ std::unique_ptr<shared_dec_data_t> shared {new shared_dec_data_t};
+ std::unique_ptr<dec_data_t> 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 {
+ "<DecodeStarted> gen=21\n"
+ "buffered record\n"
+ "<DecodeFinished> 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 {
+ "<DecodeStarted> gen=9\r\n"
+ "first record\r\n"
+ "second record\n"
+ "<DecodeFinished> 0 2 0 gen=9\r\n"};
+ DecoderOutputFramer framer;
+ QVector<QByteArray> 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<QByteArray> ({"first record\r\n",
+ "second record\n"}));
+}
+
+void TestDecoderOutputFramer::malformedFramesAreReported ()
+{
+ QByteArray output {
+ "unframed record\n"
+ "<DecodeStarted> gen=0\n"
+ "<DecodeStarted> gen=14\n"
+ "<DecodeFinished> 0 0 0 gen=15\n"
+ "<DecodeFinished> 0 0 0 gen=14\n"};
+ QBuffer buffer {&output};
+ QVERIFY (buffer.open (QIODevice::ReadOnly));
+
+ DecoderOutputFramer framer;
+ QVector<DecoderOutputFramer::EventType> eventTypes;
+ framer.drain (buffer, [&] (DecoderOutputFramer::Event const& event) {
+ eventTypes.append (event.type);
+ });
+
+ QCOMPARE (eventTypes, QVector<DecoderOutputFramer::EventType> ({
+ 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<DecoderOutputFramer::Event> 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 {
+ "<DecodeStarted> 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 {
+ "<DecodeStarted> gen=12\n"
+ "replacement record\n"
+ "<DecodeFinished> 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"