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 | |
| 12 | SyncStateMsgPtr msg1; |
| 13 | SyncStateMsgPtr msg2; |
| 14 | |
| 15 | void callback1(const SyncStateMsgPtr &ptr) |
| 16 | { |
| 17 | msg1 = ptr; |
| 18 | } |
| 19 | |
| 20 | void callback2(const SyncStateMsgPtr &ptr) |
| 21 | { |
| 22 | msg2 = ptr; |
| 23 | } |
| 24 | |
| 25 | BOOST_AUTO_TEST_CASE(SyncCoreTest) |
| 26 | { |
| 27 | string dir = "./SyncCoreTest"; |
Zhenkai Zhu | 05de64a | 2013-01-14 15:48:23 -0800 | [diff] [blame^] | 28 | string dir1 = "./SyncCoreTest/1"; |
| 29 | string dir2 = "./SyncCoreTest/2"; |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 30 | Name user1("/joker"); |
| 31 | Name loc1("/gotham1"); |
| 32 | Name user2("/darkknight"); |
| 33 | Name loc2("/gotham2"); |
| 34 | Name syncPrefix("/broadcast/darkknight"); |
| 35 | CcnxWrapperPtr c1(new CcnxWrapper()); |
| 36 | CcnxWrapperPtr c2(new CcnxWrapper()); |
| 37 | SchedulerPtr scheduler(new Scheduler()); |
| 38 | scheduler->start(); |
| 39 | |
| 40 | // clean the test dir |
| 41 | path d(dir); |
| 42 | if (exists(d)) |
| 43 | { |
| 44 | remove_all(d); |
| 45 | } |
| 46 | |
Zhenkai Zhu | 05de64a | 2013-01-14 15:48:23 -0800 | [diff] [blame^] | 47 | SyncCore *core1 = new SyncCore(dir1, user1, loc1, syncPrefix, bind(callback1, _1), c1, scheduler); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 48 | usleep(10000); |
Zhenkai Zhu | 05de64a | 2013-01-14 15:48:23 -0800 | [diff] [blame^] | 49 | SyncCore *core2 = new SyncCore(dir2, user2, loc2, syncPrefix, bind(callback2, _1), c2, scheduler); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 50 | usleep(10000); |
| 51 | |
| 52 | SyncState state; |
| 53 | |
| 54 | core1->updateLocalState(1); |
| 55 | usleep(100000); |
| 56 | BOOST_CHECK_EQUAL(msg2->state_size(), 1); |
| 57 | state = msg2->state(0); |
| 58 | BOOST_CHECK_EQUAL(state.seq(), 1); |
| 59 | BOOST_CHECK_EQUAL(user1, state.name()); |
| 60 | BOOST_CHECK_EQUAL(loc1, state.locator()); |
| 61 | |
| 62 | core1->updateLocalState(5); |
| 63 | usleep(100000); |
| 64 | state = msg2->state(0); |
| 65 | BOOST_CHECK_EQUAL(state.seq(), 5); |
| 66 | |
| 67 | core2->updateLocalState(10); |
| 68 | usleep(100000); |
| 69 | state = msg1->state(0); |
| 70 | BOOST_CHECK_EQUAL(state.seq(), 10); |
| 71 | |
| 72 | // simple simultaneous data generation |
| 73 | core1->updateLocalState(11); |
| 74 | core2->updateLocalState(12); |
| 75 | usleep(100000); |
| 76 | state = msg1->state(0); |
| 77 | BOOST_CHECK_EQUAL(state.seq(), 12); |
| 78 | state = msg2->state(0); |
| 79 | BOOST_CHECK_EQUAL(state.seq(), 11); |
| 80 | |
| 81 | // clean the test dir |
| 82 | if (exists(d)) |
| 83 | { |
| 84 | remove_all(d); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | BOOST_AUTO_TEST_SUITE_END() |