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