Currently, my unittest uses a QTest::qWait(ms) to give enough time for a DBUS signal to be caught and the slot to execute in order to satisfy the EXPECT_CALL test. I want to remove the wait, and make GMock EXPECT_CALL, using std::condition_variable and std::mutex, to wait for the call.
Using the info found here: Expecting googlemock calls from another thread I ended up with the following code;
std::condition_variable cv;
std::mutex mu;
bool ready{false};
BatteryLevelEvent batteryFlatEvt(EventType::EVT_BATTERY_FLAT, 12,
"MockEvent");
// setting up the expected call on the DB EXPECT_CALL(mock_db_, Insert(batteryFlatEvt.ToString()))
.Times(1)
.WillOnce(InvokeWithoutArgs([&]() {
std::lock_guard<std::mutex> lock(mu);
ready = true;
cv.notify_one();
}));
// send DBUS message to trigger the Insert(...) call on mock_db_
connection.send(batteryLevelEventSignal);
// QTest::qWait(1000); //<-- test passes with this!
// test fails after 5 seconds
std::unique_lock<std::mutex> lock(mu);
EXPECT_TRUE(
cv.wait_for(lock, std::chrono::seconds(5), [&ready] { return ready; }));
lock.unlock();
Mock::VerifyAndClearExpectations(&mock_db_);
The signal is definitely being sent as I can see it on dbus-monitor
Thanks heaps for your help.
Using;
-
C++11,
-
Gtest Master Branch,
Results...
Actual function call count doesn't match EXPECT_CALL(mock_db_, Insert(batteryFlatEvt.ToString()))...
Expected: to be called once
Actual: never called - unsatisfied and active
[ FAILED ] EventServiceTester.TestReceivingEventSignals (5002 ms)
using QTest:qWait(ms)
[ OK ] EventServiceTester.TestReceivingEventSignals (1002 ms)
Aucun commentaire:
Enregistrer un commentaire