Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, 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" |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 22 | |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 23 | #include "test-common.hpp" |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 24 | |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 25 | namespace ndn { |
| 26 | namespace chronoshare { |
| 27 | namespace tests { |
| 28 | |
| 29 | namespace fs = boost::filesystem; |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame] | 31 | _LOG_INIT(Test.FetchTaskDb); |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 32 | |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 33 | BOOST_AUTO_TEST_SUITE(TestFetchTaskDb) |
| 34 | |
| 35 | class Checker |
| 36 | { |
| 37 | public: |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 38 | Checker(const Name& deviceName, const Name& baseName, uint64_t minSeqNo, uint64_t maxSeqNo, int priority) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 39 | : m_deviceName(deviceName) |
| 40 | , m_baseName(baseName) |
| 41 | , m_minSeqNo(minSeqNo) |
| 42 | , m_maxSeqNo(maxSeqNo) |
| 43 | , m_priority(priority) |
| 44 | { |
| 45 | } |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 46 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 47 | 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 Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 55 | |
| 56 | bool |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 57 | operator==(const Checker& other) |
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 | 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 Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 67 | std::cout << m_deviceName << ", " << m_baseName << ", " << m_minSeqNo << ", " << m_maxSeqNo << ", " |
| 68 | << m_priority << std::endl; |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 69 | } |
| 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 Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 78 | std::map<Name, Checker> checkers; |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 79 | int g_counter = 0; |
| 80 | |
| 81 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 82 | getChecker(const Name& deviceName, const Name& baseName, uint64_t minSeqNo, uint64_t maxSeqNo, |
| 83 | int priority) |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 84 | { |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 85 | _LOG_DEBUG("deviceName: " << deviceName << " baseName "); |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 86 | Checker checker(deviceName, baseName, minSeqNo, maxSeqNo, priority); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 87 | g_counter++; |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 88 | Name whole(checker.m_deviceName); |
| 89 | whole.append(checker.m_baseName); |
| 90 | if (checkers.find(whole) != checkers.end()) { |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 91 | BOOST_FAIL("duplicated checkers"); |
| 92 | } |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 93 | |
| 94 | checkers.insert(make_pair(whole, checker)); |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 97 | BOOST_AUTO_TEST_CASE(FetchTaskDbTest) |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 98 | { |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 99 | fs::path tmpdir = fs::unique_path(UNIT_TEST_CONFIG_PATH) / "TaskDbTest"; |
| 100 | if (exists(tmpdir)) { |
| 101 | remove_all(tmpdir); |
| 102 | } |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 103 | |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 104 | fs::create_directories(tmpdir / ".chronoshare"); |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 105 | |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 106 | FetchTaskDbPtr db = make_shared<FetchTaskDb>(tmpdir, "test"); |
| 107 | |
| 108 | std::map<Name, Checker> m1; |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 109 | g_counter = 0; |
| 110 | |
| 111 | checkers.clear(); |
| 112 | |
| 113 | Name deviceNamePrefix("/device"); |
| 114 | Name baseNamePrefix("/device/base"); |
| 115 | |
| 116 | // add 10 tasks |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 117 | for (uint64_t i = 0; i < 10; i++) { |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 118 | Name d = deviceNamePrefix; |
| 119 | Name b = baseNamePrefix; |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 120 | Checker c(d.appendNumber(i), b.appendNumber(i), i, 11, 1); |
| 121 | m1.insert(make_pair(d.append(b), c)); |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 122 | 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 Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 126 | for (uint64_t i = 5; i < 10; i++) { |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 127 | Name d = deviceNamePrefix; |
| 128 | Name b = baseNamePrefix; |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 129 | d.appendNumber(i); |
| 130 | b.appendNumber(i); |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 131 | db->deleteTask(d, b); |
| 132 | } |
| 133 | |
| 134 | // add back 3 to 7, 3 and 4 should not be added twice |
| 135 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 136 | for (uint64_t i = 3; i < 8; i++) { |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 137 | Name d = deviceNamePrefix; |
| 138 | Name b = baseNamePrefix; |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 139 | Checker c(d.appendNumber(i), b.appendNumber(i), i, 11, 1); |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 140 | 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 Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 147 | std::map<Name, Checker>::iterator it = checkers.begin(); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 148 | while (it != checkers.end()) { |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 149 | _LOG_DEBUG("first -> " << it->first); |
| 150 | std::map<Name, Checker>::iterator mt = m1.find(it->first); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 151 | if (mt == m1.end()) { |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 152 | BOOST_FAIL("unknown task found"); |
| 153 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 154 | else { |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 155 | Checker c1 = it->second; |
| 156 | Checker c2 = mt->second; |
| 157 | BOOST_CHECK(c1 == c2); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 158 | if (!(c1 == c2)) { |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 159 | std::cout << "C1: " << std::endl; |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 160 | c1.show(); |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 161 | std::cout << "C2: " << std::endl; |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 162 | c2.show(); |
| 163 | } |
| 164 | } |
| 165 | ++it; |
| 166 | } |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 169 | BOOST_AUTO_TEST_SUITE_END() |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 170 | |
| 171 | } // namespace tests |
| 172 | } // namespace chronoshare |
| 173 | } // namespace ndn |