JayBeams  0.1
Another project to have fun coding.
ut_process_iostream_mlist.cpp
Go to the documentation of this file.
5 
7 
8 #include <jb/gmock/init.hpp>
9 #include <boost/test/unit_test.hpp>
10 
11 #include <initializer_list>
12 
13 namespace {
14 
15 class mock_message_handler {
16 public:
17  mock_message_handler() {
18  }
19 
20  typedef int time_point;
21 
22  MOCK_METHOD0(now, int());
23  MOCK_METHOD2(
24  handle_unknown, void(int const&, jb::itch5::unknown_message const&));
25  MOCK_METHOD4(
26  handle_message,
27  void(
28  int const&, std::uint64_t msgcnt, std::size_t msgoffset,
30  MOCK_METHOD4(
31  handle_message,
32  void(
33  int const&, std::uint64_t msgcnt, std::size_t msgoffset,
35  MOCK_METHOD4(
36  handle_message,
37  void(
38  int const&, std::uint64_t msgcnt, std::size_t msgoffset,
40 };
41 
42 std::string create_message_stream(
43  std::initializer_list<std::pair<char const*, std::size_t>> const& rhs);
44 
45 } // anonymous namespace
46 
47 /**
48  * @test Verify that jb::itch5::process_iostream_mlist<> works as expected.
49  */
50 BOOST_AUTO_TEST_CASE(process_iostream_mlist_simple) {
51  // TODO(#5) this is a really trivial test, its main purpose is to
52  // get the code to compile. The functions are tested elsewhere, but
53  // this is a big shameful.
54  mock_message_handler handler;
55  using namespace ::testing;
56  EXPECT_CALL(handler, now()).WillRepeatedly(Return(0));
57 
58  std::string bytes = create_message_stream(
66  std::istringstream is(bytes);
67 
68  EXPECT_CALL(handler, now()).Times(21);
69  EXPECT_CALL(
70  handler,
71  handle_message(_, _, _, An<jb::itch5::add_order_message const&>()))
72  .Times(4);
73  EXPECT_CALL(
74  handler,
75  handle_message(_, _, _, An<jb::itch5::stock_directory_message const&>()))
76  .Times(3);
77  EXPECT_CALL(
78  handler,
79  handle_message(_, _, _, An<jb::itch5::system_event_message const&>()))
80  .Times(2);
81  EXPECT_CALL(handler, handle_unknown(_, _)).Times(1);
83  mock_message_handler, jb::itch5::system_event_message,
85  is, handler);
86 }
87 
88 /**
89  * @test Verify that jb::itch5::process_iostream_mlist<> exits
90  * gracefully on I/O errors.
91  */
92 BOOST_AUTO_TEST_CASE(process_iostream_mlist_errors) {
93  mock_message_handler handler;
94 
95  std::string bytes =
96  create_message_stream({jb::itch5::testing::system_event(),
101  std::istringstream is(bytes);
102 
103  int count = 0;
104  // Simulate a iostream failure on the fifth read() ...
105  using namespace ::testing;
106  EXPECT_CALL(handler, now()).WillRepeatedly(Invoke([&is, &count]() {
107  if (++count == 5) {
108  is.setstate(std::ios::failbit);
109  }
110  return 0;
111  }));
112 
113  EXPECT_CALL(
114  handler,
115  handle_message(_, _, _, An<jb::itch5::system_event_message const&>()))
116  .Times(1);
117  EXPECT_CALL(
118  handler,
119  handle_message(_, _, _, An<jb::itch5::stock_directory_message const&>()))
120  .Times(1);
121  EXPECT_CALL(handler, handle_unknown(_, _)).Times(0);
123  mock_message_handler, jb::itch5::system_event_message,
125  is, handler);
126 }
127 
128 namespace {
129 
130 std::string create_message_stream(
131  std::initializer_list<std::pair<char const*, std::size_t>> const& rhs) {
132  std::string bytes;
133  for (auto const& p : rhs) {
134  std::size_t len = p.second;
135  if (len == 0 or len >= std::size_t(1 << 16)) {
136  throw std::invalid_argument(
137  "arguments to create_message_stream must have "
138  "length in the [0,65536] range");
139  }
140  int hi = len / 256;
141  int lo = len % 256;
142  bytes.push_back(char(hi));
143  bytes.push_back(char(lo));
144  bytes.append(p.first, p.second);
145  }
146  return bytes;
147 }
148 
149 } // anonymous namespace
void process_iostream_mlist(std::istream &is, message_handler &handler)
clock_type::time_point time_point
A convenience alias for clock_type::time_point.
std::pair< char const *, std::size_t > add_order()
Definition: data.cpp:33
std::pair< char const *, std::size_t > system_event()
Definition: data.cpp:262
BOOST_AUTO_TEST_CASE(process_iostream_mlist_simple)
std::pair< char const *, std::size_t > stock_directory()
Definition: data.cpp:228
Initialize GMock to work with Boost.Test.
Represent a &#39;Stock Directory&#39; message in the ITCH-5.0 protocol.
Represent an &#39;Add Order&#39; message in the ITCH-5.0 protocol.
Represent a &#39;System Event Message&#39; in the ITCH-5.0 protocol.
std::pair< char const *, std::size_t > trade()
Definition: data.cpp:271