diff --git a/src/lib/CMakeDeps.cmake b/src/lib/CMakeDeps.cmake index 6e2d44896..8b0e4802b 100644 --- a/src/lib/CMakeDeps.cmake +++ b/src/lib/CMakeDeps.cmake @@ -1,2 +1,2 @@ # generated by make process -set(sources api/apicore.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/services/ApiUnitTestService.proto;api/services/HelloService.proto;api/services/MarketDataRelay.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/UserService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/Account.conf;config/defaults/Executor.conf;config/defaults/MarketData.conf;config/defaults/Symbols.conf;config/defaults/User.conf;config/defaults/edge.conf;config/defaults/expiration_display_offset.conf;config/defaults/logging.conf;config/defaults/testkeys.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/Account.proto;config/protos/Executor.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Securities.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/User.proto;config/protos/environment.proto;config/protos/expiration_display_offset.proto;config/protos/jetfix.proto;config/protos/logging.proto;config/protos/quickbus_cfg.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.proto;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPriceLantencyTester.cpp;md/Diag/ShmDumper.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MDRecoverableConsumer.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/SecurityIDRecorder.cpp;md/SettlementRecorder.cpp;md/Structure/FixedSecurity.cpp;md/Structure/PriceSize.cpp;md/common.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2TestInstrument.cpp;md/test/md2_tests.cpp;order_man/protos/OrderMan.proto;os/os.cpp;os/tests/os_tests.cpp;quickbus/core/LocalNode.cpp;quickbus/core/Quickbus.cpp;quickbus/core/SubscriptionID.cpp;quickbus/core/TableID.cpp;quickbus/message_model/Update.cpp;quickbus/message_model/merge_updates.cpp;quickbus/network/QBConnection.cpp;quickbus/network/QBConnectionManager.cpp;quickbus/network/endpoint_description.cpp;quickbus/network/quickbus_msgs.proto;quickbus/pubsub/TablePublisher.cpp;quickbus/pubsub/TableSubscriber.cpp;quickbus/pubsub/tests/qb_pubsub_tests.cpp;quickbus/routing/INode.cpp;quickbus/routing/NodeID.cpp;quickbus/routing/Router.cpp;quickbus/rpc/RPCClient.cpp;quickbus/rpc/RPCServer.cpp;quickbus/rpc/RPCSessionID.cpp;quickbus/rpc/rpc_utils.cpp;quickbus/rpc/tests/qb_rpc_tests.cpp;quickbus/tests/quickbus_tests.cpp;security_model/SecurityHelper.cpp;security_model/old_security.proto;sparkproto/SparkProtos.proto;testing/windows_test_driver.cpp;utils/Dispatcher.cpp;utils/HTTPStream.cpp;utils/PBUtils.cpp;utils/Puid.cpp;utils/SharedMemory.cpp;utils/Sqlite_utils.cpp;utils/Uuid.cpp;utils/datetime.cpp;utils/file_utils.cpp;utils/init_spark_lib.cpp;utils/is_unittest_environment.cpp) +set(sources api/apicore.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/services/ApiUnitTestService.proto;api/services/HelloService.proto;api/services/MarketDataRelay.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/UserService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/Account.conf;config/defaults/Executor.conf;config/defaults/MarketData.conf;config/defaults/Symbols.conf;config/defaults/User.conf;config/defaults/edge.conf;config/defaults/expiration_display_offset.conf;config/defaults/logging.conf;config/defaults/testkeys.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/Account.proto;config/protos/Executor.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Securities.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/User.proto;config/protos/environment.proto;config/protos/expiration_display_offset.proto;config/protos/jetfix.proto;config/protos/logging.proto;config/protos/quickbus_cfg.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.proto;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPriceLantencyTester.cpp;md/Diag/ShmDumper.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MDRecoverableConsumer.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/SecurityIDRecorder.cpp;md/SettlementRecorder.cpp;md/Structure/FixedSecurity.cpp;md/Structure/PriceSize.cpp;md/common.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2TestInstrument.cpp;md/test/md2_tests.cpp;os/os.cpp;os/tests/os_tests.cpp;quickbus/core/LocalNode.cpp;quickbus/core/Quickbus.cpp;quickbus/core/SubscriptionID.cpp;quickbus/core/TableID.cpp;quickbus/message_model/Update.cpp;quickbus/message_model/merge_updates.cpp;quickbus/network/QBConnection.cpp;quickbus/network/QBConnectionManager.cpp;quickbus/network/endpoint_description.cpp;quickbus/network/quickbus_msgs.proto;quickbus/pubsub/TablePublisher.cpp;quickbus/pubsub/TableSubscriber.cpp;quickbus/pubsub/tests/qb_pubsub_tests.cpp;quickbus/routing/INode.cpp;quickbus/routing/NodeID.cpp;quickbus/routing/Router.cpp;quickbus/rpc/RPCClient.cpp;quickbus/rpc/RPCServer.cpp;quickbus/rpc/RPCSessionID.cpp;quickbus/rpc/rpc_utils.cpp;quickbus/rpc/tests/qb_rpc_tests.cpp;quickbus/tests/quickbus_tests.cpp;security_model/SecurityHelper.cpp;security_model/old_security.proto;sparkproto/SparkProtos.proto;testing/windows_test_driver.cpp;utils/Dispatcher.cpp;utils/HTTPStream.cpp;utils/PBUtils.cpp;utils/Puid.cpp;utils/SharedMemory.cpp;utils/Sqlite_utils.cpp;utils/Uuid.cpp;utils/datetime.cpp;utils/file_utils.cpp;utils/init_spark_lib.cpp;utils/is_unittest_environment.cpp) diff --git a/src/lib/md/MDRecoverableConsumer.cpp b/src/lib/md/MDRecoverableConsumer.cpp index 1848da828..918024ae6 100644 --- a/src/lib/md/MDRecoverableConsumer.cpp +++ b/src/lib/md/MDRecoverableConsumer.cpp @@ -48,13 +48,14 @@ unique_ptr MDRecoverableConsumer::init(s return result; } -size_t MDRecoverableConsumer::walk_security_list(OldSecurityCallback callback) { +size_t MDRecoverableConsumer::walk_security_list(SecurityCallback callback) { return _current->consumer.walk_security_list(callback); } -void MDRecoverableConsumer::on_security(OldSecurityCallback cb) { + +void MDRecoverableConsumer::on_security(SecurityCallback cb) { // Add a filter to the callback to avoid calling the on_security callback with duplicates - auto filtered_cb = [this,cb](const OldSecurity& security, MDConsumerHandle handle) { - auto key = security.security_key(); + auto filtered_cb = [this,cb](const Security& security, MDConsumerHandle handle) { + auto key = security.id(); if (_seen.find(key) == _seen.end()) { _seen.insert(key); cb(security, handle); @@ -153,3 +154,4 @@ set get_symbols(Exchange::type exchange) { }}//namespace #endif + diff --git a/src/lib/md/MDRecoverableConsumer.h b/src/lib/md/MDRecoverableConsumer.h index 1bb535f4b..8076a89da 100644 --- a/src/lib/md/MDRecoverableConsumer.h +++ b/src/lib/md/MDRecoverableConsumer.h @@ -23,8 +23,8 @@ public: // // Security functions - size_t walk_security_list(md2::OldSecurityCallback callback); - void on_security(md2::OldSecurityCallback cb); + size_t walk_security_list(md2::SecurityCallback callback); + void on_security(md2::SecurityCallback cb); // Scans for added securities - updates managed handles size_t scan_securities(); @@ -53,7 +53,7 @@ private: std::unique_ptr _current; std::vector> _old; - std::vector _callbacks; + std::vector _callbacks; std::set _seen; typedef tbb::mutex Mutex; Mutex _lock; diff --git a/src/lib/md/test/md2_tests.cpp b/src/lib/md/test/md2_tests.cpp index eefbd4bb0..0f8ae855d 100644 --- a/src/lib/md/test/md2_tests.cpp +++ b/src/lib/md/test/md2_tests.cpp @@ -1,1361 +1,144 @@ -#ifndef NO_CPP0X - -#include "catch/catch.hpp" -#include "../SettlementRecorder.h" -#include "../coconut2.h" -#include "../relay/QueryServer.h" -#include "Co2TestInstrument.h" -#include "lib/config/config.h" -#include "lib/os/os.h" -#include "lib/security_model/SecurityHelper.h" -#include "lib/utils/Dispatcher.h" -#include "lib/utils/defer.h" -#include "lib/utils/warn_ctrl.h" -#include "lib/utils/bts_chrono.h" - -#include "lib/config/protos/MarketData.pb.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -using namespace bts::md2; -using namespace bts::md; -using namespace std; - -namespace bts { -namespace md2 { -namespace test { - -// -// Helper functions for Producer/Consumer tests -// -vector create_test_securities(MDProducer& producer, const char* postfix ="") { - FixedOptionSecurity fos; - FixedFutureSecurity ffs; - ffs.symbol("FOO"+string(postfix)); - fos.symbol("FOOX"+string(postfix)); - ffs.key = invalid_security_key; - fos.key = invalid_security_key; - ffs.expiration_year = fos.expiration_year = 2015; - ffs.expiration_day = fos.expiration_day = 27; - fos.strike = 1000; - fos.call_put = PutOrCall::CALL; - // Add 12 expirations to 2 different shared memory regions - vector result; - for (uint32_t x = 1; x <= 12; ++x) { - ffs.expiration_month = fos.expiration_month = x; - result.push_back(producer.add_security(ffs)); - result.push_back(producer.add_security(fos)); - // In a real app these securities would now be passed to the SecurityIDRecorder - } - return result; -} - -set get_test_symbols(const char* postfix="") { - set symbols; - UserSymbol future_symbol(string("FOO")+postfix, Exchange::TESTEXCHANGE, SecurityType::FUTURE); - symbols.insert(future_symbol); - UserSymbol option_symbol(string("FOOX")+postfix, Exchange::TESTEXCHANGE, SecurityType::OPTION); - symbols.insert(option_symbol); - return symbols; -} - -OldSecurity mkfuture(string symbol, security_key key, int year, int month, int day) { - OldSecurity result; - result.set_security_type(SecurityType::FUTURE); - SecurityHelper::set_symbol(result, symbol); - result.set_exchange(Exchange::TESTEXCHANGE); - result.set_security_key(key); - result.mutable_future()->set_expiration_day(day); - result.mutable_future()->set_expiration_month(month); - result.mutable_future()->set_expiration_year(year); - return result; -} -OldSecurity mkoption(string symbol, security_key key, int year, int month, int day, double strike) { - OldSecurity result; - result.set_security_type(SecurityType::OPTION); - SecurityHelper::set_symbol(result, symbol); - result.set_exchange(Exchange::TESTEXCHANGE); - result.set_security_key(key); - result.mutable_option()->set_expiration_day(day); - result.mutable_option()->set_expiration_month(month); - result.mutable_option()->set_expiration_year(year); - result.mutable_option()->set_strike(strike); - result.mutable_option()->set_call_put(PutOrCall::PUT); - return result; -} -FixedOptionSecurity mkfixedoption(string symbol, security_key key, int year, int month, int day, double strike, PutOrCall::type callput) { - FixedOptionSecurity fos; - fos.type = SecurityType::OPTION; - fos.exchange = Exchange::TESTEXCHANGE; - fos.key = key; - fos.strike = strike; - fos.expiration_day = day; - fos.expiration_month = month; - fos.expiration_year = year; - fos.call_put = callput; - fos.symbol(symbol); - return fos; -} -OldSecurity mkspread(string symbol, security_key key, const vector& legs) { - OldSecurity result; - result.set_security_type(SecurityType::SPREAD); - SecurityHelper::set_symbol(result, symbol); - result.set_exchange(Exchange::TESTEXCHANGE); - result.set_security_key(key); - for (const auto& leg : legs) { - auto* pleg = result.mutable_spread()->add_legs(); - pleg->mutable_security()->CopyFrom(leg); - } - return result; -} -OldSecurity mkspread(string symbol, security_key key, OldSecurity leg1, OldSecurity leg2) { - OldSecurity legs[] = { leg1, leg2 }; - vector vlegs(begin(legs), end(legs)); - return mkspread(symbol, key, vlegs); -} -OldSecurity mkspread(string symbol, security_key key, OldSecurity leg1, OldSecurity leg2, OldSecurity leg3) { - OldSecurity legs[] = { leg1, leg2, leg3 }; - vector vlegs(begin(legs), end(legs)); - return mkspread(symbol, key, vlegs); -} - -void assign_ids(vector::iterator begin, vector::iterator end, security_key start_key = 1000) { - for (; begin != end; ++begin) { - (*begin).set_key(++start_key); - } -} - -class Co2Tests -{ -public: - void setup() { - config::internal::initialize_for_test( - "PlatformMap {" - " value {" - " sim {" - " exchange: TESTEXCHANGE" - " }" - " }" - "}" - "PublisherOptions {" - " value {" - " heartbeat_ms: 100" - " heartbeat_boot_seconds: 1" - " }" - "}" - ); - } - -#ifndef WIN32 - void test_security_id_recorder() { - // Verify callbacks happen after work complete - SecurityIDRecorder idsvc, idsvc2; - security_key result = invalid_security_key; - std::string desc = "FOOX 1"; - idsvc.get_key(desc, SecurityType::FUTURE, [](void* arg, security_key key) { - security_key* presult = (security_key*)arg; - *presult = key; - }, &result); - idsvc.do_work(); - REQUIRE(result != invalid_security_key); - security_key result2 = invalid_security_key; - idsvc2.get_key(desc, SecurityType::OPTION, [](void* arg, security_key key) { - *((security_key*)arg) = key; - }, &result2); - idsvc2.do_work(); - REQUIRE(result == result2); - } -#endif - - void test_price_size() { - // PriceSize violates some type punning rules - // So it is important to have this test work correctly on the target platform - PriceSize p, p2; - p = PriceSize::from_double<2>(4.5, 14); - SingleWriterPriceSizeStore memory; - memory.store(p); - p2 = memory.load(); - REQUIRE(p.price() == 4.5); - REQUIRE(p.size() == 14); - REQUIRE(p2.price() == 4.5); - REQUIRE(p2.size() == 14); - - p = PriceSize::from_double<2>(4.0, 5); - memory.store(p); - p2 = memory.load(); - REQUIRE(p2.price() == 4.0); - REQUIRE(p2.size() == 5); - - p = PriceSize::from_double<2>(-4.2, 6); - memory.store(p); - p2 = memory.load(); - REQUIRE(p2.price() == -4.2); - REQUIRE(p2.size() == 6); - - p = PriceSize(4, -1, 2); - memory.store(p); - p2 = memory.load(); - REQUIRE(p2.price() == 0.4); - REQUIRE(p2.size() == 2); - - // Test requirement for more than 32 bit mantissa - double bigval = -9999.9921875; - p = PriceSize::from_double<7>(bigval, 42); - memory.store(p); - p2 = memory.load(); - REQUIRE(p2.price() == bigval); - REQUIRE(p2.size() == 42); - } - - void test_slow_double_conversion() { - double values[] = { 1.5, 0.5, 0.2155, 0.216, 42.1 }; - int expected[] = { -1, -1, -4, -3, -1 }; - static_assert(BTS_ARRAYSIZE(values) == BTS_ARRAYSIZE(expected), ""); - for (size_t x = 0; x < BTS_ARRAYSIZE(values); ++x) { - REQUIRE(to_exponent(values[x]) == expected[x]); - } - - // Tests powers of 10. Important for display_factor conversion - for (int x = 8; x > -8; --x) { - double d = pow10(x); - REQUIRE(to_exponent(d) == x); - } - } - - struct ExchangeDecimal { - int64_t mantissa; - int32_t exponent; - }; - bool close_enough(double a, double b) { - // We need at least 7 places but we're testing with 10 just in case - const double acceptable_error = 1E-12; - double delta = abs(a - b); - // Equal if the delta is less than error - return delta < acceptable_error; - } - void test_compact_exponent() { - ExchangeDecimal test_values[] = { - { 2030000000000, -9 }, // mantissa > pow(2,40) - { -2030000000000, -9 }, // mantissa > pow(2,40) - { 2022750000000, -9 },// mantissa > pow(2,40) - { -2022750000000, -9 },// mantissa > pow(2,40) - { 20227500000000, -10 },// mantissa > pow(2,40) - { 202275000000000, -11 },// mantissa > pow(2,40) - { 2022750000000000, -12 },// mantissa > pow(2,40) - { 20227500000000000, -13 },// mantissa > pow(2,40) - { -20227500000000000, -13 },// mantissa > pow(2,40) - { 203, 1}, // control - { -14000000000, -8 }, // control - { -14, 1 }, // control - { 20227500, -4 }, // control - }; - for (auto p : test_values) { - double expected = p.mantissa * pow10(p.exponent); - compact_mantissa(p.mantissa, p.exponent); - double compacted = p.mantissa * pow10(p.exponent); - REQUIRE(close_enough(expected, compacted)); - } - } - - void test_producer_consumer() { - MDProducer producer(Platform::SIM); - vector producers = create_test_securities(producer); - - // Add bbo data for those expirations - auto write_time = TimeStamp::now(); - for (auto& p : producers) { - p.update_bid(write_time, PriceSize::from_double<2>(5.0, 5)); - p.update_ask(write_time, PriceSize::from_double<2>(5.0, 5)); - } - - // Symbol list for the consumer - set symbols; - UserSymbol future_symbol( "FOO", Exchange::TESTEXCHANGE, SecurityType::FUTURE ); - symbols.insert(future_symbol); - UserSymbol option_symbol( "FOOX", Exchange::TESTEXCHANGE, SecurityType::OPTION ); - symbols.insert(option_symbol); - MDConsumer consumer(symbols); - - vector consumer_securities; - // Poll for security list - none visible until they have keys - size_t count = consumer.walk_security_list([&](const OldSecurity& security, MDConsumerHandle) {consumer_securities.push_back(security);}); - // Since no security has an id none are "public" - REQUIRE(consumer_securities.size() == count); - REQUIRE(count == 0); - // Assign a unique ID to each security (from the producer) - assign_ids(producers.begin(), producers.end()); - // Now securities can be enumerated by the client - count = consumer.walk_security_list([&](const OldSecurity& security, MDConsumerHandle) {consumer_securities.push_back(security);}); - REQUIRE(consumer_securities.size() == count); - REQUIRE(count == 24); - - vector consumers; - for(const auto& security : consumer_securities) { - consumers.push_back(consumer.get_handle(security.security_key())); - } - - for (const auto& c : consumers) { - PriceSize bid, ask; - TimeStamp read_time; - // get_boo returns true becuase this time is after the change - REQUIRE(c.get_bbo(read_time, bid, ask) == HandleReadResult::SUCCESS); - REQUIRE((read_time - TimeStamp()).seconds() > 0); - REQUIRE(bid.price() == 5.0); - REQUIRE(ask.price() == 5.0); - REQUIRE(bid.size() == 5); - REQUIRE(ask.size() == 5); - } - - // Second query before a write has happend finds no updates - auto read_time = TimeStamp::now(); - for (const auto& c : consumers) { - PriceSize bid, ask; - REQUIRE(c.get_bbo(read_time, bid, ask) != HandleReadResult::SUCCESS); - } - - // update everything once more - write_time = TimeStamp::now(); - for (auto& p : producers) { - // Update bid/ask with one call - p.update_bbo(write_time, PriceSize::from_double<2>(6.55, 6), PriceSize::from_double<2>(6.55, 6)); - } - // Now read_time is < write_time - for (const auto& c : consumers) { - TimeStamp modified_time = read_time; - PriceSize bid, ask; - // get_bbo returns true becuase this time is after the change - REQUIRE(c.get_bbo(modified_time, bid, ask) == HandleReadResult::SUCCESS); - REQUIRE( (modified_time - read_time).seconds() > 0); - REQUIRE(bid.price() == 6.55); - REQUIRE(ask.price() == 6.55); - REQUIRE(bid.size() == 6); - REQUIRE(ask.size() == 6); - } - } - - // When a security is added the consumer should provide callback - void test_consumer_callback_on_security_added() { - MDProducer producer(Platform::SIM); - vector producers = create_test_securities(producer); - - // Symbol list for the consumer - set symbols; - UserSymbol future_symbol( "FOO", Exchange::TESTEXCHANGE, SecurityType::FUTURE ); - symbols.insert(future_symbol); - UserSymbol option_symbol( "FOOX", Exchange::TESTEXCHANGE, SecurityType::OPTION ); - symbols.insert(option_symbol); - MDConsumer consumer(symbols); - - // Add the on_security callback - vector securities; - consumer.on_security([&](const OldSecurity& security, MDConsumerHandle handle) { - REQUIRE(security.security_key() != invalid_security_key); - REQUIRE(security.security_key() == handle.key()); - securities.push_back(security); - }); - // No securities have ids yet - REQUIRE(securities.size() == 0); - // Check for new securities - consumer.scan_securities(); - // Still none - REQUIRE(securities.size() == 0); - producers.front().set_key(1001); - consumer.scan_securities(); - REQUIRE(securities.size() == 1); - - // Assign the rest of the ids - assign_ids(producers.begin() + 1, producers.end(), producers.front().security().key); - consumer.scan_securities(); - REQUIRE(securities.size() == producers.size()); - } - - // If an MDConsumer is asked for a handle to a security which does not exist in the system - // The consumer should return a handle which always returns false on all calls but wakes up - // when the security is added. - // * Case 1 - the shared memory has been created but the security has not been discovered - // * Case 2 - The producer has never been created - void test_consumer_defer_handle1() { - MDProducer producer(Platform::SIM); - vector producers = create_test_securities(producer); - - set symbols; - UserSymbol future_symbol( "FOO", Exchange::TESTEXCHANGE, SecurityType::FUTURE ); - symbols.insert(future_symbol); - UserSymbol option_symbol( "FOOX", Exchange::TESTEXCHANGE, SecurityType::OPTION ); - symbols.insert(option_symbol); - MDConsumer consumer(symbols); - - // Get a handle to 24 consumers which haven't been created yet - vector consumers; - vector> managed_consumers; - security_key key = 1; - for(; key < 24; ++key) { - consumers.push_back(consumer.get_handle(key+1000)); - managed_consumers.push_back(consumer.get_managed_handle(key+1000)); - } - - // Set bbo on the producers - still not public - TimeStamp write_time = TimeStamp::now(); - for (auto& p : producers) { - p.update_bbo(write_time, PriceSize::from_double<3>(4.5, 5), PriceSize::from_double<3>(6.75, 7)); - } - // Even though bbo is available it is not given out because the producing security does not have an id - PriceSize bid, ask; - for(auto& c : consumers) { - TimeStamp zerotime; - REQUIRE(c.get_bbo(zerotime, bid, ask) != HandleReadResult::SUCCESS); - } - for(auto& mc : managed_consumers) { - TimeStamp zerotime; - REQUIRE(mc->get_bbo(zerotime, bid, ask) != HandleReadResult::SUCCESS); - } - - assign_ids(producers.begin(), producers.end()); - consumer.scan_securities(); - size_t bbo_count = 0; - // Existing old handles remain invalid - for(auto& c : consumers) { - TimeStamp zerotime; - if (c.get_bbo(zerotime, bid, ask) == HandleReadResult::SUCCESS) { - bbo_count++; - } - } - REQUIRE(bbo_count == 0); - // Now, magically, the existing (managed) handles are valid - for(auto& mc : managed_consumers) { - TimeStamp zerotime; - if (mc->get_bbo(zerotime, bid, ask) == HandleReadResult::SUCCESS) { - bbo_count++; - } - } - REQUIRE(bbo_count > 0); - } - - - // It is legal and valid to create a consumer before the producer process has started - // Handles given out by that consumer will become valid once the securities are discovered - void test_consumer_defer_handle2() { - set symbols; - UserSymbol future_symbol( "FOO", Exchange::TESTEXCHANGE, SecurityType::FUTURE ); - symbols.insert(future_symbol); - UserSymbol option_symbol( "FOOX", Exchange::TESTEXCHANGE, SecurityType::OPTION ); - symbols.insert(option_symbol); - MDConsumer consumer(symbols); - - // Get a handle to 24 consumers which haven't been created yet - vector consumers; - vector> managed_consumers; - security_key key = 1; - for(; key < 24; ++key) { - consumers.push_back(consumer.get_handle(key+1000)); - managed_consumers.push_back(consumer.get_managed_handle(key+1000)); - } - - // Even though bbo is available it is not given out because the producing security does not have an id - PriceSize bid, ask; - for(auto& c : consumers) { - TimeStamp zerotime; - REQUIRE(c.get_bbo(zerotime, bid, ask) != HandleReadResult::SUCCESS); - } - for(auto& mc : managed_consumers) { - TimeStamp zerotime; - REQUIRE(mc->get_bbo(zerotime, bid, ask) != HandleReadResult::SUCCESS); - } - - // Only NOW create the producers (and the shared memory) - MDProducer producer(Platform::SIM); - vector producers = create_test_securities(producer); - // Set bbo on the producers - still not public - TimeStamp write_time = TimeStamp::now(); - for (auto& p : producers) { - p.update_bbo(write_time, PriceSize::from_double<3>( 4.5, 5 ), PriceSize::from_double<3>(6.75, 7)); - } - - assign_ids(producers.begin(), producers.end()); - consumer.scan_securities(); - size_t bbo_count = 0; - // Existing old handles remain invalid - for(auto& c : consumers) { - TimeStamp zerotime; - if (c.get_bbo(zerotime, bid, ask) == HandleReadResult::SUCCESS) { - bbo_count++; - } - } - REQUIRE(bbo_count == 0); - // Now, magically, the existing (managed) handles are valid - for(auto& mc : managed_consumers) { - TimeStamp zerotime; - if (mc->get_bbo(zerotime, bid, ask) == HandleReadResult::SUCCESS) { - bbo_count++; - } - } - REQUIRE(bbo_count > 0); - } - - void test_spread_resolver() { - SpreadResolver resolver; - vector legs; - legs.push_back("1"); - legs.push_back("2"); - legs.push_back("3"); - legs.push_back("4"); - resolver.add("1234", legs); - legs.clear(); - legs.push_back("4"); - legs.push_back("5"); - resolver.add("45", legs); - auto key_map = [](string& key) -> security_key { - if (key == "1") return 1; - if (key == "2") return 2; - if (key == "3") return 3; - if (key == "4") return 4; - if (key == "5") return invalid_security_key; - REQUIRE(false); - return 0; - }; - auto key_map2 = [](string& key) -> security_key { - if (key == "4") return 4; - if (key == "5") return 5; - REQUIRE(false); - return 0; - }; - - // 2 unresolved spreads - REQUIRE(resolver.count() == 2); - resolver.resolve(key_map, [&](const string& key, const vector& legs) { - REQUIRE(key == "1234"); - REQUIRE(legs.size() == 4); - }); - // 1 spread remains - REQUIRE(resolver.count() == 1); - resolver.resolve(key_map2, [&](const string& key, const vector& legs) { - REQUIRE(key == "45"); - REQUIRE(legs.size() == 2); - }); - REQUIRE(resolver.count() == 0); - } - - - void test_no_double_security_callback() { - MDProducer producer(Platform::SIM); - vector producers = create_test_securities(producer); - // Assign ids so securities are already public in the producer - assign_ids(producers.begin(), producers.end()); - - // Symbol list for the consumer - auto syms = get_test_symbols(); - MDConsumer consumer = MDConsumer(syms); - - // Add the on_security callback - callback fires for existing securities - vector securities; - consumer.on_security([&](const OldSecurity& security, MDConsumerHandle handle) { - REQUIRE(security.security_key() != invalid_security_key); - REQUIRE(security.security_key() == handle.key()); - securities.push_back(security); - }); - // Found all securities - REQUIRE(securities.size() == producers.size()); - // Check for new securities - consumer.scan_securities(); - // Expect no duplicates - REQUIRE(securities.size() == producers.size()); - } - - void test_recovery() { - auto producer1 = make_shared(Platform::SIM); - vector producers = create_test_securities(*producer1); - assign_ids(producers.begin(), producers.end()); - producer1->get_channel("FOO", SecurityType::FUTURE, Exchange::TESTEXCHANGE).heartbeat(); - producer1->get_channel("FOOX", SecurityType::OPTION, Exchange::TESTEXCHANGE).heartbeat(); - for (auto& pro : producers) { pro.update_bbo(TimeStamp::now(), PriceSize(1, 0, 1), PriceSize(2, 0, 2)); } - - // Symbol list for the consumer - MDRecoverableConsumer consumer(get_test_symbols()); - map> products; - consumer.on_security([&](const OldSecurity& security, MDConsumerHandle handle) { - REQUIRE(security.security_key() == handle.key()); - products.insert(make_pair(security.security_key(), consumer.get_managed_handle(security.security_key()))); - }); - consumer.scan_securities(); - REQUIRE(products.size() == producers.size()); - - // Choose a handle - for (auto& pair : products) { - auto& handle = pair.second; - REQUIRE(handle->is_initialized()); - REQUIRE(handle->is_heart_beating()); - } - - // Delete the producer - try { - producers.clear(); - producer1.reset(); - } catch(std::exception&) { - LOG(MarketData,Error,"Failed test trying to destroy producer."); - REQUIRE(false); - } - - // Wait out the heartbeat - auto options = config::get(); - auto ms = std::max(options.heartbeat_ms(), options.heartbeat_boot_seconds()*1000)+10; - - std::this_thread::sleep_for(std::chrono::duration(ms)); - - // Make sure heartbeat is done - try { - for (auto& pair : products) { - auto& handle = pair.second; - REQUIRE(handle->is_initialized()); - REQUIRE(handle->is_heart_beating() == false); - } - } catch(std::exception&) { - LOG(MarketData,Error,"Failed to validate heartbeat while producer is down."); - REQUIRE(false); - } - - // Create a new publisher and attempt to recover - shared_ptr producer2; - try { - producer2 = make_shared(Platform::SIM); - } catch(std::exception&) { - LOG(MarketData,Error,"Failed to create new producer"); - REQUIRE(false); - } - producers = create_test_securities(*producer2); - REQUIRE(products.size() == producers.size()); - assign_ids(producers.begin(), producers.end()); - producer2->get_channel("FOO", SecurityType::FUTURE, Exchange::TESTEXCHANGE).heartbeat(); - producer2->get_channel("FOOX", SecurityType::OPTION, Exchange::TESTEXCHANGE).heartbeat(); - - // set bbo to a different value - for (auto& pro : producers) { - pro.update_bbo(TimeStamp::now(), PriceSize(4, 0, 5), PriceSize(6, 0, 7)); - } - - // Attempt to recover the managed handles - REQUIRE(consumer.try_recover()); - - // Since we know that both securities exist they should have been updated to the new section - for (auto& pair : products) { - auto& handle = pair.second; - REQUIRE(handle->is_initialized()); - REQUIRE(handle->is_heart_beating()); - TimeStamp tzero; - PriceSize bid, ask; - REQUIRE(handle->get_bbo(tzero, bid, ask) == HandleReadResult::SUCCESS); - REQUIRE(bid.price() == 4.0); - REQUIRE(bid.size() == 5); - REQUIRE(ask.price() == 6.0); - REQUIRE(ask.size() == 7); - } - } - - - void test_no_block_forever_on_publisher_crash() { -#ifdef _WIN32 -#pragma warning(push, 0) -#endif - // OK if this isn't aligned for testing - auto src = std::make_unique ("Foo", SecurityType::FUTURE); -#ifdef _WIN32 -#pragma warning(pop) -#endif - TimeStamp read = TimeStamp::now(); - src->producer().update_bbo(TimeStamp::invalid(), PriceSize::from_double<3>(100.0, 100), PriceSize::from_double<3>(101.0, 101)); - PriceSize bid, ask; - TimeStamp start = TimeStamp::now(); - auto result = src->consumer().get_bbo(read, bid, ask); - TimeStamp end = TimeStamp::now(); - REQUIRE(result != HandleReadResult::SUCCESS); - REQUIRE((end - start).seconds() < 5); - } - - void test_get_handles() { - MDProducer producer(Platform::SIM); - vector producers = create_test_securities(producer); - // Add entries to the shared memory that we aren't looking for - vector p2_no_id = create_test_securities(producer, "2"); - vector p3 = create_test_securities(producer, "3"); - - for (auto& p : producers) { p.update_bbo(TimeStamp::now(), PriceSize::from_double<3>(5.0, 5), PriceSize::from_double<3>(5.0, 5)); } - assign_ids(producers.begin(), producers.end()); - assign_ids(p3.begin(), p3.end()); - - // Symbol list for the consumer - only search half of the symbols - set symbols; - UserSymbol future_symbol( "FOO", Exchange::TESTEXCHANGE, SecurityType::FUTURE ); - symbols.insert(future_symbol); - UserSymbol option_symbol( "FOOX", Exchange::TESTEXCHANGE, SecurityType::OPTION ); - symbols.insert(option_symbol); - MDConsumer consumer(symbols); - - set keys; - consumer.walk_security_list([&](const OldSecurity& security, MDConsumerHandle) { keys.insert(security.security_key()); }); - REQUIRE(keys.size() == producers.size()); - auto map = consumer.get_handles(keys); - REQUIRE(map.size() == keys.size()); - for (const auto& entry : map) { - REQUIRE(entry.second.is_initialized()); - } - } - - void test_denormalization() { - // By using a different symbol for each security - // we put them all on separate shared mem sections (co2 channels) - security_key key = 999; - OldSecurity f1 = mkfuture("F1", ++key, 2016, 5, 1); - OldSecurity o1 = mkoption("O1", ++key, 2016, 5, 1, 100); - OldSecurity o2 = mkoption("O2", ++key, 2016, 5, 2, 200); - OldSecurity o3 = mkoption("O3", ++key, 2016, 5, 3, 300); - OldSecurity s4 = mkspread("S3", ++key, o2, o3); - OldSecurity s3 = mkspread("S4", ++key, o1, s4); - OldSecurity s2 = mkspread("S2", ++key, o1, s4, s3); - OldSecurity s1 = mkspread("S1", ++key, f1, s2); - OldSecurity all_secs_array[] = { f1, o1, o2, o3, s1, s2, s3, s4 }; - - MDProducer producer(Platform::SIM); - vector producers; - for (auto& sec : all_secs_array) { - with_security(sec.security_type(), sec.spread().legs_size(), [&](FixedSecurity& fsec) { - convert_security(sec, fsec, true); - producers.push_back(producer.add_security(fsec)); - }); - } - - set csymbols; - csymbols.insert(UserSymbol("F1", Exchange::TESTEXCHANGE, SecurityType::FUTURE)); - csymbols.insert(UserSymbol("O1", Exchange::TESTEXCHANGE, SecurityType::OPTION)); - csymbols.insert(UserSymbol("O2", Exchange::TESTEXCHANGE, SecurityType::OPTION)); - csymbols.insert(UserSymbol("O3", Exchange::TESTEXCHANGE, SecurityType::OPTION)); - csymbols.insert(UserSymbol("S1", Exchange::TESTEXCHANGE, SecurityType::SPREAD)); - csymbols.insert(UserSymbol("S2", Exchange::TESTEXCHANGE, SecurityType::SPREAD)); - csymbols.insert(UserSymbol("S3", Exchange::TESTEXCHANGE, SecurityType::SPREAD)); - csymbols.insert(UserSymbol("S4", Exchange::TESTEXCHANGE, SecurityType::SPREAD)); - MDConsumer consumer(csymbols); - - auto check_sec = [&](const OldSecurity& security, MDConsumerHandle handle) { - REQUIRE(security.security_key() == handle.key()); - auto iter = find_if(begin(all_secs_array), end(all_secs_array), [&](const OldSecurity& sec) { return sec.security_key() == security.security_key(); }); - REQUIRE(iter != end(all_secs_array)); - REQUIRE(SecurityHelper::symbol(*iter) == SecurityHelper::symbol(security)); - if (security.security_type() == SecurityType::SPREAD) { - REQUIRE(security.spread().normalized() == false); - REQUIRE(security.spread().has_symbol()); - REQUIRE(security.spread().legs_size() == iter->spread().legs_size()); - for (int x = 0; x < security.spread().legs_size(); ++x) { - REQUIRE(SecurityHelper::symbol(security.spread().legs(x).security()) == SecurityHelper::symbol(iter->spread().legs(x).security())); - } - } - }; - - consumer.walk_security_list(check_sec); - consumer.on_security(check_sec); - consumer.scan_securities(); - } - void test_denormalization2() { - // Just like test_denormailzation but with a different memory layout - security_key key = 999; - OldSecurity f1 = mkfuture("FF", ++key, 2016, 5, 1); - OldSecurity o1 = mkoption("OO", ++key, 2016, 5, 1, 100); - OldSecurity o2 = mkoption("OO", ++key, 2016, 5, 2, 200); - OldSecurity o3 = mkoption("OO", ++key, 2016, 5, 3, 300); - OldSecurity s4 = mkspread("SS", ++key, o2, o3); - OldSecurity s3 = mkspread("SS", ++key, o1, s4); - OldSecurity s2 = mkspread("SS", ++key, o1, s4, s3); - OldSecurity s1 = mkspread("SS", ++key, f1, s2); - OldSecurity all_secs_array[] = { f1, o1, o2, o3, s1, s2, s3, s4 }; - - MDProducer producer(Platform::SIM); - vector producers; - for (auto& sec : all_secs_array) { - with_security(sec.security_type(), sec.spread().legs_size(), [&](FixedSecurity& fsec) { - convert_security(sec, fsec, true); - producers.push_back(producer.add_security(fsec)); - }); - } - - set csymbols; - csymbols.insert(UserSymbol("FF", Exchange::TESTEXCHANGE, SecurityType::FUTURE)); - csymbols.insert(UserSymbol("OO", Exchange::TESTEXCHANGE, SecurityType::OPTION)); - csymbols.insert(UserSymbol("SS", Exchange::TESTEXCHANGE, SecurityType::SPREAD)); - MDConsumer consumer(csymbols); - - auto check_sec = [&](const OldSecurity& security, MDConsumerHandle handle) { - REQUIRE(security.security_key() == handle.key()); - auto iter = find_if(begin(all_secs_array), end(all_secs_array), [&](const OldSecurity& sec) { return sec.security_key() == security.security_key(); }); - REQUIRE(iter != end(all_secs_array)); - REQUIRE(SecurityHelper::symbol(*iter) == SecurityHelper::symbol(security)); - if (security.security_type() == SecurityType::SPREAD) { - REQUIRE(security.spread().normalized() == false); - REQUIRE(security.spread().has_symbol()); - REQUIRE(security.spread().legs_size() == iter->spread().legs_size()); - for (int x = 0; x < security.spread().legs_size(); ++x) { - REQUIRE(SecurityHelper::symbol(security.spread().legs(x).security()) == SecurityHelper::symbol(iter->spread().legs(x).security())); - } - } - }; - - consumer.walk_security_list(check_sec); - consumer.on_security(check_sec); - consumer.scan_securities(); - } - - void test_retry_unresolved_spread() { - // Make 2 spreads with option legs - // Leave out one leg for hte other spread - // The other spread only shows up once its option leg is produced - - OldSecurity o1 = mkoption("OO", 400, 2016, 5, 1, 100); - OldSecurity o2 = mkoption("OO", 401, 2016, 5, 2, 200); - OldSecurity o3 = mkoption("OO", 402, 2016, 5, 3, 300); - OldSecurity s1 = mkspread("SS", 500, o1, o2); - OldSecurity s2 = mkspread("SS", 501, o2, o3); - - MDProducer producer(Platform::SIM); - map handles; - auto add_security = [&](const OldSecurity& sec) { - with_security(sec.security_type(), sec.spread().legs_size(), [&](FixedSecurity& fsec) { - convert_security(sec, fsec, true); - handles.insert(make_pair(fsec.key, producer.add_security(fsec))); - }); - }; - // "options publisher has not started yet" - //add_security(o1); - //add_security(o2); - //add_security(o3); - add_security(s1); - add_security(s2); - - // Create consumer - set consumer_symbols; - consumer_symbols.insert(UserSymbol("OO", Exchange::TESTEXCHANGE, SecurityType::OPTION)); - consumer_symbols.insert(UserSymbol("SS", Exchange::TESTEXCHANGE, SecurityType::SPREAD)); - MDConsumer consumer(consumer_symbols); - - size_t security_count = 0; - // Indirection for check function - function check_func; - check_func = [&](const OldSecurity&) { - REQUIRE(false); - }; - consumer.on_security([&](const OldSecurity& sec, MDConsumerHandle) { ++security_count; check_func(sec); }); - consumer.scan_securities(); - REQUIRE(security_count == 0); - - // OK, now add enough options to resolve s1 but not s2 - add_security(o1); - add_security(o2); - check_func = [&](const OldSecurity& security) { - if (security.security_type() == SecurityType::SPREAD) { - set legs; - for (const auto& leg : security.spread().legs()) { - legs.insert(leg.security().security_key()); - } - REQUIRE(security.security_key() == 500); - REQUIRE(legs.size() == 2); - REQUIRE(legs.count(400) == 1); - REQUIRE(legs.count(401) == 1); - } - }; - consumer.scan_securities(); // finds o1,o2,s1 - consumer.scan_securities(); // finds nothing - REQUIRE(security_count == 3); - - // Then add the last security and we should see s2 - add_security(o3); - check_func = [&](const OldSecurity& security) { - if (security.security_type() == SecurityType::SPREAD) { - set legs; - for (const auto& leg : security.spread().legs()) { - legs.insert(leg.security().security_key()); - } - REQUIRE(security.security_key() == 501); - REQUIRE(legs.size() == 2); - REQUIRE(legs.count(401) == 1); - REQUIRE(legs.count(402) == 1); - } - }; - consumer.scan_securities(); - REQUIRE(security_count == 5); - } - - #ifndef WIN32 - void test_settlement_recorder() { - using namespace datetime; - security_key k1 = 401, k2 = 402; - OldSecurity o1 = mkoption("OO", k1, 2016, 5, 1, 100); - OldSecurity o2 = mkoption("OO", k2, 2016, 5, 2, 200); - MDProducer producer(Platform::SIM); - auto add_security = [&](const OldSecurity& sec) { - unique_ptr phandle; - with_security(sec.security_type(), sec.spread().legs_size(), [&](FixedSecurity& fsec) { - convert_security(sec, fsec, true); - phandle.reset(new MDProducerHandle(producer.add_security(fsec))); - }); - return phandle; - }; - auto ph1 = *add_security(o1); - auto ph2 = *add_security(o2); - set consumer_symbols; - consumer_symbols.insert(UserSymbol("OO", Exchange::TESTEXCHANGE, SecurityType::OPTION)); - MDConsumer consumer(consumer_symbols); - auto h1 = consumer.get_handle(k1); - REQUIRE(h1.is_initialized()); - auto h2 = consumer.get_handle(k2); - - // Pick 2 valid dates in the future - auto today = datetime::today(); - today.year += 1; - auto yesterday = datetime::add_days(today, -1); - - { - co2::SettlementRecorder recorder(Platform::UNSET); - recorder.add_settlement(ph1, 101, yesterday); - recorder.add_settlement(ph1, 102, today); - recorder.add_settlement(ph2, 199, yesterday); - recorder.add_settlement(ph2, 188, today); - recorder.do_work(); - recorder.do_work(); - } - REQUIRE(h1.misc().settlement_price == 102); - REQUIRE(h1.misc().settlement_date == today); - REQUIRE(h1.misc().previous_settlement_price == 101); - REQUIRE(h1.misc().previous_settlement_date == yesterday); - REQUIRE(h2.misc().settlement_price == 188); - REQUIRE(h2.misc().settlement_date == today); - REQUIRE(h2.misc().previous_settlement_price == 199); - REQUIRE(h2.misc().previous_settlement_date == yesterday); - - // Update but skip persist - auto tomorrow = datetime::add_days(today, 1); - ph1.update_settlement(TimeStamp::now(), 103, tomorrow); - REQUIRE(h1.misc().settlement_price == 103); - REQUIRE(h1.misc().settlement_date == tomorrow); - REQUIRE(h1.misc().previous_settlement_price == 102); - REQUIRE(h1.misc().previous_settlement_date == today); - ph1.update_previous_settlement(TimeStamp::now(), 0, Date()); - REQUIRE(h1.misc().settlement_price == 103); - REQUIRE(h1.misc().settlement_date == tomorrow); - ph1.update_settlement(TimeStamp::now(), 0, Date()); - REQUIRE(h1.misc().settlement_price == 0); - REQUIRE(h1.misc().settlement_date.day == 0); - - // pushing a new settlement for today doesn't invalidate yesterday - ph2.update_settlement(TimeStamp::now(), 201, today); - REQUIRE(h2.misc().settlement_price == 201); - REQUIRE(h2.misc().settlement_date == today); - REQUIRE(h2.misc().previous_settlement_price == 199); - REQUIRE(h2.misc().previous_settlement_date == yesterday); - - // Restore persisted state - { - co2::SettlementRecorder recorder(Platform::UNSET); - recorder.restore_settlement(ph1); - recorder.restore_settlement(ph2); - recorder.do_work(); - } - REQUIRE(h1.misc().settlement_price == 102); - REQUIRE(h1.misc().settlement_date == today); - REQUIRE(h1.misc().previous_settlement_price == 101); - REQUIRE(h1.misc().previous_settlement_date == yesterday); - REQUIRE(h2.misc().settlement_price == 188); - REQUIRE(h2.misc().settlement_date == today); - REQUIRE(h2.misc().previous_settlement_price == 199); - REQUIRE(h2.misc().previous_settlement_date == yesterday); - } - #endif - - void teardown() {} -} ; -std::unique_ptr co2_tests; - -TEST_CASE("CO2Tests","[md]") { - - if (!co2_tests) - co2_tests = std::make_unique(); - - co2_tests->setup(); - - #ifndef WIN32 - SECTION("test_security_id_recorder") { - co2_tests->test_security_id_recorder(); - } - #endif - SECTION("test_price_size") { - co2_tests->test_price_size(); - } - SECTION("test_slow_double_conversion") { - co2_tests->test_slow_double_conversion(); - } - SECTION("test_compact_exponent") { - co2_tests->test_compact_exponent(); - } - SECTION("test_producer_consumer") { - co2_tests->test_producer_consumer(); - } - SECTION("test_consumer_callback_on_security_added") { - co2_tests->test_consumer_callback_on_security_added(); - } - SECTION("test_consumer_defer_handle1") { - co2_tests->test_consumer_defer_handle1(); - } - SECTION("test_consumer_defer_handle2") { - co2_tests->test_consumer_defer_handle2(); - } - SECTION("test_spread_resolver") { - co2_tests->test_spread_resolver(); - } - SECTION("test_no_double_security_callback") { - co2_tests->test_no_double_security_callback(); - } - SECTION("test_recovery") { - co2_tests->test_recovery(); - } - SECTION("test_no_block_forever_on_publisher_crash") { - co2_tests->test_no_block_forever_on_publisher_crash(); - } - SECTION("test_get_handles") { - co2_tests->test_get_handles(); - } - SECTION("test_denormalization") { - co2_tests->test_denormalization(); - } - SECTION("test_denormalization2") { - co2_tests->test_denormalization2(); - } - SECTION("test_retry_unresolved_spread") { - co2_tests->test_retry_unresolved_spread(); - } - #ifndef WIN32 - SECTION("test_settlement_recorder") { - co2_tests->test_settlement_recorder(); - } - #endif -} - -struct Co2IndexTests { - vector random_ids; - set used_ids; - const int iterations; - tbb::task_scheduler_init _tbb; - - Co2IndexTests() : - random_ids(), - used_ids(), - iterations(50), - _tbb(tbb::task_scheduler_init::automatic) - {} - - void setup() { - used_ids.clear(); - random_ids.clear(); - - std::uniform_int_distribution rand_distribution(1, 0x7FFFFFFF); - std::mt19937 random_number_engine; - auto genrandom = std::bind(rand_distribution, random_number_engine); - set temp_ids; - for (int x = 0; x < 9999; ++x) { - auto r = genrandom(); - if (x < 1000) { used_ids.insert(r); } - temp_ids.insert(r); - } - for (uint64_t id : temp_ids) { - random_ids.push_back(id); - } - REQUIRE(random_ids.size() > 1000); - } - - struct Handle { - Handle() : _key(0) {} - uint64_t _key; - security_key key() const { return _key; } - void key(security_key key) { _key = key; } - }; - - struct SecurityData { - uint64_t exchange_id; - OldSecurity security; - Handle handle; - }; - - template - void gen_securities(Index& index) { - unique_ptr temp; - uint32_t count = 0; - for (auto& id : used_ids) { - ++count; - temp.reset(new SecurityData()); - temp->security.Clear(); - temp->security.set_security_type(SecurityType::OPTION); - temp->security.mutable_option()->set_symbol("FOOX"); - temp->security.set_numeric_id(id); - temp->exchange_id = id; - temp->handle.key(count); - index.add_security(id, move(temp)); - } - } - - - template - void test_random_search(Index& index) { - vector temp(random_ids); - std::random_shuffle(temp.begin(), temp.end()); - for (auto& id : temp) { - if (used_ids.find(id) != used_ids.end()) { - REQUIRE(index.has_security(id)); - } - else { - REQUIRE(index.get_security(id) == nullptr); - } - } - } - - template - void test_walk(Index& index) { - int count = 0; - index.walk_security_list([&count](const SecurityData&) { ++count; }); - REQUIRE(count == (int) used_ids.size()); - } - - typedef SimpleSecurityIndex MySimpleIndex; - void test_simple_index_search() { - MySimpleIndex index; - gen_securities(index); - for (int x = 0; x < iterations; ++x) { - test_random_search(index); - } - } - - void test_simple_index_parallel_search() { - MySimpleIndex index; - gen_securities(index); - tbb::parallel_for(0, iterations, [&](int) { - test_random_search(index); - }); - } - - void test_simple_index_iterate() { - MySimpleIndex index; - gen_securities(index); - test_walk(index); - } - - typedef ConcurrentSecurityIndex MyConcurrentIndex; - void test_concurrent_index_search() { - MyConcurrentIndex index; - gen_securities(index); - for (int x = 0; x < iterations; ++x) { - test_random_search(index); - } - } - void test_concurrent_index_parallel_search() { - MyConcurrentIndex index; - gen_securities(index); - tbb::parallel_for(0, iterations, [&](int) { - test_random_search(index); - }); - } - void test_concurrent_index_iterate() { - MyConcurrentIndex index; - gen_securities(index); - test_walk(index); - } -} ; - -std::unique_ptr co2_index_tests; - -TEST_CASE ("Co2IndexTests","[md]") -{ - if (!co2_index_tests) - co2_index_tests = std::make_unique (); - - co2_index_tests->setup(); - - SECTION("test_simple_index_search") { - co2_index_tests->test_simple_index_search(); - } - - SECTION("test_simple_index_parallel_search") { - co2_index_tests->test_simple_index_parallel_search(); - } - - SECTION("test_simple_index_iterate") { - co2_index_tests->test_simple_index_iterate(); - } - SECTION("test_concurrent_index_search") { - co2_index_tests->test_concurrent_index_search(); - } - SECTION("test_concurrent_index_iterate") { - co2_index_tests->test_concurrent_index_iterate(); - } - SECTION("test_concurrent_index_parallel_search") { - co2_index_tests->test_concurrent_index_parallel_search(); - } -} - -struct Co2PerfTests { - static const size_t instrument_count = 2048; - vector> test_instruments; - vector producers; - vector consumers; - - Co2PerfTests() : - test_instruments(), - producers(), - consumers() - { -#ifdef _WIN32 -#pragma warning(push, 0) -#endif - // OK if this isn't aligned for testing - for (size_t x = 0; x < instrument_count; ++x) { - test_instruments.push_back( - std::unique_ptr( - new Co2TestInstrument("Foo", SecurityType::OPTION))); - } -#ifdef _WIN32 -#pragma warning(pop) -#endif - for (size_t x = 0; x < test_instruments.size(); ++x) { - auto producer = test_instruments[x]->producer(); - producer.set_key(x + 1); - producers.push_back(producer); - auto consumer = test_instruments[x]->consumer(); - REQUIRE(consumer.key() == x + 1); - consumers.push_back(consumer); - } - - std::srand(std::time(0)); - //std::random_shuffle(producers.begin(), producers.end(), [](int i) { return std::rand() % i; }); - std::random_shuffle(consumers.begin(), consumers.end(), [](int i) { return std::rand() % i; }); - } - void setup() { } - void teardown() {}; - void test_write_bbo_perf() { - REQUIRE(producers.size() == instrument_count); - - // Warmup - for (size_t x = 0; x < producers.size(); ++x) { - auto& p = producers[x]; - // p.update_bbo(TimeStamp::now(), PriceSize::from_double<3>(100.0 + x, x), PriceSize::from_double<3>(200 + x, 2 * x)); - p.update_bbo(TimeStamp::now(), PriceSize(100+x,0,x), PriceSize(200+x,0,2*x)); - } - - // Test - TimeStamp start = TimeStamp::now(); - size_t count = 0; - const size_t iterations = 1000; - for (size_t loop = 0; loop < iterations; ++loop) { - for (size_t x = 0; x < producers.size(); ++x) { - ++count; - auto& p = producers[x]; - TimeStamp t = TimeStamp::now(); - // p.update_bbo(t, PriceSize::from_double<3>(100.0 + count, count), PriceSize::from_double<3>(200 + count, 2 * count)); - p.update_bbo(t, PriceSize(100+x,0,x), PriceSize(200+x,0,2*x)); - } - } - TimeStamp end = TimeStamp::now(); - std::stringstream ss; - // Of which approximately 1/3 is generating the timestamp on lab-lx4 - ss << "\t" << (end - start).nanoseconds() / iterations / instrument_count << " ns per update *\n"; - std::cout << ss.str(); - } - - void test_read_bbo_perf() { - // Warmup - const auto& write_all = [](vector& producers) { - // Offset each price by a random amount each iteration to ensure that writes actually happen - size_t rand_factor = std::rand() % 1024; - for (size_t x = 0; x < producers.size(); ++x) { - auto& p = producers[x]; - // p.update_bbo(TimeStamp::now(), PriceSize::from_double<3>(100.0 + x + rand_factor, x+rand_factor), PriceSize::from_double<3>(200 + x+rand_factor, 2 * x + rand_factor)); - p.update_bbo(TimeStamp::now(), PriceSize(100+x+rand_factor,0,x+rand_factor), PriceSize(200+x+rand_factor,0,2*x+rand_factor)); - } - }; - write_all(producers); - - for (auto& c : consumers) { - PriceSize bid, ask; - TimeStamp ts; - REQUIRE(c.get_bbo(ts, bid, ask) == HandleReadResult::SUCCESS); - } - - // Start a thread which loops and writes to the producers until terminated - utils::Dispatcher& writer = utils::Dispatcher::create_new("bbo_writer"); - DEFER(writer.terminate()); - tbb::queuing_mutex producer_mutex; - auto* pproducers = &producers; - function write_all_repeat = [&]() { - tbb::queuing_mutex::scoped_lock lock(producer_mutex); - if (pproducers != nullptr) { - write_all(*pproducers); - writer.post(write_all_repeat); - } - }; - write_all_repeat(); - - // Perform read test - TimeStamp start = TimeStamp::now(); - size_t read_count = 0; - const size_t iterations = 1000; - for (size_t loop = 0; loop < iterations; ++loop) { - TimeStamp ts = TimeStamp::now(); - for (size_t x = 0; x < consumers.size(); ++x) { - PriceSize bid, ask; - TimeStamp ts_temp = ts; - if (consumers[x].get_bbo(ts_temp, bid, ask) == HandleReadResult::SUCCESS) { - ++read_count; - } - } - } - REQUIRE(read_count > 0); - - TimeStamp end = TimeStamp::now(); - std::stringstream ss; - ss << "\t" << (end - start).nanoseconds() / iterations / instrument_count << " ns per update. Actual reads = " << read_count << " / " << instrument_count*iterations << " * \n"; - std::cout << ss.str(); - - writer.terminate(); - - tbb::queuing_mutex::scoped_lock lock(producer_mutex); - pproducers = nullptr; - std::this_thread::sleep_for(2s); - - } - -} ; - -std::unique_ptr co2_perf_tests; - -const size_t Co2PerfTests::instrument_count ; -TEST_CASE ("Co2PerfTests","[md]") -{ - - if (!co2_perf_tests) - co2_perf_tests = std::make_unique(); - - co2_perf_tests->setup(); - - SECTION("test_write_bbo_perf") { - // co2_perf_tests->test_write_bbo_perf(); - } - SECTION("test_read_bbo_perf") { - // co2_perf_tests->test_read_bbo_perf(); - } -} - -// struct Co2RelayTests { +// #ifndef NO_CPP0X + +// #include "catch/catch.hpp" +// #include "../SettlementRecorder.h" +// #include "../coconut2.h" +// #include "../relay/QueryServer.h" +// #include "Co2TestInstrument.h" +// #include "lib/config/config.h" +// #include "lib/os/os.h" +// #include "lib/security_model/SecurityHelper.h" +// #include "lib/utils/Dispatcher.h" +// #include "lib/utils/defer.h" +// #include "lib/utils/warn_ctrl.h" +// #include "lib/utils/bts_chrono.h" + +// #include "lib/config/protos/MarketData.pb.h" + +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include + + +// using namespace bts::md2; +// using namespace bts::md; +// using namespace std; + +// namespace bts { +// namespace md2 { +// namespace test { + +// // +// // Helper functions for Producer/Consumer tests +// // +// vector create_test_securities(MDProducer& producer, const char* postfix ="") { +// FixedOptionSecurity fos; +// FixedFutureSecurity ffs; +// ffs.symbol("FOO"+string(postfix)); +// fos.symbol("FOOX"+string(postfix)); +// ffs.key = invalid_security_key; +// fos.key = invalid_security_key; +// ffs.expiration_year = fos.expiration_year = 2015; +// ffs.expiration_day = fos.expiration_day = 27; +// fos.strike = 1000; +// fos.call_put = PutOrCall::CALL; +// // Add 12 expirations to 2 different shared memory regions +// vector result; +// for (uint32_t x = 1; x <= 12; ++x) { +// ffs.expiration_month = fos.expiration_month = x; +// result.push_back(producer.add_security(ffs)); +// result.push_back(producer.add_security(fos)); +// // In a real app these securities would now be passed to the SecurityIDRecorder +// } +// return result; +// } + +// set get_test_symbols(const char* postfix="") { +// set symbols; +// UserSymbol future_symbol(string("FOO")+postfix, Exchange::TESTEXCHANGE, SecurityType::FUTURE); +// symbols.insert(future_symbol); +// UserSymbol option_symbol(string("FOOX")+postfix, Exchange::TESTEXCHANGE, SecurityType::OPTION); +// symbols.insert(option_symbol); +// return symbols; +// } + +// OldSecurity mkfuture(string symbol, security_key key, int year, int month, int day) { +// OldSecurity result; +// result.set_security_type(SecurityType::FUTURE); +// SecurityHelper::set_symbol(result, symbol); +// result.set_exchange(Exchange::TESTEXCHANGE); +// result.set_security_key(key); +// result.mutable_future()->set_expiration_day(day); +// result.mutable_future()->set_expiration_month(month); +// result.mutable_future()->set_expiration_year(year); +// return result; +// } +// OldSecurity mkoption(string symbol, security_key key, int year, int month, int day, double strike) { +// OldSecurity result; +// result.set_security_type(SecurityType::OPTION); +// SecurityHelper::set_symbol(result, symbol); +// result.set_exchange(Exchange::TESTEXCHANGE); +// result.set_security_key(key); +// result.mutable_option()->set_expiration_day(day); +// result.mutable_option()->set_expiration_month(month); +// result.mutable_option()->set_expiration_year(year); +// result.mutable_option()->set_strike(strike); +// result.mutable_option()->set_call_put(PutOrCall::PUT); +// return result; +// } +// FixedOptionSecurity mkfixedoption(string symbol, security_key key, int year, int month, int day, double strike, PutOrCall::type callput) { +// FixedOptionSecurity fos; +// fos.type = SecurityType::OPTION; +// fos.exchange = Exchange::TESTEXCHANGE; +// fos.key = key; +// fos.strike = strike; +// fos.expiration_day = day; +// fos.expiration_month = month; +// fos.expiration_year = year; +// fos.call_put = callput; +// fos.symbol(symbol); +// return fos; +// } +// OldSecurity mkspread(string symbol, security_key key, const vector& legs) { +// OldSecurity result; +// result.set_security_type(SecurityType::SPREAD); +// SecurityHelper::set_symbol(result, symbol); +// result.set_exchange(Exchange::TESTEXCHANGE); +// result.set_security_key(key); +// for (const auto& leg : legs) { +// auto* pleg = result.mutable_spread()->add_legs(); +// pleg->mutable_security()->CopyFrom(leg); +// } +// return result; +// } +// OldSecurity mkspread(string symbol, security_key key, OldSecurity leg1, OldSecurity leg2) { +// OldSecurity legs[] = { leg1, leg2 }; +// vector vlegs(begin(legs), end(legs)); +// return mkspread(symbol, key, vlegs); +// } +// OldSecurity mkspread(string symbol, security_key key, OldSecurity leg1, OldSecurity leg2, OldSecurity leg3) { +// OldSecurity legs[] = { leg1, leg2, leg3 }; +// vector vlegs(begin(legs), end(legs)); +// return mkspread(symbol, key, vlegs); +// } + +// void assign_ids(vector::iterator begin, vector::iterator end, security_key start_key = 1000) { +// for (; begin != end; ++begin) { +// (*begin).set_key(++start_key); +// } +// } + +// class Co2Tests +// { +// public: // void setup() { // config::internal::initialize_for_test( // "PlatformMap {" @@ -1364,177 +147,1394 @@ TEST_CASE ("Co2PerfTests","[md]") // " exchange: TESTEXCHANGE" // " }" // " }" -// "}"); +// "}" +// "PublisherOptions {" +// " value {" +// " heartbeat_ms: 100" +// " heartbeat_boot_seconds: 1" +// " }" +// "}" +// ); // } -// void teardown() {} -// MDConsumer create_consumer() { +// #ifndef WIN32 +// void test_security_id_recorder() { +// // Verify callbacks happen after work complete +// SecurityIDRecorder idsvc, idsvc2; +// security_key result = invalid_security_key; +// std::string desc = "FOOX 1"; +// idsvc.get_key(desc, SecurityType::FUTURE, [](void* arg, security_key key) { +// security_key* presult = (security_key*)arg; +// *presult = key; +// }, &result); +// idsvc.do_work(); +// REQUIRE(result != invalid_security_key); +// security_key result2 = invalid_security_key; +// idsvc2.get_key(desc, SecurityType::OPTION, [](void* arg, security_key key) { +// *((security_key*)arg) = key; +// }, &result2); +// idsvc2.do_work(); +// REQUIRE(result == result2); +// } +// #endif + +// void test_price_size() { +// // PriceSize violates some type punning rules +// // So it is important to have this test work correctly on the target platform +// PriceSize p, p2; +// p = PriceSize::from_double<2>(4.5, 14); +// SingleWriterPriceSizeStore memory; +// memory.store(p); +// p2 = memory.load(); +// REQUIRE(p.price() == 4.5); +// REQUIRE(p.size() == 14); +// REQUIRE(p2.price() == 4.5); +// REQUIRE(p2.size() == 14); + +// p = PriceSize::from_double<2>(4.0, 5); +// memory.store(p); +// p2 = memory.load(); +// REQUIRE(p2.price() == 4.0); +// REQUIRE(p2.size() == 5); + +// p = PriceSize::from_double<2>(-4.2, 6); +// memory.store(p); +// p2 = memory.load(); +// REQUIRE(p2.price() == -4.2); +// REQUIRE(p2.size() == 6); + +// p = PriceSize(4, -1, 2); +// memory.store(p); +// p2 = memory.load(); +// REQUIRE(p2.price() == 0.4); +// REQUIRE(p2.size() == 2); + +// // Test requirement for more than 32 bit mantissa +// double bigval = -9999.9921875; +// p = PriceSize::from_double<7>(bigval, 42); +// memory.store(p); +// p2 = memory.load(); +// REQUIRE(p2.price() == bigval); +// REQUIRE(p2.size() == 42); +// } + +// void test_slow_double_conversion() { +// double values[] = { 1.5, 0.5, 0.2155, 0.216, 42.1 }; +// int expected[] = { -1, -1, -4, -3, -1 }; +// static_assert(BTS_ARRAYSIZE(values) == BTS_ARRAYSIZE(expected), ""); +// for (size_t x = 0; x < BTS_ARRAYSIZE(values); ++x) { +// REQUIRE(to_exponent(values[x]) == expected[x]); +// } + +// // Tests powers of 10. Important for display_factor conversion +// for (int x = 8; x > -8; --x) { +// double d = pow10(x); +// REQUIRE(to_exponent(d) == x); +// } +// } + +// struct ExchangeDecimal { +// int64_t mantissa; +// int32_t exponent; +// }; +// bool close_enough(double a, double b) { +// // We need at least 7 places but we're testing with 10 just in case +// const double acceptable_error = 1E-12; +// double delta = abs(a - b); +// // Equal if the delta is less than error +// return delta < acceptable_error; +// } +// void test_compact_exponent() { +// ExchangeDecimal test_values[] = { +// { 2030000000000, -9 }, // mantissa > pow(2,40) +// { -2030000000000, -9 }, // mantissa > pow(2,40) +// { 2022750000000, -9 },// mantissa > pow(2,40) +// { -2022750000000, -9 },// mantissa > pow(2,40) +// { 20227500000000, -10 },// mantissa > pow(2,40) +// { 202275000000000, -11 },// mantissa > pow(2,40) +// { 2022750000000000, -12 },// mantissa > pow(2,40) +// { 20227500000000000, -13 },// mantissa > pow(2,40) +// { -20227500000000000, -13 },// mantissa > pow(2,40) +// { 203, 1}, // control +// { -14000000000, -8 }, // control +// { -14, 1 }, // control +// { 20227500, -4 }, // control +// }; +// for (auto p : test_values) { +// double expected = p.mantissa * pow10(p.exponent); +// compact_mantissa(p.mantissa, p.exponent); +// double compacted = p.mantissa * pow10(p.exponent); +// REQUIRE(close_enough(expected, compacted)); +// } +// } + +// void test_producer_consumer() { +// MDProducer producer(Platform::SIM); +// vector producers = create_test_securities(producer); + +// // Add bbo data for those expirations +// auto write_time = TimeStamp::now(); +// for (auto& p : producers) { +// p.update_bid(write_time, PriceSize::from_double<2>(5.0, 5)); +// p.update_ask(write_time, PriceSize::from_double<2>(5.0, 5)); +// } + +// // Symbol list for the consumer // set symbols; -// UserSymbol future_symbol("FOO", Exchange::TESTEXCHANGE, SecurityType::FUTURE); +// UserSymbol future_symbol( "FOO", Exchange::TESTEXCHANGE, SecurityType::FUTURE ); // symbols.insert(future_symbol); -// UserSymbol option_symbol("FOOX", Exchange::TESTEXCHANGE, SecurityType::OPTION); +// UserSymbol option_symbol( "FOOX", Exchange::TESTEXCHANGE, SecurityType::OPTION ); // symbols.insert(option_symbol); // MDConsumer consumer(symbols); -// return consumer; + +// vector consumer_securities; +// // Poll for security list - none visible until they have keys +// size_t count = consumer.walk_security_list([&](const OldSecurity& security, MDConsumerHandle) {consumer_securities.push_back(security);}); +// // Since no security has an id none are "public" +// REQUIRE(consumer_securities.size() == count); +// REQUIRE(count == 0); +// // Assign a unique ID to each security (from the producer) +// assign_ids(producers.begin(), producers.end()); +// // Now securities can be enumerated by the client +// count = consumer.walk_security_list([&](const OldSecurity& security, MDConsumerHandle) {consumer_securities.push_back(security);}); +// REQUIRE(consumer_securities.size() == count); +// REQUIRE(count == 24); + +// vector consumers; +// for(const auto& security : consumer_securities) { +// consumers.push_back(consumer.get_handle(security.security_key())); +// } + +// for (const auto& c : consumers) { +// PriceSize bid, ask; +// TimeStamp read_time; +// // get_boo returns true becuase this time is after the change +// REQUIRE(c.get_bbo(read_time, bid, ask) == HandleReadResult::SUCCESS); +// REQUIRE((read_time - TimeStamp()).seconds() > 0); +// REQUIRE(bid.price() == 5.0); +// REQUIRE(ask.price() == 5.0); +// REQUIRE(bid.size() == 5); +// REQUIRE(ask.size() == 5); +// } + +// // Second query before a write has happend finds no updates +// auto read_time = TimeStamp::now(); +// for (const auto& c : consumers) { +// PriceSize bid, ask; +// REQUIRE(c.get_bbo(read_time, bid, ask) != HandleReadResult::SUCCESS); +// } + +// // update everything once more +// write_time = TimeStamp::now(); +// for (auto& p : producers) { +// // Update bid/ask with one call +// p.update_bbo(write_time, PriceSize::from_double<2>(6.55, 6), PriceSize::from_double<2>(6.55, 6)); +// } +// // Now read_time is < write_time +// for (const auto& c : consumers) { +// TimeStamp modified_time = read_time; +// PriceSize bid, ask; +// // get_bbo returns true becuase this time is after the change +// REQUIRE(c.get_bbo(modified_time, bid, ask) == HandleReadResult::SUCCESS); +// REQUIRE( (modified_time - read_time).seconds() > 0); +// REQUIRE(bid.price() == 6.55); +// REQUIRE(ask.price() == 6.55); +// REQUIRE(bid.size() == 6); +// REQUIRE(ask.size() == 6); +// } // } - -// template -// T simulate_wire_transfer(const T& request) { -// T temp; -// std::string buffer; -// request.SerializeToString(&buffer); -// temp.ParseFromString(buffer); -// return temp; -// } - -// PriceSize convert(const co2::PriceSizeMessage& psm) { -// return PriceSize(psm.mantissa(), psm.exponent(), psm.size()); -// } - -// void relay_nans() { +// // When a security is added the consumer should provide callback +// void test_consumer_callback_on_security_added() { // MDProducer producer(Platform::SIM); -// UserSymbol symbol("FOO", Exchange::TESTEXCHANGE, SecurityType::OPTION); - -// // initialize data -// vector producers; -// for (size_t x = 0; x < 100; ++x) { -// auto strike = 1000 + (100 * x); -// producers.push_back( -// producer.add_security( -// mkfixedoption("FOO", invalid_security_key, 2017, 1, 28, strike, PutOrCall::PUT))); -// producers.push_back( -// producer.add_security( -// mkfixedoption("FOO", invalid_security_key, 2017, 1, 28, strike, PutOrCall::CALL))); -// } -// assign_ids(producers.begin(), producers.end(), 1000); -// PriceSize price35 = PriceSize::from_double<2>(3.5, 5); -// PriceSize price5 = PriceSize::from_double<2>(5, 5); -// for(auto p : producers) { -// p.update_bbo(TimeStamp::now(), price35, price35); -// } +// vector producers = create_test_securities(producer); -// // Read using QueryServer +// // Symbol list for the consumer // set symbols; -// symbols.insert(symbol); -// auto server = create_query_server(symbols); -// REQUIRE(server); -// server->scan_securities(); +// UserSymbol future_symbol( "FOO", Exchange::TESTEXCHANGE, SecurityType::FUTURE ); +// symbols.insert(future_symbol); +// UserSymbol option_symbol( "FOOX", Exchange::TESTEXCHANGE, SecurityType::OPTION ); +// symbols.insert(option_symbol); +// MDConsumer consumer(symbols); -// co2::MDRequest request; -// auto* chan = request.add_channels(); -// chan->set_type(symbol.type()); -// chan->set_symbol(symbol.symbol()); -// chan->set_exchange(symbol.exchange()); -// chan->set_flags(co2::MDRequest::BBO | co2::MDRequest::SECURITY); +// // Add the on_security callback +// vector securities; +// consumer.on_security([&](const OldSecurity& security, MDConsumerHandle handle) { +// REQUIRE(security.security_key() != invalid_security_key); +// REQUIRE(security.security_key() == handle.key()); +// securities.push_back(security); +// }); +// // No securities have ids yet +// REQUIRE(securities.size() == 0); +// // Check for new securities +// consumer.scan_securities(); +// // Still none +// REQUIRE(securities.size() == 0); +// producers.front().set_key(1001); +// consumer.scan_securities(); +// REQUIRE(securities.size() == 1); -// // This request should include everything -// co2::MDReply reply; -// server->query(request, &reply); -// reply = simulate_wire_transfer(reply); -// REQUIRE((size_t)reply.channels().size() == symbols.size()); -// REQUIRE((size_t)reply.channels(0).updates().size() == producers.size()); -// // update the request to match the server time -// for (auto& update : reply.channels(0).updates()) { -// REQUIRE(update.has_security()); -// REQUIRE(update.has_book()); -// REQUIRE(update.book().asks_size() > 0); -// REQUIRE(update.book().bids_size() > 0); -// REQUIRE(convert(update.book().bids(0)) == price35); -// REQUIRE(convert(update.book().asks(0)) == price35); +// // Assign the rest of the ids +// assign_ids(producers.begin() + 1, producers.end(), producers.front().security().key); +// consumer.scan_securities(); +// REQUIRE(securities.size() == producers.size()); +// } + +// // If an MDConsumer is asked for a handle to a security which does not exist in the system +// // The consumer should return a handle which always returns false on all calls but wakes up +// // when the security is added. +// // * Case 1 - the shared memory has been created but the security has not been discovered +// // * Case 2 - The producer has never been created +// void test_consumer_defer_handle1() { +// MDProducer producer(Platform::SIM); +// vector producers = create_test_securities(producer); + +// set symbols; +// UserSymbol future_symbol( "FOO", Exchange::TESTEXCHANGE, SecurityType::FUTURE ); +// symbols.insert(future_symbol); +// UserSymbol option_symbol( "FOOX", Exchange::TESTEXCHANGE, SecurityType::OPTION ); +// symbols.insert(option_symbol); +// MDConsumer consumer(symbols); + +// // Get a handle to 24 consumers which haven't been created yet +// vector consumers; +// vector> managed_consumers; +// security_key key = 1; +// for(; key < 24; ++key) { +// consumers.push_back(consumer.get_handle(key+1000)); +// managed_consumers.push_back(consumer.get_managed_handle(key+1000)); // } + +// // Set bbo on the producers - still not public +// TimeStamp write_time = TimeStamp::now(); +// for (auto& p : producers) { +// p.update_bbo(write_time, PriceSize::from_double<3>(4.5, 5), PriceSize::from_double<3>(6.75, 7)); +// } +// // Even though bbo is available it is not given out because the producing security does not have an id +// PriceSize bid, ask; +// for(auto& c : consumers) { +// TimeStamp zerotime; +// REQUIRE(c.get_bbo(zerotime, bid, ask) != HandleReadResult::SUCCESS); +// } +// for(auto& mc : managed_consumers) { +// TimeStamp zerotime; +// REQUIRE(mc->get_bbo(zerotime, bid, ask) != HandleReadResult::SUCCESS); +// } + +// assign_ids(producers.begin(), producers.end()); +// consumer.scan_securities(); +// size_t bbo_count = 0; +// // Existing old handles remain invalid +// for(auto& c : consumers) { +// TimeStamp zerotime; +// if (c.get_bbo(zerotime, bid, ask) == HandleReadResult::SUCCESS) { +// bbo_count++; +// } +// } +// REQUIRE(bbo_count == 0); +// // Now, magically, the existing (managed) handles are valid +// for(auto& mc : managed_consumers) { +// TimeStamp zerotime; +// if (mc->get_bbo(zerotime, bid, ask) == HandleReadResult::SUCCESS) { +// bbo_count++; +// } +// } +// REQUIRE(bbo_count > 0); +// } + + +// // It is legal and valid to create a consumer before the producer process has started +// // Handles given out by that consumer will become valid once the securities are discovered +// void test_consumer_defer_handle2() { +// set symbols; +// UserSymbol future_symbol( "FOO", Exchange::TESTEXCHANGE, SecurityType::FUTURE ); +// symbols.insert(future_symbol); +// UserSymbol option_symbol( "FOOX", Exchange::TESTEXCHANGE, SecurityType::OPTION ); +// symbols.insert(option_symbol); +// MDConsumer consumer(symbols); + +// // Get a handle to 24 consumers which haven't been created yet +// vector consumers; +// vector> managed_consumers; +// security_key key = 1; +// for(; key < 24; ++key) { +// consumers.push_back(consumer.get_handle(key+1000)); +// managed_consumers.push_back(consumer.get_managed_handle(key+1000)); +// } + +// // Even though bbo is available it is not given out because the producing security does not have an id +// PriceSize bid, ask; +// for(auto& c : consumers) { +// TimeStamp zerotime; +// REQUIRE(c.get_bbo(zerotime, bid, ask) != HandleReadResult::SUCCESS); +// } +// for(auto& mc : managed_consumers) { +// TimeStamp zerotime; +// REQUIRE(mc->get_bbo(zerotime, bid, ask) != HandleReadResult::SUCCESS); +// } + +// // Only NOW create the producers (and the shared memory) +// MDProducer producer(Platform::SIM); +// vector producers = create_test_securities(producer); +// // Set bbo on the producers - still not public +// TimeStamp write_time = TimeStamp::now(); +// for (auto& p : producers) { +// p.update_bbo(write_time, PriceSize::from_double<3>( 4.5, 5 ), PriceSize::from_double<3>(6.75, 7)); +// } + +// assign_ids(producers.begin(), producers.end()); +// consumer.scan_securities(); +// size_t bbo_count = 0; +// // Existing old handles remain invalid +// for(auto& c : consumers) { +// TimeStamp zerotime; +// if (c.get_bbo(zerotime, bid, ask) == HandleReadResult::SUCCESS) { +// bbo_count++; +// } +// } +// REQUIRE(bbo_count == 0); +// // Now, magically, the existing (managed) handles are valid +// for(auto& mc : managed_consumers) { +// TimeStamp zerotime; +// if (mc->get_bbo(zerotime, bid, ask) == HandleReadResult::SUCCESS) { +// bbo_count++; +// } +// } +// REQUIRE(bbo_count > 0); +// } + +// void test_spread_resolver() { +// SpreadResolver resolver; +// vector legs; +// legs.push_back("1"); +// legs.push_back("2"); +// legs.push_back("3"); +// legs.push_back("4"); +// resolver.add("1234", legs); +// legs.clear(); +// legs.push_back("4"); +// legs.push_back("5"); +// resolver.add("45", legs); +// auto key_map = [](string& key) -> security_key { +// if (key == "1") return 1; +// if (key == "2") return 2; +// if (key == "3") return 3; +// if (key == "4") return 4; +// if (key == "5") return invalid_security_key; +// REQUIRE(false); +// return 0; +// }; +// auto key_map2 = [](string& key) -> security_key { +// if (key == "4") return 4; +// if (key == "5") return 5; +// REQUIRE(false); +// return 0; +// }; + +// // 2 unresolved spreads +// REQUIRE(resolver.count() == 2); +// resolver.resolve(key_map, [&](const string& key, const vector& legs) { +// REQUIRE(key == "1234"); +// REQUIRE(legs.size() == 4); +// }); +// // 1 spread remains +// REQUIRE(resolver.count() == 1); +// resolver.resolve(key_map2, [&](const string& key, const vector& legs) { +// REQUIRE(key == "45"); +// REQUIRE(legs.size() == 2); +// }); +// REQUIRE(resolver.count() == 0); +// } + + +// void test_no_double_security_callback() { +// MDProducer producer(Platform::SIM); +// vector producers = create_test_securities(producer); +// // Assign ids so securities are already public in the producer +// assign_ids(producers.begin(), producers.end()); + +// // Symbol list for the consumer +// auto syms = get_test_symbols(); +// MDConsumer consumer = MDConsumer(syms); + +// // Add the on_security callback - callback fires for existing securities +// vector securities; +// consumer.on_security([&](const OldSecurity& security, MDConsumerHandle handle) { +// REQUIRE(security.security_key() != invalid_security_key); +// REQUIRE(security.security_key() == handle.key()); +// securities.push_back(security); +// }); +// // Found all securities +// REQUIRE(securities.size() == producers.size()); +// // Check for new securities +// consumer.scan_securities(); +// // Expect no duplicates +// REQUIRE(securities.size() == producers.size()); +// } + +// void test_recovery() { +// auto producer1 = make_shared(Platform::SIM); +// vector producers = create_test_securities(*producer1); +// assign_ids(producers.begin(), producers.end()); +// producer1->get_channel("FOO", SecurityType::FUTURE, Exchange::TESTEXCHANGE).heartbeat(); +// producer1->get_channel("FOOX", SecurityType::OPTION, Exchange::TESTEXCHANGE).heartbeat(); +// for (auto& pro : producers) { pro.update_bbo(TimeStamp::now(), PriceSize(1, 0, 1), PriceSize(2, 0, 2)); } + +// // Symbol list for the consumer +// MDRecoverableConsumer consumer(get_test_symbols()); +// map> products; +// consumer.on_security([&](const OldSecurity& security, MDConsumerHandle handle) { +// REQUIRE(security.security_key() == handle.key()); +// products.insert(make_pair(security.security_key(), consumer.get_managed_handle(security.security_key()))); +// }); +// consumer.scan_securities(); +// REQUIRE(products.size() == producers.size()); + +// // Choose a handle +// for (auto& pair : products) { +// auto& handle = pair.second; +// REQUIRE(handle->is_initialized()); +// REQUIRE(handle->is_heart_beating()); +// } + +// // Delete the producer +// try { +// producers.clear(); +// producer1.reset(); +// } catch(std::exception&) { +// LOG(MarketData,Error,"Failed test trying to destroy producer."); +// REQUIRE(false); +// } + +// // Wait out the heartbeat +// auto options = config::get(); +// auto ms = std::max(options.heartbeat_ms(), options.heartbeat_boot_seconds()*1000)+10; -// // Second request with updated time should include nothing -// request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); -// reply.Clear(); -// server->query(request, &reply); -// reply = simulate_wire_transfer(reply); -// REQUIRE(reply.channels().size() == 1); -// REQUIRE(reply.channels(0).updates().size() == 0); +// std::this_thread::sleep_for(std::chrono::duration(ms)); -// // Update a single price and make sure we get that back -// producers[0].update_bbo(TimeStamp::now(), price5, price5, 1); -// request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); -// reply.Clear(); -// server->query(request, &reply); -// reply = simulate_wire_transfer(reply); -// REQUIRE(reply.channels().size() == 1); -// REQUIRE(reply.channels(0).updates().size() == 1); -// REQUIRE(reply.channels(0).updates(0).has_book()); -// REQUIRE(convert(reply.channels(0).updates(0).book().bids(0)) == price5); -// REQUIRE(convert(reply.channels(0).updates(0).book().asks(0)) == price5); - -// // NaN out all the prices - expect to get a full set of updates -// for(auto p : producers) { -// p.update_bbo(TimeStamp::now(), PriceSize::NO_MARKET, PriceSize::NO_MARKET, 2); -// } -// request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); -// reply.Clear(); -// server->query(request, &reply); -// reply = simulate_wire_transfer(reply); -// REQUIRE(reply.channels().size() == 1); -// REQUIRE((size_t)reply.channels(0).updates().size() == producers.size()); -// for (auto& update : reply.channels(0).updates()) { -// REQUIRE(update.has_book()); -// REQUIRE(update.book().bids_size() > 0); -// REQUIRE(update.book().asks_size() > 0); -// REQUIRE(convert(update.book().bids(0)) == PriceSize::NO_MARKET); -// REQUIRE(convert(update.book().asks(0)) == PriceSize::NO_MARKET); +// // Make sure heartbeat is done +// try { +// for (auto& pair : products) { +// auto& handle = pair.second; +// REQUIRE(handle->is_initialized()); +// REQUIRE(handle->is_heart_beating() == false); +// } +// } catch(std::exception&) { +// LOG(MarketData,Error,"Failed to validate heartbeat while producer is down."); +// REQUIRE(false); // } -// // Fill up the markets again -// for(auto p : producers) { -// p.update_bbo(TimeStamp::now(), price35, price35, 3); +// // Create a new publisher and attempt to recover +// shared_ptr producer2; +// try { +// producer2 = make_shared(Platform::SIM); +// } catch(std::exception&) { +// LOG(MarketData,Error,"Failed to create new producer"); +// REQUIRE(false); // } -// request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); -// reply.Clear(); -// server->query(request, &reply); -// reply = simulate_wire_transfer(reply); -// REQUIRE(reply.channels().size() == 1); -// REQUIRE((size_t)reply.channels(0).updates().size() == producers.size()); -// for (auto& update : reply.channels(0).updates()) { -// REQUIRE(update.has_book()); -// REQUIRE(update.book().bids_size() > 0); -// REQUIRE(update.book().asks_size() > 0); -// REQUIRE(convert(update.book().bids(0)) == price35); -// REQUIRE(convert(update.book().asks(0)) == price35); +// producers = create_test_securities(*producer2); +// REQUIRE(products.size() == producers.size()); +// assign_ids(producers.begin(), producers.end()); +// producer2->get_channel("FOO", SecurityType::FUTURE, Exchange::TESTEXCHANGE).heartbeat(); +// producer2->get_channel("FOOX", SecurityType::OPTION, Exchange::TESTEXCHANGE).heartbeat(); + +// // set bbo to a different value +// for (auto& pro : producers) { +// pro.update_bbo(TimeStamp::now(), PriceSize(4, 0, 5), PriceSize(6, 0, 7)); // } -// // NaN out the bids -// for(auto p : producers) { -// p.update_bbo(TimeStamp::now(), PriceSize::NO_MARKET, price35, 4); +// // Attempt to recover the managed handles +// REQUIRE(consumer.try_recover()); + +// // Since we know that both securities exist they should have been updated to the new section +// for (auto& pair : products) { +// auto& handle = pair.second; +// REQUIRE(handle->is_initialized()); +// REQUIRE(handle->is_heart_beating()); +// TimeStamp tzero; +// PriceSize bid, ask; +// REQUIRE(handle->get_bbo(tzero, bid, ask) == HandleReadResult::SUCCESS); +// REQUIRE(bid.price() == 4.0); +// REQUIRE(bid.size() == 5); +// REQUIRE(ask.price() == 6.0); +// REQUIRE(ask.size() == 7); // } -// request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); -// reply.Clear(); -// server->query(request, &reply); -// reply = simulate_wire_transfer(reply); -// REQUIRE(reply.channels().size() == 1); -// REQUIRE((size_t)reply.channels(0).updates().size() == producers.size()); -// for (auto& update : reply.channels(0).updates()) { -// REQUIRE(update.has_book()); -// REQUIRE(update.book().bids_size() > 0); -// REQUIRE(update.book().asks_size() > 0); -// REQUIRE(convert(update.book().bids(0)) == PriceSize::NO_MARKET); -// REQUIRE(convert(update.book().asks(0)) == price35); +// } + + +// void test_no_block_forever_on_publisher_crash() { +// #ifdef _WIN32 +// #pragma warning(push, 0) +// #endif +// // OK if this isn't aligned for testing +// auto src = std::make_unique ("Foo", SecurityType::FUTURE); +// #ifdef _WIN32 +// #pragma warning(pop) +// #endif +// TimeStamp read = TimeStamp::now(); +// src->producer().update_bbo(TimeStamp::invalid(), PriceSize::from_double<3>(100.0, 100), PriceSize::from_double<3>(101.0, 101)); +// PriceSize bid, ask; +// TimeStamp start = TimeStamp::now(); +// auto result = src->consumer().get_bbo(read, bid, ask); +// TimeStamp end = TimeStamp::now(); +// REQUIRE(result != HandleReadResult::SUCCESS); +// REQUIRE((end - start).seconds() < 5); +// } + +// void test_get_handles() { +// MDProducer producer(Platform::SIM); +// vector producers = create_test_securities(producer); +// // Add entries to the shared memory that we aren't looking for +// vector p2_no_id = create_test_securities(producer, "2"); +// vector p3 = create_test_securities(producer, "3"); + +// for (auto& p : producers) { p.update_bbo(TimeStamp::now(), PriceSize::from_double<3>(5.0, 5), PriceSize::from_double<3>(5.0, 5)); } +// assign_ids(producers.begin(), producers.end()); +// assign_ids(p3.begin(), p3.end()); + +// // Symbol list for the consumer - only search half of the symbols +// set symbols; +// UserSymbol future_symbol( "FOO", Exchange::TESTEXCHANGE, SecurityType::FUTURE ); +// symbols.insert(future_symbol); +// UserSymbol option_symbol( "FOOX", Exchange::TESTEXCHANGE, SecurityType::OPTION ); +// symbols.insert(option_symbol); +// MDConsumer consumer(symbols); + +// set keys; +// consumer.walk_security_list([&](const OldSecurity& security, MDConsumerHandle) { keys.insert(security.security_key()); }); +// REQUIRE(keys.size() == producers.size()); +// auto map = consumer.get_handles(keys); +// REQUIRE(map.size() == keys.size()); +// for (const auto& entry : map) { +// REQUIRE(entry.second.is_initialized()); // } // } + +// void test_denormalization() { +// // By using a different symbol for each security +// // we put them all on separate shared mem sections (co2 channels) +// security_key key = 999; +// OldSecurity f1 = mkfuture("F1", ++key, 2016, 5, 1); +// OldSecurity o1 = mkoption("O1", ++key, 2016, 5, 1, 100); +// OldSecurity o2 = mkoption("O2", ++key, 2016, 5, 2, 200); +// OldSecurity o3 = mkoption("O3", ++key, 2016, 5, 3, 300); +// OldSecurity s4 = mkspread("S3", ++key, o2, o3); +// OldSecurity s3 = mkspread("S4", ++key, o1, s4); +// OldSecurity s2 = mkspread("S2", ++key, o1, s4, s3); +// OldSecurity s1 = mkspread("S1", ++key, f1, s2); +// OldSecurity all_secs_array[] = { f1, o1, o2, o3, s1, s2, s3, s4 }; + +// MDProducer producer(Platform::SIM); +// vector producers; +// for (auto& sec : all_secs_array) { +// with_security(sec.security_type(), sec.spread().legs_size(), [&](FixedSecurity& fsec) { +// convert_security(sec, fsec, true); +// producers.push_back(producer.add_security(fsec)); +// }); +// } + +// set csymbols; +// csymbols.insert(UserSymbol("F1", Exchange::TESTEXCHANGE, SecurityType::FUTURE)); +// csymbols.insert(UserSymbol("O1", Exchange::TESTEXCHANGE, SecurityType::OPTION)); +// csymbols.insert(UserSymbol("O2", Exchange::TESTEXCHANGE, SecurityType::OPTION)); +// csymbols.insert(UserSymbol("O3", Exchange::TESTEXCHANGE, SecurityType::OPTION)); +// csymbols.insert(UserSymbol("S1", Exchange::TESTEXCHANGE, SecurityType::SPREAD)); +// csymbols.insert(UserSymbol("S2", Exchange::TESTEXCHANGE, SecurityType::SPREAD)); +// csymbols.insert(UserSymbol("S3", Exchange::TESTEXCHANGE, SecurityType::SPREAD)); +// csymbols.insert(UserSymbol("S4", Exchange::TESTEXCHANGE, SecurityType::SPREAD)); +// MDConsumer consumer(csymbols); + +// auto check_sec = [&](const OldSecurity& security, MDConsumerHandle handle) { +// REQUIRE(security.security_key() == handle.key()); +// auto iter = find_if(begin(all_secs_array), end(all_secs_array), [&](const OldSecurity& sec) { return sec.security_key() == security.security_key(); }); +// REQUIRE(iter != end(all_secs_array)); +// REQUIRE(SecurityHelper::symbol(*iter) == SecurityHelper::symbol(security)); +// if (security.security_type() == SecurityType::SPREAD) { +// REQUIRE(security.spread().normalized() == false); +// REQUIRE(security.spread().has_symbol()); +// REQUIRE(security.spread().legs_size() == iter->spread().legs_size()); +// for (int x = 0; x < security.spread().legs_size(); ++x) { +// REQUIRE(SecurityHelper::symbol(security.spread().legs(x).security()) == SecurityHelper::symbol(iter->spread().legs(x).security())); +// } +// } +// }; + +// consumer.walk_security_list(check_sec); +// consumer.on_security(check_sec); +// consumer.scan_securities(); +// } +// void test_denormalization2() { +// // Just like test_denormailzation but with a different memory layout +// security_key key = 999; +// OldSecurity f1 = mkfuture("FF", ++key, 2016, 5, 1); +// OldSecurity o1 = mkoption("OO", ++key, 2016, 5, 1, 100); +// OldSecurity o2 = mkoption("OO", ++key, 2016, 5, 2, 200); +// OldSecurity o3 = mkoption("OO", ++key, 2016, 5, 3, 300); +// OldSecurity s4 = mkspread("SS", ++key, o2, o3); +// OldSecurity s3 = mkspread("SS", ++key, o1, s4); +// OldSecurity s2 = mkspread("SS", ++key, o1, s4, s3); +// OldSecurity s1 = mkspread("SS", ++key, f1, s2); +// OldSecurity all_secs_array[] = { f1, o1, o2, o3, s1, s2, s3, s4 }; + +// MDProducer producer(Platform::SIM); +// vector producers; +// for (auto& sec : all_secs_array) { +// with_security(sec.security_type(), sec.spread().legs_size(), [&](FixedSecurity& fsec) { +// convert_security(sec, fsec, true); +// producers.push_back(producer.add_security(fsec)); +// }); +// } + +// set csymbols; +// csymbols.insert(UserSymbol("FF", Exchange::TESTEXCHANGE, SecurityType::FUTURE)); +// csymbols.insert(UserSymbol("OO", Exchange::TESTEXCHANGE, SecurityType::OPTION)); +// csymbols.insert(UserSymbol("SS", Exchange::TESTEXCHANGE, SecurityType::SPREAD)); +// MDConsumer consumer(csymbols); + +// auto check_sec = [&](const OldSecurity& security, MDConsumerHandle handle) { +// REQUIRE(security.security_key() == handle.key()); +// auto iter = find_if(begin(all_secs_array), end(all_secs_array), [&](const OldSecurity& sec) { return sec.security_key() == security.security_key(); }); +// REQUIRE(iter != end(all_secs_array)); +// REQUIRE(SecurityHelper::symbol(*iter) == SecurityHelper::symbol(security)); +// if (security.security_type() == SecurityType::SPREAD) { +// REQUIRE(security.spread().normalized() == false); +// REQUIRE(security.spread().has_symbol()); +// REQUIRE(security.spread().legs_size() == iter->spread().legs_size()); +// for (int x = 0; x < security.spread().legs_size(); ++x) { +// REQUIRE(SecurityHelper::symbol(security.spread().legs(x).security()) == SecurityHelper::symbol(iter->spread().legs(x).security())); +// } +// } +// }; + +// consumer.walk_security_list(check_sec); +// consumer.on_security(check_sec); +// consumer.scan_securities(); +// } + +// void test_retry_unresolved_spread() { +// // Make 2 spreads with option legs +// // Leave out one leg for hte other spread +// // The other spread only shows up once its option leg is produced + +// OldSecurity o1 = mkoption("OO", 400, 2016, 5, 1, 100); +// OldSecurity o2 = mkoption("OO", 401, 2016, 5, 2, 200); +// OldSecurity o3 = mkoption("OO", 402, 2016, 5, 3, 300); +// OldSecurity s1 = mkspread("SS", 500, o1, o2); +// OldSecurity s2 = mkspread("SS", 501, o2, o3); + +// MDProducer producer(Platform::SIM); +// map handles; +// auto add_security = [&](const OldSecurity& sec) { +// with_security(sec.security_type(), sec.spread().legs_size(), [&](FixedSecurity& fsec) { +// convert_security(sec, fsec, true); +// handles.insert(make_pair(fsec.key, producer.add_security(fsec))); +// }); +// }; +// // "options publisher has not started yet" +// //add_security(o1); +// //add_security(o2); +// //add_security(o3); +// add_security(s1); +// add_security(s2); + +// // Create consumer +// set consumer_symbols; +// consumer_symbols.insert(UserSymbol("OO", Exchange::TESTEXCHANGE, SecurityType::OPTION)); +// consumer_symbols.insert(UserSymbol("SS", Exchange::TESTEXCHANGE, SecurityType::SPREAD)); +// MDConsumer consumer(consumer_symbols); + +// size_t security_count = 0; +// // Indirection for check function +// function check_func; +// check_func = [&](const OldSecurity&) { +// REQUIRE(false); +// }; +// consumer.on_security([&](const OldSecurity& sec, MDConsumerHandle) { ++security_count; check_func(sec); }); +// consumer.scan_securities(); +// REQUIRE(security_count == 0); + +// // OK, now add enough options to resolve s1 but not s2 +// add_security(o1); +// add_security(o2); +// check_func = [&](const OldSecurity& security) { +// if (security.security_type() == SecurityType::SPREAD) { +// set legs; +// for (const auto& leg : security.spread().legs()) { +// legs.insert(leg.security().security_key()); +// } +// REQUIRE(security.security_key() == 500); +// REQUIRE(legs.size() == 2); +// REQUIRE(legs.count(400) == 1); +// REQUIRE(legs.count(401) == 1); +// } +// }; +// consumer.scan_securities(); // finds o1,o2,s1 +// consumer.scan_securities(); // finds nothing +// REQUIRE(security_count == 3); + +// // Then add the last security and we should see s2 +// add_security(o3); +// check_func = [&](const OldSecurity& security) { +// if (security.security_type() == SecurityType::SPREAD) { +// set legs; +// for (const auto& leg : security.spread().legs()) { +// legs.insert(leg.security().security_key()); +// } +// REQUIRE(security.security_key() == 501); +// REQUIRE(legs.size() == 2); +// REQUIRE(legs.count(401) == 1); +// REQUIRE(legs.count(402) == 1); +// } +// }; +// consumer.scan_securities(); +// REQUIRE(security_count == 5); +// } + +// #ifndef WIN32 +// void test_settlement_recorder() { +// using namespace datetime; +// security_key k1 = 401, k2 = 402; +// OldSecurity o1 = mkoption("OO", k1, 2016, 5, 1, 100); +// OldSecurity o2 = mkoption("OO", k2, 2016, 5, 2, 200); +// MDProducer producer(Platform::SIM); +// auto add_security = [&](const OldSecurity& sec) { +// unique_ptr phandle; +// with_security(sec.security_type(), sec.spread().legs_size(), [&](FixedSecurity& fsec) { +// convert_security(sec, fsec, true); +// phandle.reset(new MDProducerHandle(producer.add_security(fsec))); +// }); +// return phandle; +// }; +// auto ph1 = *add_security(o1); +// auto ph2 = *add_security(o2); +// set consumer_symbols; +// consumer_symbols.insert(UserSymbol("OO", Exchange::TESTEXCHANGE, SecurityType::OPTION)); +// MDConsumer consumer(consumer_symbols); +// auto h1 = consumer.get_handle(k1); +// REQUIRE(h1.is_initialized()); +// auto h2 = consumer.get_handle(k2); + +// // Pick 2 valid dates in the future +// auto today = datetime::today(); +// today.year += 1; +// auto yesterday = datetime::add_days(today, -1); + +// { +// co2::SettlementRecorder recorder(Platform::UNSET); +// recorder.add_settlement(ph1, 101, yesterday); +// recorder.add_settlement(ph1, 102, today); +// recorder.add_settlement(ph2, 199, yesterday); +// recorder.add_settlement(ph2, 188, today); +// recorder.do_work(); +// recorder.do_work(); +// } +// REQUIRE(h1.misc().settlement_price == 102); +// REQUIRE(h1.misc().settlement_date == today); +// REQUIRE(h1.misc().previous_settlement_price == 101); +// REQUIRE(h1.misc().previous_settlement_date == yesterday); +// REQUIRE(h2.misc().settlement_price == 188); +// REQUIRE(h2.misc().settlement_date == today); +// REQUIRE(h2.misc().previous_settlement_price == 199); +// REQUIRE(h2.misc().previous_settlement_date == yesterday); + +// // Update but skip persist +// auto tomorrow = datetime::add_days(today, 1); +// ph1.update_settlement(TimeStamp::now(), 103, tomorrow); +// REQUIRE(h1.misc().settlement_price == 103); +// REQUIRE(h1.misc().settlement_date == tomorrow); +// REQUIRE(h1.misc().previous_settlement_price == 102); +// REQUIRE(h1.misc().previous_settlement_date == today); +// ph1.update_previous_settlement(TimeStamp::now(), 0, Date()); +// REQUIRE(h1.misc().settlement_price == 103); +// REQUIRE(h1.misc().settlement_date == tomorrow); +// ph1.update_settlement(TimeStamp::now(), 0, Date()); +// REQUIRE(h1.misc().settlement_price == 0); +// REQUIRE(h1.misc().settlement_date.day == 0); + +// // pushing a new settlement for today doesn't invalidate yesterday +// ph2.update_settlement(TimeStamp::now(), 201, today); +// REQUIRE(h2.misc().settlement_price == 201); +// REQUIRE(h2.misc().settlement_date == today); +// REQUIRE(h2.misc().previous_settlement_price == 199); +// REQUIRE(h2.misc().previous_settlement_date == yesterday); + +// // Restore persisted state +// { +// co2::SettlementRecorder recorder(Platform::UNSET); +// recorder.restore_settlement(ph1); +// recorder.restore_settlement(ph2); +// recorder.do_work(); +// } +// REQUIRE(h1.misc().settlement_price == 102); +// REQUIRE(h1.misc().settlement_date == today); +// REQUIRE(h1.misc().previous_settlement_price == 101); +// REQUIRE(h1.misc().previous_settlement_date == yesterday); +// REQUIRE(h2.misc().settlement_price == 188); +// REQUIRE(h2.misc().settlement_date == today); +// REQUIRE(h2.misc().previous_settlement_price == 199); +// REQUIRE(h2.misc().previous_settlement_date == yesterday); +// } +// #endif + +// void teardown() {} +// } ; +// std::unique_ptr co2_tests; + +// TEST_CASE("CO2Tests","[md]") { + +// if (!co2_tests) +// co2_tests = std::make_unique(); + +// co2_tests->setup(); + +// #ifndef WIN32 +// SECTION("test_security_id_recorder") { +// co2_tests->test_security_id_recorder(); +// } +// #endif +// SECTION("test_price_size") { +// co2_tests->test_price_size(); +// } +// SECTION("test_slow_double_conversion") { +// co2_tests->test_slow_double_conversion(); +// } +// SECTION("test_compact_exponent") { +// co2_tests->test_compact_exponent(); +// } +// SECTION("test_producer_consumer") { +// co2_tests->test_producer_consumer(); +// } +// SECTION("test_consumer_callback_on_security_added") { +// co2_tests->test_consumer_callback_on_security_added(); +// } +// SECTION("test_consumer_defer_handle1") { +// co2_tests->test_consumer_defer_handle1(); +// } +// SECTION("test_consumer_defer_handle2") { +// co2_tests->test_consumer_defer_handle2(); +// } +// SECTION("test_spread_resolver") { +// co2_tests->test_spread_resolver(); +// } +// SECTION("test_no_double_security_callback") { +// co2_tests->test_no_double_security_callback(); +// } +// SECTION("test_recovery") { +// co2_tests->test_recovery(); +// } +// SECTION("test_no_block_forever_on_publisher_crash") { +// co2_tests->test_no_block_forever_on_publisher_crash(); +// } +// SECTION("test_get_handles") { +// co2_tests->test_get_handles(); +// } +// SECTION("test_denormalization") { +// co2_tests->test_denormalization(); +// } +// SECTION("test_denormalization2") { +// co2_tests->test_denormalization2(); +// } +// SECTION("test_retry_unresolved_spread") { +// co2_tests->test_retry_unresolved_spread(); +// } +// #ifndef WIN32 +// SECTION("test_settlement_recorder") { +// co2_tests->test_settlement_recorder(); +// } +// #endif +// } + +// struct Co2IndexTests { +// vector random_ids; +// set used_ids; +// const int iterations; +// tbb::task_scheduler_init _tbb; + +// Co2IndexTests() : +// random_ids(), +// used_ids(), +// iterations(50), +// _tbb(tbb::task_scheduler_init::automatic) +// {} + +// void setup() { +// used_ids.clear(); +// random_ids.clear(); + +// std::uniform_int_distribution rand_distribution(1, 0x7FFFFFFF); +// std::mt19937 random_number_engine; +// auto genrandom = std::bind(rand_distribution, random_number_engine); +// set temp_ids; +// for (int x = 0; x < 9999; ++x) { +// auto r = genrandom(); +// if (x < 1000) { used_ids.insert(r); } +// temp_ids.insert(r); +// } +// for (uint64_t id : temp_ids) { +// random_ids.push_back(id); +// } +// REQUIRE(random_ids.size() > 1000); +// } + +// struct Handle { +// Handle() : _key(0) {} +// uint64_t _key; +// security_key key() const { return _key; } +// void key(security_key key) { _key = key; } +// }; + +// struct SecurityData { +// uint64_t exchange_id; +// OldSecurity security; +// Handle handle; +// }; + +// template +// void gen_securities(Index& index) { +// unique_ptr temp; +// uint32_t count = 0; +// for (auto& id : used_ids) { +// ++count; +// temp.reset(new SecurityData()); +// temp->security.Clear(); +// temp->security.set_security_type(SecurityType::OPTION); +// temp->security.mutable_option()->set_symbol("FOOX"); +// temp->security.set_numeric_id(id); +// temp->exchange_id = id; +// temp->handle.key(count); +// index.add_security(id, move(temp)); +// } +// } + + +// template +// void test_random_search(Index& index) { +// vector temp(random_ids); +// std::random_shuffle(temp.begin(), temp.end()); +// for (auto& id : temp) { +// if (used_ids.find(id) != used_ids.end()) { +// REQUIRE(index.has_security(id)); +// } +// else { +// REQUIRE(index.get_security(id) == nullptr); +// } +// } +// } + +// template +// void test_walk(Index& index) { +// int count = 0; +// index.walk_security_list([&count](const SecurityData&) { ++count; }); +// REQUIRE(count == (int) used_ids.size()); +// } + +// typedef SimpleSecurityIndex MySimpleIndex; +// void test_simple_index_search() { +// MySimpleIndex index; +// gen_securities(index); +// for (int x = 0; x < iterations; ++x) { +// test_random_search(index); +// } +// } + +// void test_simple_index_parallel_search() { +// MySimpleIndex index; +// gen_securities(index); +// tbb::parallel_for(0, iterations, [&](int) { +// test_random_search(index); +// }); +// } + +// void test_simple_index_iterate() { +// MySimpleIndex index; +// gen_securities(index); +// test_walk(index); +// } + +// typedef ConcurrentSecurityIndex MyConcurrentIndex; +// void test_concurrent_index_search() { +// MyConcurrentIndex index; +// gen_securities(index); +// for (int x = 0; x < iterations; ++x) { +// test_random_search(index); +// } +// } +// void test_concurrent_index_parallel_search() { +// MyConcurrentIndex index; +// gen_securities(index); +// tbb::parallel_for(0, iterations, [&](int) { +// test_random_search(index); +// }); +// } +// void test_concurrent_index_iterate() { +// MyConcurrentIndex index; +// gen_securities(index); +// test_walk(index); +// } // } ; -//std::unique_ptr co2_relay_tests; - +// std::unique_ptr co2_index_tests; -// TEST_CASE ("Co2RelayTests","[md]") +// TEST_CASE ("Co2IndexTests","[md]") // { -// if (!co2_relay_tests) -// co2_relay_tests = std::make_unique(); -// co2_relay_tests->setup(); +// if (!co2_index_tests) +// co2_index_tests = std::make_unique (); + +// co2_index_tests->setup(); -// SECTION("relay_nans") { -// co2_relay_tests->relay_nans(); +// SECTION("test_simple_index_search") { +// co2_index_tests->test_simple_index_search(); +// } + +// SECTION("test_simple_index_parallel_search") { +// co2_index_tests->test_simple_index_parallel_search(); +// } + +// SECTION("test_simple_index_iterate") { +// co2_index_tests->test_simple_index_iterate(); +// } +// SECTION("test_concurrent_index_search") { +// co2_index_tests->test_concurrent_index_search(); +// } +// SECTION("test_concurrent_index_iterate") { +// co2_index_tests->test_concurrent_index_iterate(); +// } +// SECTION("test_concurrent_index_parallel_search") { +// co2_index_tests->test_concurrent_index_parallel_search(); // } // } +// struct Co2PerfTests { +// static const size_t instrument_count = 2048; +// vector> test_instruments; +// vector producers; +// vector consumers; -}}} +// Co2PerfTests() : +// test_instruments(), +// producers(), +// consumers() +// { +// #ifdef _WIN32 +// #pragma warning(push, 0) +// #endif +// // OK if this isn't aligned for testing +// for (size_t x = 0; x < instrument_count; ++x) { +// test_instruments.push_back( +// std::unique_ptr( +// new Co2TestInstrument("Foo", SecurityType::OPTION))); +// } +// #ifdef _WIN32 +// #pragma warning(pop) +// #endif +// for (size_t x = 0; x < test_instruments.size(); ++x) { +// auto producer = test_instruments[x]->producer(); +// producer.set_key(x + 1); +// producers.push_back(producer); +// auto consumer = test_instruments[x]->consumer(); +// REQUIRE(consumer.key() == x + 1); +// consumers.push_back(consumer); +// } -#endif +// std::srand(std::time(0)); +// //std::random_shuffle(producers.begin(), producers.end(), [](int i) { return std::rand() % i; }); +// std::random_shuffle(consumers.begin(), consumers.end(), [](int i) { return std::rand() % i; }); +// } +// void setup() { } +// void teardown() {}; +// void test_write_bbo_perf() { +// REQUIRE(producers.size() == instrument_count); + +// // Warmup +// for (size_t x = 0; x < producers.size(); ++x) { +// auto& p = producers[x]; +// // p.update_bbo(TimeStamp::now(), PriceSize::from_double<3>(100.0 + x, x), PriceSize::from_double<3>(200 + x, 2 * x)); +// p.update_bbo(TimeStamp::now(), PriceSize(100+x,0,x), PriceSize(200+x,0,2*x)); +// } + +// // Test +// TimeStamp start = TimeStamp::now(); +// size_t count = 0; +// const size_t iterations = 1000; +// for (size_t loop = 0; loop < iterations; ++loop) { +// for (size_t x = 0; x < producers.size(); ++x) { +// ++count; +// auto& p = producers[x]; +// TimeStamp t = TimeStamp::now(); +// // p.update_bbo(t, PriceSize::from_double<3>(100.0 + count, count), PriceSize::from_double<3>(200 + count, 2 * count)); +// p.update_bbo(t, PriceSize(100+x,0,x), PriceSize(200+x,0,2*x)); +// } +// } +// TimeStamp end = TimeStamp::now(); +// std::stringstream ss; +// // Of which approximately 1/3 is generating the timestamp on lab-lx4 +// ss << "\t" << (end - start).nanoseconds() / iterations / instrument_count << " ns per update *\n"; +// std::cout << ss.str(); +// } + +// void test_read_bbo_perf() { +// // Warmup +// const auto& write_all = [](vector& producers) { +// // Offset each price by a random amount each iteration to ensure that writes actually happen +// size_t rand_factor = std::rand() % 1024; +// for (size_t x = 0; x < producers.size(); ++x) { +// auto& p = producers[x]; +// // p.update_bbo(TimeStamp::now(), PriceSize::from_double<3>(100.0 + x + rand_factor, x+rand_factor), PriceSize::from_double<3>(200 + x+rand_factor, 2 * x + rand_factor)); +// p.update_bbo(TimeStamp::now(), PriceSize(100+x+rand_factor,0,x+rand_factor), PriceSize(200+x+rand_factor,0,2*x+rand_factor)); +// } +// }; +// write_all(producers); + +// for (auto& c : consumers) { +// PriceSize bid, ask; +// TimeStamp ts; +// REQUIRE(c.get_bbo(ts, bid, ask) == HandleReadResult::SUCCESS); +// } + +// // Start a thread which loops and writes to the producers until terminated +// utils::Dispatcher& writer = utils::Dispatcher::create_new("bbo_writer"); +// DEFER(writer.terminate()); +// tbb::queuing_mutex producer_mutex; +// auto* pproducers = &producers; +// function write_all_repeat = [&]() { +// tbb::queuing_mutex::scoped_lock lock(producer_mutex); +// if (pproducers != nullptr) { +// write_all(*pproducers); +// writer.post(write_all_repeat); +// } +// }; +// write_all_repeat(); + +// // Perform read test +// TimeStamp start = TimeStamp::now(); +// size_t read_count = 0; +// const size_t iterations = 1000; +// for (size_t loop = 0; loop < iterations; ++loop) { +// TimeStamp ts = TimeStamp::now(); +// for (size_t x = 0; x < consumers.size(); ++x) { +// PriceSize bid, ask; +// TimeStamp ts_temp = ts; +// if (consumers[x].get_bbo(ts_temp, bid, ask) == HandleReadResult::SUCCESS) { +// ++read_count; +// } +// } +// } +// REQUIRE(read_count > 0); + +// TimeStamp end = TimeStamp::now(); +// std::stringstream ss; +// ss << "\t" << (end - start).nanoseconds() / iterations / instrument_count << " ns per update. Actual reads = " << read_count << " / " << instrument_count*iterations << " * \n"; +// std::cout << ss.str(); + +// writer.terminate(); + +// tbb::queuing_mutex::scoped_lock lock(producer_mutex); +// pproducers = nullptr; +// std::this_thread::sleep_for(2s); + +// } + +// } ; + +// std::unique_ptr co2_perf_tests; + +// const size_t Co2PerfTests::instrument_count ; +// TEST_CASE ("Co2PerfTests","[md]") +// { + +// if (!co2_perf_tests) +// co2_perf_tests = std::make_unique(); + +// co2_perf_tests->setup(); + +// SECTION("test_write_bbo_perf") { +// // co2_perf_tests->test_write_bbo_perf(); +// } +// SECTION("test_read_bbo_perf") { +// // co2_perf_tests->test_read_bbo_perf(); +// } +// } + +// // struct Co2RelayTests { +// // void setup() { +// // config::internal::initialize_for_test( +// // "PlatformMap {" +// // " value {" +// // " sim {" +// // " exchange: TESTEXCHANGE" +// // " }" +// // " }" +// // "}"); +// // } +// // void teardown() {} + +// // MDConsumer create_consumer() { +// // set symbols; +// // UserSymbol future_symbol("FOO", Exchange::TESTEXCHANGE, SecurityType::FUTURE); +// // symbols.insert(future_symbol); +// // UserSymbol option_symbol("FOOX", Exchange::TESTEXCHANGE, SecurityType::OPTION); +// // symbols.insert(option_symbol); +// // MDConsumer consumer(symbols); +// // return consumer; +// // } + + +// // template +// // T simulate_wire_transfer(const T& request) { +// // T temp; +// // std::string buffer; +// // request.SerializeToString(&buffer); +// // temp.ParseFromString(buffer); +// // return temp; +// // } + +// // PriceSize convert(const co2::PriceSizeMessage& psm) { +// // return PriceSize(psm.mantissa(), psm.exponent(), psm.size()); +// // } + +// // void relay_nans() { +// // MDProducer producer(Platform::SIM); +// // UserSymbol symbol("FOO", Exchange::TESTEXCHANGE, SecurityType::OPTION); + +// // // initialize data +// // vector producers; +// // for (size_t x = 0; x < 100; ++x) { +// // auto strike = 1000 + (100 * x); +// // producers.push_back( +// // producer.add_security( +// // mkfixedoption("FOO", invalid_security_key, 2017, 1, 28, strike, PutOrCall::PUT))); +// // producers.push_back( +// // producer.add_security( +// // mkfixedoption("FOO", invalid_security_key, 2017, 1, 28, strike, PutOrCall::CALL))); +// // } +// // assign_ids(producers.begin(), producers.end(), 1000); +// // PriceSize price35 = PriceSize::from_double<2>(3.5, 5); +// // PriceSize price5 = PriceSize::from_double<2>(5, 5); +// // for(auto p : producers) { +// // p.update_bbo(TimeStamp::now(), price35, price35); +// // } + +// // // Read using QueryServer +// // set symbols; +// // symbols.insert(symbol); +// // auto server = create_query_server(symbols); +// // REQUIRE(server); +// // server->scan_securities(); + +// // co2::MDRequest request; +// // auto* chan = request.add_channels(); +// // chan->set_type(symbol.type()); +// // chan->set_symbol(symbol.symbol()); +// // chan->set_exchange(symbol.exchange()); +// // chan->set_flags(co2::MDRequest::BBO | co2::MDRequest::SECURITY); + +// // // This request should include everything +// // co2::MDReply reply; +// // server->query(request, &reply); +// // reply = simulate_wire_transfer(reply); +// // REQUIRE((size_t)reply.channels().size() == symbols.size()); +// // REQUIRE((size_t)reply.channels(0).updates().size() == producers.size()); +// // // update the request to match the server time +// // for (auto& update : reply.channels(0).updates()) { +// // REQUIRE(update.has_security()); +// // REQUIRE(update.has_book()); +// // REQUIRE(update.book().asks_size() > 0); +// // REQUIRE(update.book().bids_size() > 0); +// // REQUIRE(convert(update.book().bids(0)) == price35); +// // REQUIRE(convert(update.book().asks(0)) == price35); +// // } + +// // // Second request with updated time should include nothing +// // request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); +// // reply.Clear(); +// // server->query(request, &reply); +// // reply = simulate_wire_transfer(reply); +// // REQUIRE(reply.channels().size() == 1); +// // REQUIRE(reply.channels(0).updates().size() == 0); + +// // // Update a single price and make sure we get that back +// // producers[0].update_bbo(TimeStamp::now(), price5, price5, 1); +// // request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); +// // reply.Clear(); +// // server->query(request, &reply); +// // reply = simulate_wire_transfer(reply); +// // REQUIRE(reply.channels().size() == 1); +// // REQUIRE(reply.channels(0).updates().size() == 1); +// // REQUIRE(reply.channels(0).updates(0).has_book()); +// // REQUIRE(convert(reply.channels(0).updates(0).book().bids(0)) == price5); +// // REQUIRE(convert(reply.channels(0).updates(0).book().asks(0)) == price5); + +// // // NaN out all the prices - expect to get a full set of updates +// // for(auto p : producers) { +// // p.update_bbo(TimeStamp::now(), PriceSize::NO_MARKET, PriceSize::NO_MARKET, 2); +// // } +// // request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); +// // reply.Clear(); +// // server->query(request, &reply); +// // reply = simulate_wire_transfer(reply); +// // REQUIRE(reply.channels().size() == 1); +// // REQUIRE((size_t)reply.channels(0).updates().size() == producers.size()); +// // for (auto& update : reply.channels(0).updates()) { +// // REQUIRE(update.has_book()); +// // REQUIRE(update.book().bids_size() > 0); +// // REQUIRE(update.book().asks_size() > 0); +// // REQUIRE(convert(update.book().bids(0)) == PriceSize::NO_MARKET); +// // REQUIRE(convert(update.book().asks(0)) == PriceSize::NO_MARKET); +// // } + +// // // Fill up the markets again +// // for(auto p : producers) { +// // p.update_bbo(TimeStamp::now(), price35, price35, 3); +// // } +// // request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); +// // reply.Clear(); +// // server->query(request, &reply); +// // reply = simulate_wire_transfer(reply); +// // REQUIRE(reply.channels().size() == 1); +// // REQUIRE((size_t)reply.channels(0).updates().size() == producers.size()); +// // for (auto& update : reply.channels(0).updates()) { +// // REQUIRE(update.has_book()); +// // REQUIRE(update.book().bids_size() > 0); +// // REQUIRE(update.book().asks_size() > 0); +// // REQUIRE(convert(update.book().bids(0)) == price35); +// // REQUIRE(convert(update.book().asks(0)) == price35); +// // } + +// // // NaN out the bids +// // for(auto p : producers) { +// // p.update_bbo(TimeStamp::now(), PriceSize::NO_MARKET, price35, 4); +// // } +// // request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); +// // reply.Clear(); +// // server->query(request, &reply); +// // reply = simulate_wire_transfer(reply); +// // REQUIRE(reply.channels().size() == 1); +// // REQUIRE((size_t)reply.channels(0).updates().size() == producers.size()); +// // for (auto& update : reply.channels(0).updates()) { +// // REQUIRE(update.has_book()); +// // REQUIRE(update.book().bids_size() > 0); +// // REQUIRE(update.book().asks_size() > 0); +// // REQUIRE(convert(update.book().bids(0)) == PriceSize::NO_MARKET); +// // REQUIRE(convert(update.book().asks(0)) == price35); +// // } +// // } +// // } ; + +// //std::unique_ptr co2_relay_tests; + + +// // TEST_CASE ("Co2RelayTests","[md]") +// // { +// // if (!co2_relay_tests) +// // co2_relay_tests = std::make_unique(); +// // co2_relay_tests->setup(); + +// // SECTION("relay_nans") { +// // co2_relay_tests->relay_nans(); +// // } +// // } + + +// }}} + +// #endif diff --git a/src/lib/order_man/protos/OrderMan.proto b/src/lib/order_man/protos/OrderMan.proto deleted file mode 100644 index d1e6e34ed..000000000 --- a/src/lib/order_man/protos/OrderMan.proto +++ /dev/null @@ -1,7 +0,0 @@ -syntax='proto3' ; -package bts.orderman ; - - -message B{} - -message C{} diff --git a/src/server/sparkd/CMakeDeps.cmake b/src/server/sparkd/CMakeDeps.cmake index 719ee2809..428b107bd 100644 --- a/src/server/sparkd/CMakeDeps.cmake +++ b/src/server/sparkd/CMakeDeps.cmake @@ -1,2 +1,2 @@ # generated by make process -set(sources ;fabric/Actor.cpp;fabric/Domain.cpp;fabric/Executor.cpp;fabric/IActor.cpp;fabric/IDomain.cpp;fabric/TopicDirectory.cpp;fabric/tests/baseline_perf_fabric_test.cpp;fabric/tests/fabric_test.cpp;grid/Node.cpp;jetfix/codec/FIXArray.cpp;jetfix/common/OutFIX.cpp;jetfix/connection/AsyncReader.cpp;jetfix/connection/IConnection.cpp;jetfix/connection/SocketConnection.cpp;jetfix/gateway/CoreGateway.cpp;jetfix/gateway/JetFIXGateway.cpp;jetfix/gateway/Session.cpp;jetfix/store/FIXFileStore.cpp;jetfix/store/session_name.cpp;jetfix/tests/async_reader_tests.cpp;jetfix/tests/decoding_tests.cpp;jetfix/tests/fix_encoding_stream_tests.cpp;jetfix/tests/fix_fragment_tests.cpp;jetfix/tests/jetfix_performance_tests.cpp;jetfix/tests/store_tests.cpp;main.cpp;md/platforms/mdp3_onixs/MDP3Listener.cpp;md/platforms/mdp3_onixs/MDP3Main.cpp;md/platforms/mdp3_onixs/MDP3Utils.cpp;md/platforms/mdp3_onixs/tests/mdp3_tests.cpp;md/run_md.cpp;order_man/IFIXGateway.cpp;order_man/IOrderObserver.cpp;order_man/OldOrderMan.cpp;order_man/OrderMan.cpp;order_man/OrderState.cpp;order_man/OrderStore.cpp;order_man/WorkingOrder.cpp;order_man/fix_order_codec.cpp;order_man/order_man_common.cpp;order_man/order_man_utils.cpp;persistence/IStore.cpp;persistence/PersistenceServiceServer.cpp;persistence/SqliteStore.cpp;persistence/tests/PersistenceServiceTests.cpp;risk_limits/IRiskLimitObserver.cpp;risk_limits/RiskLimitManager.cpp;risk_limits/RiskLimiter.cpp;setup/BootstrapActor.cpp;setup/ServiceHostActor.cpp;spreader/SimpleSpreader.cpp;spreader/SpreaderBase.cpp;testing/catch_test_runner.cpp;user/UserServiceServer.cpp) +set(sources ;fabric/Actor.cpp;fabric/Domain.cpp;fabric/Executor.cpp;fabric/IActor.cpp;fabric/IDomain.cpp;fabric/TopicDirectory.cpp;fabric/tests/baseline_perf_fabric_test.cpp;fabric/tests/fabric_test.cpp;grid/Node.cpp;jetfix/codec/FIXArray.cpp;jetfix/common/OutFIX.cpp;jetfix/connection/AsyncReader.cpp;jetfix/connection/IConnection.cpp;jetfix/connection/SocketConnection.cpp;jetfix/gateway/CoreGateway.cpp;jetfix/gateway/JetFIXGateway.cpp;jetfix/gateway/Session.cpp;jetfix/store/FIXFileStore.cpp;jetfix/store/session_name.cpp;jetfix/tests/async_reader_tests.cpp;jetfix/tests/decoding_tests.cpp;jetfix/tests/fix_encoding_stream_tests.cpp;jetfix/tests/fix_fragment_tests.cpp;jetfix/tests/jetfix_performance_tests.cpp;jetfix/tests/store_tests.cpp;main.cpp;md/platforms/mdp3_onixs/MDP3Listener.cpp;md/platforms/mdp3_onixs/MDP3Main.cpp;md/platforms/mdp3_onixs/MDP3Utils.cpp;md/platforms/mdp3_onixs/tests/mdp3_tests.cpp;md/run_md.cpp;order_man/IFIXGateway.cpp;order_man/IOrderObserver.cpp;order_man/OldOrderMan.cpp;order_man/OrderMan.cpp;order_man/OrderState.cpp;order_man/OrderStore.cpp;order_man/WorkingOrder.cpp;order_man/components/MDSource.cpp;order_man/components/SecuritySource.cpp;order_man/fix_order_codec.cpp;order_man/order_man_common.cpp;order_man/order_man_utils.cpp;persistence/IStore.cpp;persistence/PersistenceServiceServer.cpp;persistence/SqliteStore.cpp;persistence/tests/PersistenceServiceTests.cpp;risk_limits/IRiskLimitObserver.cpp;risk_limits/RiskLimitManager.cpp;risk_limits/RiskLimiter.cpp;setup/BootstrapActor.cpp;setup/ServiceHostActor.cpp;spreader/SimpleSpreader.cpp;spreader/SpreaderBase.cpp;testing/catch_test_runner.cpp;user/UserServiceServer.cpp) diff --git a/src/server/sparkd/main.cpp b/src/server/sparkd/main.cpp index 8ea581366..3a5903663 100644 --- a/src/server/sparkd/main.cpp +++ b/src/server/sparkd/main.cpp @@ -10,11 +10,28 @@ #include "testing/catch_test_runner.h" +#include +#include "lib/md/MDConsumerHandle.h" + +namespace bts { +namespace api {class Security;} + +struct NewSecurities +{ + boost::container::small_vector,1> value; +}; +} + int main( int argc, char*argv [] ) { using namespace bts; os::set_thread_name("main"); + std::cout < 1 && std::string(argv[1]) == "test") bts::run_tests(--argc, ++argv); else { diff --git a/src/server/sparkd/order_man/OrderMan.cpp b/src/server/sparkd/order_man/OrderMan.cpp index 94235dddb..a6738fded 100644 --- a/src/server/sparkd/order_man/OrderMan.cpp +++ b/src/server/sparkd/order_man/OrderMan.cpp @@ -1,189 +1,20 @@ -// #include "OrderMan.h" +#include "OrderMan.h" +#include "server/sparkd/grid/Node.h" +#include "events/NewSecurities.h" -// namespace bts { -// namespace orderman { +namespace bts { +namespace orderman { -// OrderMan::OrderMan(IOrderManObserver &o, -// IFIXGateway &gw, -// OrderKeyGenerator const &key_gen) : -// state_(State::IN_RECOVERY), -// observer_(o), -// fix_gw_(gw), -// orders_(), -// key_gen_(key_gen), -// keys_left_(0), -// next_key_(), -// outgoing_fix_msgs_(), -// time_observers_() -// { -// gen_key(); -// } - -// OrderMan::~OrderMan() {} - -// void OrderMan::on_end_recovery() -// { -// bts_assert(state_ == State::IN_RECOVERY); -// transition(State::DISCONNECTED); -// } - -// void OrderMan::send_pending_messages() -// { -// if (outgoing_fix_msgs_->first_stream().n_msgs()) { -// if (state_ == State::READY) { -// outgoing_fix_msgs_->first_stream().close(); -// fix_gw_.send(outgoing_fix_msgs_); -// } -// outgoing_fix_msgs_ = fix_gw_.make_outfix_buffer(); -// } -// } - -// void OrderMan::on_venue_ready() { -// bts_assert(state_ == State::DISCONNECTED); -// transition(State::READY); -// } - -// void OrderMan::on_venue_disconnect() { -// bts_assert(state_ == State::READY); -// transition(State::DISCONNECTED); -// } - -// void OrderMan::on_begin_replay() -// { -// bts_assert(state_ == State::DISCONNECTED); -// transition(State::REPLAYING); -// } - -// void OrderMan::on_end_replay() -// { -// bts_assert(state_ == State::REPLAYING); -// transition(State::DISCONNECTED); -// } - -// void OrderMan::on_exe_report(api::Security const &sec, -// OrderKey order_key, -// jetfix::fix::ExecutionReportMsg const&exe_report) -// { -// using namespace jetfix; - -// LOG(OrderMan,Info, -// "Received execution report {} for order {} on {}", -// exe_report.get(), -// order_key.value(), -// fix_gw_.name()); - -// auto it =orders_.find(order_key); -// bool is_new_order = it == orders_.end(); - -// // is this an order we know about? -// if (is_new_order) { - -// // we have never seen this order so we must have crashed -// // before we could persist it. see if we can recover the -// // order from the execution report - -// if (exe_report.get() == api::orderman::OT_LIMIT && -// (exe_report.get() == api::orderman::ET_NEW || -// exe_report.get() == api::orderman::ET_REJECTED)) { - -// // recoverable -// LOG(OrderMan,Error, -// "Received execution report for unknown limit order {} on {}. " -// "This should be investigated." -// " Will try to recreate the order from the execution report", -// order_key.value(),fix_gw_.name()); - -// it = orders_.emplace(order_key, -// std::unique_ptr -// (new LimitOrder(order_key,*this,exe_report))).first; -// } -// else { -// // unrecoverable -// LOG(OrderMan,Error, -// "Received execution report for unknown order {} on {}. " -// "Will not be able to recover " -// "order state. This should never happen. Please investigate", -// order_key.value(),fix_gw_.name()); -// return; -// } -// } - -// it->second->on_exe_report(exe_report); -// observer_.on_order_change(*(it->second)); -// if (exe_report.get () == api::orderman::ET_FILL || -// exe_report.get () == api::orderman::ET_PARTIAL_FILL) { -// OrderFill fill; -// extract_fill(sec,exe_report,&fill); -// observer_.on_fill(*(it->second),fill); -// } -// } - -// void OrderMan::on_cancel_reject(api::Security const&, -// OrderKey order_key, -// jetfix::fix::CancelRejectMsg const&msg) -// { -// using namespace jetfix; - -// LOG(OrderMan,Error, "Received cancel reject {} for order {} on {}", -// msg.get(), -// order_key.value(),fix_gw_.name()); - -// auto it = orders_.find(order_key); -// if (it == orders_.end()) { -// LOG(OrderMan,Error, -// "Received cancel rejection for unknown order {} on {}. " -// " This should never happen. Please investigate", -// order_key.value(),fix_gw_.name()); -// return; -// } -// else { -// it->second->on_cancel_reject(msg); -// observer_.on_order_change(*(it->second)); -// } -// } +struct OrderMan::PImpl +{ + PImpl(OnOrderState const&s,OnFill const&f) : on_state(s), on_fills(f) {} + OnOrderState on_state; + OnFill on_fills; -// void OrderMan::on_time( const OrderManTime&t) -// { -// for (auto &o : time_observers_) -// o(t); -// } +}; -// OrderKey OrderMan::gen_key() -// { -// const auto KEY_BATCH_SIZE = 1000; +OrderMan::OrderMan(OnOrderState const&s,OnFill const&f) : me (new PImpl(s,f)) {} +OrderMan::~OrderMan () {} - -// if (keys_left_ == 0) { -// keys_left_ = KEY_BATCH_SIZE - 1; -// next_key_ = key_gen_(KEY_BATCH_SIZE); -// } -// auto result = next_key_; -// keys_left_--; -// next_key_ = OrderKey(next_key_.value()+1); -// return result; -// } - -// const char* OrderMan::state_name(State state) -// { -// switch (state) { -// case State::IN_RECOVERY: -// return "IN_RECOVERY"; -// case State::DISCONNECTED: -// return "DISCONNECTED"; -// case State::REPLAYING: -// return "REPLAYING"; -// case State::READY: -// return "READY"; -// } -// } - -// void OrderMan::transition(State new_state) { -// LOG(OrderMan,Info, "Order man for {} transitioning from {} to {}", -// fix_gw_.name(),state_name(state_),state_name(new_state)); - -// state_ = new_state; - -// } - -// }} +}} diff --git a/src/server/sparkd/order_man/OrderMan.h b/src/server/sparkd/order_man/OrderMan.h index 1c50003ab..4f770f8a1 100644 --- a/src/server/sparkd/order_man/OrderMan.h +++ b/src/server/sparkd/order_man/OrderMan.h @@ -1,186 +1,37 @@ #pragma once #include - -#include "lib/api/services/OrderService.pb.h" -#include "lib/logging/logging.h" -#include "lib/utils/bts_assert.h" - -#include "server/sparkd/jetfix/codec/MessageDefinitions.h" -#include "server/sparkd/jetfix/common/OutFIX.h" - #include "order_man_common.h" -#include "IFIXGateway.h" -#include "fix_order_codec.h" - -#include namespace bts { +namespace api { namespace orderman { +class OrderState; +class Fill; +class CreateOrderRequest; +class UpdateOrderRequest; +class CancelOrderRequest; +}} -class OrderManTime; -class IMDSource; -class OrderFill {}; -using TimeObserver = std::function; - -class Order { -public: - virtual void on_exe_report(jetfix::fix::ExecutionReportMsg const&) {} - virtual void on_cancel_reject(jetfix::fix::CancelRejectMsg const&) {} -}; - -class IOrderManObserver { -public: - virtual void on_order_change(Order const &order) = 0; - virtual void on_fill(Order const&order, OrderFill const &rf) = 0; -}; - - -class IFIXGateway; -class CreateOrder; -class UpdateOrder; -class CancelOrder; - - -class OrderMan; - -class LimitOrder : public Order{ -public: - LimitOrder(OrderKey, OrderMan &,jetfix::fix::ExecutionReportMsg const&); -}; - +namespace orderman { class OrderMan { public: - struct Result {api::orderman::OrderError error;OrderKey key;}; - - OrderMan(IOrderManObserver &o, - IFIXGateway &gw, OrderKeyGenerator const &key_gen) ; + using OnOrderState = std::function; + using OnFill = std::function; + + OrderMan(OnOrderState const&,OnFill const&); ~OrderMan(); - template - Result create(typename OrderType::Create const &create_arg) - { - if (state_!= State::READY) { - LOG(OrderMan,Info, "Cannot create order because venue {} is not ready", fix_gw_.name()); - return Result {api::orderman::OE_VENUE_NOT_READY,OrderKey(0)}; - } - - auto k = gen_key(); - auto order_ptr = new OrderType (k,*this,create_arg); - auto &order_slot = orders_[k]; - bts_assert(!order_slot); - order_slot = std::unique_ptr(order_ptr); - return {api::orderman::OE_OK,k}; - } - - template - Result update(OrderKey k, typename OrderType::Update const& update_arg) - { - if (state_!= State::READY) { - LOG(OrderMan,Info, "Cannot update order {} because venue {} is not ready", k.value(),fix_gw_.name()); - return Result {api::orderman::OE_VENUE_NOT_READY,k}; - } - - auto it = orders_.find(k); - if (it == orders_.end()) { - LOG(OrderMan,Error, - "Cannot update order: {} on venue {} because no such order exists", - k.value(),fix_gw_.name()); - return Result {api::orderman::OE_UNKNOWN_ORDER,k}; - } - - auto abstract_order_ptr = it->second.get(); - auto concrete_order_ptr = dynamic_cast (abstract_order_ptr); - if (!concrete_order_ptr) { - LOG(OrderMan,Error, - "Cannot update order: {} on venue {} because its type does not match the requested type. " - "This is a serious problem. Contact development immediately", - k.value(),fix_gw_.name()); - return Result {api::orderman::OE_ORDER_TYPE_MISMATCH,k}; - } - - concrete_order_ptr->update(update_arg); - return Result {api::orderman::OE_OK,k}; - } - - template - Result cancel(OrderKey k, typename OrderType::Cancel const& cancel_arg) - { - if (state_!= State::READY) { - LOG(OrderMan,Info, "Cannot cancel order {} because venue {} is not ready", k.value(),fix_gw_.name()); - return Result {api::orderman::OE_VENUE_NOT_READY,k}; - } - - auto it = orders_.find(k); - if (it == orders_.end()) { - LOG(OrderMan,Error, - "Cannot cancel order: {} on venue {} because no such order exists", - k.value(),fix_gw_.name()); - return Result {api::orderman::OE_UNKNOWN_ORDER,k}; - } - - auto abstract_order_ptr = it->second.get(); - auto concrete_order_ptr = dynamic_cast (abstract_order_ptr); - if (!concrete_order_ptr) { - LOG(OrderMan,Error, - "Cannot cancel order: {} on venue {} because its type does not match the requested type. " - "This is a serious problem. Contact development immediately", - k.value(),fix_gw_.name()); - return Result {api::orderman::OE_ORDER_TYPE_MISMATCH,k}; - } - - concrete_order_ptr->cancel(cancel_arg); - return Result {api::orderman::OE_OK,k}; - } - - template - Result recover(OrderKey k,typename OrderType::State const&recovered_state) - { - bts_assert(state_ == State::IN_RECOVERY); - auto order_ptr = new OrderType (k,*this,recovered_state); - auto &order_slot = orders_[k]; - bts_assert(!order_slot); - order_slot = std::unique_ptr(order_ptr); - return {api::orderman::OE_OK,k}; - } - - void on_end_recovery(); - void send_pending_messages(); - void on_venue_ready(); - void on_venue_disconnect(); - void on_begin_replay(); - void on_end_replay(); - void on_exe_report(api::Security const &sec, OrderKey order_key, - jetfix::fix::ExecutionReportMsg const&exe_report); - void on_cancel_reject(api::Security const&, OrderKey order_key, - jetfix::fix::CancelRejectMsg const&msg); - void on_time( const OrderManTime&t); + void start(); + void create_order(OrderKey key,api::orderman::CreateOrderRequest const&r); + void update_order(api::orderman::UpdateOrderRequest const&r); + void cancel_order(api::orderman::CancelOrderRequest const&r); private: - - enum class State {IN_RECOVERY,DISCONNECTED, REPLAYING, READY }; - using OrderMap = boost::container::flat_map> ; - - OrderKey gen_key(); - const char* state_name(State state); - void transition(State new_state) ; - - State state_; - // IMDSource &md_src_; - IOrderManObserver &observer_; - IFIXGateway &fix_gw_; - - OrderMap orders_; - OrderKeyGenerator key_gen_; - size_t keys_left_; - OrderKey next_key_; - - jetfix::OutFIXPtr outgoing_fix_msgs_; - std::vector time_observers_; + struct PImpl; + std::unique_ptr me; }; }} - - diff --git a/src/server/sparkd/order_man/OrderManSetup.h b/src/server/sparkd/order_man/OrderManSetup.h new file mode 100644 index 000000000..537d35c4d --- /dev/null +++ b/src/server/sparkd/order_man/OrderManSetup.h @@ -0,0 +1,18 @@ +#pragma once + +#include + +namespace bts { +namespace orderman { + +class OrderManSetup +{ +public: + OrderManSetup(); + ~OrderManSetup(); +private: + struct PImpl; + std::unique_ptr me; +}; + +}} diff --git a/src/server/sparkd/order_man/components/MDSource.cpp b/src/server/sparkd/order_man/components/MDSource.cpp new file mode 100644 index 000000000..1751156b6 --- /dev/null +++ b/src/server/sparkd/order_man/components/MDSource.cpp @@ -0,0 +1 @@ +#include "MDSource.h" diff --git a/src/server/sparkd/order_man/components/MDSource.h b/src/server/sparkd/order_man/components/MDSource.h new file mode 100644 index 000000000..c3fa3425c --- /dev/null +++ b/src/server/sparkd/order_man/components/MDSource.h @@ -0,0 +1,47 @@ +#pragma once + +#include "server/sparkd/order_man/events/NewSecurities.h" +#include "server/sparkd/order_man/events/BookUpdate.h" +#include "lib/utils/TimeStamp.h" +#include "lib/md/MDConsumerHandle.h" + +namespace bts { +namespace orderman { + +class MDSource +{ +public: + using OnBook = std::function; + + MDSource(OnBook const&cb) : _on_book(cb), _handles() {} + + void on(event::NewSecurities const&secs) + { + for (auto const &s : secs.value) + _handles.push_back({utils::TimeStamp(),s.handle}); + } + + + void poll_md() { + event::BookUpdate book; + for (auto &h : _handles) + if (h.handle->get_depth(h.last, + std::tuple_size(), + &book.bid()[0], + &book.ask()[0]) == md2::HandleReadResult::SUCCESS) { + book.security_key = SecurityKey(h.handle->key()); + _on_book(book); + } + } + +private: + OnBook _on_book; + struct HandleRecord { + utils::TimeStamp last; + md2::MDConsumerHandle* handle; + }; + + std::vector _handles; +}; + +}} diff --git a/src/server/sparkd/order_man/components/SecuritySource.cpp b/src/server/sparkd/order_man/components/SecuritySource.cpp new file mode 100644 index 000000000..fcbdcfa3b --- /dev/null +++ b/src/server/sparkd/order_man/components/SecuritySource.cpp @@ -0,0 +1,52 @@ +#include "SecuritySource.h" + +#include "lib/domain_model/common.h" +#include "lib/md/MDRecoverableConsumer.h" +#include "lib/utils/bts_assert.h" +#include "server/sparkd/order_man/events/NewSecurities.h" +#include "server/sparkd/order_man/order_man_utils.h" + +#include + +namespace bts { +namespace orderman { + +struct SecuritySource::PImpl +{ + PImpl(OnSecurities const&cb) + : on_securities(cb),md_consumer(),known_securities(),pending_event(), + keep_alive() + {} + + OnSecurities on_securities; + md::MDRecoverableConsumer md_consumer; + std::map> known_securities; + event::NewSecurities pending_event; + std::vector> keep_alive; +}; + +SecuritySource::SecuritySource (OnSecurities const&cb) : me (new PImpl(cb)) +{ + me->md_consumer.on_security ([this] (api::Security const &sec, md2::MDConsumerHandle ) { + auto &ptr = me->known_securities[SecurityKey(sec.id())]; + bts_assert(!ptr); + ptr = std::make_unique(sec); + auto handle = me->md_consumer.get_managed_handle(sec.id()); + me->keep_alive.push_back(handle); + me->pending_event.value.push_back({ptr.get(),handle.get()}); + }); +} + + +void SecuritySource::discover() +{ + me->md_consumer.scan_securities (); + if (me->pending_event.value.size()) { + me->on_securities(me->pending_event); + me->pending_event.value.clear(); + } +} + +SecuritySource::~SecuritySource () {} + +}} diff --git a/src/server/sparkd/order_man/components/SecuritySource.h b/src/server/sparkd/order_man/components/SecuritySource.h new file mode 100644 index 000000000..43bfcacff --- /dev/null +++ b/src/server/sparkd/order_man/components/SecuritySource.h @@ -0,0 +1,23 @@ +#pragma once + +#include + +namespace bts { +namespace orderman { + +namespace event {class NewSecurities;} + +class SecuritySource +{ +public: + using OnSecurities = std::function; + SecuritySource(OnSecurities const&cb); + void discover(); + + ~SecuritySource(); +private: + struct PImpl; + std::unique_ptr me; +}; + +}} diff --git a/src/server/sparkd/order_man/events/BookUpdate.h b/src/server/sparkd/order_man/events/BookUpdate.h new file mode 100644 index 000000000..8ec82db4f --- /dev/null +++ b/src/server/sparkd/order_man/events/BookUpdate.h @@ -0,0 +1,30 @@ +#pragma once + +#include +#include "lib/domain_model/common.h" +#include "lib/config/protos/MarketData.pb.h" +#include "lib/md/Structure/PriceSize.h" + +namespace bts { +namespace orderman { +namespace event { + +using BookSide = std::array ; + +class BookUpdate { +public: + SecurityKey security_key; + BookSide &bid () {return _book[0];} + BookSide &ask () {return _book[1];} + + BookSide& get_side(api::BuySell bs) + { + bts_assert_d(bs != api::UNDEFINED); + return _book[bs - api::BUY]; + } + +private: + std::array _book; +}; + +}}} diff --git a/src/server/sparkd/order_man/events/NewSecurities.h b/src/server/sparkd/order_man/events/NewSecurities.h new file mode 100644 index 000000000..0c4960bfb --- /dev/null +++ b/src/server/sparkd/order_man/events/NewSecurities.h @@ -0,0 +1,23 @@ +#pragma once + +#include + +namespace bts { + +namespace api {class Security;} +namespace md2 {class MDConsumerHandle;} + +namespace orderman { +namespace event { + +class NewSecurities +{ +public: + struct SecRec { + api::Security const* sec; + md2::MDConsumerHandle* handle; + }; + std::vector value; +}; + +}}} diff --git a/src/server/sparkd/order_man/order_man_utils.h b/src/server/sparkd/order_man/order_man_utils.h index aa8967910..853699986 100644 --- a/src/server/sparkd/order_man/order_man_utils.h +++ b/src/server/sparkd/order_man/order_man_utils.h @@ -8,6 +8,7 @@ #include "lib/domain_model/common.h" namespace bts { +namespace api {class Security;} namespace orderman { using OrderKey = utils::ValueWrapper; using FIXSessionName = utils::ValueWrapper; @@ -15,6 +16,6 @@ using OperatorID = utils::ValueWrapper; using ExchangeOrderID = utils::ValueWrapper; using OrderTimeStamp = std::array ; using SymbolName = utils::ValueWrapper; - +using SecurityPtr = const api::Security *; }} diff --git a/tools/get_system_includes.sh b/tools/get_system_includes.sh index 3fcddd858..0a14f7a3a 100755 --- a/tools/get_system_includes.sh +++ b/tools/get_system_includes.sh @@ -7,3 +7,5 @@ cd $ROOT # external includes find externals/linux/include/ -name "*.h" -or -name "*.hpp" | sed s/externals\\/linux\\/include\\/// +# stdlib includes +find /usr/include/c++/6 -maxdepth 1 | sed s/\\/usr\\/include\\/c\+\+\\/6\\///