#include "catch/catch.hpp" #include "lib/jetfix/codec/FIXEncodingStream.h" #include "lib/jetfix/common/InMsgBuffer.h" #include "lib/jetfix/common/OutFIX.h" #include "lib/jetfix/common/OutFIXPtr.h" #include "lib/jetfix/store/FIXFileStore.h" #include "lib/jetfix/store/session_name.h" #include "lib/logging/logging.h" #include "lib/utils/TempDir.h" #include "lib/utils/TimeStamp.h" #include "lib/utils/datetime.h" #include #include #include namespace bts { namespace jetfix { TEST_CASE("JetFIX SessionNameTests", "[jetfix]") { SECTION("greg_weekday_works") { const char *days[] = {"sun", "Monday", "tuesday", "Wedn", "Thursday", "Friday", "sat"}; int i = 0; for (auto d : days) REQUIRE(parse_weekday(d) == boost::gregorian::greg_weekday(i++)); } SECTION("closest_before_works") { auto same_day_before = closest_before(datetime::make_ptime(2015, 7, 17, 17, 0, 0, 0), boost::gregorian::Friday, boost::posix_time::time_duration(16, 59, 59)); REQUIRE(datetime::FIX_date_time_string(same_day_before) == "20150717-16:59:59"); auto same_day_after = closest_before( datetime::make_ptime(2015, 7, 17, 17, 0, 0, 0), boost::gregorian::Friday, boost::posix_time::time_duration(17, 0, 1)); REQUIRE(datetime::FIX_date_time_string(same_day_after) == "20150710-17:00:01"); auto another_day_same_week = closest_before(datetime::make_ptime(2015, 7, 17, 17, 0, 0, 0), boost::gregorian::Thursday, boost::posix_time::time_duration(16, 59, 59)); REQUIRE(datetime::FIX_date_time_string(another_day_same_week) == "20150716-16:59:59"); auto another_day_previous_week = closest_before(datetime::make_ptime(2015, 7, 17, 17, 0, 0, 0), boost::gregorian::Saturday, boost::posix_time::time_duration(16, 59, 59)); REQUIRE(datetime::FIX_date_time_string(another_day_previous_week) == "20150711-16:59:59"); } SECTION("in_session_works") { REQUIRE(in_session(datetime::make_ptime(2015, 7, 17, 17, 0, 0, 0), boost::gregorian::Sunday, boost::posix_time::time_duration(16, 59, 59), boost::gregorian::Friday, boost::posix_time::time_duration(17, 0, 1))); REQUIRE(!in_session(datetime::make_ptime(2015, 7, 17, 17, 0, 0, 0), boost::gregorian::Sunday, boost::posix_time::time_duration(16, 59, 59), boost::gregorian::Friday, boost::posix_time::time_duration(16, 59, 59))); REQUIRE(in_session(datetime::make_ptime(2015, 7, 17, 17, 0, 0, 0), boost::posix_time::time_duration(16, 59, 59), boost::posix_time::time_duration(17, 59, 59))); REQUIRE(!in_session(datetime::make_ptime(2015, 7, 17, 17, 0, 0, 0), boost::posix_time::time_duration(0, 0, 0), boost::posix_time::time_duration(16, 0, 0))); REQUIRE(in_session(datetime::make_ptime(2015, 7, 17, 17, 0, 0, 0), boost::posix_time::time_duration(17, 50, 59), boost::posix_time::time_duration(17, 45, 59))); REQUIRE(!in_session(datetime::make_ptime(2015, 7, 17, 17, 47, 0, 0), boost::posix_time::time_duration(17, 50, 59), boost::posix_time::time_duration(17, 45, 59))); } SECTION("session_name_works") { REQUIRE(session_name(datetime::make_ptime(2015, 7, 17, 17, 0, 0, 0), boost::gregorian::Sunday, boost::posix_time::time_duration(16, 59, 59), boost::gregorian::Friday, boost::posix_time::time_duration(17, 0, 1)).as_string == "2015-07-12"); REQUIRE(session_name(datetime::make_ptime(2015, 7, 17, 17, 0, 0, 0), boost::gregorian::Sunday, boost::posix_time::time_duration(16, 59, 59), boost::gregorian::Friday, boost::posix_time::time_duration(16, 59, 59)).as_string == ""); REQUIRE(session_name(datetime::make_ptime(2015, 7, 17, 17, 0, 0, 0), boost::posix_time::time_duration(16, 59, 59), boost::posix_time::time_duration(17, 59, 59)).as_string == "2015-07-17"); REQUIRE(session_name(datetime::make_ptime(2015, 7, 17, 18, 0, 0, 0), boost::posix_time::time_duration(16, 59, 59), boost::posix_time::time_duration(17, 59, 59)).as_string == ""); REQUIRE(session_name(datetime::make_ptime(2015, 7, 17, 17, 0, 0, 0), boost::posix_time::time_duration(17, 50, 59), boost::posix_time::time_duration(17, 45, 59)).as_string == "2015-07-16"); REQUIRE(session_name(datetime::make_ptime(2015, 7, 17, 17, 51, 0, 0), boost::posix_time::time_duration(17, 50, 59), boost::posix_time::time_duration(17, 45, 59)).as_string == "2015-07-17"); REQUIRE(session_name(datetime::make_ptime(2015, 7, 17, 17, 47, 0, 0), boost::posix_time::time_duration(17, 50, 59), boost::posix_time::time_duration(17, 45, 59)).as_string == ""); } } TEST_CASE("JetFIX FileStoreTests", "[jetfix]") { SECTION("empty_store_works") { config::JetFIX::Connection c; c.set_name("test"); c.set_sender_compid("CME"); c.set_sender_subid("G"); c.set_target_compid("1U7001N"); c.set_target_subid("BLUESTREAM"); c.set_start_time("00:00:00"); c.set_end_time("23:59:59"); utils::TempDir td; FIXFileStore fs(c, td.name()); REQUIRE(fs.session_active()); REQUIRE(fs.replay().size() == 0); REQUIRE(fs.last_seq_num() == 0); REQUIRE(fs.last_seq_num_processed() == 0); } SECTION("in_msg_is_stored") { auto td = std::make_shared(); config::JetFIX::Connection c; c.set_name("test"); c.set_sender_compid("CME"); c.set_sender_subid("G"); c.set_target_compid("1U7001N"); c.set_target_subid("BLUESTREAM"); c.set_start_time("00:00:00"); c.set_end_time("23:59:59"); FIXEncodingStream stream(c); auto &buf = stream.new_msg(MessageTypes::Logon); buf.add(std::string_view{"US,NC"}); buf.add(0); buf.add(30); buf.add(std::string_view{"O2"}); buf.add(std::string_view{"1.0"}); buf.add(std::string_view{"BTS"}); stream.close(); stream.stamp(2, 3, datetime::FIX_timestamp(datetime::now())); auto fix = stream.to_str(); { FIXFileStore fs(c, td->name()); fs.store(InMsgBuffer(fix)); fs.set_last_msg_processed(3); } FIXFileStore fs(c, td->name()); auto replay = fs.replay(); REQUIRE(replay.size() == 1); REQUIRE(replay[0].type() == MessageTypes::Logon); REQUIRE(replay[0].to_str() == fix); REQUIRE(fs.last_seq_num() == 0); REQUIRE(fs.last_seq_num_processed() == 3); } SECTION("out_msg_is_stored") { auto td = std::make_shared(); config::JetFIX::Connection c; c.set_name("test"); c.set_sender_compid("CME"); c.set_sender_subid("G"); c.set_target_compid("1U7001N"); c.set_target_subid("BLUESTREAM"); c.set_start_time("00:00:00"); c.set_end_time("23:59:59"); auto msg_vec = std::make_shared(c); auto &stream = msg_vec->fix.back(); auto &buf = stream.new_msg(MessageTypes::Logon); buf.add(std::string_view{"US,NC"}); buf.add(0); buf.add(30); buf.add(std::string_view{"O2"}); buf.add(std::string_view{"1.0"}); buf.add(std::string_view{"BTS"}); stream.close(); stream.stamp(2, 3, datetime::FIX_timestamp(datetime::now())); { FIXFileStore fs(c, td->name()); fs.store(msg_vec); } FIXFileStore fs(c, td->name()); auto replay = fs.replay(); REQUIRE(replay.size() == 0); REQUIRE(fs.last_seq_num() == 3); REQUIRE(fs.last_seq_num_processed() == 0); } SECTION("test_performance") { auto td = std::make_shared(); config::JetFIX::Connection c; c.set_name("test"); c.set_sender_compid("CME"); c.set_sender_subid("G"); c.set_target_compid("1U7001N"); c.set_target_subid("BLUESTREAM"); c.set_start_time("00:00:00"); c.set_end_time("23:59:59"); auto start = utils::TimeStamp::now(); { FIXFileStore fs(c, td->name()); for (int i = 0; i < 10000; i++) { auto msg_vec = std::make_shared>(); msg_vec->emplace_back(c); auto &stream = msg_vec->back(); auto &buf = stream.new_msg(MessageTypes::Logon); buf.add(std::string_view{"US,NC"}); buf.add(0); buf.add(30); buf.add(std::string_view{"O2"}); buf.add(std::string_view{"1.0"}); buf.add(std::string_view{"BTS"}); stream.close(); stream.stamp(i, 3, datetime::FIX_timestamp(datetime::now() - boost::gregorian::days(1))); fs.store(InMsgBuffer(stream.to_str())); } } auto stop = utils::TimeStamp::now(); LOG(Testing, Info, "jetfix store gen: {} ms", (stop - start).milliseconds()); start = utils::TimeStamp::now(); FIXFileStore fs(c, td->name()); stop = utils::TimeStamp::now(); LOG(Testing, Info, "jetfix store load: {} ms", (stop - start).milliseconds()); } } } // namespace jetfix } // namespace bts