Chops Net IP
Loading...
Searching...
No Matches
mock_classes.hpp
Go to the documentation of this file.
1
16#ifndef MOCK_CLASSES_HPP_INCLUDED
17#define MOCK_CLASSES_HPP_INCLUDED
18
19#include <memory> // std::shared_ptr
20#include <string_view>
21#include <cstddef> // std::size_t, std::byte
22#include <system_error>
23
24#include "asio/ip/udp.hpp" // ip::udp::endpoint
25
26#include "buffer/shared_buffer.hpp"
27
32
33
34namespace chops {
35namespace test {
36
37inline std::size_t mock_hdr_decoder_func (const std::byte*, std::size_t) { return 0u; }
38
40 using endpoint_type = asio::ip::udp::endpoint;
41
42 double mock_sock = 42.0;
43
44 bool started = false;
45 constexpr static std::size_t qs_base = 42;
46
47 bool is_io_started() const { return started; }
48
49 template <typename F>
50 void visit_socket(F&& f) {
51 f(mock_sock);
52 }
53
54 chops::net::output_queue_stats get_output_queue_stats() const {
55 return chops::net::output_queue_stats { qs_base, qs_base +1 };
56 }
57
58 bool send_called = false;
59
60 bool send(chops::const_shared_buffer) { send_called = true; return true; }
61 bool send(chops::const_shared_buffer, const endpoint_type&) { send_called = true; return true; }
62
63 bool mf_sio_called = false;
64 bool simple_var_len_sio_called = false;
65 bool delim_sio_called = false;
66 bool rd_sio_called = false;
67 bool rd_endp_sio_called = false;
68 bool send_sio_called = false;
69 bool send_endp_sio_called = false;
70
71 template <typename MH, typename MF>
72 bool start_io(std::size_t, MH&&, MF&&) {
73 return started ? false : started = true, mf_sio_called = true, true;
74 }
75
76 template <typename MH>
77 bool start_io(std::size_t, MH&&, chops::net::hdr_decoder_func) {
78 return started ? false : started = true, simple_var_len_sio_called = true, true;
79 }
80
81 template <typename MH>
82 bool start_io(std::string_view, MH&&) {
83 return started ? false : started = true, delim_sio_called = true, true;
84 }
85
86 template <typename MH>
87 bool start_io(std::size_t, MH&&) {
88 return started ? false : started = true, rd_sio_called = true, true;
89 }
90
91 template <typename MH>
92 bool start_io(const endpoint_type&, std::size_t, MH&&) {
93 return started ? false : started = true, rd_endp_sio_called = true, true;
94 }
95
96 bool start_io() {
97 return started ? false : started = true, send_sio_called = true, true;
98 }
99
100 bool start_io(const endpoint_type&) {
101 return started ? false : started = true, send_endp_sio_called = true, true;
102 }
103
104 bool stop_io() {
105 return started ? started = false, true : false;
106 }
107
108};
109
112
114
115 using ios_chg_func = std::function<void (io_interface_mock, std::size_t, bool)>;
116 ios_chg_func io_cb;
117 std::shared_ptr<io_handler_mock> mock_ioh_sp = std::make_shared<io_handler_mock>();
118 float mock_sock = 11.0f;
119 bool started = false;
120
121// methods below
122
123 bool is_started() const { return started; }
124
125 template <typename F>
126 void visit_socket(F&& f) {
127 f(mock_sock);
128 }
129
130 template <typename F>
131 std::size_t visit_io_output(F&& f) {
132 f(io_output_mock(mock_ioh_sp));
133 return 1u;
134 }
135
136 template <typename F1, typename F2>
137 std::error_code start(F1&& ios, F2&&) {
138 if (started) {
139 return std::make_error_code(std::errc::too_many_files_open);
140 }
141 started = true;
142 io_cb = ios;
143 // immediately invoke state_chg
144 io_cb(io_interface_mock(mock_ioh_sp), 1u, true);
145 return std::error_code {};
146 }
147
148 std::error_code stop() {
149 if (!started) {
150 return std::make_error_code(std::errc::too_many_files_open);
151 }
152 started = false;
153 io_cb(io_interface_mock(mock_ioh_sp), 0u, false);
154 return std::error_code {};
155 }
156
157};
158
159inline void io_state_chg_mock(io_interface_mock, std::size_t, bool) { }
160inline void err_func_mock(io_interface_mock, std::error_code) { }
161
162
163} // end namespace test
164} // end namespace chops
165
166#endif
167
basic_io_interface class template, providing start_io, stop_io, visit_socket, make_io_output and rela...
basic_io_output class template, providing send and get_output_queue_stats methods.
The basic_io_interface class template provides access to an underlying network IO handler (TCP or UDP...
Definition basic_io_interface.hpp:100
The basic_io_output class template provides methods for sending data to an associated network IO hand...
Definition basic_io_output.hpp:57
Error codes, exception class, and error category within Chops net_ip library.
Function object class and declaration for simple variable length TCP message framing.
output_queue_stats provides information on the internal output queue.
Definition queue_stats.hpp:29
Definition mock_classes.hpp:39
Definition mock_classes.hpp:113