Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016, Regents of the University of California. |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 7 | * 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 Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 11 | * 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 Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 14 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 15 | * 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 Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 21 | #include "fetch-task-db.hpp" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 22 | #include "logging.hpp" |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 23 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 24 | #include <boost/bind.hpp> |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 25 | #include <boost/filesystem.hpp> |
| 26 | #include <boost/filesystem/fstream.hpp> |
| 27 | #include <boost/function.hpp> |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 28 | |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 29 | #include <boost/make_shared.hpp> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 30 | #include <boost/test/unit_test.hpp> |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 31 | #include <iostream> |
| 32 | #include <iterator> |
| 33 | #include <map> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 34 | #include <unistd.h> |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 35 | #include <utility> |
| 36 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 37 | INIT_LOGGER("Test.FetchTaskDb"); |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 38 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 39 | using namespace Ndnx; |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 40 | using namespace std; |
| 41 | using namespace boost; |
| 42 | namespace fs = boost::filesystem; |
| 43 | |
| 44 | BOOST_AUTO_TEST_SUITE(TestFetchTaskDb) |
| 45 | |
| 46 | class Checker |
| 47 | { |
| 48 | public: |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 49 | 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 Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 58 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 59 | 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 Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 67 | |
| 68 | bool |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 69 | operator==(const Checker& other) |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 70 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 71 | 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 Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 81 | } |
| 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 | |
| 90 | map<Name, Checker> checkers; |
| 91 | int g_counter = 0; |
| 92 | |
| 93 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 94 | getChecker(const Name& deviceName, const Name& baseName, uint64_t minSeqNo, uint64_t maxSeqNo, |
| 95 | int priority) |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 96 | { |
| 97 | Checker checker(deviceName, baseName, minSeqNo, maxSeqNo, priority); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 98 | g_counter++; |
| 99 | if (checkers.find(checker.m_deviceName + checker.m_baseName) != checkers.end()) { |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 100 | BOOST_FAIL("duplicated checkers"); |
| 101 | } |
| 102 | checkers.insert(make_pair(checker.m_deviceName + checker.m_baseName, checker)); |
| 103 | } |
| 104 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 105 | BOOST_AUTO_TEST_CASE(FetchTaskDbTest) |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 106 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 107 | INIT_LOGGERS(); |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 108 | 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 Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 122 | for (uint64_t i = 0; i < 10; i++) { |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 123 | 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 Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 131 | for (uint64_t i = 5; i < 10; i++) { |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 132 | 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 Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 141 | for (uint64_t i = 3; i < 8; i++) { |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 142 | 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 Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 153 | while (it != checkers.end()) { |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 154 | map<Name, Checker>::iterator mt = m1.find(it->first); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 155 | if (mt == m1.end()) { |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 156 | BOOST_FAIL("unknown task found"); |
| 157 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 158 | else { |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 159 | Checker c1 = it->second; |
| 160 | Checker c2 = mt->second; |
| 161 | BOOST_CHECK(c1 == c2); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 162 | if (!(c1 == c2)) { |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 163 | 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 | |
| 175 | BOOST_AUTO_TEST_SUITE_END() |