blob: 4219e0699ae5ae71c621042fe3a0a52ca3a1c603 [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"
Zhenkai Zhuda686882013-01-29 22:32:24 -080022
Yukai Tu35963e02016-10-24 13:48:01 -070023#include "test-common.hpp"
Zhenkai Zhuda686882013-01-29 22:32:24 -080024
Yukai Tu35963e02016-10-24 13:48:01 -070025namespace ndn {
26namespace chronoshare {
27namespace tests {
28
29namespace fs = boost::filesystem;
Zhenkai Zhuda686882013-01-29 22:32:24 -080030
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -080031_LOG_INIT(Test.FetchTaskDb);
Zhenkai Zhuda686882013-01-29 22:32:24 -080032
Zhenkai Zhuda686882013-01-29 22:32:24 -080033BOOST_AUTO_TEST_SUITE(TestFetchTaskDb)
34
35class Checker
36{
37public:
Yukai Tu35963e02016-10-24 13:48:01 -070038 Checker(const Name& deviceName, const Name& baseName, uint64_t minSeqNo, uint64_t maxSeqNo, int priority)
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080039 : m_deviceName(deviceName)
40 , m_baseName(baseName)
41 , m_minSeqNo(minSeqNo)
42 , m_maxSeqNo(maxSeqNo)
43 , m_priority(priority)
44 {
45 }
Zhenkai Zhuda686882013-01-29 22:32:24 -080046
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080047 Checker(const Checker& other)
48 : m_deviceName(other.m_deviceName)
49 , m_baseName(other.m_baseName)
50 , m_minSeqNo(other.m_minSeqNo)
51 , m_maxSeqNo(other.m_maxSeqNo)
52 , m_priority(other.m_priority)
53 {
54 }
Zhenkai Zhuda686882013-01-29 22:32:24 -080055
56 bool
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080057 operator==(const Checker& other)
Zhenkai Zhuda686882013-01-29 22:32:24 -080058 {
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080059 return m_deviceName == other.m_deviceName && m_baseName == other.m_baseName &&
60 m_minSeqNo == other.m_minSeqNo && m_maxSeqNo == other.m_maxSeqNo &&
61 m_priority == other.m_priority;
62 }
63
64 void
65 show()
66 {
Yukai Tu35963e02016-10-24 13:48:01 -070067 std::cout << m_deviceName << ", " << m_baseName << ", " << m_minSeqNo << ", " << m_maxSeqNo << ", "
68 << m_priority << std::endl;
Zhenkai Zhuda686882013-01-29 22:32:24 -080069 }
70
71 Name m_deviceName;
72 Name m_baseName;
73 uint64_t m_minSeqNo;
74 uint64_t m_maxSeqNo;
75 int m_priority;
76};
77
Yukai Tu35963e02016-10-24 13:48:01 -070078std::map<Name, Checker> checkers;
Zhenkai Zhuda686882013-01-29 22:32:24 -080079int g_counter = 0;
80
81void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080082getChecker(const Name& deviceName, const Name& baseName, uint64_t minSeqNo, uint64_t maxSeqNo,
83 int priority)
Zhenkai Zhuda686882013-01-29 22:32:24 -080084{
Yukai Tu35963e02016-10-24 13:48:01 -070085 _LOG_DEBUG("deviceName: " << deviceName << " baseName ");
Zhenkai Zhuda686882013-01-29 22:32:24 -080086 Checker checker(deviceName, baseName, minSeqNo, maxSeqNo, priority);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080087 g_counter++;
Yukai Tu35963e02016-10-24 13:48:01 -070088 Name whole(checker.m_deviceName);
89 whole.append(checker.m_baseName);
90 if (checkers.find(whole) != checkers.end()) {
Zhenkai Zhuda686882013-01-29 22:32:24 -080091 BOOST_FAIL("duplicated checkers");
92 }
Yukai Tu35963e02016-10-24 13:48:01 -070093
94 checkers.insert(make_pair(whole, checker));
Zhenkai Zhuda686882013-01-29 22:32:24 -080095}
96
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080097BOOST_AUTO_TEST_CASE(FetchTaskDbTest)
Zhenkai Zhuda686882013-01-29 22:32:24 -080098{
Yukai Tu35963e02016-10-24 13:48:01 -070099 fs::path tmpdir = fs::unique_path(UNIT_TEST_CONFIG_PATH) / "TaskDbTest";
100 if (exists(tmpdir)) {
101 remove_all(tmpdir);
102 }
Zhenkai Zhuda686882013-01-29 22:32:24 -0800103
Yukai Tu35963e02016-10-24 13:48:01 -0700104 fs::create_directories(tmpdir / ".chronoshare");
Zhenkai Zhuda686882013-01-29 22:32:24 -0800105
Yukai Tu35963e02016-10-24 13:48:01 -0700106 FetchTaskDbPtr db = make_shared<FetchTaskDb>(tmpdir, "test");
107
108 std::map<Name, Checker> m1;
Zhenkai Zhuda686882013-01-29 22:32:24 -0800109 g_counter = 0;
110
111 checkers.clear();
112
113 Name deviceNamePrefix("/device");
114 Name baseNamePrefix("/device/base");
115
116 // add 10 tasks
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800117 for (uint64_t i = 0; i < 10; i++) {
Zhenkai Zhuda686882013-01-29 22:32:24 -0800118 Name d = deviceNamePrefix;
119 Name b = baseNamePrefix;
Yukai Tu35963e02016-10-24 13:48:01 -0700120 Checker c(d.appendNumber(i), b.appendNumber(i), i, 11, 1);
121 m1.insert(make_pair(d.append(b), c));
Zhenkai Zhuda686882013-01-29 22:32:24 -0800122 db->addTask(c.m_deviceName, c.m_baseName, c.m_minSeqNo, c.m_maxSeqNo, c.m_priority);
123 }
124
125 // delete the latter 5
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800126 for (uint64_t i = 5; i < 10; i++) {
Zhenkai Zhuda686882013-01-29 22:32:24 -0800127 Name d = deviceNamePrefix;
128 Name b = baseNamePrefix;
Yukai Tu35963e02016-10-24 13:48:01 -0700129 d.appendNumber(i);
130 b.appendNumber(i);
Zhenkai Zhuda686882013-01-29 22:32:24 -0800131 db->deleteTask(d, b);
132 }
133
134 // add back 3 to 7, 3 and 4 should not be added twice
135
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800136 for (uint64_t i = 3; i < 8; i++) {
Zhenkai Zhuda686882013-01-29 22:32:24 -0800137 Name d = deviceNamePrefix;
138 Name b = baseNamePrefix;
Yukai Tu35963e02016-10-24 13:48:01 -0700139 Checker c(d.appendNumber(i), b.appendNumber(i), i, 11, 1);
Zhenkai Zhuda686882013-01-29 22:32:24 -0800140 db->addTask(c.m_deviceName, c.m_baseName, c.m_minSeqNo, c.m_maxSeqNo, c.m_priority);
141 }
142
143 db->foreachTask(bind(getChecker, _1, _2, _3, _4, _5));
144
145 BOOST_CHECK_EQUAL(g_counter, 8);
146
Yukai Tu35963e02016-10-24 13:48:01 -0700147 std::map<Name, Checker>::iterator it = checkers.begin();
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800148 while (it != checkers.end()) {
Yukai Tu35963e02016-10-24 13:48:01 -0700149 _LOG_DEBUG("first -> " << it->first);
150 std::map<Name, Checker>::iterator mt = m1.find(it->first);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800151 if (mt == m1.end()) {
Zhenkai Zhuda686882013-01-29 22:32:24 -0800152 BOOST_FAIL("unknown task found");
153 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800154 else {
Zhenkai Zhuda686882013-01-29 22:32:24 -0800155 Checker c1 = it->second;
156 Checker c2 = mt->second;
157 BOOST_CHECK(c1 == c2);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800158 if (!(c1 == c2)) {
Yukai Tu35963e02016-10-24 13:48:01 -0700159 std::cout << "C1: " << std::endl;
Zhenkai Zhuda686882013-01-29 22:32:24 -0800160 c1.show();
Yukai Tu35963e02016-10-24 13:48:01 -0700161 std::cout << "C2: " << std::endl;
Zhenkai Zhuda686882013-01-29 22:32:24 -0800162 c2.show();
163 }
164 }
165 ++it;
166 }
Zhenkai Zhuda686882013-01-29 22:32:24 -0800167}
168
Zhenkai Zhuda686882013-01-29 22:32:24 -0800169BOOST_AUTO_TEST_SUITE_END()
Yukai Tu35963e02016-10-24 13:48:01 -0700170
171} // namespace tests
172} // namespace chronoshare
173} // namespace ndn