refs #679 CValueCache local signal relay moved to the point of emission, to tolerate different orders of initialization of application.

This commit is contained in:
Mathew Sutcliffe
2016-06-30 02:06:16 +01:00
parent 8b73da6ed6
commit 242e041ceb
6 changed files with 18 additions and 46 deletions

View File

@@ -207,15 +207,6 @@ namespace BlackCore
bool s = this->startHookIn(); bool s = this->startHookIn();
// enable local relay of settings changes in case there is no context
if (!this->supportsContexts())
{
connect(CSettingsCache::instance(), &CSettingsCache::valuesChangedByLocal, CSettingsCache::instance(), [](const CValueCachePacket & values)
{
CSettingsCache::instance()->changeValuesFromRemote(values, CIdentifier());
});
}
// trigger loading and saving of settings in appropriate scenarios // trigger loading and saving of settings in appropriate scenarios
if (this->m_coreFacadeConfig.getModeApplication() != CCoreFacadeConfig::Remote) if (this->m_coreFacadeConfig.getModeApplication() != CCoreFacadeConfig::Remote)
{ {

View File

@@ -68,8 +68,7 @@ namespace BlackMisc
CDataCacheRevision *m_rev = nullptr; CDataCacheRevision *m_rev = nullptr;
}; };
CDataCache::CDataCache() : CDataCache::CDataCache()
CValueCache(CValueCache::Distributed)
{ {
if (! QDir::root().mkpath(persistentStore())) if (! QDir::root().mkpath(persistentStore()))
{ {

View File

@@ -15,8 +15,7 @@
namespace BlackMisc namespace BlackMisc
{ {
CSettingsCache::CSettingsCache() : CSettingsCache::CSettingsCache()
CValueCache(CValueCache::Distributed)
{} {}
CSettingsCache *CSettingsCache::instance() CSettingsCache *CSettingsCache::instance()

View File

@@ -136,18 +136,8 @@ namespace BlackMisc
return cats; return cats;
} }
CValueCache::CValueCache(CValueCache::DistributionMode mode, QObject *parent) : CValueCache::CValueCache(QObject *parent) : QObject(parent)
QObject(parent) {}
{
if (mode == LocalOnly)
{
// loopback signal to own slot for local operation
connect(this, &CValueCache::valuesChangedByLocal, this, [ = ](const CValueCachePacket & values)
{
changeValuesFromRemote(values, CIdentifier());
});
}
}
struct CValueCache::Element struct CValueCache::Element
{ {
@@ -242,8 +232,10 @@ namespace BlackMisc
if (values.valuesChanged()) { emit valuesChanged(values, sender()); } if (values.valuesChanged()) { emit valuesChanged(values, sender()); }
emit valuesChangedByLocal(values); emit valuesChangedByLocal(values);
Q_ASSERT_X(isSignalConnected(QMetaMethod::fromSignal(&CValueCache::valuesChangedByLocal)), Q_FUNC_INFO, if (! isSignalConnected(QMetaMethod::fromSignal(&CValueCache::valuesChangedByLocal)))
"signal must be connected for cache to function properly"); {
changeValuesFromRemote(values, CIdentifier());
}
} }
void CValueCache::changeValuesFromRemote(const CValueCachePacket &values, const CIdentifier &originator) void CValueCache::changeValuesFromRemote(const CValueCachePacket &values, const CIdentifier &originator)

View File

@@ -147,20 +147,11 @@ namespace BlackMisc
public: public:
class BatchGuard; class BatchGuard;
//! Whether or not the cache can be distributed among multiple processes.
enum DistributionMode
{
LocalOnly, //!< Not distributed.
Distributed //!< Distributed among multiple processes.
};
//! Log categories //! Log categories
static const CLogCategoryList &getLogCategories(); static const CLogCategoryList &getLogCategories();
//! Constructor. //! Constructor.
//! \param mode Whether or not the cache can be distributed among multiple processes. explicit CValueCache(QObject *parent = nullptr);
//! \param parent The parent of the QObject.
explicit CValueCache(DistributionMode mode, QObject *parent = nullptr);
//! Return map containing all values in the cache. //! Return map containing all values in the cache.
//! If prefix is provided then only those values whose keys start with that prefix. //! If prefix is provided then only those values whose keys start with that prefix.

View File

@@ -77,7 +77,7 @@ namespace BlackMiscTest
{ "value4", CVariant::from(4) } { "value4", CVariant::from(4) }
}; };
CValueCache cache(CValueCache::LocalOnly); CValueCache cache;
QVERIFY(cache.getAllValues() == CVariantMap()); QVERIFY(cache.getAllValues() == CVariantMap());
cache.insertValues({ testData, QDateTime::currentMSecsSinceEpoch() }); cache.insertValues({ testData, QDateTime::currentMSecsSinceEpoch() });
QVERIFY(cache.getAllValues() == testData); QVERIFY(cache.getAllValues() == testData);
@@ -131,7 +131,7 @@ namespace BlackMiscTest
void CTestValueCache::localOnly() void CTestValueCache::localOnly()
{ {
CValueCache cache(CValueCache::LocalOnly); CValueCache cache;
for (int i = 0; i < 4; ++i) { QTest::ignoreMessage(QtDebugMsg, QRegularExpression("Empty cache value")); } for (int i = 0; i < 4; ++i) { QTest::ignoreMessage(QtDebugMsg, QRegularExpression("Empty cache value")); }
CValueCacheUser user1(&cache); CValueCacheUser user1(&cache);
CValueCacheUser user2(&cache); CValueCacheUser user2(&cache);
@@ -140,7 +140,7 @@ namespace BlackMiscTest
void CTestValueCache::localOnlyWithThreads() void CTestValueCache::localOnlyWithThreads()
{ {
CValueCache cache(CValueCache::LocalOnly); CValueCache cache;
for (int i = 0; i < 4; ++i) { QTest::ignoreMessage(QtDebugMsg, QRegularExpression("Empty cache value")); } for (int i = 0; i < 4; ++i) { QTest::ignoreMessage(QtDebugMsg, QRegularExpression("Empty cache value")); }
CValueCacheUser user1(&cache); CValueCacheUser user1(&cache);
CValueCacheUser user2(&cache); CValueCacheUser user2(&cache);
@@ -158,8 +158,8 @@ namespace BlackMiscTest
json.insert("processId", otherProcess.getProcessId() + 1); json.insert("processId", otherProcess.getProcessId() + 1);
otherProcess.convertFromJson(json); otherProcess.convertFromJson(json);
CValueCache thisCache(CValueCache::Distributed); CValueCache thisCache;
CValueCache otherCache(CValueCache::Distributed); CValueCache otherCache;
connect(&thisCache, &CValueCache::valuesChangedByLocal, &thisCache, [ & ](const CValueCachePacket &values) connect(&thisCache, &CValueCache::valuesChangedByLocal, &thisCache, [ & ](const CValueCachePacket &values)
{ {
QMetaObject::invokeMethod(&thisCache, "changeValuesFromRemote", Q_ARG(BlackMisc::CValueCachePacket, values), Q_ARG(BlackMisc::CIdentifier, thisProcess)); QMetaObject::invokeMethod(&thisCache, "changeValuesFromRemote", Q_ARG(BlackMisc::CValueCachePacket, values), Q_ARG(BlackMisc::CIdentifier, thisProcess));
@@ -191,7 +191,7 @@ namespace BlackMiscTest
void CTestValueCache::batched() void CTestValueCache::batched()
{ {
CValueCache cache(CValueCache::LocalOnly); CValueCache cache;
for (int i = 0; i < 4; ++i) { QTest::ignoreMessage(QtDebugMsg, QRegularExpression("Empty cache value")); } for (int i = 0; i < 4; ++i) { QTest::ignoreMessage(QtDebugMsg, QRegularExpression("Empty cache value")); }
CValueCacheUser user1(&cache); CValueCacheUser user1(&cache);
CValueCacheUser user2(&cache); CValueCacheUser user2(&cache);
@@ -225,7 +225,7 @@ namespace BlackMiscTest
{ "value3", CVariant::from(3) } { "value3", CVariant::from(3) }
}; };
CValueCache cache(CValueCache::LocalOnly); CValueCache cache;
cache.loadFromJson(testJson); cache.loadFromJson(testJson);
QVERIFY(cache.getAllValues() == testData); QVERIFY(cache.getAllValues() == testData);
QVERIFY(cache.saveToJson() == testJson); QVERIFY(cache.saveToJson() == testJson);
@@ -243,7 +243,7 @@ namespace BlackMiscTest
{ "namespace2/aircraft", CVariant::from(aircraft) }, { "namespace2/aircraft", CVariant::from(aircraft) },
{ "namespace2/atcstations", CVariant::from(atcStations) } { "namespace2/atcstations", CVariant::from(atcStations) }
}; };
CValueCache cache(CValueCache::LocalOnly); CValueCache cache;
cache.insertValues({ testData, QDateTime::currentMSecsSinceEpoch() }); cache.insertValues({ testData, QDateTime::currentMSecsSinceEpoch() });
QDir dir(QDir::currentPath() + "/testcache"); QDir dir(QDir::currentPath() + "/testcache");
@@ -257,7 +257,7 @@ namespace BlackMiscTest
QCOMPARE(files[0].fileName(), QString("namespace1.json")); QCOMPARE(files[0].fileName(), QString("namespace1.json"));
QCOMPARE(files[1].fileName(), QString("namespace2.json")); QCOMPARE(files[1].fileName(), QString("namespace2.json"));
CValueCache cache2(CValueCache::LocalOnly); CValueCache cache2;
status = cache2.loadFromFiles(dir.absolutePath()); status = cache2.loadFromFiles(dir.absolutePath());
QVERIFY(status.isSuccess()); QVERIFY(status.isSuccess());
QCOMPARE(cache2.getAllValues(), testData); QCOMPARE(cache2.getAllValues(), testData);