diff options
author | Uwe Klotz <uwe_klotz@web.de> | 2017-06-22 07:39:16 +0200 |
---|---|---|
committer | Uwe Klotz <uwe_klotz@web.de> | 2017-06-22 07:40:15 +0200 |
commit | 2ce4adbd09fb7a26f58d11b0a80a1d308486534d (patch) | |
tree | 58ed93706a8f1408f86bb48151e5a6efaed2f435 | |
parent | 4aedce29dc78d4692f06489a95cf479cc7cb27be (diff) |
Test move semantics of DbConnectionPooler
-rw-r--r-- | src/analyzer/analyzerqueue.cpp | 10 | ||||
-rw-r--r-- | src/preferences/upgrade.cpp | 2 | ||||
-rw-r--r-- | src/test/dbconnectionpool_test.cpp | 37 | ||||
-rw-r--r-- | src/util/db/dbconnectionpooler.h | 6 |
4 files changed, 46 insertions, 9 deletions
diff --git a/src/analyzer/analyzerqueue.cpp b/src/analyzer/analyzerqueue.cpp index ceba67a124..26f7da95fc 100644 --- a/src/analyzer/analyzerqueue.cpp +++ b/src/analyzer/analyzerqueue.cpp @@ -311,14 +311,16 @@ void AnalyzerQueue::execThread() { // m_pAnalysisDao remains null if no analyzer needs database access. // Currently only waveform analyses makes use of it. if (m_pAnalysisDao) { - dbConnectionPooler = mixxx::DbConnectionPooler(m_pDbConnectionPool); - if (!dbConnectionPooler) { + dbConnectionPooler = mixxx::DbConnectionPooler(m_pDbConnectionPool); // move assignment + if (!dbConnectionPooler.isPooling()) { kLogger.warning() - << "Failed to open database connection for analyzer queue thread"; + << "Failed to obtain database connection for analyzer queue thread"; return; } // Obtain and use the newly created database connection within this thread - m_pAnalysisDao->initialize(mixxx::DbConnectionPooled(m_pDbConnectionPool)); + QSqlDatabase dbConnection = mixxx::DbConnectionPooled(m_pDbConnectionPool); + DEBUG_ASSERT(dbConnection.isOpen()); + m_pAnalysisDao->initialize(dbConnection); } m_progressInfo.current_track.reset(); diff --git a/src/preferences/upgrade.cpp b/src/preferences/upgrade.cpp index c67d6ab642..cc54f1d703 100644 --- a/src/preferences/upgrade.cpp +++ b/src/preferences/upgrade.cpp @@ -366,7 +366,7 @@ UserSettingsPointer Upgrade::versionUpgrade(const QString& settingsPath) { MixxxDb mixxxDb(config); const mixxx::DbConnectionPooler dbConnectionPooler( mixxxDb.connectionPool()); - if (dbConnectionPooler) { + if (dbConnectionPooler.isPooling()) { QSqlDatabase dbConnection = mixxx::DbConnectionPooled(mixxxDb.connectionPool()); DEBUG_ASSERT(dbConnection.isOpen()); if (MixxxDb::initDatabaseSchema(dbConnection)) { diff --git a/src/test/dbconnectionpool_test.cpp b/src/test/dbconnectionpool_test.cpp new file mode 100644 index 0000000000..cfef128462 --- /dev/null +++ b/src/test/dbconnectionpool_test.cpp @@ -0,0 +1,37 @@ +#include <gtest/gtest.h> + +#include "test/mixxxtest.h" + +#include "database/mixxxdb.h" +#include "util/db/dbconnectionpooler.h" +#include "util/db/dbconnectionpooled.h" + +#include "library/dao/settingsdao.h" + +#include "util/assert.h" + + +class DbConnectionPoolTest : public MixxxTest { + protected: + DbConnectionPoolTest() + : m_mixxxDb(config()) { + } + + protected: + const MixxxDb m_mixxxDb; +}; + +TEST_F(DbConnectionPoolTest, MoveSemantics) { + mixxx::DbConnectionPooler p1(m_mixxxDb.connectionPool()); + EXPECT_TRUE(p1.isPooling()); + + // Move construction + mixxx::DbConnectionPooler p2(std::move(p1)); + EXPECT_FALSE(p1.isPooling()); + EXPECT_TRUE(p2.isPooling()); + + // Move assignment + p1 = std::move(p2); + EXPECT_TRUE(p1.isPooling()); + EXPECT_FALSE(p2.isPooling()); +} diff --git a/src/util/db/dbconnectionpooler.h b/src/util/db/dbconnectionpooler.h index c3229040e0..f03956ce96 100644 --- a/src/util/db/dbconnectionpooler.h +++ b/src/util/db/dbconnectionpooler.h @@ -33,9 +33,8 @@ class DbConnectionPooler final { ~DbConnectionPooler(); // Checks if a thread-local connection has actually been created - // during construction. Otherwise this instance does not store - // any reference to the connection pool and is non-functional. - explicit operator bool() const { + // during construction and is owned by this instance. + bool isPooling() const { return static_cast<bool>(m_pDbConnectionPool); } @@ -51,7 +50,6 @@ class DbConnectionPooler final { #endif private: - // Prevent heap allocation static void * operator new(std::size_t); static void * operator new[](std::size_t); |