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 <QObject>
11 #include <QString>
12 #include <QTest>
13 #include <QtDebug>
14 
15 #include "test.h"
16 
17 #include "core/application.h"
18 #include "core/setupreader.h"
19 #include "misc/applicationinfo.h"
21 #include "misc/statusmessagelist.h"
22 
23 using namespace swift::core;
24 using namespace swift::core::db;
25 using namespace swift::misc;
26 using namespace swift::misc::network;
27 
28 namespace swiftcoretest
29 {
31  class CTestConnectivity : public QObject
32  {
33  Q_OBJECT
34 
35  private slots:
37  void initTestCase();
38 
40  void checkSetupReader();
41 
43  void connectServer();
44 
46  void pingServer();
47  };
48 
49  void CTestConnectivity::initTestCase() { QVERIFY2(sApp, "sApp not available"); }
50 
51  void CTestConnectivity::checkSetupReader()
52  {
53  if (!sApp->hasSetupReader()) { QSKIP("Cannot load bootstrap file, skip unit test"); }
54  }
55 
56  void CTestConnectivity::connectServer()
57  {
58  if (!sApp->hasSetupReader()) { QSKIP("Cannot load bootstrap file, skip unit test"); }
59  const CUrl url = sApp->getGlobalSetup().getDbHomePageUrl();
60  QElapsedTimer timer;
61  timer.start();
62  constexpr int max = 5;
63  for (int i = 0; i < max; i++)
64  {
65  bool ok = CNetworkUtils::canConnect(url, CNetworkUtils::getLongTimeoutMs());
66  if (!ok) { QSKIP(qPrintable("Cannot connect " + url.getFullUrl())); }
67  }
68  int elapsedMs = timer.elapsed();
69  qDebug() << "Completed" << max << "connection tests in" << elapsedMs << "ms to" << url.getFullUrl();
70  QVERIFY2(true, "connectServer");
71  }
72 
73  void CTestConnectivity::pingServer()
74  {
75  if (!sApp->hasSetupReader()) { QSKIP("Cannot load bootstrap file, skip unit test"); }
76  const CUrl url = sApp->getGlobalSetup().getDbHomePageUrl();
77  const QString host(url.getHost());
78  QElapsedTimer timer;
79  timer.start();
80  constexpr int max = 5;
81  for (int i = 0; i < max; i++)
82  {
83  bool ok = canPing(host);
84  if (!ok) { QSKIP(qPrintable("Cannot ping " + url.getFullUrl())); }
85  }
86  int elapsedMs = timer.elapsed();
87  qDebug() << "Completed" << max << "ping tests in" << elapsedMs << "ms to" << url.getFullUrl();
88  QVERIFY2(true, "pingServer");
89  }
90 } // namespace swiftcoretest
91 
93 int main(int argc, char *argv[])
94 {
95  QCoreApplication app(argc, argv);
97  CApplication a(CApplicationInfo::UnitTest);
99  const bool setup = a.parseCommandLineArgsAndLoadSetup();
100  if (!setup) { qWarning() << "No setup loaded"; }
101  int r = EXIT_FAILURE;
102  if (a.start()) { r = QTest::qExec(&to, args); }
103  a.gracefulShutdown();
104  return r;
105 }
106 
107 #include "testconnectivity.moc"
108 
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.
#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