Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 1 | #include "sync-core.h" |
| 2 | |
Alexander Afanasyev | abe952a | 2013-01-17 17:06:32 -0800 | [diff] [blame] | 3 | |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 4 | #include <boost/test/unit_test.hpp> |
| 5 | #include <boost/filesystem.hpp> |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame^] | 6 | #include <boost/make_shared.hpp> |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 7 | |
| 8 | using namespace std; |
| 9 | using namespace Ccnx; |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame^] | 10 | using namespace boost; |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 11 | using namespace boost::filesystem; |
| 12 | |
| 13 | BOOST_AUTO_TEST_SUITE(SyncCoreTests) |
| 14 | |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 15 | void callback(const SyncStateMsgPtr &msg) |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 16 | { |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 17 | BOOST_CHECK(msg->state_size() > 0); |
Zhenkai Zhu | 085aae7 | 2013-01-17 21:09:01 -0800 | [diff] [blame] | 18 | int size = msg->state_size(); |
| 19 | int index = 0; |
| 20 | while (index < size) |
| 21 | { |
| 22 | SyncState state = msg->state(index); |
| 23 | BOOST_CHECK(state.has_old_seq()); |
| 24 | BOOST_CHECK(state.old_seq() >= 0); |
| 25 | if (state.seq() != 0) |
| 26 | { |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 27 | BOOST_CHECK(state.old_seq() != state.seq()); |
Zhenkai Zhu | 085aae7 | 2013-01-17 21:09:01 -0800 | [diff] [blame] | 28 | } |
| 29 | index++; |
| 30 | } |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 31 | } |
| 32 | |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 33 | void checkRoots(const HashPtr &root1, const HashPtr &root2) |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 34 | { |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 35 | BOOST_CHECK_EQUAL(*root1, *root2); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | BOOST_AUTO_TEST_CASE(SyncCoreTest) |
| 39 | { |
| 40 | string dir = "./SyncCoreTest"; |
Zhenkai Zhu | 4e1c1d9 | 2013-01-17 14:12:46 -0800 | [diff] [blame] | 41 | // clean the test dir |
| 42 | path d(dir); |
| 43 | if (exists(d)) |
| 44 | { |
| 45 | remove_all(d); |
| 46 | } |
| 47 | |
Zhenkai Zhu | 05de64a | 2013-01-14 15:48:23 -0800 | [diff] [blame] | 48 | string dir1 = "./SyncCoreTest/1"; |
| 49 | string dir2 = "./SyncCoreTest/2"; |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 50 | Name user1("/joker"); |
| 51 | Name loc1("/gotham1"); |
| 52 | Name user2("/darkknight"); |
| 53 | Name loc2("/gotham2"); |
| 54 | Name syncPrefix("/broadcast/darkknight"); |
| 55 | CcnxWrapperPtr c1(new CcnxWrapper()); |
| 56 | CcnxWrapperPtr c2(new CcnxWrapper()); |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 57 | SyncLogPtr log1(new SyncLog(dir1, user1.toString())); |
| 58 | SyncLogPtr log2(new SyncLog(dir2, user2.toString())); |
| 59 | |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame^] | 60 | // should not have used the same scheduler... |
| 61 | SchedulerPtr scheduler1 = make_shared<Scheduler> (); |
| 62 | SchedulerPtr scheduler2 = make_shared<Scheduler> (); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 63 | |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame^] | 64 | SyncCore *core1 = new SyncCore(log1, user1, loc1, syncPrefix, bind(callback, _1), c1, scheduler1); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 65 | usleep(10000); |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame^] | 66 | SyncCore *core2 = new SyncCore(log2, user2, loc2, syncPrefix, bind(callback, _1), c2, scheduler2); |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 67 | usleep(1000000); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 68 | checkRoots(core1->root(), core2->root()); |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 69 | |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame^] | 70 | // cout << "\n\n\n\n\n\n----------\n"; |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 71 | core1->updateLocalState(1); |
| 72 | usleep(100000); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 73 | checkRoots(core1->root(), core2->root()); |
| 74 | BOOST_CHECK_EQUAL(core2->seq(user1), 1); |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 75 | BOOST_CHECK_EQUAL(log2->LookupLocator (user1), loc1); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 76 | |
| 77 | core1->updateLocalState(5); |
| 78 | usleep(100000); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 79 | checkRoots(core1->root(), core2->root()); |
| 80 | BOOST_CHECK_EQUAL(core2->seq(user1), 5); |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 81 | BOOST_CHECK_EQUAL(log2->LookupLocator (user1), loc1); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 82 | |
| 83 | core2->updateLocalState(10); |
| 84 | usleep(100000); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 85 | checkRoots(core1->root(), core2->root()); |
| 86 | BOOST_CHECK_EQUAL(core1->seq(user2), 10); |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 87 | BOOST_CHECK_EQUAL(log1->LookupLocator (user2), loc2); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 88 | |
| 89 | // simple simultaneous data generation |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame^] | 90 | // cout << "\n\n\n\n\n\n----------Simultaneous\n"; |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 91 | core1->updateLocalState(11); |
Alexander Afanasyev | beee0b4 | 2013-01-16 18:25:08 -0800 | [diff] [blame] | 92 | usleep(100); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 93 | core2->updateLocalState(15); |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame^] | 94 | usleep(2000000); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 95 | checkRoots(core1->root(), core2->root()); |
| 96 | BOOST_CHECK_EQUAL(core1->seq(user2), 15); |
| 97 | BOOST_CHECK_EQUAL(core2->seq(user1), 11); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 98 | |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 99 | BOOST_CHECK_EQUAL(log1->LookupLocator (user1), loc1); |
| 100 | BOOST_CHECK_EQUAL(log1->LookupLocator (user2), loc2); |
| 101 | BOOST_CHECK_EQUAL(log2->LookupLocator (user1), loc1); |
| 102 | BOOST_CHECK_EQUAL(log2->LookupLocator (user2), loc2); |
Zhenkai Zhu | 4e1c1d9 | 2013-01-17 14:12:46 -0800 | [diff] [blame] | 103 | |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 104 | // clean the test dir |
| 105 | if (exists(d)) |
| 106 | { |
| 107 | remove_all(d); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | BOOST_AUTO_TEST_SUITE_END() |