From 2a95364be5b28ff00f8415210e268280cf8f4a3e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 29 Nov 2014 01:38:17 +0000 Subject: Unit tests for daemon and fixes for highlighted issues --- project2/ice/iceDaemon.cpp | 26 +++-- project2/ice/iceViewSerializer.cpp | 15 ++- project2/ice/unittests/Jamfile.jam | 14 ++- project2/ice/unittests/data/unittest-data.xml | 19 ++++ project2/ice/unittests/lib/testrows.xml | 19 ++++ .../tasks/UnitTest/SimpleInterface/SomeTask.xml | 4 + .../UnitTest/SimpleInterface/SomeTaskParams.xml | 7 ++ project2/ice/unittests/testDaemon.cpp | 108 +++++++++++++++++++++ .../views/UnitTest/SimpleInterface/SomeRows.xml | 5 + .../UnitTest/SimpleInterface/SomeRowsParams.xml | 5 + 10 files changed, 206 insertions(+), 16 deletions(-) create mode 100644 project2/ice/unittests/data/unittest-data.xml create mode 100644 project2/ice/unittests/lib/testrows.xml create mode 100644 project2/ice/unittests/tasks/UnitTest/SimpleInterface/SomeTask.xml create mode 100644 project2/ice/unittests/tasks/UnitTest/SimpleInterface/SomeTaskParams.xml create mode 100644 project2/ice/unittests/views/UnitTest/SimpleInterface/SomeRows.xml create mode 100644 project2/ice/unittests/views/UnitTest/SimpleInterface/SomeRowsParams.xml diff --git a/project2/ice/iceDaemon.cpp b/project2/ice/iceDaemon.cpp index a8fb8c2..7f506d4 100644 --- a/project2/ice/iceDaemon.cpp +++ b/project2/ice/iceDaemon.cpp @@ -68,23 +68,24 @@ IceDaemon::shutdown() const void IceDaemon::run() const { - Logger()->messagebf(LOG_INFO, " %s starting...", __PRETTY_FUNCTION__); + Logger()->messagebf(LOG_DEBUG, " %s creating adapter %s [%s]...", __PRETTY_FUNCTION__, adapterName, adapterEndpoint); Ice::ObjectAdapterPtr adapter = ic->createObjectAdapterWithEndpoints(adapterName, adapterEndpoint); + + Logger()->messagebf(LOG_DEBUG, " %s installing servants...", __PRETTY_FUNCTION__); std::set interfaces; InstanceSet::OnAll([this, adapter, &interfaces](IceDaemonAdapterHandlerLoader * loader) { IceDaemonAdapterHandlerPtr interfacePtr = loader->create(); interfacePtr->add(adapter, this, ic); interfaces.insert(interfacePtr); }); + + Logger()->messagebf(LOG_DEBUG, " %s starting...", __PRETTY_FUNCTION__); adapter->activate(); + Logger()->messagebf(LOG_INFO, " %s running...", __PRETTY_FUNCTION__); - BOOST_FOREACH(const auto & interfacePtr, interfaces) { - interfacePtr->remove(adapter, ic); - } ic->waitForShutdown(); - Logger()->messagebf(LOG_INFO, " %s stopped...", __PRETTY_FUNCTION__); - Logger()->messagebf(LOG_INFO, "<<< %s", __PRETTY_FUNCTION__); + Logger()->messagebf(LOG_INFO, " %s stopped", __PRETTY_FUNCTION__); } class IceDaemonViewHost : public virtual CommonObjects, public virtual CheckHost { @@ -98,7 +99,9 @@ class IceDaemonViewHost : public virtual CommonObjects, public virtual CheckHost void executeView(RowSetPresenterPtr presenter, ExecContext * ec) const { loadScriptComponents(); + Logger()->messagebf(LOG_DEBUG, "%s: run %d checks", __PRETTY_FUNCTION__, CheckHost::checks.size()); runChecks(ec); + Logger()->messagebf(LOG_DEBUG, "%s: execute view", __PRETTY_FUNCTION__); view->execute(presenter.get(), ec); // Caches might open transactions BOOST_FOREACH(const CommonObjects::DataSources::value_type & ds, CommonObjects::datasources) { @@ -136,22 +139,25 @@ void IceDaemon::executeView(const std::string & name, Slicer::ModelPartPtr p, const ParamMap & pm) const { ScriptNodePtr s = ScriptReader::resolveScript(IceDaemon::viewRoot, name, false)->root(); - IceDaemonViewHost f(s); + boost::intrusive_ptr v = new IceDaemonViewHost(s); IceCallContext icc(pm); - f.executeView(new IceViewSerializer(p), &icc); + v->executeView(new IceViewSerializer(p), &icc); } class IceDaemonTaskHost : public TaskHost { public: IceDaemonTaskHost(ScriptNodePtr s) : SourceObject(s), + CommonObjects(s), CheckHost(s), TaskHost(s) { } void executeTask(ExecContext * ec) const { + Logger()->messagebf(LOG_DEBUG, "%s: run %d checks", __PRETTY_FUNCTION__, CheckHost::checks.size()); runChecks(ec); + Logger()->messagebf(LOG_DEBUG, "%s: execute %d tasks", __PRETTY_FUNCTION__, TaskHost::tasks.size()); execute(ec); } }; @@ -160,9 +166,9 @@ void IceDaemon::executeTask(const std::string & name, const ParamMap & pm) const { ScriptNodePtr s = ScriptReader::resolveScript(IceDaemon::taskRoot, name, false)->root(); - IceDaemonTaskHost t(s); + boost::intrusive_ptr t = new IceDaemonTaskHost(s); IceCallContext icc(pm); - t.executeTask(&icc); + t->executeTask(&icc); } IceCompile::CPtr diff --git a/project2/ice/iceViewSerializer.cpp b/project2/ice/iceViewSerializer.cpp index 2a95f9f..b690224 100644 --- a/project2/ice/iceViewSerializer.cpp +++ b/project2/ice/iceViewSerializer.cpp @@ -1,5 +1,6 @@ #include "iceViewSerializer.h" #include +#include using namespace Slicer; @@ -7,8 +8,8 @@ class Assign : public ValueSource, public TValueSource { public: Assign(const VariableType & v) : vt(v) - { - } + { + } void set(boost::posix_time::ptime & v) const override { v = vt; } void set(bool & v) const override { v = vt; } @@ -37,6 +38,12 @@ IceViewSerializer::addNamedValue(const Glib::ustring & name, const VariableType if (field) { field->SetValue(new Assign(vt)); } + else { + Logger()->messagebf(LOG_INFO, "%s: Field not found", __PRETTY_FUNCTION__); + } + } + else { + Logger()->messagebf(LOG_WARNING, "%s: No active row", __PRETTY_FUNCTION__); } } @@ -44,11 +51,15 @@ void IceViewSerializer::addNewRow(const Glib::ustring & name) const { rowmpp = mpp->GetChild(name); + if (rowmpp) { + rowmpp->Create(); + } } void IceViewSerializer::finishRow() const { + rowmpp->Complete(); rowmpp = NULL; } diff --git a/project2/ice/unittests/Jamfile.jam b/project2/ice/unittests/Jamfile.jam index a793918..b23579d 100644 --- a/project2/ice/unittests/Jamfile.jam +++ b/project2/ice/unittests/Jamfile.jam @@ -7,6 +7,9 @@ lib unittest : ..//Ice ..//IceUtil ..//pthread + : : + . + ..//pthread ; lib unittestr : @@ -19,6 +22,7 @@ lib unittestr : ..//pthread : : . + ..//pthread ; path-constant me : . ; @@ -86,9 +90,11 @@ unit-test testDaemon : ..//Ice ..//IceUtil ..//boost_filesystem -#testDaemon/task.xml -#testDaemon/taskParams.xml -#testDaemon/view.xml -#testDaemon/viewParams.xml + data/unittest-data.xml + lib/testrows.xml + tasks/UnitTest/SimpleInterface/SomeTask.xml + tasks/UnitTest/SimpleInterface/SomeTaskParams.xml + views/UnitTest/SimpleInterface/SomeRows.xml + views/UnitTest/SimpleInterface/SomeRowsParams.xml ; diff --git a/project2/ice/unittests/data/unittest-data.xml b/project2/ice/unittests/data/unittest-data.xml new file mode 100644 index 0000000..d4b76df --- /dev/null +++ b/project2/ice/unittests/data/unittest-data.xml @@ -0,0 +1,19 @@ + + + + 1 + first + + + 2 + second + + + 1 + third + + + 2 + first + + diff --git a/project2/ice/unittests/lib/testrows.xml b/project2/ice/unittests/lib/testrows.xml new file mode 100644 index 0000000..876e311 --- /dev/null +++ b/project2/ice/unittests/lib/testrows.xml @@ -0,0 +1,19 @@ + + + /unittest-data.xml + + + + + + + + + /root/record[a= or b=''] + + + + + + + diff --git a/project2/ice/unittests/tasks/UnitTest/SimpleInterface/SomeTask.xml b/project2/ice/unittests/tasks/UnitTest/SimpleInterface/SomeTask.xml new file mode 100644 index 0000000..e042d7b --- /dev/null +++ b/project2/ice/unittests/tasks/UnitTest/SimpleInterface/SomeTask.xml @@ -0,0 +1,4 @@ + + + + diff --git a/project2/ice/unittests/tasks/UnitTest/SimpleInterface/SomeTaskParams.xml b/project2/ice/unittests/tasks/UnitTest/SimpleInterface/SomeTaskParams.xml new file mode 100644 index 0000000..67f1cc8 --- /dev/null +++ b/project2/ice/unittests/tasks/UnitTest/SimpleInterface/SomeTaskParams.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/project2/ice/unittests/testDaemon.cpp b/project2/ice/unittests/testDaemon.cpp index ce2630c..9e86a03 100644 --- a/project2/ice/unittests/testDaemon.cpp +++ b/project2/ice/unittests/testDaemon.cpp @@ -4,6 +4,9 @@ #include #include #include +#include +#include +#include #define XSTR(s) STR(s) #define STR(s) #s @@ -11,12 +14,112 @@ const auto bindir = boost::filesystem::canonical("/proc/self/exe").parent_path() const boost::filesystem::path iceroot(XSTR(ROOT)); const auto headers = iceroot.parent_path().parent_path(); +class DummyTask : public Task { + public: + DummyTask(ScriptNodePtr n) : SourceObject(n), Task(n) { } + void execute(ExecContext *) const + { + Logger()->messagebf(LOG_DEBUG, "%s: %d", __PRETTY_FUNCTION__, __LINE__); + execCount += 1; + } + + static unsigned int execCount; +}; +DECLARE_LOADER("DummyTask", DummyTask); +unsigned int DummyTask::execCount = 0; + +class DummyParamTask : public Task { + public: + DummyParamTask(ScriptNodePtr n) : + SourceObject(n), + Task(n), + a(n, "a"), + b(n, "b") + { } + void execute(ExecContext * ec) const + { + Logger()->messagebf(LOG_DEBUG, "%s: %d", __PRETTY_FUNCTION__, __LINE__); + execCount += 1; + execA = a(ec); + execB = b(ec); + } + + const Variable a; + const Variable b; + + static unsigned int execCount; + static VariableType execA; + static VariableType execB; +}; +DECLARE_LOADER("DummyParamTask", DummyParamTask); +unsigned int DummyParamTask::execCount = 0; +VariableType DummyParamTask::execA; +VariableType DummyParamTask::execB; + static void commonTests() { BOOST_TEST_CHECKPOINT("Verify loaded"); BOOST_REQUIRE(IceDaemonAdapterHandlerLoader::getFor("UnitTest")); + + int dummy = 0; + BOOST_TEST_CHECKPOINT("Run daemon"); + DaemonPtr id = new IceDaemon(dummy, NULL); + std::thread run(&Daemon::run, id.get()); + + BOOST_TEST_CHECKPOINT("Create and verify proxy"); + Ice::CommunicatorPtr ic = Ice::initialize(dummy, NULL); + UnitTest::SimpleInterfacePrx si = UnitTest::SimpleInterfacePrx::checkedCast(ic->stringToProxy("UnitTestSimpleInterface:tcp -p 12024")); + BOOST_REQUIRE(si); + si->ice_ping(); + + BOOST_TEST_CHECKPOINT("Call view (no parameters)"); + auto rows = si->SomeRows(); + BOOST_REQUIRE_EQUAL(4, rows.size()); + BOOST_REQUIRE(rows[0]); + BOOST_REQUIRE(rows[1]); + BOOST_REQUIRE(rows[2]); + BOOST_REQUIRE(rows[3]); + BOOST_REQUIRE_EQUAL(1, rows[0]->a); + BOOST_REQUIRE_EQUAL("first", rows[0]->b); + BOOST_REQUIRE_EQUAL(2, rows[1]->a); + BOOST_REQUIRE_EQUAL("second", rows[1]->b); + BOOST_REQUIRE_EQUAL(1, rows[2]->a); + BOOST_REQUIRE_EQUAL("third", rows[2]->b); + BOOST_REQUIRE_EQUAL(2, rows[3]->a); + BOOST_REQUIRE_EQUAL("first", rows[3]->b); + + BOOST_TEST_CHECKPOINT("Call view (parameters)"); + auto paramRows = si->SomeRowsParams(1, "first"); + BOOST_REQUIRE_EQUAL(3, paramRows.size()); + BOOST_REQUIRE_EQUAL(1, paramRows[0]->a); + BOOST_REQUIRE_EQUAL("first", paramRows[0]->b); + BOOST_REQUIRE_EQUAL(1, paramRows[1]->a); + BOOST_REQUIRE_EQUAL("third", paramRows[1]->b); + BOOST_REQUIRE_EQUAL(2, paramRows[2]->a); + BOOST_REQUIRE_EQUAL("first", paramRows[2]->b); + + BOOST_TEST_CHECKPOINT("Call task (no parameters)"); + BOOST_REQUIRE_EQUAL(0, DummyTask::execCount); + si->SomeTask(); + BOOST_REQUIRE_EQUAL(1, DummyTask::execCount); + si->SomeTask(); + BOOST_REQUIRE_EQUAL(2, DummyTask::execCount); + + BOOST_TEST_CHECKPOINT("Call task (parameters)"); + BOOST_REQUIRE_EQUAL(0, DummyParamTask::execCount); + si->SomeTaskParams(1, "first"); + BOOST_REQUIRE_EQUAL(1, DummyParamTask::execCount); + BOOST_REQUIRE_EQUAL(1, DummyParamTask::execA.as()); + BOOST_REQUIRE_EQUAL("first", DummyParamTask::execB.as()); + si->SomeTaskParams(2, "second"); + BOOST_REQUIRE_EQUAL(2, DummyParamTask::execCount); + BOOST_REQUIRE_EQUAL(2, DummyParamTask::execA.as()); + BOOST_REQUIRE_EQUAL("second", DummyParamTask::execB.as()); + + id->shutdown(); + run.join(); } static @@ -34,8 +137,13 @@ BOOST_AUTO_TEST_CASE( test_daemon ) boost::filesystem::remove_all(tmpdir); BOOST_TEST_CHECKPOINT("Configure, compile, link, load"); + TestOptionsSource::LoadTestOptions({ }); TestOptionsSource::LoadTestOptions({ + { "application.dataroot", "file://" + (iceroot / "data").string() }, { "library", (bindir / "slicer-yes" / "libunittestr.so").string() }, + { "ice.daemon.adapterEndpoint", "tcp -p 12024" }, + { "ice.daemon.viewRoot", (iceroot / "views").string() }, + { "ice.daemon.taskRoot", (iceroot / "tasks").string() }, { "ice.compile.tmpdir", tmpdir }, { "ice.compile.headers", headers.string() }, { "ice.daemon.slicerdaemon", (iceroot / "unittest.ice").string() } diff --git a/project2/ice/unittests/views/UnitTest/SimpleInterface/SomeRows.xml b/project2/ice/unittests/views/UnitTest/SimpleInterface/SomeRows.xml new file mode 100644 index 0000000..b4950e3 --- /dev/null +++ b/project2/ice/unittests/views/UnitTest/SimpleInterface/SomeRows.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/project2/ice/unittests/views/UnitTest/SimpleInterface/SomeRowsParams.xml b/project2/ice/unittests/views/UnitTest/SimpleInterface/SomeRowsParams.xml new file mode 100644 index 0000000..d8a3f21 --- /dev/null +++ b/project2/ice/unittests/views/UnitTest/SimpleInterface/SomeRowsParams.xml @@ -0,0 +1,5 @@ + + + + + -- cgit v1.2.3