Project
Loading...
Searching...
No Matches
testBasicCCDBManager.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
17
18#define BOOST_TEST_MODULE CCDB
19#define BOOST_TEST_MAIN
20#define BOOST_TEST_DYN_LINK
21
22#include "CCDB/CcdbApi.h"
24#include "Framework/Logger.h"
25#include <boost/test/unit_test.hpp>
26#include <cstdlib>
27
28using namespace o2::ccdb;
29
30static std::string basePath;
31std::string ccdbUrl = "http://ccdb-test.cern.ch:8080";
32bool hostReachable = false;
33
37struct Fixture {
39 {
40 CcdbApi api;
41 // These suites upload, so they need a WRITABLE instance -- ccdb-test by
42 // default, not the official CCDB.
43 if (const char* host = std::getenv("ALICEO2_CCDB_HOST")) {
44 ccdbUrl = host;
45 }
46 api.init(ccdbUrl);
47 std::cout << "ccdb url: " << ccdbUrl << std::endl;
49 std::cout << "Is host reachable ? --> " << hostReachable << std::endl;
50 char hostname[_POSIX_HOST_NAME_MAX];
51 gethostname(hostname, _POSIX_HOST_NAME_MAX);
52 basePath = std::string("Test/") + hostname + "/pid" + getpid() + "/BasicCCDBManager/";
53 std::cout << "Path we will use in this test suite : " + basePath << std::endl;
54 }
56 {
57 if (hostReachable) {
58 CcdbApi api;
59 api.init(ccdbUrl);
60 api.truncate(basePath + "*");
61 std::cout << "Test data truncated (" << basePath << ")" << std::endl;
62 }
63 }
64};
66
67BOOST_AUTO_TEST_CASE(TestBasicCCDBManager)
68{
69 CcdbApi api;
70 api.init(ccdbUrl);
71 if (!api.isHostReachable()) {
72 LOG(warning) << "Host " << ccdbUrl << " is not reacheable, abandoning the test";
73 return;
74 }
75 //
76 std::string pathA = basePath + "CachingA";
77 std::string pathB = basePath + "CachingB";
78 std::string ccdbObjO = "testObjectO";
79 std::string ccdbObjN = "testObjectN";
80 std::map<std::string, std::string> md;
81 long start = 1000, stop = 2000;
82 api.storeAsTFileAny(&ccdbObjO, pathA, md, start, stop);
83 api.storeAsTFileAny(&ccdbObjN, pathA, md, stop, stop + (stop - start)); // extra slot
84 api.storeAsTFileAny(&ccdbObjO, pathB, md, start, stop);
85
86 // test reading
88 cdb.setURL(ccdbUrl);
89 cdb.setTimestamp((start + stop) / 2);
90 cdb.setCaching(true);
91
92 auto* objA = cdb.get<std::string>(pathA); // will be loaded from scratch and fill the cache
93 LOG(info) << "1st reading of A: " << *objA;
94 BOOST_CHECK(objA && (*objA) == ccdbObjO); // make sure correct object is loaded
95
96 auto* objB = cdb.get<std::string>(pathB); // will be loaded from scratch and fill the cache
97 BOOST_CHECK(objB && (*objB) == ccdbObjO); // make sure correct object is loaded
98
99 std::string hack = "Cached";
100 (*objA) = hack;
101 (*objB) = hack;
102 objA = cdb.get<std::string>(pathA); // should get already cached and hacked object
103 LOG(info) << "Reading of cached and modified A: " << *objA;
104 BOOST_CHECK(objA && (*objA) == hack); // make sure correct object is loaded
105
106 // now check wrong object reading, 0 will be returned and cache will be cleaned
107 cdb.setFatalWhenNull(false);
108 objA = cdb.getForTimeStamp<std::string>(pathA, start - (stop - start) / 2); // wrong time
109 LOG(info) << "Read for wrong time, expect null: " << objA;
110 BOOST_CHECK(objA == nullptr);
111 cdb.setFatalWhenNull(true);
112 objA = cdb.get<std::string>(pathA); // cache again
113 LOG(info) << "Reading of A from scratch after error: " << *objA;
114 BOOST_CHECK(objA && (*objA) != hack); // make sure we did not get cached object
115 (*objA) = hack;
116
117 // read object from another time slot
118 objA = cdb.getForTimeStamp<std::string>(pathA, stop + (stop - start) / 2); // will be loaded from scratch and fill the cache
119 LOG(info) << "Reading of A for different time slost, expect non-cached object: " << *objA;
120 BOOST_CHECK(objA && (*objA) == ccdbObjN); // make sure correct object is loaded
121
122 // clear specific object cache
123 cdb.clearCache(pathA);
124 objA = cdb.get<std::string>(pathA); // will be loaded from scratch and fill the cache
125 LOG(info) << "Reading of A after cleaning its cache, expect non-cached object: " << *objA;
126 BOOST_CHECK(objA && (*objA) == ccdbObjO); // make sure correct object is loaded
127 (*objA) = hack;
128 objA = cdb.get<std::string>(pathA); // should get already cached and hacked object
129 LOG(info) << "Reading same A, expect cached and modified value: " << *objA;
130 BOOST_CHECK(objA && (*objA) == hack); // make sure correct object is loaded
131
132 objB = cdb.get<std::string>(pathB); // should get already cached and hacked object, since is was not reset
133 LOG(info) << "Reading B, expect cached since only A cache was cleaned: " << *objB;
134 BOOST_CHECK(objB && (*objB) == hack); // make sure correct object is loaded
135
136 // clear all caches
137 cdb.clearCache();
138 objB = cdb.get<std::string>(pathB); // will be loaded from scratch and fill the cache
139 LOG(info) << "Reading B after cleaning cache completely: " << *objB;
140 BOOST_CHECK(objB && (*objB) == ccdbObjO); // make sure correct object is loaded
141
142 // get object in TimeMachine mode in the past
143 cdb.setCreatedNotAfter(1); // set upper object validity
144 cdb.setFatalWhenNull(false);
145 objA = cdb.get<std::string>(pathA); // should not be loaded
146 BOOST_CHECK(!objA); // make sure correct object is not loaded
147 cdb.resetCreatedNotAfter(); // resetting upper validity limit
148
149 // get object in TimeMachine mode in the future
150 cdb.setCreatedNotBefore(4108971600000); // set upper object validity
151 objA = cdb.get<std::string>(pathA); // should not be loaded
152 BOOST_CHECK(!objA); // make sure correct object is not loaded
153 cdb.resetCreatedNotBefore(); // resetting upper validity limit
154 cdb.setFatalWhenNull(true);
155
156 // disable cache at all (will also clean it)
157 cdb.setCaching(false);
158 objA = cdb.get<std::string>(pathA); // will be loaded from scratch, w/o filling the cache
159 LOG(info) << "Reading A after disabling the cache: " << *objA;
160 BOOST_CHECK(objA && (*objA) == ccdbObjO); // make sure correct object is loaded
161 (*objA) = hack;
162 objA = cdb.get<std::string>(pathA); // will be loaded from scratch
163 LOG(info) << "Reading A again, it should not be cached: " << *objA;
164 BOOST_CHECK(objA && (*objA) != hack); // make sure correct object is loaded
165}
static BasicCCDBManager & instance()
int storeAsTFileAny(const T *obj, std::string const &path, std::map< std::string, std::string > const &metadata, long startValidityTimestamp=-1, long endValidityTimestamp=-1, std::vector< char >::size_type maxSize=0) const
Definition CcdbApi.h:163
void init(std::string const &hosts)
Definition CcdbApi.cxx:237
bool isHostReachable() const
Definition CcdbApi.cxx:1436
void truncate(std::string const &path) const
Definition CcdbApi.cxx:1394
GLuint GLuint pathB
Definition glcorearb.h:5477
GLuint start
Definition glcorearb.h:469
GLuint pathA
Definition glcorearb.h:5477
information complementary to a CCDB object (path, metadata, startTimeValidity, endTimeValidity etc)
BOOST_AUTO_TEST_CASE(TestBasicCCDBManager)
std::string ccdbUrl
BOOST_GLOBAL_FIXTURE(Fixture)
bool hostReachable
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"
BOOST_CHECK(tree)