Skip to content

Commit

Permalink
rework tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jkarneges committed Feb 25, 2025
1 parent 4bd3fb0 commit d2ff573
Showing 1 changed file with 57 additions and 29 deletions.
86 changes: 57 additions & 29 deletions src/core/defercalltest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,48 +23,72 @@
#include <QtTest/QtTest>
#include "timer.h"
#include "defercall.h"
#include "eventloop.h"

class DeferCallTest : public QObject
{
Q_OBJECT

private slots:
void initTestCase()
private:
// loop_advance should process enough events to cause the calls to run,
// without sleeping, in order to prove the calls are run immediately
int runDeferCall(std::function<void ()> loop_advance)
{
Timer::init(100);
}
int count = 0;

void cleanupTestCase()
{
DeferCall::cleanup();
Timer::deinit();
DeferCall::global()->defer([&] {
++count;

DeferCall::global()->defer([&] {
++count;
});
});

loop_advance();

return count;
}

private slots:
void deferCall()
{
bool first = false;
bool second = false;
EventLoop loop(1);

DeferCall::global()->defer([&] {
first = true;
int count = runDeferCall([&] {
// run the first call and queue the second
loop.step();

DeferCall::global()->defer([&] {
second = true;
});
// run the second
loop.step();
});

// the second deferred call should cause the underlying timer to
// re-arm. since we aren't contending with other timers within this
// test, both timeouts should get processed during a single timer
// processing pass. therefore, both calls should get processed within
// a single eventloop pass
QTest::qWait(10);
QVERIFY(first);
QVERIFY(second);
QCOMPARE(count, 2);

DeferCall::cleanup();
}

void deferCallQt()
{
Timer::init(1);

int count = runDeferCall([] {
// the underlying timer's qt-based implementation will process
// both timeouts during a single timer processing pass.
// therefore, both calls should run within a single event loop
// pass
QCoreApplication::processEvents(QEventLoop::AllEvents);
});

QCOMPARE(count, 2);

DeferCall::cleanup();
Timer::deinit();
}

void retract()
{
Timer::init(1);

bool called = false;

{
Expand All @@ -77,26 +101,30 @@ private slots:

DeferCall::cleanup();
QVERIFY(!called);

Timer::deinit();
}

void managerCleanup()
{
bool first = false;
bool second = false;
Timer::init(1);

int count = 0;

DeferCall::global()->defer([&] {
first = true;
++count;

DeferCall::global()->defer([&] {
second = true;
++count;
});
});

// cleanup should process deferred calls queued so far as well as
// those queued during processing
DeferCall::cleanup();
QVERIFY(first);
QVERIFY(second);
QCOMPARE(count, 2);

Timer::deinit();
}
};

Expand Down

0 comments on commit d2ff573

Please sign in to comment.