blob: fa6eafd24d9763f6e1b2b384bd1fb5f78cadfd2d [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 Zhu05de64a2013-01-14 15:48:23 -080025 string dir1 = "./SyncCoreTest/1";
26 string dir2 = "./SyncCoreTest/2";
Zhenkai Zhue29616f2013-01-14 15:40:57 -080027 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 Zhue29616f2013-01-14 15:40:57 -080034
35 // clean the test dir
36 path d(dir);
37 if (exists(d))
38 {
39 remove_all(d);
40 }
41
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080042 SyncCore *core1 = new SyncCore(dir1, user1, loc1, syncPrefix, bind(callback, _1), c1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080043 usleep(10000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080044 SyncCore *core2 = new SyncCore(dir2, user2, loc2, syncPrefix, bind(callback, _1), c2);
Zhenkai Zhue573ae82013-01-15 13:15:52 -080045 usleep(1000000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080046 checkRoots(core1->root(), core2->root());
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -080047
48 cout << "\n\n\n\n\n\n----------\n";
Zhenkai Zhue29616f2013-01-14 15:40:57 -080049 core1->updateLocalState(1);
50 usleep(100000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080051 checkRoots(core1->root(), core2->root());
52 BOOST_CHECK_EQUAL(core2->seq(user1), 1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080053
54 core1->updateLocalState(5);
55 usleep(100000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080056 checkRoots(core1->root(), core2->root());
57 BOOST_CHECK_EQUAL(core2->seq(user1), 5);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080058
59 core2->updateLocalState(10);
60 usleep(100000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080061 checkRoots(core1->root(), core2->root());
62 BOOST_CHECK_EQUAL(core1->seq(user2), 10);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080063
64 // simple simultaneous data generation
Zhenkai Zhu9f31e862013-01-15 23:56:31 -080065 cout << "\n\n\n\n\n\n----------Simultaneous\n";
Zhenkai Zhue29616f2013-01-14 15:40:57 -080066 core1->updateLocalState(11);
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -080067 usleep(100);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080068 core2->updateLocalState(15);
Zhenkai Zhue573ae82013-01-15 13:15:52 -080069 usleep(1000000);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080070 checkRoots(core1->root(), core2->root());
71 BOOST_CHECK_EQUAL(core1->seq(user2), 15);
72 BOOST_CHECK_EQUAL(core2->seq(user1), 11);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080073
74 // clean the test dir
75 if (exists(d))
76 {
77 remove_all(d);
78 }
79}
80
81BOOST_AUTO_TEST_SUITE_END()