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 "logging.hpp" |
| 22 | #include "fetch-task-db.hpp" |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 23 | |
| 24 | #include <boost/filesystem.hpp> |
| 25 | #include <boost/filesystem/fstream.hpp> |
| 26 | #include <boost/function.hpp> |
| 27 | #include <boost/bind.hpp> |
| 28 | |
| 29 | #include <boost/test/unit_test.hpp> |
| 30 | #include <unistd.h> |
| 31 | #include <boost/make_shared.hpp> |
| 32 | #include <iostream> |
| 33 | #include <iterator> |
| 34 | #include <map> |
| 35 | #include <utility> |
| 36 | |
| 37 | INIT_LOGGER ("Test.FetchTaskDb"); |
| 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: |
| 49 | Checker(const Name &deviceName, const Name &baseName, uint64_t minSeqNo, uint64_t maxSeqNo, int priority) |
| 50 | : m_deviceName(deviceName), m_baseName(baseName), m_minSeqNo(minSeqNo), m_maxSeqNo(maxSeqNo), m_priority(priority) |
| 51 | {} |
| 52 | |
| 53 | Checker(const Checker &other) |
| 54 | : m_deviceName(other.m_deviceName), m_baseName(other.m_baseName), m_minSeqNo(other.m_minSeqNo), m_maxSeqNo(other.m_maxSeqNo), m_priority(other.m_priority) |
| 55 | {} |
| 56 | |
| 57 | bool |
| 58 | operator==(const Checker &other) { return m_deviceName == other.m_deviceName && m_baseName == other.m_baseName && m_minSeqNo == other.m_minSeqNo && m_maxSeqNo == other.m_maxSeqNo && m_priority == other.m_priority; } |
| 59 | |
| 60 | void show() |
| 61 | { |
| 62 | cout << m_deviceName <<", " << m_baseName << ", " << m_minSeqNo << ", " << m_maxSeqNo << ", " << m_priority << endl; |
| 63 | } |
| 64 | |
| 65 | Name m_deviceName; |
| 66 | Name m_baseName; |
| 67 | uint64_t m_minSeqNo; |
| 68 | uint64_t m_maxSeqNo; |
| 69 | int m_priority; |
| 70 | }; |
| 71 | |
| 72 | map<Name, Checker> checkers; |
| 73 | int g_counter = 0; |
| 74 | |
| 75 | void |
| 76 | getChecker(const Name &deviceName, const Name &baseName, uint64_t minSeqNo, uint64_t maxSeqNo, int priority) |
| 77 | { |
| 78 | Checker checker(deviceName, baseName, minSeqNo, maxSeqNo, priority); |
| 79 | g_counter ++; |
| 80 | if (checkers.find(checker.m_deviceName + checker.m_baseName) != checkers.end()) |
| 81 | { |
| 82 | BOOST_FAIL("duplicated checkers"); |
| 83 | } |
| 84 | checkers.insert(make_pair(checker.m_deviceName + checker.m_baseName, checker)); |
| 85 | } |
| 86 | |
| 87 | BOOST_AUTO_TEST_CASE (FetchTaskDbTest) |
| 88 | { |
| 89 | INIT_LOGGERS (); |
| 90 | fs::path folder("TaskDbTest"); |
| 91 | fs::create_directories(folder / ".chronoshare"); |
| 92 | |
| 93 | FetchTaskDbPtr db = make_shared<FetchTaskDb>(folder, "test"); |
| 94 | |
| 95 | map<Name, Checker> m1; |
| 96 | g_counter = 0; |
| 97 | |
| 98 | checkers.clear(); |
| 99 | |
| 100 | Name deviceNamePrefix("/device"); |
| 101 | Name baseNamePrefix("/device/base"); |
| 102 | |
| 103 | // add 10 tasks |
| 104 | for (uint64_t i = 0; i < 10; i++) |
| 105 | { |
| 106 | Name d = deviceNamePrefix; |
| 107 | Name b = baseNamePrefix; |
| 108 | Checker c(d.appendComp(i), b.appendComp(i), i, 11, 1); |
| 109 | m1.insert(make_pair(d + b, c)); |
| 110 | db->addTask(c.m_deviceName, c.m_baseName, c.m_minSeqNo, c.m_maxSeqNo, c.m_priority); |
| 111 | } |
| 112 | |
| 113 | // delete the latter 5 |
| 114 | for (uint64_t i = 5; i < 10; i++) |
| 115 | { |
| 116 | Name d = deviceNamePrefix; |
| 117 | Name b = baseNamePrefix; |
| 118 | d.appendComp(i); |
| 119 | b.appendComp(i); |
| 120 | db->deleteTask(d, b); |
| 121 | } |
| 122 | |
| 123 | // add back 3 to 7, 3 and 4 should not be added twice |
| 124 | |
| 125 | for (uint64_t i = 3; i < 8; i++) |
| 126 | { |
| 127 | Name d = deviceNamePrefix; |
| 128 | Name b = baseNamePrefix; |
| 129 | Checker c(d.appendComp(i), b.appendComp(i), i, 11, 1); |
| 130 | db->addTask(c.m_deviceName, c.m_baseName, c.m_minSeqNo, c.m_maxSeqNo, c.m_priority); |
| 131 | } |
| 132 | |
| 133 | db->foreachTask(bind(getChecker, _1, _2, _3, _4, _5)); |
| 134 | |
| 135 | BOOST_CHECK_EQUAL(g_counter, 8); |
| 136 | |
| 137 | map<Name, Checker>::iterator it = checkers.begin(); |
| 138 | while (it != checkers.end()) |
| 139 | { |
| 140 | map<Name, Checker>::iterator mt = m1.find(it->first); |
| 141 | if (mt == m1.end()) |
| 142 | { |
| 143 | BOOST_FAIL("unknown task found"); |
| 144 | } |
| 145 | else |
| 146 | { |
| 147 | Checker c1 = it->second; |
| 148 | Checker c2 = mt->second; |
| 149 | BOOST_CHECK(c1 == c2); |
| 150 | if (! (c1 == c2)) |
| 151 | { |
| 152 | cout << "C1: " << endl; |
| 153 | c1.show(); |
| 154 | cout << "C2: " << endl; |
| 155 | c2.show(); |
| 156 | } |
| 157 | } |
| 158 | ++it; |
| 159 | } |
| 160 | fs::remove_all(folder); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | BOOST_AUTO_TEST_SUITE_END() |