blob: 03c5de8d06485e2ffacf7329d726ee1506276796 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, Regents of the University of California.
Zhenkai Zhuda686882013-01-29 22:32:24 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Zhenkai Zhuda686882013-01-29 22:32:24 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * ChronoShare is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
Zhenkai Zhuda686882013-01-29 22:32:24 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
Zhenkai Zhuda686882013-01-29 22:32:24 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * You should have received copies of the GNU General Public License along with
16 * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ChronoShare authors and contributors.
Zhenkai Zhuda686882013-01-29 22:32:24 -080019 */
20
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080021#include "fetch-task-db.hpp"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080022#include "logging.hpp"
Zhenkai Zhuda686882013-01-29 22:32:24 -080023
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080024#include <boost/bind.hpp>
Zhenkai Zhuda686882013-01-29 22:32:24 -080025#include <boost/filesystem.hpp>
26#include <boost/filesystem/fstream.hpp>
27#include <boost/function.hpp>
Zhenkai Zhuda686882013-01-29 22:32:24 -080028
Zhenkai Zhuda686882013-01-29 22:32:24 -080029#include <boost/make_shared.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080030#include <boost/test/unit_test.hpp>
Zhenkai Zhuda686882013-01-29 22:32:24 -080031#include <iostream>
32#include <iterator>
33#include <map>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080034#include <unistd.h>
Zhenkai Zhuda686882013-01-29 22:32:24 -080035#include <utility>
36
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080037INIT_LOGGER("Test.FetchTaskDb");
Zhenkai Zhuda686882013-01-29 22:32:24 -080038
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070039using namespace Ndnx;
Zhenkai Zhuda686882013-01-29 22:32:24 -080040using namespace std;
41using namespace boost;
42namespace fs = boost::filesystem;
43
44BOOST_AUTO_TEST_SUITE(TestFetchTaskDb)
45
46class Checker
47{
48public:
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080049 Checker(const Name& deviceName, const Name& baseName, uint64_t minSeqNo, uint64_t maxSeqNo,
50 int priority)
51 : m_deviceName(deviceName)
52 , m_baseName(baseName)
53 , m_minSeqNo(minSeqNo)
54 , m_maxSeqNo(maxSeqNo)
55 , m_priority(priority)
56 {
57 }
Zhenkai Zhuda686882013-01-29 22:32:24 -080058
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080059 Checker(const Checker& other)
60 : m_deviceName(other.m_deviceName)
61 , m_baseName(other.m_baseName)
62 , m_minSeqNo(other.m_minSeqNo)
63 , m_maxSeqNo(other.m_maxSeqNo)
64 , m_priority(other.m_priority)
65 {
66 }
Zhenkai Zhuda686882013-01-29 22:32:24 -080067
68 bool
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080069 operator==(const Checker& other)
Zhenkai Zhuda686882013-01-29 22:32:24 -080070 {
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080071 return m_deviceName == other.m_deviceName && m_baseName == other.m_baseName &&
72 m_minSeqNo == other.m_minSeqNo && m_maxSeqNo == other.m_maxSeqNo &&
73 m_priority == other.m_priority;
74 }
75
76 void
77 show()
78 {
79 cout << m_deviceName << ", " << m_baseName << ", " << m_minSeqNo << ", " << m_maxSeqNo << ", "
80 << m_priority << endl;
Zhenkai Zhuda686882013-01-29 22:32:24 -080081 }
82
83 Name m_deviceName;
84 Name m_baseName;
85 uint64_t m_minSeqNo;
86 uint64_t m_maxSeqNo;
87 int m_priority;
88};
89
90map<Name, Checker> checkers;
91int g_counter = 0;
92
93void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080094getChecker(const Name& deviceName, const Name& baseName, uint64_t minSeqNo, uint64_t maxSeqNo,
95 int priority)
Zhenkai Zhuda686882013-01-29 22:32:24 -080096{
97 Checker checker(deviceName, baseName, minSeqNo, maxSeqNo, priority);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080098 g_counter++;
99 if (checkers.find(checker.m_deviceName + checker.m_baseName) != checkers.end()) {
Zhenkai Zhuda686882013-01-29 22:32:24 -0800100 BOOST_FAIL("duplicated checkers");
101 }
102 checkers.insert(make_pair(checker.m_deviceName + checker.m_baseName, checker));
103}
104
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800105BOOST_AUTO_TEST_CASE(FetchTaskDbTest)
Zhenkai Zhuda686882013-01-29 22:32:24 -0800106{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800107 INIT_LOGGERS();
Zhenkai Zhuda686882013-01-29 22:32:24 -0800108 fs::path folder("TaskDbTest");
109 fs::create_directories(folder / ".chronoshare");
110
111 FetchTaskDbPtr db = make_shared<FetchTaskDb>(folder, "test");
112
113 map<Name, Checker> m1;
114 g_counter = 0;
115
116 checkers.clear();
117
118 Name deviceNamePrefix("/device");
119 Name baseNamePrefix("/device/base");
120
121 // add 10 tasks
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800122 for (uint64_t i = 0; i < 10; i++) {
Zhenkai Zhuda686882013-01-29 22:32:24 -0800123 Name d = deviceNamePrefix;
124 Name b = baseNamePrefix;
125 Checker c(d.appendComp(i), b.appendComp(i), i, 11, 1);
126 m1.insert(make_pair(d + b, c));
127 db->addTask(c.m_deviceName, c.m_baseName, c.m_minSeqNo, c.m_maxSeqNo, c.m_priority);
128 }
129
130 // delete the latter 5
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800131 for (uint64_t i = 5; i < 10; i++) {
Zhenkai Zhuda686882013-01-29 22:32:24 -0800132 Name d = deviceNamePrefix;
133 Name b = baseNamePrefix;
134 d.appendComp(i);
135 b.appendComp(i);
136 db->deleteTask(d, b);
137 }
138
139 // add back 3 to 7, 3 and 4 should not be added twice
140
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800141 for (uint64_t i = 3; i < 8; i++) {
Zhenkai Zhuda686882013-01-29 22:32:24 -0800142 Name d = deviceNamePrefix;
143 Name b = baseNamePrefix;
144 Checker c(d.appendComp(i), b.appendComp(i), i, 11, 1);
145 db->addTask(c.m_deviceName, c.m_baseName, c.m_minSeqNo, c.m_maxSeqNo, c.m_priority);
146 }
147
148 db->foreachTask(bind(getChecker, _1, _2, _3, _4, _5));
149
150 BOOST_CHECK_EQUAL(g_counter, 8);
151
152 map<Name, Checker>::iterator it = checkers.begin();
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800153 while (it != checkers.end()) {
Zhenkai Zhuda686882013-01-29 22:32:24 -0800154 map<Name, Checker>::iterator mt = m1.find(it->first);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800155 if (mt == m1.end()) {
Zhenkai Zhuda686882013-01-29 22:32:24 -0800156 BOOST_FAIL("unknown task found");
157 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800158 else {
Zhenkai Zhuda686882013-01-29 22:32:24 -0800159 Checker c1 = it->second;
160 Checker c2 = mt->second;
161 BOOST_CHECK(c1 == c2);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800162 if (!(c1 == c2)) {
Zhenkai Zhuda686882013-01-29 22:32:24 -0800163 cout << "C1: " << endl;
164 c1.show();
165 cout << "C2: " << endl;
166 c2.show();
167 }
168 }
169 ++it;
170 }
171 fs::remove_all(folder);
172}
173
174
175BOOST_AUTO_TEST_SUITE_END()