Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 1 | #include "ccnx-wrapper.h" |
| 2 | #include "ccnx-closure.h" |
| 3 | #include "ccnx-name.h" |
Zhenkai Zhu | a6472e0 | 2013-01-06 14:33:37 -0800 | [diff] [blame] | 4 | #include "ccnx-selectors.h" |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 5 | #include "ccnx-pco.h" |
| 6 | #include <unistd.h> |
| 7 | |
| 8 | #include <boost/test/unit_test.hpp> |
| 9 | |
| 10 | |
| 11 | using namespace Ccnx; |
| 12 | using namespace std; |
| 13 | using namespace boost; |
| 14 | |
| 15 | BOOST_AUTO_TEST_SUITE(CcnxWrapperTests) |
| 16 | |
| 17 | CcnxWrapperPtr c1(new CcnxWrapper()); |
| 18 | CcnxWrapperPtr c2(new CcnxWrapper()); |
Zhenkai Zhu | 3722d43 | 2013-01-04 16:23:36 -0800 | [diff] [blame] | 19 | int g_timeout_counter = 0; |
| 20 | int g_dataCallback_counter = 0; |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 21 | |
| 22 | void publish1(const Name &name) |
| 23 | { |
| 24 | string content = name.toString(); |
| 25 | c1->publishData(name, (const unsigned char*)content.c_str(), content.size(), 5); |
| 26 | } |
| 27 | |
| 28 | void publish2(const Name &name) |
| 29 | { |
| 30 | string content = name.toString(); |
| 31 | c2->publishData(name, (const unsigned char*)content.c_str(), content.size(), 5); |
| 32 | } |
| 33 | |
| 34 | void dataCallback(const Name &name, const Bytes &content) |
| 35 | { |
| 36 | string msg((const char*)&content[0], content.size()); |
Zhenkai Zhu | 3722d43 | 2013-01-04 16:23:36 -0800 | [diff] [blame] | 37 | g_dataCallback_counter ++; |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 38 | BOOST_CHECK_EQUAL(name, msg); |
| 39 | } |
| 40 | |
| 41 | Closure::TimeoutCallbackReturnValue timeout(const Name &name) |
| 42 | { |
Zhenkai Zhu | 3722d43 | 2013-01-04 16:23:36 -0800 | [diff] [blame] | 43 | g_timeout_counter ++; |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 44 | return Closure::RESULT_OK; |
| 45 | } |
| 46 | |
| 47 | BOOST_AUTO_TEST_CASE (CcnxWrapperTest) |
| 48 | { |
| 49 | Name prefix1("/c1"); |
| 50 | Name prefix2("/c2"); |
| 51 | |
| 52 | c1->setInterestFilter(prefix1, bind(publish1, _1)); |
Zhenkai Zhu | 03a511d | 2013-01-04 17:13:28 -0800 | [diff] [blame] | 53 | usleep(100000); |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 54 | c2->setInterestFilter(prefix2, bind(publish2, _1)); |
| 55 | |
| 56 | Closure *closure = new Closure(1, bind(dataCallback, _1, _2), bind(timeout, _1)); |
| 57 | |
| 58 | c1->sendInterest(Name("/c2/hi"), closure); |
| 59 | usleep(100000); |
| 60 | c2->sendInterest(Name("/c1/hi"), closure); |
Zhenkai Zhu | d34106a | 2013-01-04 15:51:55 -0800 | [diff] [blame] | 61 | sleep(1); |
Zhenkai Zhu | 3722d43 | 2013-01-04 16:23:36 -0800 | [diff] [blame] | 62 | BOOST_CHECK_EQUAL(g_dataCallback_counter, 2); |
| 63 | // reset |
| 64 | g_dataCallback_counter = 0; |
| 65 | g_timeout_counter = 0; |
| 66 | delete closure; |
| 67 | } |
| 68 | |
| 69 | BOOST_AUTO_TEST_CASE (CcnxWrapperSelector) |
| 70 | { |
| 71 | |
| 72 | Closure *closure = new Closure(1, bind(dataCallback, _1, _2), bind(timeout, _1)); |
| 73 | |
| 74 | Selectors selectors; |
| 75 | selectors.interestLifetime(1); |
| 76 | |
| 77 | string n1 = "/random/01"; |
| 78 | c1->sendInterest(Name(n1), closure, selectors); |
| 79 | sleep(2); |
| 80 | c2->publishData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 4); |
| 81 | usleep(100000); |
| 82 | BOOST_CHECK_EQUAL(g_timeout_counter, 1); |
| 83 | BOOST_CHECK_EQUAL(g_dataCallback_counter, 0); |
| 84 | |
| 85 | string n2 = "/random/02"; |
| 86 | selectors.interestLifetime(2); |
| 87 | c1->sendInterest(Name(n2), closure, selectors); |
| 88 | sleep(1); |
| 89 | c2->publishData(Name(n2), (const unsigned char *)n2.c_str(), n2.size(), 4); |
| 90 | usleep(100000); |
| 91 | BOOST_CHECK_EQUAL(g_timeout_counter, 1); |
| 92 | BOOST_CHECK_EQUAL(g_dataCallback_counter, 1); |
| 93 | |
| 94 | // reset |
| 95 | g_dataCallback_counter = 0; |
| 96 | g_timeout_counter = 0; |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 97 | delete closure; |
| 98 | } |
| 99 | |
| 100 | BOOST_AUTO_TEST_SUITE_END() |