Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | * Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 20 | */ |
| 21 | |
| 22 | #include "logging.h" |
| 23 | #include "fetch-task-db.h" |
| 24 | |
| 25 | #include <boost/filesystem.hpp> |
| 26 | #include <boost/filesystem/fstream.hpp> |
| 27 | #include <boost/function.hpp> |
| 28 | #include <boost/bind.hpp> |
| 29 | |
| 30 | #include <boost/test/unit_test.hpp> |
| 31 | #include <unistd.h> |
| 32 | #include <boost/make_shared.hpp> |
| 33 | #include <iostream> |
| 34 | #include <iterator> |
| 35 | #include <map> |
| 36 | #include <utility> |
| 37 | |
| 38 | INIT_LOGGER ("Test.FetchTaskDb"); |
| 39 | |
| 40 | using namespace Ccnx; |
| 41 | using namespace std; |
| 42 | using namespace boost; |
| 43 | namespace fs = boost::filesystem; |
| 44 | |
| 45 | BOOST_AUTO_TEST_SUITE(TestFetchTaskDb) |
| 46 | |
| 47 | class Checker |
| 48 | { |
| 49 | public: |
| 50 | Checker(const Name &deviceName, const Name &baseName, uint64_t minSeqNo, uint64_t maxSeqNo, int priority) |
| 51 | : m_deviceName(deviceName), m_baseName(baseName), m_minSeqNo(minSeqNo), m_maxSeqNo(maxSeqNo), m_priority(priority) |
| 52 | {} |
| 53 | |
| 54 | Checker(const Checker &other) |
| 55 | : 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) |
| 56 | {} |
| 57 | |
| 58 | bool |
| 59 | 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; } |
| 60 | |
| 61 | void show() |
| 62 | { |
| 63 | cout << m_deviceName <<", " << m_baseName << ", " << m_minSeqNo << ", " << m_maxSeqNo << ", " << m_priority << endl; |
| 64 | } |
| 65 | |
| 66 | Name m_deviceName; |
| 67 | Name m_baseName; |
| 68 | uint64_t m_minSeqNo; |
| 69 | uint64_t m_maxSeqNo; |
| 70 | int m_priority; |
| 71 | }; |
| 72 | |
| 73 | map<Name, Checker> checkers; |
| 74 | int g_counter = 0; |
| 75 | |
| 76 | void |
| 77 | getChecker(const Name &deviceName, const Name &baseName, uint64_t minSeqNo, uint64_t maxSeqNo, int priority) |
| 78 | { |
| 79 | Checker checker(deviceName, baseName, minSeqNo, maxSeqNo, priority); |
| 80 | g_counter ++; |
| 81 | if (checkers.find(checker.m_deviceName + checker.m_baseName) != checkers.end()) |
| 82 | { |
| 83 | BOOST_FAIL("duplicated checkers"); |
| 84 | } |
| 85 | checkers.insert(make_pair(checker.m_deviceName + checker.m_baseName, checker)); |
| 86 | } |
| 87 | |
| 88 | BOOST_AUTO_TEST_CASE (FetchTaskDbTest) |
| 89 | { |
| 90 | INIT_LOGGERS (); |
| 91 | fs::path folder("TaskDbTest"); |
| 92 | fs::create_directories(folder / ".chronoshare"); |
| 93 | |
| 94 | FetchTaskDbPtr db = make_shared<FetchTaskDb>(folder, "test"); |
| 95 | |
| 96 | map<Name, Checker> m1; |
| 97 | g_counter = 0; |
| 98 | |
| 99 | checkers.clear(); |
| 100 | |
| 101 | Name deviceNamePrefix("/device"); |
| 102 | Name baseNamePrefix("/device/base"); |
| 103 | |
| 104 | // add 10 tasks |
| 105 | for (uint64_t i = 0; i < 10; i++) |
| 106 | { |
| 107 | Name d = deviceNamePrefix; |
| 108 | Name b = baseNamePrefix; |
| 109 | Checker c(d.appendComp(i), b.appendComp(i), i, 11, 1); |
| 110 | m1.insert(make_pair(d + b, c)); |
| 111 | db->addTask(c.m_deviceName, c.m_baseName, c.m_minSeqNo, c.m_maxSeqNo, c.m_priority); |
| 112 | } |
| 113 | |
| 114 | // delete the latter 5 |
| 115 | for (uint64_t i = 5; i < 10; i++) |
| 116 | { |
| 117 | Name d = deviceNamePrefix; |
| 118 | Name b = baseNamePrefix; |
| 119 | d.appendComp(i); |
| 120 | b.appendComp(i); |
| 121 | db->deleteTask(d, b); |
| 122 | } |
| 123 | |
| 124 | // add back 3 to 7, 3 and 4 should not be added twice |
| 125 | |
| 126 | for (uint64_t i = 3; i < 8; i++) |
| 127 | { |
| 128 | Name d = deviceNamePrefix; |
| 129 | Name b = baseNamePrefix; |
| 130 | Checker c(d.appendComp(i), b.appendComp(i), i, 11, 1); |
| 131 | db->addTask(c.m_deviceName, c.m_baseName, c.m_minSeqNo, c.m_maxSeqNo, c.m_priority); |
| 132 | } |
| 133 | |
| 134 | db->foreachTask(bind(getChecker, _1, _2, _3, _4, _5)); |
| 135 | |
| 136 | BOOST_CHECK_EQUAL(g_counter, 8); |
| 137 | |
| 138 | map<Name, Checker>::iterator it = checkers.begin(); |
| 139 | while (it != checkers.end()) |
| 140 | { |
| 141 | map<Name, Checker>::iterator mt = m1.find(it->first); |
| 142 | if (mt == m1.end()) |
| 143 | { |
| 144 | BOOST_FAIL("unknown task found"); |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | Checker c1 = it->second; |
| 149 | Checker c2 = mt->second; |
| 150 | BOOST_CHECK(c1 == c2); |
| 151 | if (! (c1 == c2)) |
| 152 | { |
| 153 | cout << "C1: " << endl; |
| 154 | c1.show(); |
| 155 | cout << "C2: " << endl; |
| 156 | c2.show(); |
| 157 | } |
| 158 | } |
| 159 | ++it; |
| 160 | } |
| 161 | fs::remove_all(folder); |
| 162 | } |
| 163 | |
| 164 | |
| 165 | BOOST_AUTO_TEST_SUITE_END() |