From 6f4c84b7841eb6670895c2a5ba708f538555dee7 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 23 Dec 2015 20:01:01 +0000 Subject: Create IceTray and DryIce for bootstrapping and dry running IceBox services --- gentoobrowse-api/etc/icebox.config | 2 +- gentoobrowse-api/service/Jamfile.jam | 1 + .../service/abstractDatabaseClient.cpp | 34 ---------- gentoobrowse-api/service/abstractDatabaseClient.h | 64 ------------------- gentoobrowse-api/service/main.cpp | 39 +++--------- gentoobrowse-api/service/portageimpl.cpp | 4 +- gentoobrowse-api/service/portageimpl.h | 6 +- gentoobrowse-api/unittests/Jamfile.jam | 17 ++--- gentoobrowse-api/unittests/mockDefs.cpp | 9 ++- gentoobrowse-api/unittests/mockDefs.h | 14 +++- gentoobrowse-api/unittests/testInit.cpp | 26 -------- gentoobrowse-api/unittests/testPortage.cpp | 74 ++++++---------------- 12 files changed, 59 insertions(+), 231 deletions(-) delete mode 100644 gentoobrowse-api/service/abstractDatabaseClient.cpp delete mode 100644 gentoobrowse-api/service/abstractDatabaseClient.h delete mode 100644 gentoobrowse-api/unittests/testInit.cpp diff --git a/gentoobrowse-api/etc/icebox.config b/gentoobrowse-api/etc/icebox.config index 48ef26d..27ead52 100644 --- a/gentoobrowse-api/etc/icebox.config +++ b/gentoobrowse-api/etc/icebox.config @@ -1 +1 @@ -IceBox.Service.GentooBrowseAPI=gentoobrowse-service:createGentooBrowseAPI --GentooBrowseAPI.ThreadPool.Size=8 --GentooBrowseAPI.ThreadPool.SizeMax=50 --GentooBrowseAPI.Database.ConnectionString="user=postgres dbname=gentoo" --GentooBrowseAPI.Endpoints="tcp -p 9001" +IceBox.Service.GentooBrowseAPI=gentoobrowse-service:createIceTrayService --GentooBrowseAPI.ThreadPool.Size=8 --GentooBrowseAPI.ThreadPool.SizeMax=50 --GentooBrowseAPI.Database.ConnectionString="user=postgres dbname=gentoo" --GentooBrowseAPI.Endpoints="tcp -p 9001" diff --git a/gentoobrowse-api/service/Jamfile.jam b/gentoobrowse-api/service/Jamfile.jam index 777c453..fd2e8b5 100644 --- a/gentoobrowse-api/service/Jamfile.jam +++ b/gentoobrowse-api/service/Jamfile.jam @@ -10,6 +10,7 @@ lib gentoobrowse-service : : pure ..//adhocutil + ../../icetray//icetray ..//dbppcore ..//IceBox ..//slicer diff --git a/gentoobrowse-api/service/abstractDatabaseClient.cpp b/gentoobrowse-api/service/abstractDatabaseClient.cpp deleted file mode 100644 index 9844cc4..0000000 --- a/gentoobrowse-api/service/abstractDatabaseClient.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include "abstractDatabaseClient.h" -#include - -template class AdHoc::Cache, std::vector>; - -AbstractDatabaseClient::AbstractDatabaseClient(AdHoc::ResourcePool & d) : - db(d) -{ -} - -template<> -void -AbstractDatabaseClient::bind1(int o, DB::Command * cmd, const std::string & p) -{ - cmd->bindParamS(o, p); -} - -template<> -void -AbstractDatabaseClient::bind1(int o, DB::Command * cmd, const int & p) -{ - cmd->bindParamI(o, p); -} - -void -AbstractDatabaseClient::bind(int, DB::Command *) -{ -} - -void -AbstractDatabaseClient::keyPushParams(CacheKey &) -{ -} - diff --git a/gentoobrowse-api/service/abstractDatabaseClient.h b/gentoobrowse-api/service/abstractDatabaseClient.h deleted file mode 100644 index a3072cb..0000000 --- a/gentoobrowse-api/service/abstractDatabaseClient.h +++ /dev/null @@ -1,64 +0,0 @@ -#ifndef ABSTRACTDATABASECLIENT_H -#define ABSTRACTDATABASECLIENT_H - -#include -#include -#include -#include -#include - -class AbstractDatabaseClient { - protected: - AbstractDatabaseClient(AdHoc::ResourcePool & d); - typedef std::vector CacheKey; - typedef boost::shared_ptr CacheItem; - - template - Domain inline fetch(const Params & ... params) - { - CacheKey key; - key.reserve(4); - key.push_back(Sql::hash); - key.push_back(typeid(Domain).hash_code()); - keyPushParams(key, params...); - if (auto cached = cache.get(key)) { - return boost::any_cast(**cached); - } - Domain d; - { - auto c = db.get(); - auto s = DB::SelectCommandPtr(c->newSelectCommand(Sql::sql)); - bind(0, s.get(), params...); - d = Slicer::DeserializeAny(*s); - } - cache.add(key, CacheItem(new boost::any(d)), time(NULL) + 40); - return d; - } - - template - static void inline bind(int offset, DB::Command * cmd, const Param & p, const Params & ... params) - { - bind1(offset, cmd, p); - bind(offset + 1, cmd, params...); - } - - static void bind(int offset, DB::Command * cmd); - - template - static void bind1(int offset, DB::Command * cmd, const Param & p); - - template - static void inline keyPushParams(CacheKey & k, const Param & p, const Params & ... params) - { - k.push_back(std::hash()(p)); - keyPushParams(k, params...); - } - - static void keyPushParams(CacheKey &); - - AdHoc::ResourcePool & db; - AdHoc::Cache cache; -}; - -#endif - diff --git a/gentoobrowse-api/service/main.cpp b/gentoobrowse-api/service/main.cpp index fcab795..21473af 100644 --- a/gentoobrowse-api/service/main.cpp +++ b/gentoobrowse-api/service/main.cpp @@ -1,39 +1,16 @@ -#include -#include -#include +#include +#include +#include #include "portageimpl.h" -#include -class Api : public IceBox::Service { +class Api : public IceTray::Service { public: - typedef boost::shared_ptr DBCPoolPtr; - void start(const std::string & name, const Ice::CommunicatorPtr & ic, const Ice::StringSeq &) + void addObjects(const std::string &, const Ice::CommunicatorPtr & ic, const Ice::StringSeq &, const Ice::ObjectAdapterPtr & adp) override { - db = DBCPoolPtr(new DB::ConnectionPool( - ic->getProperties()->getPropertyAsIntWithDefault(name + ".Database.PoolMax", 10), - ic->getProperties()->getPropertyAsIntWithDefault(name + ".Database.PoolKeep", 2), - "postgresql", - ic->getProperties()->getProperty(name + ".Database.ConnectionString"))); - adp = ic->createObjectAdapter(name); - adp->add(new Portage(*db), ic->stringToIdentity("portage")); - adp->activate(); + auto db = getConnectionPool(ic, "postgresql", "GentooBrowseAPI"); + adp->add(new Portage(db), ic->stringToIdentity("portage")); } - void stop() - { - adp->deactivate(); - adp->destroy(); - } - - DBCPoolPtr db; - Ice::ObjectAdapterPtr adp; }; -extern "C" { - DLL_PUBLIC - IceBox::Service * - createGentooBrowseAPI(Ice::CommunicatorPtr) - { - return new Api(); - } -} +NAMEDFACTORY("default", Api, IceTray::ServiceFactory); diff --git a/gentoobrowse-api/service/portageimpl.cpp b/gentoobrowse-api/service/portageimpl.cpp index 408d467..291e903 100644 --- a/gentoobrowse-api/service/portageimpl.cpp +++ b/gentoobrowse-api/service/portageimpl.cpp @@ -17,8 +17,8 @@ #include #include -Portage::Portage(AdHoc::ResourcePool & d) : - AbstractDatabaseClient(d) +Portage::Portage(boost::shared_ptr> d) : + IceTray::AbstractDatabaseClient(d) { } diff --git a/gentoobrowse-api/service/portageimpl.h b/gentoobrowse-api/service/portageimpl.h index 9a8246d..d36cb70 100644 --- a/gentoobrowse-api/service/portageimpl.h +++ b/gentoobrowse-api/service/portageimpl.h @@ -3,11 +3,11 @@ #include #include -#include "abstractDatabaseClient.h" +#include -class DLL_PUBLIC Portage : public Gentoo::Portage, AbstractDatabaseClient { +class DLL_PUBLIC Portage : public Gentoo::Portage, IceTray::AbstractDatabaseClient { public: - Portage(AdHoc::ResourcePool & d); + Portage(boost::shared_ptr> d); Gentoo::CategoryPtr getCategory(Ice::Int id, const Ice::Current &) override; Gentoo::Categories getAllCategories(const Ice::Current &) override; diff --git a/gentoobrowse-api/unittests/Jamfile.jam b/gentoobrowse-api/unittests/Jamfile.jam index e2a5b28..1a6f41c 100644 --- a/gentoobrowse-api/unittests/Jamfile.jam +++ b/gentoobrowse-api/unittests/Jamfile.jam @@ -21,9 +21,16 @@ lib testCommon : ROOT=\"$(me)\" ..//boost_system ..//boost_filesystem + ..//Ice + ..//IceUtil + ..//IceBox + ../../icetray//dryice + ../service//gentoobrowse-service : : ..//dbppcore ..//adhocutil + ..//IceBox + ../../icetray//dryice ; @@ -53,13 +60,3 @@ run testPerf.cpp boost_utf : testPerf ; -run testInit.cpp - : : : - BOOST_TEST_DYN_LINK - ../service//gentoobrowse-service - boost_utf - ..//Ice - ..//IceBox - dl - : testInit ; - diff --git a/gentoobrowse-api/unittests/mockDefs.cpp b/gentoobrowse-api/unittests/mockDefs.cpp index abcff03..3d7a679 100644 --- a/gentoobrowse-api/unittests/mockDefs.cpp +++ b/gentoobrowse-api/unittests/mockDefs.cpp @@ -1,10 +1,15 @@ #include "mockDefs.h" #include -StandardMockDatabase::StandardMockDatabase() : - PQ::Mock("user=postgres dbname=postgres", "postgres", { +Service::Service() : + PQ::Mock("user=postgres dbname=postgres", "GentooBrowseAPI", { rootDir.parent_path() / "db" / "schema.sql", rootDir / "data.sql" }) { } +TestClient::TestClient() : + p(Gentoo::PortagePrx::checkedCast(ic->stringToProxy("portage:tcp -p 9002"))) +{ +} + diff --git a/gentoobrowse-api/unittests/mockDefs.h b/gentoobrowse-api/unittests/mockDefs.h index ca43d4d..8b4d642 100644 --- a/gentoobrowse-api/unittests/mockDefs.h +++ b/gentoobrowse-api/unittests/mockDefs.h @@ -3,12 +3,20 @@ #include #include +#include +#include -#define DECLAREMOCK(Name) class DLL_PUBLIC Name : public PQ::Mock { public: Name(); } +class DLL_PUBLIC Service : public IceTray::DryIce, PQ::Mock { + public: + Service(); +}; -DECLAREMOCK(StandardMockDatabase); +class DLL_PUBLIC TestClient : public IceTray::DryIceClient { + public: + TestClient(); -#undef DECLAREMOCK + Gentoo::PortagePrx p; +}; #endif diff --git a/gentoobrowse-api/unittests/testInit.cpp b/gentoobrowse-api/unittests/testInit.cpp deleted file mode 100644 index 15479ac..0000000 --- a/gentoobrowse-api/unittests/testInit.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#define BOOST_TEST_MODULE TestInit -#include -#include -#include -#include - -typedef IceBox::Service *(* SetupFunction)(Ice::CommunicatorPtr); - -BOOST_AUTO_TEST_CASE( init ) -{ - void * i = dlsym(NULL, "createGentooBrowseAPI"); - BOOST_REQUIRE(i); - auto sf = (SetupFunction)i; - BOOST_REQUIRE(sf); - Ice::StringSeq args; - Ice::InitializationData id; - id.properties = Ice::createProperties(); - id.properties->setProperty("GentooBrowseAPI.Endpoints", "tcp -p 9002"); - auto ic = Ice::initialize(args, id); - IceBox::Service * s = sf(nullptr); - s->start("GentooBrowseAPI", ic, {}); - s->stop(); - delete s; - ic->destroy(); -} - diff --git a/gentoobrowse-api/unittests/testPortage.cpp b/gentoobrowse-api/unittests/testPortage.cpp index 271ff34..fee3751 100644 --- a/gentoobrowse-api/unittests/testPortage.cpp +++ b/gentoobrowse-api/unittests/testPortage.cpp @@ -4,37 +4,18 @@ #include "mockDefs.h" #include -BOOST_GLOBAL_FIXTURE( StandardMockDatabase ); +BOOST_GLOBAL_FIXTURE( Service ); -class MockPool : public AdHoc::ResourcePool { - public: - MockPool() : AdHoc::ResourcePool(1, 1) { } - DB::Connection * createResource() const override { - return DB::MockDatabase::openConnectionTo("postgres"); - } -}; - -class TestPortage { - public: - TestPortage() : - p(new Portage(mp)) - { - } - - MockPool mp; - Portage::PointerType p; -}; - -BOOST_FIXTURE_TEST_SUITE(tp, TestPortage) +BOOST_FIXTURE_TEST_SUITE(tp, TestClient) BOOST_AUTO_TEST_CASE( getCategoryMissing ) { - BOOST_REQUIRE_THROW(p->getCategory(0, Ice::Current()), std::exception); + BOOST_REQUIRE_THROW(p->getCategory(0), std::exception); } BOOST_AUTO_TEST_CASE( getCategory ) { - auto cat = p->getCategory(316, Ice::Current()); + auto cat = p->getCategory(316); BOOST_REQUIRE(cat); BOOST_REQUIRE_EQUAL(316, cat->categoryid); BOOST_REQUIRE_EQUAL("app-backup", cat->name); @@ -44,7 +25,7 @@ BOOST_AUTO_TEST_CASE( getCategory ) BOOST_AUTO_TEST_CASE( getAllCategories ) { - auto cats = p->getAllCategories(Ice::Current()); + auto cats = p->getAllCategories(); BOOST_REQUIRE_EQUAL(5, cats.size()); BOOST_REQUIRE_EQUAL(316, cats.front()->categoryid); BOOST_REQUIRE_EQUAL(313, cats.back()->categoryid); @@ -54,38 +35,38 @@ BOOST_AUTO_TEST_CASE( getAllCategories ) BOOST_AUTO_TEST_CASE( getCategoriesInSuper ) { - auto cats = p->getCategoriesInSuper("dev", Ice::Current()); + auto cats = p->getCategoriesInSuper("dev"); BOOST_REQUIRE_EQUAL(2, cats.size()); BOOST_REQUIRE_EQUAL("dev-ml", cats.front()->name); BOOST_REQUIRE_EQUAL("dev-vcs", cats.back()->name); - cats = p->getCategoriesInSuper("net", Ice::Current()); + cats = p->getCategoriesInSuper("net"); BOOST_REQUIRE_EQUAL(1, cats.size()); - cats = p->getCategoriesInSuper("sys", Ice::Current()); + cats = p->getCategoriesInSuper("sys"); BOOST_REQUIRE_EQUAL(0, cats.size()); } BOOST_AUTO_TEST_CASE( getOverviewCategoriesInSuper ) { - auto cats = p->getOverviewCategoriesInSuper("dev", Ice::Current()); + auto cats = p->getOverviewCategoriesInSuper("dev"); BOOST_REQUIRE_EQUAL(2, cats.size()); BOOST_REQUIRE_EQUAL("dev-ml", cats.front()->name); BOOST_REQUIRE_EQUAL("dev-vcs", cats.back()->name); BOOST_REQUIRE_EQUAL(136, cats.front()->packagecount); BOOST_REQUIRE_EQUAL(88, cats.back()->packagecount); - cats = p->getOverviewCategoriesInSuper("net", Ice::Current()); + cats = p->getOverviewCategoriesInSuper("net"); BOOST_REQUIRE_EQUAL(1, cats.size()); - cats = p->getOverviewCategoriesInSuper("sys", Ice::Current()); + cats = p->getOverviewCategoriesInSuper("sys"); BOOST_REQUIRE_EQUAL(0, cats.size()); } BOOST_AUTO_TEST_CASE( getPackageMissing ) { - BOOST_REQUIRE_THROW(p->getPackage(0, Ice::Current()), std::exception); + BOOST_REQUIRE_THROW(p->getPackage(0), std::exception); } BOOST_AUTO_TEST_CASE( getPackage ) { - auto pkg = p->getPackage(53258, Ice::Current()); + auto pkg = p->getPackage(53258); BOOST_REQUIRE(pkg); BOOST_REQUIRE_EQUAL(53258, pkg->packageid); BOOST_REQUIRE_EQUAL(311, pkg->categoryid); @@ -99,20 +80,20 @@ BOOST_AUTO_TEST_CASE( getPackage ) BOOST_AUTO_TEST_CASE( findPackage ) { - auto cat = p->findPackage("not", "here", Ice::Current()); + auto cat = p->findPackage("not", "here"); BOOST_REQUIRE(!cat); } BOOST_AUTO_TEST_CASE( findPackageMissing ) { - auto pkg = p->findPackage("dev-vcs", "archway", Ice::Current()); + auto pkg = p->findPackage("dev-vcs", "archway"); BOOST_REQUIRE(pkg); BOOST_REQUIRE_EQUAL(53258, (*pkg)->packageid); } BOOST_AUTO_TEST_CASE( getPackagesInCategory ) { - auto pkgs = p->getPackagesInCategory(311, Ice::Current()); + auto pkgs = p->getPackagesInCategory(311); BOOST_REQUIRE_EQUAL(88, pkgs.size()); BOOST_REQUIRE_EQUAL("archway", pkgs.front()->name); BOOST_REQUIRE_EQUAL("veracity", pkgs.back()->name); @@ -120,13 +101,13 @@ BOOST_AUTO_TEST_CASE( getPackagesInCategory ) BOOST_AUTO_TEST_CASE( searchEmpty ) { - auto matches = p->getPackagesSearch("", Ice::Current()); + auto matches = p->getPackagesSearch(""); BOOST_REQUIRE(matches.empty()); } BOOST_AUTO_TEST_CASE( search ) { - auto matches = p->getPackagesSearch("git", Ice::Current()); + auto matches = p->getPackagesSearch("git"); BOOST_REQUIRE_EQUAL(40, matches.size()); BOOST_REQUIRE_EQUAL(58252, matches.front()->packageid); BOOST_REQUIRE_CLOSE(0.72, matches.front()->rank, 1.0); @@ -134,7 +115,7 @@ BOOST_AUTO_TEST_CASE( search ) BOOST_AUTO_TEST_CASE( searchNotFound ) { - auto matches = p->getPackagesSearch("something that doesn't exist", Ice::Current()); + auto matches = p->getPackagesSearch("something that doesn't exist"); BOOST_REQUIRE(matches.empty()); } @@ -259,22 +240,5 @@ BOOST_AUTO_TEST_CASE( getNewsRecent ) BOOST_REQUIRE_EQUAL("2015-10-07T00:00:00", ns.back()->posted); } -BOOST_AUTO_TEST_CASE( cache ) -{ - auto c1 = p->getCategory(311, Ice::Current()); - BOOST_REQUIRE(c1); - BOOST_REQUIRE_EQUAL(311, c1->categoryid); - auto c2 = p->getCategory(311, Ice::Current()); - BOOST_REQUIRE(c2); - BOOST_REQUIRE_EQUAL(311, c2->categoryid); - BOOST_REQUIRE_EQUAL(c1.get(), c2.get()); - auto c3 = p->getCategory(316, Ice::Current()); - BOOST_REQUIRE(c3); - BOOST_REQUIRE_EQUAL(316, c3->categoryid); - BOOST_REQUIRE(c1.get() != c3.get()); - BOOST_REQUIRE_EQUAL(311, c1->categoryid); - BOOST_REQUIRE_EQUAL(311, c2->categoryid); -} - BOOST_AUTO_TEST_SUITE_END(); -- cgit v1.2.3