blob: 1fbe4e6c268df5b2913d5e0153f03d46efdac1cc [file] [log] [blame]
Zhenkai Zhue29616f2013-01-14 15:40:57 -08001#include "sync-core.h"
2
Alexander Afanasyevabe952a2013-01-17 17:06:32 -08003
Zhenkai Zhue29616f2013-01-14 15:40:57 -08004#include <boost/test/unit_test.hpp>
5#include <boost/filesystem.hpp>
6
7using namespace std;
8using namespace Ccnx;
9using namespace boost::filesystem;
10
11BOOST_AUTO_TEST_SUITE(SyncCoreTests)
12
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080013void callback(const SyncStateMsgPtr &msg)
Zhenkai Zhue29616f2013-01-14 15:40:57 -080014{
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080015 BOOST_CHECK(msg->state_size() > 0);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080016}
17
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080018void checkRoots(const HashPtr &root1, const HashPtr &root2)
Zhenkai Zhue29616f2013-01-14 15:40:57 -080019{
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080020 BOOST_CHECK_EQUAL(*root1, *root2);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080021}
22
23BOOST_AUTO_TEST_CASE(SyncCoreTest)
24{
25 string dir = "./SyncCoreTest";
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -080026 // clean the test dir
27 path d(dir);
28 if (exists(d))
29 {
30 remove_all(d);
31 }
32
Zhenkai Zhu05de64a2013-01-14 15:48:23 -080033 string dir1 = "./SyncCoreTest/1";
34 string dir2 = "./SyncCoreTest/2";
Zhenkai Zhue29616f2013-01-14 15:40:57 -080035 Name user1("/joker");
36 Name loc1("/gotham1");
37 Name user2("/darkknight");
38 Name loc2("/gotham2");
39 Name syncPrefix("/broadcast/darkknight");
40 CcnxWrapperPtr c1(new CcnxWrapper());
41 CcnxWrapperPtr c2(new CcnxWrapper());
Zhenkai Zhub330aed2013-01-17 13:29:37 -080042 SyncLogPtr log1(new SyncLog(dir1, user1.toString()));
43 SyncLogPtr log2(new SyncLog(dir2, user2.toString()));
44
45 SchedulerPtr scheduler(new Scheduler());
Zhenkai Zhue29616f2013-01-14 15:40:57 -080046
Zhenkai Zhue29616f2013-01-14 15:40:57 -080047
Zhenkai Zhub330aed2013-01-17 13:29:37 -080048 SyncCore *core1 = new SyncCore(log1, user1, loc1, syncPrefix, bind(callback, _1), c1, scheduler);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080049 usleep(10000);
Zhenkai Zhub330aed2013-01-17 13:29:37 -080050 SyncCore *core2 = new SyncCore(log2, user2, loc2, syncPrefix, bind(callback, _1), c2, scheduler);
Zhenkai Zhue573ae82013-01-15 13:15:52 -080051 usleep(1000000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080052 checkRoots(core1->root(), core2->root());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -080053
54 cout << "\n\n\n\n\n\n----------\n";
Zhenkai Zhue29616f2013-01-14 15:40:57 -080055 core1->updateLocalState(1);
56 usleep(100000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080057 checkRoots(core1->root(), core2->root());
58 BOOST_CHECK_EQUAL(core2->seq(user1), 1);
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -080059 BOOST_CHECK_EQUAL(core2->yp(user1), loc1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080060
61 core1->updateLocalState(5);
62 usleep(100000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080063 checkRoots(core1->root(), core2->root());
64 BOOST_CHECK_EQUAL(core2->seq(user1), 5);
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -080065 BOOST_CHECK_EQUAL(core2->yp(user1), loc1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080066
67 core2->updateLocalState(10);
68 usleep(100000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080069 checkRoots(core1->root(), core2->root());
70 BOOST_CHECK_EQUAL(core1->seq(user2), 10);
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -080071 BOOST_CHECK_EQUAL(core1->yp(user2), loc2);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080072
73 // simple simultaneous data generation
Zhenkai Zhu9f31e862013-01-15 23:56:31 -080074 cout << "\n\n\n\n\n\n----------Simultaneous\n";
Zhenkai Zhue29616f2013-01-14 15:40:57 -080075 core1->updateLocalState(11);
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -080076 usleep(100);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080077 core2->updateLocalState(15);
Zhenkai Zhue573ae82013-01-15 13:15:52 -080078 usleep(1000000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080079 checkRoots(core1->root(), core2->root());
80 BOOST_CHECK_EQUAL(core1->seq(user2), 15);
81 BOOST_CHECK_EQUAL(core2->seq(user1), 11);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080082
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -080083 BOOST_CHECK_EQUAL(core1->yp(user1), loc1);
84 BOOST_CHECK_EQUAL(core1->yp(user2), loc2);
85 BOOST_CHECK_EQUAL(core2->yp(user1), loc1);
86 BOOST_CHECK_EQUAL(core2->yp(user2), loc2);
87
Zhenkai Zhue29616f2013-01-14 15:40:57 -080088 // clean the test dir
89 if (exists(d))
90 {
91 remove_all(d);
92 }
93}
94
95BOOST_AUTO_TEST_SUITE_END()