test: allow an exitTest to work in Kit and propagate its result.

Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
Change-Id: I0d32d46e81eb3ed42d8531860ef2d8e06bdca591
This commit is contained in:
Michael Meeks 2024-04-27 11:33:39 +01:00 committed by Miklos Vajna
parent a866719881
commit 12310c7dec
5 changed files with 63 additions and 3 deletions

View file

@ -67,6 +67,7 @@ UnitBase** UnitBase::linkAndCreateUnit([[maybe_unused]] UnitType type,
// avoid std:string de-allocation during failure / exit.
UnitLibPath = strdup(unitLibPath.c_str());
TST_LOG_NAME("UnitBase", "Opened unit-test lib " << UnitLibPath);
const char *symbol = nullptr;
switch (type)
@ -119,6 +120,7 @@ UnitBase** UnitBase::linkAndCreateUnit([[maybe_unused]] UnitType type,
LOG_ERR("No " << symbol << " symbol in " << unitLibPath);
return nullptr;
}
TST_LOG_NAME("UnitBase", "Hooked symbol " << symbol << " from unit-test lib " << UnitLibPath);
UnitBase* hooks = createHooks();
if (hooks)
@ -531,11 +533,39 @@ void UnitBase::exitTest(TestResult result, const std::string& reason)
}
_result = result;
endTest(reason);
_reason = reason;
_setRetValue = true;
// Notify inheritors.
onExitTest(result, reason);
// the kit needs to send a 'unitresult:' message to wsd to exit there.
if (_type == UnitType::Kit)
SocketPoll::wakeupWorld();
else // otherwise exit.
{
endTest(reason);
// Notify inheritors.
onExitTest(result, reason);
}
}
std::string UnitKit::getResultMessage() const
{
assert(isFinished());
return std::string("unitresult: ") +
toStringShort(_result) + " " + _reason;
}
void UnitWSD::processUnitResult(const StringVector &tokens)
{
UnitBase::TestResult result = UnitBase::TestResult::TimedOut;
TST_LOG("Received " << tokens[0] << " from kit:" << tokens[1] << " " << tokens[2]);
assert (tokens[0] == "unitresult:");
if (tokens[1] == "Ok")
result = UnitBase::TestResult::Ok;
else if (tokens[1] == "Failed")
result = UnitBase::TestResult::Failed;
exitTest(result, tokens[2]);
}
void UnitBase::timeout()

View file

@ -326,8 +326,11 @@ private:
static TestOptions GlobalTestOptions; //< The test options for this Test Suite.
static TestResult GlobalResult; //< The result of all tests. Latches at first failure.
/// Did we set the result of the test yet ?
bool _setRetValue;
TestResult _result;
std::string _reason;
std::chrono::milliseconds _timeoutMilliSeconds;
/// The time at which this particular test started, relative to the start of the Test Suite.
std::chrono::milliseconds _startTimeMilliSeconds;
@ -407,6 +410,9 @@ public:
}
}
/// Process result message from kit
void processUnitResult(const StringVector &tokens);
/// When a new child kit process reports
virtual void newChild(const std::shared_ptr<ChildProcess>& /*child*/) {}
@ -529,6 +535,9 @@ public:
// ---------------- Kit hooks ----------------
/// Build message with test result to send from kit -> wsd
std::string getResultMessage() const;
/// Post fork hook - just before we init the child kit
virtual void postFork();

View file

@ -722,6 +722,7 @@ int forkit_main(int argc, char** argv)
return EX_USAGE;
}
LOG_ERR("About to init Kit UnitBase with " << UnitTestLibrary);
if (!Util::isKitInProcess() && !UnitBase::init(UnitBase::UnitType::Kit, UnitTestLibrary))
{
LOG_FTL("Failed to load kit unit test library");

View file

@ -2527,6 +2527,20 @@ int KitSocketPoll::kitPoll(int timeoutMicroS)
}
#if ENABLE_DEBUG
#if !MOBILEAPP
auto &unitKit = UnitKit::get();
if (unitKit.isFinished())
{
static bool sentResult = false;
if (!sentResult && singletonDocument)
{
LOG_TRC("Sending unit test result");
singletonDocument->sendTextFrame(unitKit.getResultMessage());
sentResult = true;
}
}
#endif
static std::atomic<int> reentries = 0;
static int lastWarned = 1;
ReEntrancyGuard guard(reentries);

View file

@ -3249,6 +3249,12 @@ bool DocumentBroker::handleInput(const std::shared_ptr<Message>& message)
message->size() - firstLine.size() - 1);
}
}
#if ENABLE_DEBUG
else if (message->firstTokenMatches("unitresult:"))
{
UnitWSD::get().processUnitResult(message->tokens());
}
#endif
else
{
LOG_ERR("Unexpected message: [" << message->abbr() << "].");