/** * @RDE_HEADER@ * AvsCommandQueueTest * $Id$ * $HeadURL$ * $Author$ * Created by Christian Fröbel, NLSE, 2018-02-28 */ #include "avs_command_queue_test.h" #include #include #include #include #include using namespace AudioVideoManagement; namespace { class ExecutionSpy { public: void executedA(int value) { ++m_execCountA; m_lastValueA = value; } void executedB(int value) { ++m_execCountB; m_lastValueB = value; } int m_execCountA{0}; int m_lastValueA{0}; int m_execCountB{0}; int m_lastValueB{0}; }; class CommandA : public ICommand { public: CommandA(ExecutionSpy *spy, int value) : m_spy(spy) , m_value(value) { Q_ASSERT(spy); } void send() override { m_spy->executedA(m_value); } QString debug() const override { return QLatin1String("CommandA"); } bool nullifies(ICommand *other) const override { Q_ASSERT(other); CommandA *other_ = dynamic_cast(other); return other_ != nullptr; } private: ExecutionSpy *m_spy; int m_value; }; class CommandB : public ICommand { public: CommandB(ExecutionSpy *spy, int value) : m_spy(spy) , m_value(value) { Q_ASSERT(spy); } void send() override { m_spy->executedB(m_value); } QString debug() const override { return QLatin1String("CommandB"); } bool nullifies(ICommand *other) const override { Q_ASSERT(other); CommandB *other_ = dynamic_cast(other); return other_ != nullptr; } private: ExecutionSpy *m_spy; int m_value; }; } // anonymous namespace void AvsCommandQueueTest::testReduceNeverDropCmdInExecution() { ExecutionSpy spy; std::unique_ptr cmd1(new CommandA(&spy, 10)); std::unique_ptr cmd2(new CommandA(&spy, 20)); AvsCommandQueue queue(std::chrono::seconds(3)); queue.queue(std::move(cmd1), nullptr); queue.queue(std::move(cmd2), nullptr); // Does not override cmd1 as cmd1 is being executed already and thus cannot be dropped anymore. QCOMPARE(spy.m_execCountA, 1); QCOMPARE(spy.m_lastValueA, 10); queue.done(); QCOMPARE(spy.m_execCountA, 2); QCOMPARE(spy.m_lastValueA, 20); queue.done(); QVERIFY(queue.isEmpty()); } void AvsCommandQueueTest::testReduce1() { ExecutionSpy spy; std::unique_ptr cmd1(new CommandA(&spy, 10)); std::unique_ptr cmd2(new CommandA(&spy, 20)); std::unique_ptr cmd3(new CommandA(&spy, 30)); AvsCommandQueue queue(std::chrono::seconds(3)); queue.queue(std::move(cmd1), nullptr); queue.queue(std::move(cmd2), nullptr); queue.queue(std::move(cmd3), nullptr); // causes cmd2 to be dropped // Only two commands complete as one was dropped. queue.done(); queue.done(); QVERIFY(queue.isEmpty()); QCOMPARE(spy.m_execCountA, 2); QCOMPARE(spy.m_lastValueA, 30); } void AvsCommandQueueTest::testReduce2() { ExecutionSpy spy; std::unique_ptr cmdA1(new CommandA(&spy, 10)); std::unique_ptr cmdA2(new CommandA(&spy, 20)); std::unique_ptr cmdA3(new CommandA(&spy, 30)); std::unique_ptr cmdB1(new CommandB(&spy, 10)); std::unique_ptr cmdB2(new CommandB(&spy, 20)); std::unique_ptr cmdB3(new CommandB(&spy, 30)); AvsCommandQueue queue(std::chrono::seconds(3)); queue.queue(std::move(cmdA1), nullptr); queue.queue(std::move(cmdB1), nullptr); queue.queue(std::move(cmdA2), nullptr); // cmdA1 not dropped as it is being executed already queue.queue(std::move(cmdB2), nullptr); // causes cmdB1 to be dropped queue.queue(std::move(cmdA3), nullptr); // causes cmdA2 to be dropped queue.queue(std::move(cmdB3), nullptr); // causes cmdB2 to be dropped // Only three commands complete as one was dropped. queue.done(); queue.done(); queue.done(); QVERIFY(queue.isEmpty()); QCOMPARE(spy.m_execCountA, 2); QCOMPARE(spy.m_lastValueA, 30); QCOMPARE(spy.m_execCountB, 1); QCOMPARE(spy.m_lastValueB, 30); }