File indexing completed on 2025-02-16 04:50:08

0001 /*
0002     Copyright (C) 2019 Krzysztof Nowicki <krissn@op.pl>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Library General Public
0006     License as published by the Free Software Foundation; either
0007     version 2 of the License, or (at your option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012     Library General Public License for more details.
0013 
0014     You should have received a copy of the GNU Library General Public License
0015     along with this library; see the file COPYING.LIB.  If not, write to
0016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017     Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include <Akonadi/AgentInstanceCreateJob>
0021 #include <Akonadi/AgentManager>
0022 #include <Akonadi/CollectionFetchScope>
0023 #include <Akonadi/Control>
0024 #include <Akonadi/Monitor>
0025 #include <Akonadi/SpecialCollectionAttribute>
0026 #include <akonadi/qtest_akonadi.h>
0027 
0028 #include "ewsresourceinterface.h"
0029 #include "ewssettings.h"
0030 #include "ewswallet.h"
0031 #include "fakeewsserverthread.h"
0032 #include "isolatedtestbase.h"
0033 #include "statemonitor.h"
0034 
0035 class BasicTest : public IsolatedTestBase
0036 {
0037     Q_OBJECT
0038 public:
0039     explicit BasicTest(QObject *parent = nullptr);
0040     ~BasicTest() override;
0041 private Q_SLOTS:
0042     void initTestCase();
0043     void cleanupTestCase();
0044     void busyInit();
0045 
0046 private:
0047     QScopedPointer<TestAgentInstance> mTestInstance;
0048 };
0049 
0050 QTEST_AKONADIMAIN(BasicTest)
0051 
0052 using namespace Akonadi;
0053 
0054 constexpr int DesiredStateTimeoutMs = 200000;
0055 
0056 BasicTest::BasicTest(QObject *parent)
0057     : IsolatedTestBase(parent)
0058 {
0059 }
0060 
0061 BasicTest::~BasicTest() = default;
0062 
0063 void BasicTest::initTestCase()
0064 {
0065     init();
0066 
0067     mTestInstance.reset(new TestAgentInstance(QStringLiteral("http://127.0.0.1:%1/EWS/Exchange.asmx").arg(mFakeServerThread->portNumber())));
0068     QVERIFY(mTestInstance->isValid());
0069     mTestInstance->resourceInterface().setInitialReconnectTimeout(2);
0070 }
0071 
0072 void BasicTest::cleanupTestCase()
0073 {
0074     cleanup();
0075 }
0076 
0077 void BasicTest::busyInit()
0078 {
0079     bool requestTriggered = false;
0080     mFakeServerThread->setOverrideReplyCallback([&](const QString &, QXmlResultItems &, const QXmlNamePool &) {
0081         requestTriggered = true;
0082         return FakeEwsServer::DialogEntry::HttpResponse(IsolatedTestBase::loadResourceAsString(QStringLiteral(":/xml/errorserverbusy")), 200);
0083     });
0084 
0085     QEventLoop loop;
0086 
0087     enum { InitialOffline, InitialOnline, BusyOffline, RetryOnline, RetryOffline } instanceState = InitialOffline;
0088 
0089     connect(AgentManager::self(), &AgentManager::instanceOnline, this, [&](const AgentInstance &_instance, bool online) {
0090         if (_instance.identifier() == mTestInstance->identifier()) {
0091             switch (instanceState) {
0092             case InitialOffline:
0093                 QVERIFY(online);
0094                 if (online) {
0095                     qDebug() << "Initial resource online state reached";
0096                     instanceState = InitialOnline;
0097                 }
0098                 break;
0099             case InitialOnline:
0100                 if (!online && requestTriggered) {
0101                     qDebug() << "Resource state changed to offline after busy response";
0102                     instanceState = BusyOffline;
0103                     requestTriggered = false;
0104                 }
0105                 break;
0106             case BusyOffline:
0107                 QVERIFY(online);
0108                 if (online) {
0109                     qDebug() << "Resource online after retry";
0110                     instanceState = RetryOnline;
0111                 }
0112                 break;
0113             case RetryOnline:
0114                 if (!online) {
0115                     qDebug() << "Resource state changed to offline after busy response";
0116                     instanceState = RetryOffline;
0117                     loop.exit(0);
0118                 }
0119                 break;
0120             default:
0121                 break;
0122             }
0123         }
0124     });
0125 
0126     QVERIFY(mTestInstance->setOnline(true, true));
0127 
0128     QTimer timer;
0129     timer.setSingleShot(true);
0130     connect(&timer, &QTimer::timeout, this, [&]() {
0131         qWarning() << "Timeout waiting for desired resource online state.";
0132         loop.exit(1);
0133     });
0134     timer.start(DesiredStateTimeoutMs);
0135     QCOMPARE(loop.exec(), 0);
0136 }
0137 
0138 #include "serverbusytest.moc"