blob: 3fd940e91557f5b5c38771f8568ab5710b17552a [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -08003 * Copyright (c) 2013-2017, 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 Afanasyev1cf5c432017-01-13 23:22:15 -080037_LOG_INIT(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{
Zhenkai Zhuda686882013-01-29 22:32:24 -0800107 fs::path folder("TaskDbTest");
108 fs::create_directories(folder / ".chronoshare");
109
110 FetchTaskDbPtr db = make_shared<FetchTaskDb>(folder, "test");
111
112 map<Name, Checker> m1;
113 g_counter = 0;
114
115 checkers.clear();
116
117 Name deviceNamePrefix("/device");
118 Name baseNamePrefix("/device/base");
119
120 // add 10 tasks
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800121 for (uint64_t i = 0; i < 10; i++) {
Zhenkai Zhuda686882013-01-29 22:32:24 -0800122 Name d = deviceNamePrefix;
123 Name b = baseNamePrefix;
124 Checker c(d.appendComp(i), b.appendComp(i), i, 11, 1);
125 m1.insert(make_pair(d + b, c));
126 db->addTask(c.m_deviceName, c.m_baseName, c.m_minSeqNo, c.m_maxSeqNo, c.m_priority);
127 }
128
129 // delete the latter 5
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800130 for (uint64_t i = 5; i < 10; i++) {
Zhenkai Zhuda686882013-01-29 22:32:24 -0800131 Name d = deviceNamePrefix;
132 Name b = baseNamePrefix;
133 d.appendComp(i);
134 b.appendComp(i);
135 db->deleteTask(d, b);
136 }
137
138 // add back 3 to 7, 3 and 4 should not be added twice
139
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800140 for (uint64_t i = 3; i < 8; i++) {
Zhenkai Zhuda686882013-01-29 22:32:24 -0800141 Name d = deviceNamePrefix;
142 Name b = baseNamePrefix;
143 Checker c(d.appendComp(i), b.appendComp(i), i, 11, 1);
144 db->addTask(c.m_deviceName, c.m_baseName, c.m_minSeqNo, c.m_maxSeqNo, c.m_priority);
145 }
146
147 db->foreachTask(bind(getChecker, _1, _2, _3, _4, _5));
148
149 BOOST_CHECK_EQUAL(g_counter, 8);
150
151 map<Name, Checker>::iterator it = checkers.begin();
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800152 while (it != checkers.end()) {
Zhenkai Zhuda686882013-01-29 22:32:24 -0800153 map<Name, Checker>::iterator mt = m1.find(it->first);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800154 if (mt == m1.end()) {
Zhenkai Zhuda686882013-01-29 22:32:24 -0800155 BOOST_FAIL("unknown task found");
156 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800157 else {
Zhenkai Zhuda686882013-01-29 22:32:24 -0800158 Checker c1 = it->second;
159 Checker c2 = mt->second;
160 BOOST_CHECK(c1 == c2);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800161 if (!(c1 == c2)) {
Zhenkai Zhuda686882013-01-29 22:32:24 -0800162 cout << "C1: " << endl;
163 c1.show();
164 cout << "C2: " << endl;
165 c2.show();
166 }
167 }
168 ++it;
169 }
170 fs::remove_all(folder);
171}
172
173
174BOOST_AUTO_TEST_SUITE_END()