diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2018-06-26 10:24:58 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2018-06-26 10:24:58 +0200 |
commit | da4b74e593f1b1262e83824cc499bd855f1b4e3e (patch) | |
tree | 95b56aa073a85022c4218375e1c788894f01c2b6 /tests | |
parent | 121c3bc96a273790414ae114082053cb649fc49a (diff) | |
download | sink-da4b74e593f1b1262e83824cc499bd855f1b4e3e.tar.gz sink-da4b74e593f1b1262e83824cc499bd855f1b4e3e.zip |
Avoid overwriting local changes with remote modifications.
The case we ran into is the following:
* Fetching the full payload and marking all messages of a thread as read
happens simultaneously.
* The local modification to mark as read gets immediately overwritten
when the full payload arrives.
* Eventually the modification gets replayed to the server though (and
the reversal isn't because coming from the source), so on next sync the
situation fixes itself.
To be able to improve this we try to protect local modifications in that
properties that have been modified since baseRevision (which currently
isn't, but should be equal to the last to the server replayed revision)
are not overwritten. This conflict resolution strategy thus always
prefers local modifications. baseRevision is currently set to the
current maximum revision of the store at the time when the resource
creates the modification.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/pipelinetest.cpp | 65 |
1 files changed, 58 insertions, 7 deletions
diff --git a/tests/pipelinetest.cpp b/tests/pipelinetest.cpp index 0268ec5..45e2fbb 100644 --- a/tests/pipelinetest.cpp +++ b/tests/pipelinetest.cpp | |||
@@ -101,23 +101,25 @@ QByteArray createEntityCommand(const flatbuffers::FlatBufferBuilder &entityFbb) | |||
101 | return command; | 101 | return command; |
102 | } | 102 | } |
103 | 103 | ||
104 | QByteArray modifyEntityCommand(const flatbuffers::FlatBufferBuilder &entityFbb, const QByteArray &uid, qint64 revision) | 104 | QByteArray modifyEntityCommand(const flatbuffers::FlatBufferBuilder &entityFbb, const QByteArray &uid, qint64 revision, QStringList modifiedProperties = {"summary"}, bool replayToSource = true) |
105 | { | 105 | { |
106 | flatbuffers::FlatBufferBuilder fbb; | 106 | flatbuffers::FlatBufferBuilder fbb; |
107 | auto type = fbb.CreateString(Sink::ApplicationDomain::getTypeName<Sink::ApplicationDomain::Event>().toStdString().data()); | 107 | auto type = fbb.CreateString(Sink::ApplicationDomain::getTypeName<Sink::ApplicationDomain::Event>().toStdString().data()); |
108 | auto id = fbb.CreateString(std::string(uid.constData(), uid.size())); | 108 | auto id = fbb.CreateString(std::string(uid.constData(), uid.size())); |
109 | auto summaryProperty = fbb.CreateString("summary"); | 109 | std::vector<flatbuffers::Offset<flatbuffers::String>> modifiedVector; |
110 | std::vector<flatbuffers::Offset<flatbuffers::String>> modified; | 110 | for (const auto &modified : modifiedProperties) { |
111 | modified.push_back(summaryProperty); | 111 | modifiedVector.push_back(fbb.CreateString(modified.toStdString())); |
112 | } | ||
112 | auto delta = fbb.CreateVector<uint8_t>(entityFbb.GetBufferPointer(), entityFbb.GetSize()); | 113 | auto delta = fbb.CreateVector<uint8_t>(entityFbb.GetBufferPointer(), entityFbb.GetSize()); |
113 | auto modifiedProperties = fbb.CreateVector(modified); | 114 | auto modifiedPropertiesVector = fbb.CreateVector(modifiedVector); |
114 | // auto delta = Sink::EntityBuffer::appendAsVector(fbb, buffer.constData(), buffer.size()); | ||
115 | Sink::Commands::ModifyEntityBuilder builder(fbb); | 115 | Sink::Commands::ModifyEntityBuilder builder(fbb); |
116 | builder.add_domainType(type); | 116 | builder.add_domainType(type); |
117 | builder.add_delta(delta); | 117 | builder.add_delta(delta); |
118 | builder.add_revision(revision); | 118 | builder.add_revision(revision); |
119 | builder.add_entityId(id); | 119 | builder.add_entityId(id); |
120 | builder.add_modifiedProperties(modifiedProperties); | 120 | builder.add_modifiedProperties(modifiedPropertiesVector); |
121 | builder.add_replayToSource(replayToSource); | ||
122 | |||
121 | auto location = builder.Finish(); | 123 | auto location = builder.Finish(); |
122 | Sink::Commands::FinishModifyEntityBuffer(fbb, location); | 124 | Sink::Commands::FinishModifyEntityBuffer(fbb, location); |
123 | 125 | ||
@@ -401,6 +403,55 @@ private slots: | |||
401 | QCOMPARE(testProcessor->deletedSummaries.at(0), QByteArray("summary2")); | 403 | QCOMPARE(testProcessor->deletedSummaries.at(0), QByteArray("summary2")); |
402 | } | 404 | } |
403 | } | 405 | } |
406 | |||
407 | void testModifyWithConflict() | ||
408 | { | ||
409 | flatbuffers::FlatBufferBuilder entityFbb; | ||
410 | auto command = createEntityCommand(createEvent(entityFbb, "summary", "description")); | ||
411 | |||
412 | Sink::Pipeline pipeline(getContext(), {"test"}); | ||
413 | |||
414 | auto adaptorFactory = QSharedPointer<TestEventAdaptorFactory>::create(); | ||
415 | |||
416 | // Create the initial revision | ||
417 | pipeline.startTransaction(); | ||
418 | pipeline.newEntity(command.constData(), command.size()); | ||
419 | pipeline.commit(); | ||
420 | |||
421 | // Get uid of written entity | ||
422 | auto keys = getKeys(instanceIdentifier(), "event.main"); | ||
423 | QCOMPARE(keys.size(), 1); | ||
424 | const auto key = keys.first(); | ||
425 | const auto uid = Sink::Storage::DataStore::uidFromKey(key); | ||
426 | |||
427 | //Simulate local modification | ||
428 | { | ||
429 | entityFbb.Clear(); | ||
430 | auto modifyCommand = modifyEntityCommand(createEvent(entityFbb, "summaryLocal"), uid, 1, {"summary"}, true); | ||
431 | pipeline.startTransaction(); | ||
432 | pipeline.modifiedEntity(modifyCommand.constData(), modifyCommand.size()); | ||
433 | pipeline.commit(); | ||
434 | } | ||
435 | |||
436 | |||
437 | //Simulate remote modification | ||
438 | //We assume the remote modification is not overly smart and always marks all properties as changed. | ||
439 | { | ||
440 | entityFbb.Clear(); | ||
441 | auto modifyCommand = modifyEntityCommand(createEvent(entityFbb, "summaryRemote", "descriptionRemote"), uid, 2, {"summary", "description"}, false); | ||
442 | pipeline.startTransaction(); | ||
443 | pipeline.modifiedEntity(modifyCommand.constData(), modifyCommand.size()); | ||
444 | pipeline.commit(); | ||
445 | } | ||
446 | |||
447 | // Ensure we've got the new revision with the modification | ||
448 | auto buffer = getEntity(instanceIdentifier(), "event.main", Sink::Storage::DataStore::assembleKey(uid, 3)); | ||
449 | QVERIFY(!buffer.isEmpty()); | ||
450 | Sink::EntityBuffer entityBuffer(buffer.data(), buffer.size()); | ||
451 | auto adaptor = adaptorFactory->createAdaptor(entityBuffer.entity()); | ||
452 | QVERIFY2(adaptor->getProperty("summary").toString() == QString("summaryLocal"), "The local modification was reverted."); | ||
453 | QVERIFY2(adaptor->getProperty("description").toString() == QString("descriptionRemote"), "The remote modification was not applied."); | ||
454 | } | ||
404 | }; | 455 | }; |
405 | 456 | ||
406 | QTEST_MAIN(PipelineTest) | 457 | QTEST_MAIN(PipelineTest) |