RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
core/banker/testing/master_banker_test.cc
00001 /* master_banker_test.cc
00002    Wolfgang Sourdeau, 13 December 2012
00003    Copyright (c) 2012 Datacratic.  All rights reserved.
00004    
00005    Unit tests for the MasterBanker class
00006 */
00007 
00008 #define BOOST_TEST_MAIN
00009 #define BOOST_TEST_DYN_LINK
00010 
00011 #include <memory>
00012 #include <boost/test/unit_test.hpp>
00013 
00014 #include "soa/service/service_base.h" // NullEventService, ServiceProxies
00015 
00016 #include "rtbkit/core/banker/master_banker.h"
00017 
00018 using namespace std;
00019 using namespace Datacratic;
00020 using namespace RTBKIT;
00021 
00022 BOOST_AUTO_TEST_CASE( test_master_banker_onstateloaded )
00023 {
00024     auto serviceProxies = std::make_shared<ServiceProxies>();
00025 
00026     MasterBanker testBanker(serviceProxies);
00027     testBanker.accounts.createAccount(AccountKey("origAccount"), AT_BUDGET);
00028     Json::Value initialJson = testBanker.accounts.toJson();
00029 
00030     shared_ptr<Accounts> newAccounts = make_shared<Accounts>();
00031     newAccounts->createAccount(AccountKey("newAccount:spend"), AT_SPEND);
00032 
00033     /* nothing must have changed with BankerPersistence::DATA_INCONSISTENCY */
00034     testBanker.onStateLoaded(newAccounts,
00035                              BankerPersistence::DATA_INCONSISTENCY,
00036                              "inconsistency error");
00037     Json::Value newJson = testBanker.accounts.toJson();
00038     BOOST_CHECK_EQUAL(newJson, initialJson);
00039 
00040     /* nothing must have changed */
00041     testBanker.onStateLoaded(newAccounts,
00042                              BankerPersistence::BACKEND_ERROR,
00043                              "backend error");
00044     newJson = testBanker.accounts.toJson();
00045     BOOST_CHECK_EQUAL(newJson, initialJson);
00046 
00047     /* accounts must have been replaced with "newAccounts" this time */
00048     testBanker.onStateLoaded(newAccounts, BankerPersistence::SUCCESS, "");
00049     newJson = testBanker.accounts.toJson();
00050     BOOST_CHECK_EQUAL(newJson, newAccounts->toJson());
00051 
00052     /* an exception should be thrown for unknown status codes */
00053     BOOST_CHECK_THROW(testBanker.onStateLoaded(newAccounts,
00054                                                BankerPersistence::PersistenceCallbackStatus(-1),
00055                                                ""),
00056                       ML::Exception);
00057 }
00058 
00059 BOOST_AUTO_TEST_CASE( test_master_banker_onstatesaved )
00060 {
00061     auto serviceProxies = std::make_shared<ServiceProxies>();
00062 
00063     MasterBanker testBanker(serviceProxies);
00064     testBanker.accounts.createAccount(AccountKey("account1"), AT_BUDGET);
00065     testBanker.accounts.createAccount(AccountKey("account2:spend"), AT_SPEND);
00066     Json::Value initialJson = testBanker.accounts.toJson();
00067 
00068     /* BankerPersistence::SUCCESS: nothing must have changed */
00069     testBanker.onStateSaved(BankerPersistence::SUCCESS, "");
00070     Json::Value newJson = testBanker.accounts.toJson();
00071     BOOST_CHECK_EQUAL(newJson, initialJson);
00072 
00073     /* BankerPersistence::BACKEND_ERROR: nothing must have changed */
00074     testBanker.onStateSaved(BankerPersistence::BACKEND_ERROR,
00075                             "backend error");
00076     newJson = testBanker.accounts.toJson();
00077     BOOST_CHECK_EQUAL(newJson, initialJson);
00078 
00079     /* BankerPersistence::DATA_INCONSISTENCY: accounts corresponding to the
00080      * keys passed in a json array must have been marked as out of sync */
00081 
00082     Accounts newAccounts = testBanker.accounts;
00083     Json::Value badKeys = Json::parse("[ 'account2:spend' ]");
00084     testBanker.onStateSaved(BankerPersistence::DATA_INCONSISTENCY,
00085                             badKeys.toString());
00086     BOOST_CHECK(testBanker.accounts.isAccountOutOfSync({"account2", "spend"}));
00087 
00088     /* an exception should be thrown for unknown status codes */
00089     BOOST_CHECK_THROW(testBanker.onStateSaved(BankerPersistence::PersistenceCallbackStatus(-1),
00090                                               ""),
00091                       ML::Exception);
00092 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator