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
27using namespace o2::ccdb;
28
29static string basePath;
30std::string ccdbUrl = "http://ccdb-test.cern.ch:8080";
31bool hostReachable = false;
32
36struct Fixture {
38 {
39 CcdbApi api;
40 api.init(ccdbUrl);
41 std::cout << "ccdb url: " << ccdbUrl << std::endl;
43 std::cout << "Is host reachable ? --> " << hostReachable << std::endl;
44 char hostname[_POSIX_HOST_NAME_MAX];
45 gethostname(hostname, _POSIX_HOST_NAME_MAX);
46 basePath = string("Test/") + hostname + "/pid" + getpid() + "/BasicCCDBManager/";
47 std::cout << "Path we will use in this test suite : " + basePath << std::endl;
48 }
50 {
51 if (hostReachable) {
52 CcdbApi api;
53 api.init(ccdbUrl);
54 api.truncate(basePath + "*");
55 std::cout << "Test data truncated (" << basePath << ")" << std::endl;
56 }
57 }
58};
60
61BOOST_AUTO_TEST_CASE(TestBasicCCDBManager)
62{
63 CcdbApi api;
64 api.init(ccdbUrl);
65 if (!api.isHostReachable()) {
66 LOG(warning) << "Host " << ccdbUrl << " is not reacheable, abandoning the test";
67 return;
68 }
69 //
70 std::string pathA = basePath + "CachingA";
71 std::string pathB = basePath + "CachingB";
72 std::string ccdbObjO = "testObjectO";
73 std::string ccdbObjN = "testObjectN";
74 std::map<std::string, std::string> md;
75 long start = 1000, stop = 2000;
76 api.storeAsTFileAny(&ccdbObjO, pathA, md, start, stop);
77 api.storeAsTFileAny(&ccdbObjN, pathA, md, stop, stop + (stop - start)); // extra slot
78 api.storeAsTFileAny(&ccdbObjO, pathB, md, start, stop);
79
80 // test reading
82 cdb.setURL(ccdbUrl);
83 cdb.setTimestamp((start + stop) / 2);
84 cdb.setCaching(true);
85
86 auto* objA = cdb.get<std::string>(pathA); // will be loaded from scratch and fill the cache
87 LOG(info) << "1st reading of A: " << *objA;
88 BOOST_CHECK(objA && (*objA) == ccdbObjO); // make sure correct object is loaded
89
90 auto* objB = cdb.get<std::string>(pathB); // will be loaded from scratch and fill the cache
91 BOOST_CHECK(objB && (*objB) == ccdbObjO); // make sure correct object is loaded
92
93 std::string hack = "Cached";
94 (*objA) = hack;
95 (*objB) = hack;
96 objA = cdb.get<std::string>(pathA); // should get already cached and hacked object
97 LOG(info) << "Reading of cached and modified A: " << *objA;
98 BOOST_CHECK(objA && (*objA) == hack); // make sure correct object is loaded
99
100 // now check wrong object reading, 0 will be returned and cache will be cleaned
101 cdb.setFatalWhenNull(false);
102 objA = cdb.getForTimeStamp<std::string>(pathA, start - (stop - start) / 2); // wrong time
103 LOG(info) << "Read for wrong time, expect null: " << objA;
104 BOOST_CHECK(objA == nullptr);
105 cdb.setFatalWhenNull(true);
106 objA = cdb.get<std::string>(pathA); // cache again
107 LOG(info) << "Reading of A from scratch after error: " << *objA;
108 BOOST_CHECK(objA && (*objA) != hack); // make sure we did not get cached object
109 (*objA) = hack;
110
111 // read object from another time slot
112 objA = cdb.getForTimeStamp<std::string>(pathA, stop + (stop - start) / 2); // will be loaded from scratch and fill the cache
113 LOG(info) << "Reading of A for different time slost, expect non-cached object: " << *objA;
114 BOOST_CHECK(objA && (*objA) == ccdbObjN); // make sure correct object is loaded
115
116 // clear specific object cache
117 cdb.clearCache(pathA);
118 objA = cdb.get<std::string>(pathA); // will be loaded from scratch and fill the cache
119 LOG(info) << "Reading of A after cleaning its cache, expect non-cached object: " << *objA;
120 BOOST_CHECK(objA && (*objA) == ccdbObjO); // make sure correct object is loaded
121 (*objA) = hack;
122 objA = cdb.get<std::string>(pathA); // should get already cached and hacked object
123 LOG(info) << "Reading same A, expect cached and modified value: " << *objA;
124 BOOST_CHECK(objA && (*objA) == hack); // make sure correct object is loaded
125
126 objB = cdb.get<std::string>(pathB); // should get already cached and hacked object, since is was not reset
127 LOG(info) << "Reading B, expect cached since only A cache was cleaned: " << *objB;
128 BOOST_CHECK(objB && (*objB) == hack); // make sure correct object is loaded
129
130 // clear all caches
131 cdb.clearCache();
132 objB = cdb.get<std::string>(pathB); // will be loaded from scratch and fill the cache
133 LOG(info) << "Reading B after cleaning cache completely: " << *objB;
134 BOOST_CHECK(objB && (*objB) == ccdbObjO); // make sure correct object is loaded
135
136 // get object in TimeMachine mode in the past
137 cdb.setCreatedNotAfter(1); // set upper object validity
138 cdb.setFatalWhenNull(false);
139 objA = cdb.get<std::string>(pathA); // should not be loaded
140 BOOST_CHECK(!objA); // make sure correct object is not loaded
141 cdb.resetCreatedNotAfter(); // resetting upper validity limit
142
143 // get object in TimeMachine mode in the future
144 cdb.setCreatedNotBefore(4108971600000); // set upper object validity
145 objA = cdb.get<std::string>(pathA); // should not be loaded
146 BOOST_CHECK(!objA); // make sure correct object is not loaded
147 cdb.resetCreatedNotBefore(); // resetting upper validity limit
148 cdb.setFatalWhenNull(true);
149
150 // disable cache at all (will also clean it)
151 cdb.setCaching(false);
152 objA = cdb.get<std::string>(pathA); // will be loaded from scratch, w/o filling the cache
153 LOG(info) << "Reading A after disabling the cache: " << *objA;
154 BOOST_CHECK(objA && (*objA) == ccdbObjO); // make sure correct object is loaded
155 (*objA) = hack;
156 objA = cdb.get<std::string>(pathA); // will be loaded from scratch
157 LOG(info) << "Reading A again, it should not be cached: " << *objA;
158 BOOST_CHECK(objA && (*objA) != hack); // make sure correct object is loaded
159}
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:157
void init(std::string const &hosts)
Definition CcdbApi.cxx:165
bool isHostReachable() const
Definition CcdbApi.cxx:1301
void truncate(std::string const &path) const
Definition CcdbApi.cxx:1270
GLsizei const GLchar *const * string
Definition glcorearb.h:809
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)