Skip to content

Commit

Permalink
Fix flaky tst_QXmppAccountMigrationManager tests
Browse files Browse the repository at this point in the history
The QXmppExportData class store data in an unordered_map and would then
serialize each extensions in a random order, which break string based
unit tests.
We then for now try to always serialize in the std::type_index sorting
order, unfortunately, its internals are compiler specific and may
(or not) break with others compilers.
  • Loading branch information
pasnox committed Oct 12, 2024
1 parent 12c6fe1 commit 9c7f0fd
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/client/QXmppAccountMigrationManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,28 @@ std::variant<QXmppExportData, QXmppError> QXmppExportData::fromDom(const QDomEle

void QXmppExportData::toXml(QXmlStreamWriter *writer) const
{
// We need to generate the xml file with nodes always in the same order.
// This is needed for our unit tests which are based on xml generation.
const auto sortedExtensionsKeys = [this]() {
const auto key_selector = [](auto pair) { return pair.first; };
std::vector<std::type_index> keys(d->extensions.size(), std::type_index(typeid(std::nullptr_t)));
std::transform(d->extensions.begin(), d->extensions.end(), keys.begin(), key_selector);
std::stable_sort(keys.begin(), keys.end());
return keys;
}();

writer->writeStartDocument();
writer->writeStartElement(QSL65("account-data"));
writer->writeDefaultNamespace(toString65(ns_qxmpp_export));
writer->writeAttribute(QSL65("jid"), d->accountJid);

const auto &serializers = accountDataSerializers();
for (const auto &[typeIndex, extension] : std::as_const(d->extensions)) {
for (const auto &typeIndex: sortedExtensionsKeys) {
const auto serializer = serializers.find(typeIndex);
if (serializer != serializers.end()) {
const auto &[_, serialize] = *serializer;

serialize(extension, *writer);
serialize(d->extensions.at(typeIndex), *writer);
}
}

Expand Down

0 comments on commit 9c7f0fd

Please sign in to comment.