blob: e91b0f57abfcd9a39cb289764496ed8f2f9a8f24 [file] [log] [blame]
Zhenkai Zhue29616f2013-01-14 15:40:57 -08001#include "sync-core.h"
2
3#include <boost/test/unit_test.hpp>
4#include <boost/filesystem.hpp>
5
6using namespace std;
7using namespace Ccnx;
8using namespace boost::filesystem;
9
10BOOST_AUTO_TEST_SUITE(SyncCoreTests)
11
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080012void callback(const SyncStateMsgPtr &msg)
Zhenkai Zhue29616f2013-01-14 15:40:57 -080013{
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080014 BOOST_CHECK(msg->state_size() > 0);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080015}
16
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080017void checkRoots(const HashPtr &root1, const HashPtr &root2)
Zhenkai Zhue29616f2013-01-14 15:40:57 -080018{
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080019 BOOST_CHECK_EQUAL(*root1, *root2);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080020}
21
22BOOST_AUTO_TEST_CASE(SyncCoreTest)
23{
24 string dir = "./SyncCoreTest";
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -080025 // clean the test dir
26 path d(dir);
27 if (exists(d))
28 {
29 remove_all(d);
30 }
31
Zhenkai Zhu05de64a2013-01-14 15:48:23 -080032 string dir1 = "./SyncCoreTest/1";
33 string dir2 = "./SyncCoreTest/2";
Zhenkai Zhue29616f2013-01-14 15:40:57 -080034 Name user1("/joker");
35 Name loc1("/gotham1");
36 Name user2("/darkknight");
37 Name loc2("/gotham2");
38 Name syncPrefix("/broadcast/darkknight");
39 CcnxWrapperPtr c1(new CcnxWrapper());
40 CcnxWrapperPtr c2(new CcnxWrapper());
Zhenkai Zhub330aed2013-01-17 13:29:37 -080041 SyncLogPtr log1(new SyncLog(dir1, user1.toString()));
42 SyncLogPtr log2(new SyncLog(dir2, user2.toString()));
43
44 SchedulerPtr scheduler(new Scheduler());
Zhenkai Zhue29616f2013-01-14 15:40:57 -080045
Zhenkai Zhue29616f2013-01-14 15:40:57 -080046
Zhenkai Zhub330aed2013-01-17 13:29:37 -080047 SyncCore *core1 = new SyncCore(log1, user1, loc1, syncPrefix, bind(callback, _1), c1, scheduler);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080048 usleep(10000);
Zhenkai Zhub330aed2013-01-17 13:29:37 -080049 SyncCore *core2 = new SyncCore(log2, user2, loc2, syncPrefix, bind(callback, _1), c2, scheduler);
Zhenkai Zhue573ae82013-01-15 13:15:52 -080050 usleep(1000000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080051 checkRoots(core1->root(), core2->root());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -080052
53 cout << "\n\n\n\n\n\n----------\n";
Zhenkai Zhue29616f2013-01-14 15:40:57 -080054 core1->updateLocalState(1);
55 usleep(100000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080056 checkRoots(core1->root(), core2->root());
57 BOOST_CHECK_EQUAL(core2->seq(user1), 1);
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -080058 BOOST_CHECK_EQUAL(core2->yp(user1), loc1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080059
60 core1->updateLocalState(5);
61 usleep(100000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080062 checkRoots(core1->root(), core2->root());
63 BOOST_CHECK_EQUAL(core2->seq(user1), 5);
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -080064 BOOST_CHECK_EQUAL(core2->yp(user1), loc1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080065
66 core2->updateLocalState(10);
67 usleep(100000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080068 checkRoots(core1->root(), core2->root());
69 BOOST_CHECK_EQUAL(core1->seq(user2), 10);
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -080070 BOOST_CHECK_EQUAL(core1->yp(user2), loc2);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080071
72 // simple simultaneous data generation
Zhenkai Zhu9f31e862013-01-15 23:56:31 -080073 cout << "\n\n\n\n\n\n----------Simultaneous\n";
Zhenkai Zhue29616f2013-01-14 15:40:57 -080074 core1->updateLocalState(11);
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -080075 usleep(100);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080076 core2->updateLocalState(15);
Zhenkai Zhue573ae82013-01-15 13:15:52 -080077 usleep(1000000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080078 checkRoots(core1->root(), core2->root());
79 BOOST_CHECK_EQUAL(core1->seq(user2), 15);
80 BOOST_CHECK_EQUAL(core2->seq(user1), 11);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080081
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -080082 BOOST_CHECK_EQUAL(core1->yp(user1), loc1);
83 BOOST_CHECK_EQUAL(core1->yp(user2), loc2);
84 BOOST_CHECK_EQUAL(core2->yp(user1), loc1);
85 BOOST_CHECK_EQUAL(core2->yp(user2), loc2);
86
Zhenkai Zhue29616f2013-01-14 15:40:57 -080087 // clean the test dir
88 if (exists(d))
89 {
90 remove_all(d);
91 }
92}
93
94BOOST_AUTO_TEST_SUITE_END()