From 1aa842ad5a3efd7c1d821d0d0bb9c8e07de053b4 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Thu, 14 Sep 2017 17:24:25 +0200 Subject: [PATCH] Ref T150, Ref T156 unit test for connectivity, watchdog etc. --- tests/blackcore/main.cpp | 11 +++- tests/blackcore/testblackcoremain.cpp | 5 ++ tests/blackcore/testconnectivity.cpp | 94 +++++++++++++++++++++++++++ tests/blackcore/testconnectivity.h | 63 ++++++++++++++++++ 4 files changed, 170 insertions(+), 3 deletions(-) create mode 100644 tests/blackcore/testconnectivity.cpp create mode 100644 tests/blackcore/testconnectivity.h diff --git a/tests/blackcore/main.cpp b/tests/blackcore/main.cpp index a469e533a..0715dbc68 100644 --- a/tests/blackcore/main.cpp +++ b/tests/blackcore/main.cpp @@ -17,8 +17,9 @@ #include "testblackcoremain.h" #include "blackcore/application.h" #include -#include +#include +using namespace BlackCore; using namespace BlackCoreTest; //! Starter for test cases @@ -26,14 +27,18 @@ int main(int argc, char *argv[]) { QCoreApplication qa(argc, argv); Q_UNUSED(qa); - BlackCore::CApplication a; + CApplication a; a.addVatlibOptions(); + const bool setup = a.parseAndSynchronizeSetup(); + if (!setup) { qWarning() << "No setup loaded"; } if (!a.start()) { a.gracefulShutdown(); return EXIT_FAILURE; } - return CBlackCoreTestMain::unitMain(argc, argv); + const int r = CBlackCoreTestMain::unitMain(argc, argv); + a.gracefulShutdown(); + return r; } //! \endcond diff --git a/tests/blackcore/testblackcoremain.cpp b/tests/blackcore/testblackcoremain.cpp index 11ac57cb7..f2c12126b 100644 --- a/tests/blackcore/testblackcoremain.cpp +++ b/tests/blackcore/testblackcoremain.cpp @@ -18,6 +18,7 @@ #include "testnetwork.h" #include "testreaders.h" #include "testcontext.h" +#include "testconnectivity.h" #include "blackmisc/test/test.h" #include #include @@ -28,6 +29,10 @@ namespace BlackCoreTest { BlackMisc::Test::CTest test(argc, argv); int status = 0; + { + CTestConnectivity connectivityTest; + status |= test.exec(&connectivityTest, "blackcore_network"); + } { CTestContext contextTests; status |= test.exec(&contextTests, "blackcore_context"); diff --git a/tests/blackcore/testconnectivity.cpp b/tests/blackcore/testconnectivity.cpp new file mode 100644 index 000000000..d075c869b --- /dev/null +++ b/tests/blackcore/testconnectivity.cpp @@ -0,0 +1,94 @@ +/* Copyright (C) 2017 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \cond PRIVATE_TESTS +//! \file +//! \ingroup testblackcore + +#include "testconnectivity.h" +#include "blackcore/application.h" +#include +#include +#include +#include +#include + +using namespace BlackCore; +using namespace BlackMisc::Network; + +namespace BlackCoreTest +{ + CTestConnectivity::CTestConnectivity(QObject *parent) : + QObject(parent) + { } + + CTestConnectivity::~CTestConnectivity() + { } + + void CTestConnectivity::initTestCase() + { + QVERIFY2(sApp, "sApp not available"); + QVERIFY2(sApp->getNetworkWatchdog(), "No network watchdog"); + + const int n = sApp->triggerNetworkChecks(); + QVERIFY2(n >= 0, "Cannot trigger setup reader"); + m_networkCheckCount = n; + qDebug() << "Initial network check count:" << n; + } + + void CTestConnectivity::checkSetupReader() + { + if (!sApp->hasSetupReader()) { QSKIP("Cannot load bootstrap file, skip unit test"); } + } + + void CTestConnectivity::connectServer() + { + if (!sApp->hasSetupReader()) { QSKIP("Cannot load bootstrap file, skip unit test"); } + const CUrl url = sApp->getGlobalSetup().getDbHomePageUrl(); + QTime timer; + timer.start(); + constexpr int max = 5; + for (int i = 0; i < max; i++) + { + bool ok = CNetworkUtils::canConnect(url, CNetworkUtils::getLongTimeoutMs()); + if (!ok) { QSKIP(qPrintable("Cannot connect " + url.getFullUrl())); } + } + int elapsedMs = timer.elapsed(); + qDebug() << "Completed" << max << "connection tests in" << elapsedMs << "ms to" << url.getFullUrl(); + QVERIFY2(true, "connectServer"); + } + + void CTestConnectivity::pingServer() + { + if (!sApp->hasSetupReader()) { QSKIP("Cannot load bootstrap file, skip unit test"); } + const CUrl url = sApp->getGlobalSetup().getDbHomePageUrl(); + const QString host(url.getHost()); + QTime timer; + timer.start(); + constexpr int max = 5; + for (int i = 0; i < max; i++) + { + bool ok = CNetworkUtils::canPing(host); + if (!ok) { QSKIP(qPrintable("Cannot ping " + url.getFullUrl())); } + } + int elapsedMs = timer.elapsed(); + qDebug() << "Completed" << max << "ping tests in" << elapsedMs << "ms to" << url.getFullUrl(); + QVERIFY2(true, "pingServer"); + } + + void CTestConnectivity::testNetworkWatchdog() + { + QVERIFY2(sApp->getNetworkWatchdog(), "No network watchdog"); + QTRY_VERIFY2_WITH_TIMEOUT(sApp->isSwiftDbAccessible(), "Watchdog cannot connect db", 20000); + QTRY_VERIFY2_WITH_TIMEOUT(sApp->getNetworkWatchdog()->getCheckCount() >= m_networkCheckCount + 1, "Timeout of network check", 30000); + qDebug() << "Current network check count:" << sApp->getNetworkWatchdog()->getCheckCount(); + } +} // ns + +//! \endcond diff --git a/tests/blackcore/testconnectivity.h b/tests/blackcore/testconnectivity.h new file mode 100644 index 000000000..5ed2c076d --- /dev/null +++ b/tests/blackcore/testconnectivity.h @@ -0,0 +1,63 @@ +/* Copyright (C) 2017 + * swift project community / contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#ifndef BLACKCORETEST_TESTCONNECTIVITY_H +#define BLACKCORETEST_TESTCONNECTIVITY_H + +//! \cond PRIVATE_TESTS +//! \file +//! \ingroup testblackcore + +#include "blackcore/db/networkwatchdog.h" +#include "blackcore/setupreader.h" +#include "blackmisc/network/networkutils.h" +#include "blackmisc/statusmessagelist.h" +#include + +namespace BlackCoreTest +{ + /*! + * Test connectivity such as \c canConnect \c ping and + * BlackCore::Db::CNetworkWatchdog + */ + class CTestConnectivity : public QObject + { + Q_OBJECT + + public: + //! Constructor. + explicit CTestConnectivity(QObject *parent = nullptr); + + //! Destructor + virtual ~CTestConnectivity(); + + private slots: + //! Init + void initTestCase(); + + //! Setup reader + void checkSetupReader(); + + //! Connecting test server + void connectServer(); + + //! ping test server + void pingServer(); + + //! Test the watchdog BlackCore::Db::CNetworkWatchdog + void testNetworkWatchdog(); + + private: + int m_networkCheckCount = -1; + }; +} //namespace + +//! \endcond + +#endif // guard