testblacklist.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * This software is in the public domain, furnished "as is", without technical
  3. * support, and with no warranty, express or implied, as to its usefulness for
  4. * any purpose.
  5. *
  6. */
  7. #include <QtTest>
  8. #include "syncenginetestutils.h"
  9. #include <syncengine.h>
  10. using namespace OCC;
  11. SyncFileItemPtr findItem(const QSignalSpy &spy, const QString &path)
  12. {
  13. for (const QList<QVariant> &args : spy) {
  14. auto item = args[0].value<SyncFileItemPtr>();
  15. if (item->destination() == path)
  16. return item;
  17. }
  18. return SyncFileItemPtr(new SyncFileItem);
  19. }
  20. SyncJournalFileRecord journalRecord(FakeFolder &folder, const QByteArray &path)
  21. {
  22. SyncJournalFileRecord rec;
  23. folder.syncJournal().getFileRecord(path, &rec);
  24. return rec;
  25. }
  26. class TestBlacklist : public QObject
  27. {
  28. Q_OBJECT
  29. private slots:
  30. void testBlacklistBasic_data()
  31. {
  32. QTest::addColumn<bool>("remote");
  33. QTest::newRow("remote") << true;
  34. QTest::newRow("local") << false;
  35. }
  36. void testBlacklistBasic()
  37. {
  38. QFETCH(bool, remote);
  39. FakeFolder fakeFolder{ FileInfo::A12_B12_C12_S12() };
  40. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  41. QSignalSpy completeSpy(&fakeFolder.syncEngine(), SIGNAL(itemCompleted(const SyncFileItemPtr &)));
  42. auto &modifier = remote ? fakeFolder.remoteModifier() : fakeFolder.localModifier();
  43. int counter = 0;
  44. QByteArray reqId;
  45. fakeFolder.setServerOverride([&](QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *) -> QNetworkReply * {
  46. reqId = req.rawHeader("X-Request-ID");
  47. if (!remote && op == QNetworkAccessManager::PutOperation)
  48. ++counter;
  49. if (remote && op == QNetworkAccessManager::GetOperation)
  50. ++counter;
  51. return nullptr;
  52. });
  53. auto cleanup = [&]() {
  54. completeSpy.clear();
  55. };
  56. auto initialEtag = journalRecord(fakeFolder, "A")._etag;
  57. QVERIFY(!initialEtag.isEmpty());
  58. // The first sync and the download will fail - the item will be blacklisted
  59. modifier.insert("A/new");
  60. fakeFolder.serverErrorPaths().append("A/new", 500); // will be blacklisted
  61. QVERIFY(!fakeFolder.syncOnce());
  62. {
  63. auto it = findItem(completeSpy, "A/new");
  64. QVERIFY(it);
  65. QCOMPARE(it->_status, SyncFileItem::NormalError); // initial error visible
  66. QCOMPARE(it->_instruction, CSYNC_INSTRUCTION_NEW);
  67. auto entry = fakeFolder.syncJournal().errorBlacklistEntry("A/new");
  68. QVERIFY(entry.isValid());
  69. QCOMPARE(entry._errorCategory, SyncJournalErrorBlacklistRecord::Normal);
  70. QCOMPARE(entry._retryCount, 1);
  71. QCOMPARE(counter, 1);
  72. QVERIFY(entry._ignoreDuration > 0);
  73. QCOMPARE(entry._requestId, reqId);
  74. if (remote)
  75. QCOMPARE(journalRecord(fakeFolder, "A")._etag, initialEtag);
  76. }
  77. cleanup();
  78. // Ignored during the second run - but soft errors are also errors
  79. QVERIFY(!fakeFolder.syncOnce());
  80. {
  81. auto it = findItem(completeSpy, "A/new");
  82. QVERIFY(it);
  83. QCOMPARE(it->_status, SyncFileItem::BlacklistedError);
  84. QCOMPARE(it->_instruction, CSYNC_INSTRUCTION_IGNORE); // no retry happened!
  85. auto entry = fakeFolder.syncJournal().errorBlacklistEntry("A/new");
  86. QVERIFY(entry.isValid());
  87. QCOMPARE(entry._errorCategory, SyncJournalErrorBlacklistRecord::Normal);
  88. QCOMPARE(entry._retryCount, 1);
  89. QCOMPARE(counter, 1);
  90. QVERIFY(entry._ignoreDuration > 0);
  91. QCOMPARE(entry._requestId, reqId);
  92. if (remote)
  93. QCOMPARE(journalRecord(fakeFolder, "A")._etag, initialEtag);
  94. }
  95. cleanup();
  96. // Let's expire the blacklist entry to verify it gets retried
  97. {
  98. auto entry = fakeFolder.syncJournal().errorBlacklistEntry("A/new");
  99. entry._ignoreDuration = 1;
  100. entry._lastTryTime -= 1;
  101. fakeFolder.syncJournal().setErrorBlacklistEntry(entry);
  102. }
  103. QVERIFY(!fakeFolder.syncOnce());
  104. {
  105. auto it = findItem(completeSpy, "A/new");
  106. QVERIFY(it);
  107. QCOMPARE(it->_status, SyncFileItem::BlacklistedError); // blacklisted as it's just a retry
  108. QCOMPARE(it->_instruction, CSYNC_INSTRUCTION_NEW); // retry!
  109. auto entry = fakeFolder.syncJournal().errorBlacklistEntry("A/new");
  110. QVERIFY(entry.isValid());
  111. QCOMPARE(entry._errorCategory, SyncJournalErrorBlacklistRecord::Normal);
  112. QCOMPARE(entry._retryCount, 2);
  113. QCOMPARE(counter, 2);
  114. QVERIFY(entry._ignoreDuration > 0);
  115. QCOMPARE(entry._requestId, reqId);
  116. if (remote)
  117. QCOMPARE(journalRecord(fakeFolder, "A")._etag, initialEtag);
  118. }
  119. cleanup();
  120. // When the file changes a retry happens immediately
  121. modifier.appendByte("A/new");
  122. QVERIFY(!fakeFolder.syncOnce());
  123. {
  124. auto it = findItem(completeSpy, "A/new");
  125. QVERIFY(it);
  126. QCOMPARE(it->_status, SyncFileItem::BlacklistedError);
  127. QCOMPARE(it->_instruction, CSYNC_INSTRUCTION_NEW); // retry!
  128. auto entry = fakeFolder.syncJournal().errorBlacklistEntry("A/new");
  129. QVERIFY(entry.isValid());
  130. QCOMPARE(entry._errorCategory, SyncJournalErrorBlacklistRecord::Normal);
  131. QCOMPARE(entry._retryCount, 3);
  132. QCOMPARE(counter, 3);
  133. QVERIFY(entry._ignoreDuration > 0);
  134. QCOMPARE(entry._requestId, reqId);
  135. if (remote)
  136. QCOMPARE(journalRecord(fakeFolder, "A")._etag, initialEtag);
  137. }
  138. cleanup();
  139. // When the error goes away and the item is retried, the sync succeeds
  140. fakeFolder.serverErrorPaths().clear();
  141. {
  142. auto entry = fakeFolder.syncJournal().errorBlacklistEntry("A/new");
  143. entry._ignoreDuration = 1;
  144. entry._lastTryTime -= 1;
  145. fakeFolder.syncJournal().setErrorBlacklistEntry(entry);
  146. }
  147. QVERIFY(fakeFolder.syncOnce());
  148. {
  149. auto it = findItem(completeSpy, "A/new");
  150. QVERIFY(it);
  151. QCOMPARE(it->_status, SyncFileItem::Success);
  152. QCOMPARE(it->_instruction, CSYNC_INSTRUCTION_NEW);
  153. auto entry = fakeFolder.syncJournal().errorBlacklistEntry("A/new");
  154. QVERIFY(!entry.isValid());
  155. QCOMPARE(counter, 4);
  156. if (remote)
  157. QCOMPARE(journalRecord(fakeFolder, "A")._etag, fakeFolder.currentRemoteState().find("A")->etag.toUtf8());
  158. }
  159. cleanup();
  160. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  161. }
  162. };
  163. QTEST_GUILESS_MAIN(TestBlacklist)
  164. #include "testblacklist.moc"