From f425c2070131161dc11bcf70e35f8d1848cadb65 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Tue, 6 Dec 2016 19:40:33 +0100 Subject: Interresourcemovetest/fixed interresourcemove We cant take the identifier from the entity where we just cleared the identifier. --- common/pipeline.cpp | 10 +-- tests/interresourcemovetest.cpp | 151 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+), 5 deletions(-) create mode 100644 tests/interresourcemovetest.cpp diff --git a/common/pipeline.cpp b/common/pipeline.cpp index 4ea43eb..7ee4b91 100644 --- a/common/pipeline.cpp +++ b/common/pipeline.cpp @@ -277,22 +277,22 @@ KAsync::Job Pipeline::modifiedEntity(void const *command, size_t size) SinkTrace() << "Moving entity to new resource " << newEntity.identifier() << newEntity.resourceInstanceIdentifier() << targetResource; auto job = TypeHelper{bufferType}.operator(), ApplicationDomain::ApplicationDomainType&>(newEntity); - job = job.syncThen([this, newEntity, isMove, targetResource, bufferType](const KAsync::Error &error) { + job = job.syncThen([this, current, isMove, targetResource, bufferType](const KAsync::Error &error) { if (!error) { - SinkTrace() << "Move of " << newEntity.identifier() << "was successfull"; + SinkTrace() << "Move of " << current.identifier() << "was successfull"; if (isMove) { startTransaction(); flatbuffers::FlatBufferBuilder fbb; - auto entityId = fbb.CreateString(newEntity.identifier()); + auto entityId = fbb.CreateString(current.identifier()); auto type = fbb.CreateString(bufferType); - auto location = Sink::Commands::CreateDeleteEntity(fbb, newEntity.revision(), entityId, type, true); + auto location = Sink::Commands::CreateDeleteEntity(fbb, current.revision(), entityId, type, true); Sink::Commands::FinishDeleteEntityBuffer(fbb, location); const auto data = BufferUtils::extractBuffer(fbb); deletedEntity(data, data.size()).exec(); commit(); } } else { - SinkError() << "Failed to move entity " << targetResource << " to resource " << newEntity.identifier(); + SinkError() << "Failed to move entity " << targetResource << " to resource " << current.identifier(); } }); job.exec(); diff --git a/tests/interresourcemovetest.cpp b/tests/interresourcemovetest.cpp new file mode 100644 index 0000000..7561c5b --- /dev/null +++ b/tests/interresourcemovetest.cpp @@ -0,0 +1,151 @@ +/* + * Copyright (C) 2016 Christian Mollekopf + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) version 3, or any + * later version accepted by the membership of KDE e.V. (or its + * successor approved by the membership of KDE e.V.), which shall + * act as a proxy defined in Section 6 of version 3 of the license. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + */ +#include + +#include + +#include "dummyresource/resourcefactory.h" +#include "store.h" +#include "resourceconfig.h" +#include "resourcecontrol.h" +#include "log.h" +#include "test.h" +#include "testutils.h" + +using namespace Sink; +using namespace Sink::ApplicationDomain; + +/** + * Test of complete system using the dummy resource. + * + * This test requires the dummy resource installed. + */ +class InterResourceMoveTest : public QObject +{ + Q_OBJECT + +private slots: + void initTestCase() + { + Sink::Test::initTest(); + auto factory = Sink::ResourceFactory::load("sink.dummy"); + QVERIFY(factory); + ::DummyResource::removeFromDisk("instance1"); + ::DummyResource::removeFromDisk("instance2"); + ResourceConfig::addResource("instance1", "sink.dummy"); + ResourceConfig::addResource("instance2", "sink.dummy"); + } + + void init() + { + } + + void cleanup() + { + VERIFYEXEC(Sink::Store::removeDataFromDisk(QByteArray("instance1"))); + VERIFYEXEC(Sink::Store::removeDataFromDisk(QByteArray("instance2"))); + } + + void testMove() + { + Event event("instance1"); + event.setProperty("uid", "testuid"); + QCOMPARE(event.getProperty("uid").toByteArray(), QByteArray("testuid")); + event.setProperty("summary", "summaryValue"); + VERIFYEXEC(Sink::Store::create(event)); + + + Event createdEvent; + // Ensure all local data is processed + VERIFYEXEC(Sink::ResourceControl::flushMessageQueue(QByteArrayList() << "instance1")); + { + auto query = Query().resourceFilter("instance1") ; + auto list = Sink::Store::read(query.filter("testuid")); + QCOMPARE(list.size(), 1); + createdEvent = list.first(); + } + + VERIFYEXEC(Sink::Store::move(createdEvent, "instance2")); + + //FIXME we can't guarantee that that the create command arrives at instance2 before the flush command, so we'll just wait for a little bit. + QTest::qWait(1000); + //Ensure the move has been processed + VERIFYEXEC(Sink::ResourceControl::flushMessageQueue(QByteArrayList() << "instance1")); + //Ensure the create in the target resource has been processed + VERIFYEXEC(Sink::ResourceControl::flushMessageQueue(QByteArrayList() << "instance2")); + { + auto query = Query().resourceFilter("instance2") ; + auto list = Sink::Store::read(query.filter("testuid")); + QCOMPARE(list.size(), 1); + } + + VERIFYEXEC(Sink::ResourceControl::flushMessageQueue(QByteArrayList() << "instance1")); + { + auto query = Query().resourceFilter("instance1") ; + auto list = Sink::Store::read(query.filter("testuid")); + QCOMPARE(list.size(), 0); + } + } + + void testCopy() + { + Event event("instance1"); + event.setProperty("uid", "testuid"); + QCOMPARE(event.getProperty("uid").toByteArray(), QByteArray("testuid")); + event.setProperty("summary", "summaryValue"); + VERIFYEXEC(Sink::Store::create(event)); + + + Event createdEvent; + // Ensure all local data is processed + VERIFYEXEC(Sink::ResourceControl::flushMessageQueue(QByteArrayList() << "instance1")); + { + auto query = Query().resourceFilter("instance1") ; + auto list = Sink::Store::read(query.filter("testuid")); + QCOMPARE(list.size(), 1); + createdEvent = list.first(); + } + + VERIFYEXEC(Sink::Store::copy(createdEvent, "instance2")); + + //FIXME we can't guarantee that that the create command arrives at instance2 before the flush command, so we'll just wait for a little bit. + QTest::qWait(100); + //Ensure the copy has been processed + VERIFYEXEC(Sink::ResourceControl::flushMessageQueue(QByteArrayList() << "instance1")); + //Ensure the create in the target resource has been processed + VERIFYEXEC(Sink::ResourceControl::flushMessageQueue(QByteArrayList() << "instance2")); + { + auto query = Query().resourceFilter("instance2") ; + auto list = Sink::Store::read(query.filter("testuid")); + QCOMPARE(list.size(), 1); + } + + VERIFYEXEC(Sink::ResourceControl::flushMessageQueue(QByteArrayList() << "instance1")); + { + auto query = Query().resourceFilter("instance1") ; + auto list = Sink::Store::read(query.filter("testuid")); + QCOMPARE(list.size(), 1); + } + } + +}; + +QTEST_MAIN(InterResourceMoveTest) +#include "interresourcemovetest.moc" -- cgit v1.2.3