Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 1 | #include "sync-core.h" |
| 2 | |
| 3 | #include <boost/test/unit_test.hpp> |
| 4 | #include <boost/filesystem.hpp> |
| 5 | |
| 6 | using namespace std; |
| 7 | using namespace Ccnx; |
| 8 | using namespace boost::filesystem; |
| 9 | |
| 10 | BOOST_AUTO_TEST_SUITE(SyncCoreTests) |
| 11 | |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame^] | 12 | void callback(const SyncStateMsgPtr &msg) |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 13 | { |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame^] | 14 | BOOST_CHECK(msg->state_size() > 0); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 15 | } |
| 16 | |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame^] | 17 | void checkRoots(const HashPtr &root1, const HashPtr &root2) |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 18 | { |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame^] | 19 | BOOST_CHECK_EQUAL(*root1, *root2); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | BOOST_AUTO_TEST_CASE(SyncCoreTest) |
| 23 | { |
| 24 | string dir = "./SyncCoreTest"; |
Zhenkai Zhu | 05de64a | 2013-01-14 15:48:23 -0800 | [diff] [blame] | 25 | string dir1 = "./SyncCoreTest/1"; |
| 26 | string dir2 = "./SyncCoreTest/2"; |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 27 | Name user1("/joker"); |
| 28 | Name loc1("/gotham1"); |
| 29 | Name user2("/darkknight"); |
| 30 | Name loc2("/gotham2"); |
| 31 | Name syncPrefix("/broadcast/darkknight"); |
| 32 | CcnxWrapperPtr c1(new CcnxWrapper()); |
| 33 | CcnxWrapperPtr c2(new CcnxWrapper()); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 34 | |
| 35 | // clean the test dir |
| 36 | path d(dir); |
| 37 | if (exists(d)) |
| 38 | { |
| 39 | remove_all(d); |
| 40 | } |
| 41 | |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame^] | 42 | SyncCore *core1 = new SyncCore(dir1, user1, loc1, syncPrefix, bind(callback, _1), c1); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 43 | usleep(10000); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame^] | 44 | SyncCore *core2 = new SyncCore(dir2, user2, loc2, syncPrefix, bind(callback, _1), c2); |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 45 | usleep(1000000); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame^] | 46 | checkRoots(core1->root(), core2->root()); |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 47 | |
| 48 | cout << "\n\n\n\n\n\n----------\n"; |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 49 | core1->updateLocalState(1); |
| 50 | usleep(100000); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame^] | 51 | checkRoots(core1->root(), core2->root()); |
| 52 | BOOST_CHECK_EQUAL(core2->seq(user1), 1); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 53 | |
| 54 | core1->updateLocalState(5); |
| 55 | usleep(100000); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame^] | 56 | checkRoots(core1->root(), core2->root()); |
| 57 | BOOST_CHECK_EQUAL(core2->seq(user1), 5); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 58 | |
| 59 | core2->updateLocalState(10); |
| 60 | usleep(100000); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame^] | 61 | checkRoots(core1->root(), core2->root()); |
| 62 | BOOST_CHECK_EQUAL(core1->seq(user2), 10); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 63 | |
| 64 | // simple simultaneous data generation |
Zhenkai Zhu | 9f31e86 | 2013-01-15 23:56:31 -0800 | [diff] [blame] | 65 | cout << "\n\n\n\n\n\n----------Simultaneous\n"; |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 66 | core1->updateLocalState(11); |
Alexander Afanasyev | beee0b4 | 2013-01-16 18:25:08 -0800 | [diff] [blame] | 67 | usleep(100); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame^] | 68 | core2->updateLocalState(15); |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 69 | usleep(1000000); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame^] | 70 | checkRoots(core1->root(), core2->root()); |
| 71 | BOOST_CHECK_EQUAL(core1->seq(user2), 15); |
| 72 | BOOST_CHECK_EQUAL(core2->seq(user1), 11); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 73 | |
| 74 | // clean the test dir |
| 75 | if (exists(d)) |
| 76 | { |
| 77 | remove_all(d); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | BOOST_AUTO_TEST_SUITE_END() |