testdatabaseerror.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. class TestDatabaseError : public QObject
  12. {
  13. Q_OBJECT
  14. private slots:
  15. void testDatabaseError() {
  16. /* This test will make many iteration, at each iteration, the iᵗʰ database access will fail.
  17. * The test ensure that if there is a failure, the next sync recovers. And if there was
  18. * no error, then everything was sync'ed properly.
  19. */
  20. FileInfo finalState;
  21. for (int count = 0; true; ++count) {
  22. qInfo() << "Starting Iteration" << count;
  23. FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
  24. // Do a couple of changes
  25. fakeFolder.remoteModifier().insert("A/a0");
  26. fakeFolder.remoteModifier().appendByte("A/a1");
  27. fakeFolder.remoteModifier().remove("A/a2");
  28. fakeFolder.remoteModifier().rename("S/s1", "S/s1_renamed");
  29. fakeFolder.remoteModifier().mkdir("D");
  30. fakeFolder.remoteModifier().mkdir("D/subdir");
  31. fakeFolder.remoteModifier().insert("D/subdir/file");
  32. fakeFolder.localModifier().insert("B/b0");
  33. fakeFolder.localModifier().appendByte("B/b1");
  34. fakeFolder.remoteModifier().remove("B/b2");
  35. fakeFolder.localModifier().mkdir("NewDir");
  36. fakeFolder.localModifier().rename("C", "NewDir/C");
  37. // Set the counter
  38. fakeFolder.syncJournal().autotestFailCounter = count;
  39. // run the sync
  40. bool result = fakeFolder.syncOnce();
  41. qInfo() << "Result of iteration" << count << "was" << result;
  42. if (fakeFolder.syncJournal().autotestFailCounter >= 0) {
  43. // No error was thrown, we are finished
  44. QVERIFY(result);
  45. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  46. QCOMPARE(fakeFolder.currentRemoteState(), finalState);
  47. return;
  48. }
  49. if (!result) {
  50. fakeFolder.syncJournal().autotestFailCounter = -1;
  51. // Try again
  52. QVERIFY(fakeFolder.syncOnce());
  53. }
  54. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  55. if (count == 0) {
  56. finalState = fakeFolder.currentRemoteState();
  57. } else {
  58. // the final state should be the same for every iteration
  59. QCOMPARE(fakeFolder.currentRemoteState(), finalState);
  60. }
  61. }
  62. }
  63. };
  64. QTEST_GUILESS_MAIN(TestDatabaseError)
  65. #include "testdatabaseerror.moc"