swift
testconnectivity.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 2017 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
7 
8 #include <QDateTime>
9 #include <QDebug>
10 #include <QElapsedTimer>
11 #include <QObject>
12 #include <QString>
13 #include <QTest>
14 #include <QtDebug>
15 
16 #include "test.h"
17 
18 #include "core/application.h"
19 #include "core/setupreader.h"
20 #include "misc/applicationinfo.h"
22 #include "misc/statusmessagelist.h"
23 
24 using namespace swift::core;
25 using namespace swift::core::db;
26 using namespace swift::misc;
27 using namespace swift::misc::network;
28 
29 namespace swiftcoretest
30 {
32  class CTestConnectivity : public QObject
33  {
34  Q_OBJECT
35 
36  private slots:
38  void initTestCase();
39 
41  void checkSetupReader();
42 
44  void connectServer();
45 
47  void pingServer();
48  };
49 
50  void CTestConnectivity::initTestCase() { QVERIFY2(sApp, "sApp not available"); }
51 
52  void CTestConnectivity::checkSetupReader()
53  {
54  if (!sApp->hasSetupReader()) { QSKIP("Cannot load bootstrap file, skip unit test"); }
55  }
56 
57  void CTestConnectivity::connectServer()
58  {
59  if (!sApp->hasSetupReader()) { QSKIP("Cannot load bootstrap file, skip unit test"); }
60  const CUrl url = sApp->getGlobalSetup().getDbHomePageUrl();
61  QElapsedTimer timer;
62  timer.start();
63  constexpr int max = 5;
64  for (int i = 0; i < max; i++)
65  {
66  bool ok = CNetworkUtils::canConnect(url, CNetworkUtils::getLongTimeoutMs());
67  if (!ok) { QSKIP(qPrintable("Cannot connect " + url.getFullUrl())); }
68  }
69  int elapsedMs = timer.elapsed();
70  qDebug() << "Completed" << max << "connection tests in" << elapsedMs << "ms to" << url.getFullUrl();
71  QVERIFY2(true, "connectServer");
72  }
73 
74  void CTestConnectivity::pingServer()
75  {
76  if (!sApp->hasSetupReader()) { QSKIP("Cannot load bootstrap file, skip unit test"); }
77  const CUrl url = sApp->getGlobalSetup().getDbHomePageUrl();
78  const QString host(url.getHost());
79  QElapsedTimer timer;
80  timer.start();
81  constexpr int max = 5;
82  for (int i = 0; i < max; i++)
83  {
84  bool ok = canPing(host);
85  if (!ok) { QSKIP(qPrintable("Cannot ping " + url.getFullUrl())); }
86  }
87  int elapsedMs = timer.elapsed();
88  qDebug() << "Completed" << max << "ping tests in" << elapsedMs << "ms to" << url.getFullUrl();
89  QVERIFY2(true, "pingServer");
90  }
91 } // namespace swiftcoretest
92 
94 int main(int argc, char *argv[])
95 {
96  QCoreApplication app(argc, argv);
98  CApplication a(CApplicationInfo::UnitTest);
100  const bool setup = a.parseCommandLineArgsAndLoadSetup();
101  if (!setup) { qWarning() << "No setup loaded"; }
102  int r = EXIT_FAILURE;
103  if (a.start()) { r = QTest::qExec(&to, args); }
104  a.gracefulShutdown();
105  return r;
106 }
107 
108 #include "testconnectivity.moc"
109 
SWIFT_CORE_EXPORT swift::core::CApplication * sApp
Single instance of application object.
Definition: application.cpp:71
void addNetworkOptions()
Add the network options.
bool hasSetupReader() const
Setup reader?
data::CGlobalSetup getGlobalSetup() const
Global setup.
bool parseCommandLineArgsAndLoadSetup()
Combined function that does a startup check, parses the command line arguments and loads the setup.
virtual bool start()
Start services, if not yet parsed call CApplication::parse.
virtual void gracefulShutdown()
Graceful shutdown.
swift::misc::network::CUrl getDbHomePageUrl() const
Home page url.
Definition: globalsetup.cpp:42
Value object encapsulating information of a location, kind of simplified CValueObject compliant versi...
Definition: url.h:27
const QString & getHost() const
Get host.
Definition: url.h:55
QString getFullUrl(bool withQuery=true) const
Qualified name.
Definition: url.cpp:84
Test connectivity such as canConnect ping and swift::core::db::CNetworkWatchdog.
Classes interacting with the swift database (aka "datastore").
Backend services of the swift project, like dealing with the network or the simulators.
Definition: actionbind.cpp:7
Free functions in swift::misc.
qint64 elapsed() const const
QSKIP(description)
QVERIFY2(condition, message)
int qExec(QObject *testObject, const QStringList &arguments)
#define SWIFTTEST_INIT(TestObject)
Implements a main() function that executes all tests in TestObject without instantiating a QApplicati...
Definition: test.h:18
int main(int argc, char *argv[])
main