RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 /* monitor_test.cc 00002 Wolfgang Sourdeau, January 2013 00003 Copyright (c) 2013 Datacratic. All rights reserved. 00004 00005 Unit tests for the MonitorEndpoint class 00006 */ 00007 00008 #define BOOST_TEST_MAIN 00009 #define BOOST_TEST_DYN_LINK 00010 00011 #include <boost/shared_ptr.hpp> 00012 #include <boost/test/unit_test.hpp> 00013 00014 #include "rtbkit/core/monitor/monitor_endpoint.h" 00015 00016 00017 using namespace std; 00018 using namespace Datacratic; 00019 using namespace RTBKIT; 00020 00021 00022 BOOST_AUTO_TEST_CASE( test_monitor_getMonitorStatus ) 00023 { 00024 auto proxies = std::make_shared<ServiceProxies>(); 00025 MonitorEndpoint endpoint(proxies); 00026 endpoint.init({"service1", "service2"}); 00027 00028 Date now = Date::now(); 00029 Date oldLastCheck = now.plusSeconds(-10); 00030 00031 endpoint.providersStatus_["service1"].lastCheck = now; 00032 endpoint.providersStatus_["service1"].lastStatus = true; 00033 endpoint.providersStatus_["service2"].lastCheck = now; 00034 endpoint.providersStatus_["service2"].lastStatus = false; 00035 BOOST_CHECK_EQUAL(endpoint.getMonitorStatus(), false); 00036 00037 endpoint.providersStatus_["service1"].lastCheck = now; 00038 endpoint.providersStatus_["service1"].lastStatus = false; 00039 endpoint.providersStatus_["service2"].lastCheck = now; 00040 endpoint.providersStatus_["service2"].lastStatus = true; 00041 BOOST_CHECK_EQUAL(endpoint.getMonitorStatus(), false); 00042 00043 endpoint.providersStatus_["service1"].lastCheck = oldLastCheck; 00044 endpoint.providersStatus_["service1"].lastStatus = true; 00045 endpoint.providersStatus_["service2"].lastCheck = now; 00046 endpoint.providersStatus_["service2"].lastStatus = true; 00047 BOOST_CHECK_EQUAL(endpoint.getMonitorStatus(), false); 00048 00049 endpoint.providersStatus_["service1"].lastCheck = now; 00050 endpoint.providersStatus_["service1"].lastStatus = true; 00051 endpoint.providersStatus_["service2"].lastCheck = oldLastCheck; 00052 endpoint.providersStatus_["service2"].lastStatus = true; 00053 BOOST_CHECK_EQUAL(endpoint.getMonitorStatus(), false); 00054 00055 endpoint.providersStatus_["service1"].lastCheck = now; 00056 endpoint.providersStatus_["service1"].lastStatus = true; 00057 endpoint.providersStatus_["service2"].lastCheck = now; 00058 endpoint.providersStatus_["service2"].lastStatus = true; 00059 BOOST_CHECK_EQUAL(endpoint.getMonitorStatus(), true); 00060 } 00061 00062 BOOST_AUTO_TEST_CASE( test_monitor_postServiceIndicators ) 00063 { 00064 auto proxies = std::make_shared<ServiceProxies>(); 00065 MonitorEndpoint endpoint(proxies); 00066 endpoint.init({"service1"}); 00067 00068 Date oldDate = Date::now().plusSeconds(-3600); 00069 00070 endpoint.providersStatus_["service1"].lastCheck = oldDate; 00071 endpoint.providersStatus_["service1"].lastStatus = false; 00072 00073 string statusStr = "this is no json"; 00074 // cerr << "test: invalid json\n=> rc = false\n"; 00075 bool rc = endpoint.postServiceIndicators("service1", statusStr); 00076 BOOST_CHECK_EQUAL(rc, false); 00077 BOOST_CHECK_EQUAL(endpoint.providersStatus_["service1"].lastCheck, oldDate); 00078 BOOST_CHECK_EQUAL(endpoint.providersStatus_["service1"].lastStatus, false); 00079 00080 statusStr = "null"; 00081 // cerr << "test: valid json, invalid message\n=> rc = false\n"; 00082 rc = endpoint.postServiceIndicators("service1", statusStr); 00083 BOOST_CHECK_EQUAL(rc, false); 00084 BOOST_CHECK_EQUAL(endpoint.providersStatus_["service1"].lastCheck, oldDate); 00085 BOOST_CHECK_EQUAL(endpoint.providersStatus_["service1"].lastStatus, false); 00086 00087 statusStr = "{ 'status': 'ok' }"; 00088 // cerr << "test: valid ok json message\n=> rc = true\n"; 00089 rc = endpoint.postServiceIndicators("service1", statusStr); 00090 BOOST_CHECK_EQUAL(rc, true); 00091 BOOST_CHECK(endpoint.providersStatus_["service1"].lastCheck != oldDate); 00092 BOOST_CHECK_EQUAL(endpoint.providersStatus_["service1"].lastStatus, true); 00093 00094 // cerr << "test: valid failure json message\n=> rc = false\n"; 00095 endpoint.providersStatus_["service1"].lastCheck = oldDate; 00096 statusStr = "{ 'status': 'failure' }"; 00097 rc = endpoint.postServiceIndicators("service1", statusStr); 00098 BOOST_CHECK_EQUAL(rc, true); 00099 BOOST_CHECK(endpoint.providersStatus_["service1"].lastCheck != oldDate); 00100 BOOST_CHECK_EQUAL(endpoint.providersStatus_["service1"].lastStatus, false); 00101 00102 // cerr << "test: valid ok json message\n=> rc = true\n"; 00103 endpoint.providersStatus_["service1"].lastCheck = oldDate; 00104 statusStr = "{ 'status': 'ok' }"; 00105 rc = endpoint.postServiceIndicators("service1", statusStr); 00106 BOOST_CHECK_EQUAL(rc, true); 00107 BOOST_CHECK(endpoint.providersStatus_["service1"].lastCheck != oldDate); 00108 BOOST_CHECK_EQUAL(endpoint.providersStatus_["service1"].lastStatus, true); 00109 }