RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 /* monitor_client_test.cc 00002 Wolfgang Sourdeau, January 2013 00003 Copyright (c) 2013 Datacratic. All rights reserved. 00004 00005 Unit tests for the MonitorClient class 00006 */ 00007 00008 #define BOOST_TEST_MAIN 00009 #define BOOST_TEST_DYN_LINK 00010 00011 #include <iostream> 00012 00013 #include <boost/test/unit_test.hpp> 00014 #include "soa/types/date.h" 00015 00016 #include "rtbkit/core/monitor/monitor_client.h" 00017 00018 using namespace std; 00019 using namespace Datacratic; 00020 using namespace RTBKIT; 00021 00022 BOOST_AUTO_TEST_CASE( test_monitor_client_getStatus ) 00023 { 00024 Date now = Date::now(); 00025 Date lateDate = now.plusSeconds(-3); 00026 Date pastDate = now.plusSeconds(-0.5); 00027 00028 /* setup */ 00029 std::shared_ptr<zmq::context_t> zero_context; 00030 MonitorClient client(zero_context); 00031 client.checkTimeout_ = 1; 00032 00033 /* no check yet, status = false -> false */ 00034 client.lastStatus = false; 00035 BOOST_CHECK_EQUAL(client.getStatus(), false); 00036 00037 /* timeout, status = true -> false */ 00038 client.lastCheck = lateDate; 00039 client.lastStatus = true; 00040 BOOST_CHECK_EQUAL(client.getStatus(), false); 00041 00042 /* no timeout, status = false -> false */ 00043 client.lastCheck = pastDate; 00044 client.lastStatus = false; 00045 BOOST_CHECK_EQUAL(client.getStatus(), false); 00046 00047 /* no timeout, status = true -> true */ 00048 client.lastCheck = pastDate; 00049 client.lastStatus = true; 00050 BOOST_CHECK_EQUAL(client.getStatus(), true); 00051 } 00052 00053 BOOST_AUTO_TEST_CASE( test_monitor_client_onResponseReceived ) 00054 { 00055 Date now = Date::now(); 00056 Date pastDate = now.plusSeconds(-0.5); 00057 00058 /* setup */ 00059 std::shared_ptr<zmq::context_t> zero_context; 00060 MonitorClient client(zero_context); 00061 00062 cerr << "test: code 200, invalid json\n=> status = false\n"; 00063 client.lastStatus = true; 00064 client.lastCheck = pastDate; 00065 client.onResponseReceived(nullptr, 200, "poil"); 00066 BOOST_CHECK_EQUAL(client.lastStatus, false); 00067 BOOST_CHECK(client.lastCheck != pastDate); 00068 00069 cerr << "test: code 200, valid json, invalid message\n=> status = false\n"; 00070 client.lastStatus = true; 00071 client.lastCheck = pastDate; 00072 client.onResponseReceived(nullptr, 200, "null"); 00073 BOOST_CHECK_EQUAL(client.lastStatus, false); 00074 BOOST_CHECK(client.lastCheck != pastDate); 00075 00076 cerr << "test: code 100, valid ok json message\n=> status = false\n"; 00077 client.lastStatus = true; 00078 client.lastCheck = pastDate; 00079 client.onResponseReceived(nullptr, 100, "{ 'status': 'ok' }"); 00080 BOOST_CHECK_EQUAL(client.lastStatus, false); 00081 BOOST_CHECK(client.lastCheck != pastDate); 00082 00083 cerr << "test: code 200, valid failure json message\n=> status = false\n"; 00084 client.lastStatus = true; 00085 client.lastCheck = pastDate; 00086 client.onResponseReceived(nullptr, 200, "{ 'status': 'failure' }"); 00087 BOOST_CHECK_EQUAL(client.lastStatus, false); 00088 BOOST_CHECK(client.lastCheck != pastDate); 00089 00090 cerr << "test: code 200, valid ok json message\n=> status = true\n"; 00091 client.lastStatus = false; 00092 client.lastCheck = pastDate; 00093 client.onResponseReceived(nullptr, 200, "{ 'status': 'ok' }"); 00094 BOOST_CHECK_EQUAL(client.lastStatus, true); 00095 BOOST_CHECK(client.lastCheck != pastDate); 00096 }