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 | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 12 | typedef struct |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 13 | { |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 14 | Name deviceName; |
| 15 | Name locator; |
| 16 | int64_t seq; |
| 17 | } Result; |
| 18 | |
| 19 | Result result1; |
| 20 | Result result2; |
| 21 | |
| 22 | void setResult(const SyncStateMsgPtr &msg, Result &result) |
| 23 | { |
| 24 | if (msg->state_size() > 0) |
| 25 | { |
| 26 | SyncState state = msg->state(0); |
| 27 | string strName = state.name(); |
| 28 | result.deviceName = Name((const unsigned char *)strName.c_str(), strName.size()); |
| 29 | string strLoc = state.locator(); |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 30 | result.locator = Name((const unsigned char *)strLoc.c_str(), strLoc.size()); |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 31 | result.seq = state.seq(); |
| 32 | } |
| 33 | else |
| 34 | { |
| 35 | cout << "Msg state size: " << msg->state_size() << endl; |
| 36 | } |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 39 | void callback1(const SyncStateMsgPtr &msg) |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 40 | { |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 41 | setResult(msg, result1); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 44 | void callback2(const SyncStateMsgPtr &msg) |
| 45 | { |
| 46 | setResult(msg, result2); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 51 | BOOST_AUTO_TEST_CASE(SyncCoreTest) |
| 52 | { |
| 53 | string dir = "./SyncCoreTest"; |
Zhenkai Zhu | 05de64a | 2013-01-14 15:48:23 -0800 | [diff] [blame] | 54 | string dir1 = "./SyncCoreTest/1"; |
| 55 | string dir2 = "./SyncCoreTest/2"; |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 56 | Name user1("/joker"); |
| 57 | Name loc1("/gotham1"); |
| 58 | Name user2("/darkknight"); |
| 59 | Name loc2("/gotham2"); |
| 60 | Name syncPrefix("/broadcast/darkknight"); |
| 61 | CcnxWrapperPtr c1(new CcnxWrapper()); |
| 62 | CcnxWrapperPtr c2(new CcnxWrapper()); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 63 | |
| 64 | // clean the test dir |
| 65 | path d(dir); |
| 66 | if (exists(d)) |
| 67 | { |
| 68 | remove_all(d); |
| 69 | } |
| 70 | |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 71 | SyncCore *core1 = new SyncCore(dir1, user1, loc1, syncPrefix, bind(callback1, _1), c1); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 72 | usleep(10000); |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 73 | SyncCore *core2 = new SyncCore(dir2, user2, loc2, syncPrefix, bind(callback2, _1), c2); |
| 74 | usleep(1000000); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 75 | |
| 76 | SyncState state; |
| 77 | |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 78 | HashPtr root1 = core1->root(); |
| 79 | HashPtr root2 = core2->root(); |
| 80 | BOOST_CHECK_EQUAL(*root1, *root2); |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 81 | |
| 82 | cout << "\n\n\n\n\n\n----------\n"; |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 83 | core1->updateLocalState(1); |
| 84 | usleep(100000); |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 85 | BOOST_CHECK_EQUAL(result2.seq, 1); |
| 86 | BOOST_CHECK_EQUAL(result2.deviceName, user1); |
| 87 | BOOST_CHECK_EQUAL(result2.locator, loc1); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 88 | |
| 89 | core1->updateLocalState(5); |
| 90 | usleep(100000); |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 91 | BOOST_CHECK_EQUAL(result2.seq, 5); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 92 | |
| 93 | core2->updateLocalState(10); |
| 94 | usleep(100000); |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 95 | BOOST_CHECK_EQUAL(result1.seq, 10); |
| 96 | BOOST_CHECK_EQUAL(result1.deviceName, user2); |
| 97 | BOOST_CHECK_EQUAL(result1.locator, loc2); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 98 | |
| 99 | // simple simultaneous data generation |
Zhenkai Zhu | 9f31e86 | 2013-01-15 23:56:31 -0800 | [diff] [blame] | 100 | cout << "\n\n\n\n\n\n----------Simultaneous\n"; |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 101 | core1->updateLocalState(11); |
Zhenkai Zhu | 9f31e86 | 2013-01-15 23:56:31 -0800 | [diff] [blame] | 102 | // change the value here 100, 2000, 3000, 5000, each with different error (for 2000 and 3000 run at least 3 times) |
Alexander Afanasyev | beee0b4 | 2013-01-16 18:25:08 -0800 | [diff] [blame] | 103 | usleep(100); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 104 | core2->updateLocalState(12); |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 105 | usleep(1000000); |
| 106 | BOOST_CHECK_EQUAL(result1.seq, 12); |
| 107 | BOOST_CHECK_EQUAL(result2.seq, 11); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 108 | |
| 109 | // clean the test dir |
| 110 | if (exists(d)) |
| 111 | { |
| 112 | remove_all(d); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | BOOST_AUTO_TEST_SUITE_END() |