| 1 | /* Copyright (c) 2015, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | #include "unittest/gunit/libmysqlgcs/include/gcs_base_test.h" |
| 24 | |
| 25 | #include <memory> |
| 26 | #include <string> |
| 27 | #include <vector> |
| 28 | |
| 29 | #include "plugin/group_replication/libmysqlgcs/src/bindings/xcom/gcs_xcom_control_interface.h" |
| 30 | #include "plugin/group_replication/libmysqlgcs/src/bindings/xcom/gcs_xcom_group_member_information.h" |
| 31 | #include "plugin/group_replication/libmysqlgcs/src/bindings/xcom/gcs_xcom_notification.h" |
| 32 | #include "plugin/group_replication/libmysqlgcs/src/bindings/xcom/gcs_xcom_state_exchange.h" |
| 33 | #include "plugin/group_replication/libmysqlgcs/src/bindings/xcom/gcs_xcom_utils.h" |
| 34 | #include "plugin/group_replication/libmysqlgcs/src/bindings/xcom/xcom/node_set.h" |
| 35 | #include "plugin/group_replication/libmysqlgcs/src/bindings/xcom/xcom/synode_no.h" |
| 36 | #include "plugin/group_replication/libmysqlgcs/xdr_gen/xcom_vp.h" |
| 37 | #include "template_utils.h" |
| 38 | |
| 39 | using std::string; |
| 40 | |
| 41 | using ::testing::NiceMock; |
| 42 | |
| 43 | namespace gcs_xcom_control_unittest { |
| 44 | typedef enum { JJ = 0, LL = 1, JL = 2, LJ = 3 } InvocationOrder; |
| 45 | |
| 46 | class InvocationHelper { |
| 47 | public: |
| 48 | InvocationHelper(Gcs_xcom_control *x, InvocationOrder o) |
| 49 | : xcom_control_if(x), |
| 50 | mutex(), |
| 51 | counter(0), |
| 52 | order(o), |
| 53 | count_fail(0), |
| 54 | count_success(0) { |
| 55 | mutex.init(PSI_NOT_INSTRUMENTED, nullptr); |
| 56 | } |
| 57 | |
| 58 | ~InvocationHelper() { mutex.destroy(); } |
| 59 | |
| 60 | void invokeMethod() { |
| 61 | int mycounter = 0; |
| 62 | enum_gcs_error ret = GCS_OK; |
| 63 | |
| 64 | if (order == JJ) { |
| 65 | ret = xcom_control_if->join(); |
| 66 | } else if (order == LL) { |
| 67 | ret = xcom_control_if->leave(); |
| 68 | } else if (order == JL) { |
| 69 | mutex.lock(); |
| 70 | mycounter = counter++; |
| 71 | if (mycounter == 0) { |
| 72 | ret = xcom_control_if->join(); |
| 73 | } else { |
| 74 | ret = xcom_control_if->leave(); |
| 75 | } |
| 76 | mutex.unlock(); |
| 77 | } else if (order == LJ) { |
| 78 | mutex.lock(); |
| 79 | mycounter = counter++; |
| 80 | if (mycounter == 0) { |
| 81 | ret = xcom_control_if->leave(); |
| 82 | } else { |
| 83 | ret = xcom_control_if->join(); |
| 84 | } |
| 85 | mutex.unlock(); |
| 86 | } else { |
| 87 | assert(false); |
| 88 | } |
| 89 | |
| 90 | mutex.lock(); |
| 91 | if (ret == GCS_OK) { |
| 92 | count_success++; |
| 93 | } else { |
| 94 | count_fail++; |
| 95 | } |
| 96 | mutex.unlock(); |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | Gcs_xcom_control *xcom_control_if; |
| 101 | My_xp_mutex_impl mutex; |
| 102 | int counter; |
| 103 | InvocationOrder order; |
| 104 | |
| 105 | public: |
| 106 | int count_fail; |
| 107 | int count_success; |
| 108 | }; |
| 109 | |
| 110 | void homemade_free_site_def(unsigned int n, site_def *s, |
| 111 | node_address *node_addrs) { |
| 112 | // TODO: replace the following with free_site_def(site_config) once |
| 113 | // the header file in site_def.h is fixed |
| 114 | for (unsigned int i = 0; i < n; i++) free(node_addrs[i].uuid.data.data_val); |
| 115 | free_node_set(&s->global_node_set); |
| 116 | free_node_set(&s->local_node_set); |
| 117 | remove_node_list(n, node_addrs, &s->nodes); |
| 118 | free(s->nodes.node_list_val); |
| 119 | free(s); |
| 120 | } |
| 121 | |
| 122 | class mock_gcs_xcom_view_change_control_interface |
| 123 | : public Gcs_xcom_view_change_control_interface { |
| 124 | private: |
| 125 | Gcs_view *m_current_view; |
| 126 | bool m_belongs_to_group; |
| 127 | My_xp_mutex_impl m_mutex_current_view; |
| 128 | My_xp_mutex_impl m_joining_leaving_mutex; |
| 129 | bool m_joining; |
| 130 | bool m_leaving; |
| 131 | |
| 132 | public: |
| 133 | mock_gcs_xcom_view_change_control_interface() |
| 134 | : m_current_view(nullptr), |
| 135 | m_belongs_to_group(false), |
| 136 | m_mutex_current_view(), |
| 137 | m_joining_leaving_mutex(), |
| 138 | m_joining(false), |
| 139 | m_leaving(false) |
| 140 | |
| 141 | { |
| 142 | m_mutex_current_view.init(PSI_NOT_INSTRUMENTED, nullptr); |
| 143 | m_joining_leaving_mutex.init(PSI_NOT_INSTRUMENTED, nullptr); |
| 144 | } |
| 145 | |
| 146 | ~mock_gcs_xcom_view_change_control_interface() override { |
| 147 | m_mutex_current_view.destroy(); |
| 148 | m_joining_leaving_mutex.destroy(); |
| 149 | } |
| 150 | |
| 151 | MOCK_METHOD0(start_view_exchange, void()); |
| 152 | MOCK_METHOD0(end_view_exchange, void()); |
| 153 | MOCK_METHOD0(wait_for_view_change_end, void()); |
| 154 | MOCK_METHOD0(is_view_changing, bool()); |
| 155 | |
| 156 | MOCK_METHOD0(finalize, void()); |
| 157 | MOCK_METHOD0(is_finalized, bool()); |
| 158 | |
| 159 | bool start_leave() override { |
| 160 | bool retval = false; |
| 161 | |
| 162 | m_joining_leaving_mutex.lock(); |
| 163 | retval = m_joining || m_leaving; |
| 164 | if (!retval) m_leaving = true; |
| 165 | m_joining_leaving_mutex.unlock(); |
| 166 | |
| 167 | return !retval; |
| 168 | } |
| 169 | |
| 170 | void end_leave() override { |
| 171 | m_joining_leaving_mutex.lock(); |
| 172 | m_leaving = false; |
| 173 | m_joining_leaving_mutex.unlock(); |
| 174 | } |
| 175 | |
| 176 | bool is_leaving() override { |
| 177 | bool retval; |
| 178 | |
| 179 | m_joining_leaving_mutex.lock(); |
| 180 | retval = m_leaving; |
| 181 | m_joining_leaving_mutex.unlock(); |
| 182 | |
| 183 | return retval; |
| 184 | } |
| 185 | |
| 186 | bool start_join() override { |
| 187 | bool retval = false; |
| 188 | |
| 189 | m_joining_leaving_mutex.lock(); |
| 190 | retval = m_joining || m_leaving; |
| 191 | if (!retval) m_joining = true; |
| 192 | m_joining_leaving_mutex.unlock(); |
| 193 | |
| 194 | return !retval; |
| 195 | } |
| 196 | |
| 197 | void end_join() override { |
| 198 | m_joining_leaving_mutex.lock(); |
| 199 | m_joining = false; |
| 200 | m_joining_leaving_mutex.unlock(); |
| 201 | } |
| 202 | |
| 203 | bool is_joining() override { |
| 204 | bool retval; |
| 205 | |
| 206 | m_joining_leaving_mutex.lock(); |
| 207 | retval = m_joining; |
| 208 | m_joining_leaving_mutex.unlock(); |
| 209 | |
| 210 | return retval; |
| 211 | } |
| 212 | |
| 213 | void set_current_view(Gcs_view *view) override { |
| 214 | m_mutex_current_view.lock(); |
| 215 | delete m_current_view; |
| 216 | m_current_view = view; |
| 217 | m_mutex_current_view.unlock(); |
| 218 | } |
| 219 | |
| 220 | void set_unsafe_current_view(Gcs_view *view) override { |
| 221 | set_current_view(view); |
| 222 | } |
| 223 | |
| 224 | Gcs_view *get_current_view() override { |
| 225 | Gcs_view *view = nullptr; |
| 226 | |
| 227 | m_mutex_current_view.lock(); |
| 228 | if (m_current_view != nullptr) view = new Gcs_view(*m_current_view); |
| 229 | m_mutex_current_view.unlock(); |
| 230 | |
| 231 | return view; |
| 232 | } |
| 233 | |
| 234 | Gcs_view *get_unsafe_current_view() override { return m_current_view; } |
| 235 | |
| 236 | bool belongs_to_group() override { return m_belongs_to_group; } |
| 237 | |
| 238 | void set_belongs_to_group(bool belong) override { |
| 239 | m_belongs_to_group = belong; |
| 240 | } |
| 241 | }; |
| 242 | |
| 243 | // This typedef is needed because GMock can't deal with multiple template args. |
| 244 | typedef std::map<Gcs_member_identifier, Xcom_member_state *> Stored_States; |
| 245 | |
| 246 | class mock_gcs_xcom_state_exchange_interface |
| 247 | : public Gcs_xcom_state_exchange_interface { |
| 248 | private: |
| 249 | int m_process_member_state_iteration; |
| 250 | |
| 251 | bool free_xcom_member_state(Xcom_member_state *m, |
| 252 | const Gcs_member_identifier &, |
| 253 | Gcs_protocol_version, Gcs_protocol_version) { |
| 254 | delete m; |
| 255 | if (m_process_member_state_iteration == 0) { |
| 256 | m_process_member_state_iteration++; |
| 257 | return false; |
| 258 | } else |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | bool free_members_joined(synode_no, |
| 263 | std::vector<Gcs_member_identifier *> &total, |
| 264 | std::vector<Gcs_member_identifier *> &, |
| 265 | std::vector<Gcs_member_identifier *> &joined, |
| 266 | std::vector<std::unique_ptr<Gcs_message_data>> &, |
| 267 | Gcs_view *, std::string *, |
| 268 | const Gcs_member_identifier &, |
| 269 | const Gcs_xcom_nodes &) { |
| 270 | std::vector<Gcs_member_identifier *>::iterator it; |
| 271 | |
| 272 | for (it = total.begin(); it != total.end(); it++) delete (*it); |
| 273 | total.clear(); |
| 274 | |
| 275 | for (it = joined.begin(); it != joined.end(); it++) delete (*it); |
| 276 | joined.clear(); |
| 277 | |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | public: |
| 282 | mock_gcs_xcom_state_exchange_interface() |
| 283 | : m_process_member_state_iteration(0) { |
| 284 | ON_CALL(*this, process_member_state(_, _, _, _)) |
| 285 | .WillByDefault(Invoke( |
| 286 | this, |
| 287 | &mock_gcs_xcom_state_exchange_interface::free_xcom_member_state)); |
| 288 | |
| 289 | ON_CALL(*this, state_exchange(_, _, _, _, _, _, _, _, _)) |
| 290 | .WillByDefault(Invoke( |
| 291 | this, |
| 292 | &mock_gcs_xcom_state_exchange_interface::free_members_joined)); |
| 293 | ON_CALL(*this, compute_incompatible_members()) |
| 294 | .WillByDefault(Return(std::vector<Gcs_xcom_node_information>())); |
| 295 | ON_CALL(*this, process_recovery_state()).WillByDefault(Return(true)); |
| 296 | } |
| 297 | |
| 298 | MOCK_METHOD0(init, void()); |
| 299 | MOCK_METHOD0(reset, void()); |
| 300 | MOCK_METHOD0(reset_with_flush, void()); |
| 301 | MOCK_METHOD0(end, void()); |
| 302 | |
| 303 | MOCK_METHOD0(compute_incompatible_members, |
| 304 | std::vector<Gcs_xcom_node_information>()); |
| 305 | MOCK_METHOD0(process_recovery_state, bool()); |
| 306 | |
| 307 | MOCK_METHOD9( |
| 308 | state_exchange, |
| 309 | bool(synode_no configuration_id, |
| 310 | std::vector<Gcs_member_identifier *> &total, |
| 311 | std::vector<Gcs_member_identifier *> &left, |
| 312 | std::vector<Gcs_member_identifier *> &joined, |
| 313 | std::vector<std::unique_ptr<Gcs_message_data>> &exchangeable_data, |
| 314 | Gcs_view *current_view, std::string *group, |
| 315 | const Gcs_member_identifier &local_info, const Gcs_xcom_nodes &)); |
| 316 | MOCK_METHOD4(process_member_state, |
| 317 | bool(Xcom_member_state *ms_info, |
| 318 | const Gcs_member_identifier &p_id, |
| 319 | Gcs_protocol_version max_protocol_version, |
| 320 | Gcs_protocol_version used_protocol_version)); |
| 321 | MOCK_METHOD0(get_new_view_id, Gcs_xcom_view_identifier *()); |
| 322 | MOCK_METHOD0(get_joined, std::set<Gcs_member_identifier *> *()); |
| 323 | MOCK_METHOD0(get_left, std::set<Gcs_member_identifier *> *()); |
| 324 | MOCK_METHOD0(get_total, std::set<Gcs_member_identifier *> *()); |
| 325 | MOCK_METHOD0(get_group, string *()); |
| 326 | MOCK_METHOD0(get_member_states, Stored_States *()); |
| 327 | MOCK_METHOD0(compute_maximum_supported_protocol_version, void()); |
| 328 | }; |
| 329 | |
| 330 | class mock_gcs_xcom_proxy : public Gcs_xcom_proxy_base { |
| 331 | private: |
| 332 | node_list nl; |
| 333 | |
| 334 | void reset_me() { ::delete_node_address(nl.node_list_len, nl.node_list_val); } |
| 335 | |
| 336 | public: |
| 337 | mock_gcs_xcom_proxy() { |
| 338 | ON_CALL(*this, xcom_input_connect(_, _)).WillByDefault(Return(true)); |
| 339 | ON_CALL(*this, test_xcom_tcp_connection(_, _)).WillByDefault(Return(true)); |
| 340 | ON_CALL(*this, xcom_client_boot(_, _)).WillByDefault(Return(true)); |
| 341 | ON_CALL(*this, xcom_client_add_node(_, _, _)).WillByDefault(Return(false)); |
| 342 | ON_CALL(*this, xcom_client_send_data(_, _)).WillByDefault(Return(10)); |
| 343 | ON_CALL(*this, new_node_address_uuid(_, _, _)) |
| 344 | .WillByDefault(Invoke(::new_node_address_uuid)); |
| 345 | ON_CALL(*this, delete_node_address(_, _)) |
| 346 | .WillByDefault(Invoke(::delete_node_address)); |
| 347 | ON_CALL(*this, xcom_wait_ready()).WillByDefault(Return(GCS_OK)); |
| 348 | ON_CALL(*this, xcom_wait_for_xcom_comms_status_change(_)) |
| 349 | .WillByDefault(SetArgReferee<0>(XCOM_COMMS_OK)); |
| 350 | ON_CALL(*this, xcom_wait_exit()).WillByDefault(Return(GCS_OK)); |
| 351 | ON_CALL(*this, xcom_is_exit()).WillByDefault(Return(true)); |
| 352 | } |
| 353 | |
| 354 | MOCK_METHOD3(new_node_address_uuid, |
| 355 | node_address *(unsigned int n, char const *names[], |
| 356 | blob uuids[])); |
| 357 | MOCK_METHOD2(delete_node_address, void(unsigned int n, node_address *na)); |
| 358 | MOCK_METHOD3(xcom_client_add_node, bool(connection_descriptor *con, |
| 359 | node_list *nl, uint32_t group_id)); |
| 360 | MOCK_METHOD3(xcom_client_get_event_horizon, |
| 361 | bool(connection_descriptor *con, uint32_t group_id, |
| 362 | xcom_event_horizon &event_horizon)); |
| 363 | MOCK_METHOD2(xcom_client_get_event_horizon, |
| 364 | bool(uint32_t group_id, xcom_event_horizon &event_horizon)); |
| 365 | MOCK_METHOD3(xcom_client_set_event_horizon, |
| 366 | bool(connection_descriptor *con, uint32_t group_id, |
| 367 | xcom_event_horizon event_horizon)); |
| 368 | MOCK_METHOD2(xcom_client_set_event_horizon, |
| 369 | bool(uint32_t group_id, xcom_event_horizon event_horizon)); |
| 370 | MOCK_METHOD2(xcom_client_set_max_leaders, |
| 371 | bool(uint32_t group_id, node_no max_leaders)); |
| 372 | MOCK_METHOD4(xcom_client_set_leaders, |
| 373 | bool(uint32_t group_id, u_int n, char const *names[], |
| 374 | node_no max_nr_leaders)); |
| 375 | MOCK_METHOD2(xcom_client_get_leaders, |
| 376 | bool(uint32_t gid, leader_info_data &leaders)); |
| 377 | MOCK_METHOD4(xcom_client_get_synode_app_data, |
| 378 | bool(connection_descriptor *con, uint32_t group_id, |
| 379 | synode_no_array &synodes, synode_app_data_array &reply)); |
| 380 | MOCK_METHOD1(xcom_client_set_cache_size, bool(uint64_t size)); |
| 381 | MOCK_METHOD2(xcom_client_remove_node, bool(node_list *nl, uint32_t group_id)); |
| 382 | MOCK_METHOD3(xcom_client_remove_node, bool(connection_descriptor *con, |
| 383 | node_list *nl, uint32_t group_id)); |
| 384 | MOCK_METHOD2(xcom_client_boot, bool(node_list *nl, uint32_t group_id)); |
| 385 | MOCK_METHOD2(xcom_client_open_connection, |
| 386 | connection_descriptor *(std::string, xcom_port port)); |
| 387 | MOCK_METHOD1(xcom_client_close_connection, bool(connection_descriptor *con)); |
| 388 | MOCK_METHOD2(xcom_client_send_data, |
| 389 | bool(unsigned long long size, char *data)); |
| 390 | MOCK_METHOD1(xcom_init, void(xcom_port listen_port)); |
| 391 | MOCK_METHOD0(xcom_exit, void()); |
| 392 | MOCK_METHOD0(xcom_set_cleanup, void()); |
| 393 | MOCK_METHOD1(xcom_get_ssl_mode, int(const char *mode)); |
| 394 | MOCK_METHOD1(xcom_set_ssl_mode, int(int mode)); |
| 395 | MOCK_METHOD1(xcom_get_ssl_fips_mode, int(const char *mode)); |
| 396 | MOCK_METHOD1(xcom_set_ssl_fips_mode, int(int mode)); |
| 397 | MOCK_METHOD0(xcom_init_ssl, bool()); |
| 398 | MOCK_METHOD0(xcom_destroy_ssl, void()); |
| 399 | MOCK_METHOD0(xcom_use_ssl, bool()); |
| 400 | MOCK_METHOD2(xcom_set_ssl_parameters, |
| 401 | void(ssl_parameters ssl, tls_parameters tls)); |
| 402 | MOCK_METHOD1(find_site_def, site_def const *(synode_no synode)); |
| 403 | MOCK_METHOD0(xcom_wait_ready, enum_gcs_error()); |
| 404 | MOCK_METHOD0(xcom_is_ready, bool()); |
| 405 | MOCK_METHOD1(xcom_set_ready, void(bool value)); |
| 406 | MOCK_METHOD0(xcom_signal_ready, void()); |
| 407 | MOCK_METHOD1(xcom_wait_for_xcom_comms_status_change, void(int &status)); |
| 408 | MOCK_METHOD0(xcom_has_comms_status_changed, bool()); |
| 409 | MOCK_METHOD1(xcom_set_comms_status, void(int status)); |
| 410 | MOCK_METHOD1(xcom_signal_comms_status_changed, void(int status)); |
| 411 | MOCK_METHOD0(xcom_wait_exit, enum_gcs_error()); |
| 412 | MOCK_METHOD0(xcom_is_exit, bool()); |
| 413 | MOCK_METHOD1(xcom_set_exit, void(bool)); |
| 414 | MOCK_METHOD0(xcom_signal_exit, void()); |
| 415 | MOCK_METHOD3(xcom_client_force_config, int(connection_descriptor *fd, |
| 416 | node_list *nl, uint32_t group_id)); |
| 417 | MOCK_METHOD2(xcom_client_force_config, |
| 418 | bool(node_list *nl, uint32_t group_id)); |
| 419 | |
| 420 | MOCK_METHOD0(get_should_exit, bool()); |
| 421 | MOCK_METHOD1(set_should_exit, void(bool should_exit)); |
| 422 | |
| 423 | MOCK_METHOD2(xcom_input_connect, |
| 424 | bool(std::string const &address, xcom_port port)); |
| 425 | MOCK_METHOD0(xcom_input_disconnect, void()); |
| 426 | MOCK_METHOD1(xcom_input_try_push, bool(app_data_ptr data)); |
| 427 | /* Mocking fails compilation on Windows. It attempts to copy the std::future |
| 428 | * which is non-copyable. */ |
| 429 | Gcs_xcom_input_queue::future_reply xcom_input_try_push_and_get_reply( |
| 430 | app_data_ptr) { |
| 431 | return std::future<std::unique_ptr<Gcs_xcom_input_queue::Reply>>(); |
| 432 | } |
| 433 | MOCK_METHOD0(xcom_input_try_pop, xcom_input_request_ptr()); |
| 434 | MOCK_METHOD2(test_xcom_tcp_connection, |
| 435 | bool(std::string &host, xcom_port port)); |
| 436 | }; |
| 437 | |
| 438 | class mock_gcs_control_event_listener : public Gcs_control_event_listener { |
| 439 | public: |
| 440 | MOCK_CONST_METHOD2(on_view_changed, |
| 441 | void(const Gcs_view &new_view, |
| 442 | const Exchanged_data &exchanged_data)); |
| 443 | MOCK_CONST_METHOD0(get_exchangeable_data, Gcs_message_data *()); |
| 444 | MOCK_CONST_METHOD2( |
| 445 | on_suspicions, |
| 446 | void(const std::vector<Gcs_member_identifier> &members, |
| 447 | const std::vector<Gcs_member_identifier> &unreachable)); |
| 448 | }; |
| 449 | |
| 450 | class mock_my_xp_socket_util : public My_xp_socket_util { |
| 451 | public: |
| 452 | mock_my_xp_socket_util() { |
| 453 | ON_CALL(*this, disable_nagle_in_socket(_)).WillByDefault(Return(0)); |
| 454 | } |
| 455 | |
| 456 | MOCK_METHOD1(disable_nagle_in_socket, int(int fd)); |
| 457 | }; |
| 458 | |
| 459 | class mock_gcs_network_provider_operations_interface |
| 460 | : public Network_provider_operations_interface { |
| 461 | public: |
| 462 | mock_gcs_network_provider_operations_interface() { |
| 463 | ON_CALL(*this, start_active_network_provider()) |
| 464 | .WillByDefault(Return(false)); |
| 465 | ON_CALL(*this, stop_all_network_providers()).WillByDefault(Return(false)); |
| 466 | ON_CALL(*this, stop_active_network_provider()).WillByDefault(Return(false)); |
| 467 | ON_CALL(*this, configure_active_provider(_)).WillByDefault(Return(false)); |
| 468 | ON_CALL(*this, configure_active_provider_secure_connections(_)) |
| 469 | .WillByDefault(Return(false)); |
| 470 | } |
| 471 | |
| 472 | MOCK_METHOD0(start_active_network_provider, bool()); |
| 473 | MOCK_METHOD0(stop_all_network_providers, bool()); |
| 474 | MOCK_METHOD0(stop_active_network_provider, bool()); |
| 475 | MOCK_METHOD1(configure_active_provider, |
| 476 | bool(Network_configuration_parameters ¶ms)); |
| 477 | MOCK_METHOD1(configure_active_provider_secure_connections, |
| 478 | bool(Network_configuration_parameters ¶ms)); |
| 479 | MOCK_METHOD0(remove_all_network_provider, void()); |
| 480 | MOCK_METHOD1(remove_network_provider, |
| 481 | void(enum_transport_protocol provider_key)); |
| 482 | }; |
| 483 | |
| 484 | class mock_gcs_xcom_control : public Gcs_xcom_control { |
| 485 | public: |
| 486 | mock_gcs_xcom_control( |
| 487 | Gcs_xcom_node_address *xcom_node_address, |
| 488 | std::vector<Gcs_xcom_node_address *> &xcom_peers, |
| 489 | Gcs_group_identifier group_identifier, Gcs_xcom_proxy *xcom_proxy, |
| 490 | Gcs_xcom_group_management *xcom_group_management, |
| 491 | Gcs_xcom_engine *gcs_engine, |
| 492 | Gcs_xcom_state_exchange_interface *state_exchange, |
| 493 | Gcs_xcom_view_change_control_interface *view_control, bool boot, |
| 494 | My_xp_socket_util *socket_util, |
| 495 | std::unique_ptr<Network_provider_operations_interface> network_ops) |
| 496 | : Gcs_xcom_control(xcom_node_address, xcom_peers, group_identifier, |
| 497 | xcom_proxy, xcom_group_management, gcs_engine, |
| 498 | state_exchange, view_control, boot, socket_util, |
| 499 | std::move(network_ops)) {} |
| 500 | |
| 501 | enum_gcs_error join() override { return join(nullptr); } |
| 502 | |
| 503 | enum_gcs_error join(Gcs_view *view) { |
| 504 | enum_gcs_error ret = GCS_NOK; |
| 505 | |
| 506 | if (!m_view_control->start_join()) { |
| 507 | return GCS_NOK; |
| 508 | } |
| 509 | |
| 510 | if (belongs_to_group()) { |
| 511 | m_view_control->end_join(); |
| 512 | return GCS_NOK; |
| 513 | } |
| 514 | |
| 515 | if (!m_boot && m_initial_peers.empty()) { |
| 516 | m_view_control->end_join(); |
| 517 | return GCS_NOK; |
| 518 | } |
| 519 | |
| 520 | ret = do_join(false); |
| 521 | |
| 522 | if (ret == GCS_OK) { |
| 523 | m_view_control->set_current_view(view); |
| 524 | m_view_control->set_belongs_to_group(true); |
| 525 | } |
| 526 | |
| 527 | return ret; |
| 528 | } |
| 529 | |
| 530 | enum_gcs_error leave() override { |
| 531 | enum_gcs_error ret = GCS_NOK; |
| 532 | |
| 533 | if (!m_view_control->start_leave()) { |
| 534 | return GCS_NOK; |
| 535 | } |
| 536 | |
| 537 | if (!belongs_to_group()) { |
| 538 | m_view_control->end_leave(); |
| 539 | return GCS_NOK; |
| 540 | } |
| 541 | |
| 542 | ret = do_leave(); |
| 543 | |
| 544 | if (ret == GCS_OK) { |
| 545 | m_view_control->set_current_view(nullptr); |
| 546 | m_view_control->set_belongs_to_group(false); |
| 547 | } |
| 548 | |
| 549 | return ret; |
| 550 | } |
| 551 | |
| 552 | void set_xcom_running(bool running) { m_xcom_running = running; } |
| 553 | }; |
| 554 | |
| 555 | static mock_gcs_xcom_control *extern_xcom_control_if; |
| 556 | |
| 557 | void finalize_xcom() { extern_xcom_control_if->do_leave(); } |
| 558 | |
| 559 | class XComControlTest : public GcsBaseTest { |
| 560 | protected: |
| 561 | XComControlTest() = default; |
| 562 | |
| 563 | ~XComControlTest() override = default; |
| 564 | |
| 565 | void SetUp() override { |
| 566 | m_wait_called = false; |
| 567 | m_wait_called_mutex.init(PSI_NOT_INSTRUMENTED, nullptr); |
| 568 | m_wait_called_cond.init(PSI_NOT_INSTRUMENTED); |
| 569 | |
| 570 | mock_se = new NiceMock<mock_gcs_xcom_state_exchange_interface>(); |
| 571 | |
| 572 | mock_vce = new NiceMock<mock_gcs_xcom_view_change_control_interface>(); |
| 573 | |
| 574 | xcom_node_address = new Gcs_xcom_node_address("127.0.0.1:12345"); |
| 575 | peers.push_back(new Gcs_xcom_node_address("127.0.0.1:12345")); |
| 576 | peers.push_back(new Gcs_xcom_node_address("127.0.0.1:12346")); |
| 577 | peers.push_back(new Gcs_xcom_node_address("127.0.0.1:12347")); |
| 578 | |
| 579 | string group_name("only_group"); |
| 580 | group_id = new Gcs_group_identifier(group_name); |
| 581 | |
| 582 | mock_socket_util = new NiceMock<mock_my_xp_socket_util>(); |
| 583 | |
| 584 | gcs_engine.initialize(nullptr); |
| 585 | |
| 586 | xcom_group_mgm = new Gcs_xcom_group_management(&proxy, *group_id, mock_vce); |
| 587 | |
| 588 | mock_net_ops_for_interface = |
| 589 | std::make_unique<mock_gcs_network_provider_operations_interface>(); |
| 590 | mock_net_ops = mock_net_ops_for_interface.get(); |
| 591 | |
| 592 | xcom_control_if = new NiceMock<mock_gcs_xcom_control>( |
| 593 | xcom_node_address, peers, *group_id, &proxy, xcom_group_mgm, |
| 594 | &gcs_engine, mock_se, mock_vce, true, mock_socket_util, |
| 595 | std::move(mock_net_ops_for_interface)); |
| 596 | extern_xcom_control_if = xcom_control_if; |
| 597 | } |
| 598 | |
| 599 | void TearDown() override { |
| 600 | gcs_engine.finalize(finalize_xcom); |
| 601 | |
| 602 | delete mock_socket_util; |
| 603 | delete group_id; |
| 604 | delete xcom_group_mgm; |
| 605 | delete xcom_control_if; |
| 606 | delete mock_se; |
| 607 | delete mock_vce; |
| 608 | delete xcom_node_address; |
| 609 | std::vector<Gcs_xcom_node_address *>::iterator it; |
| 610 | for (it = peers.begin(); it != peers.end(); ++it) delete (*it); |
| 611 | peers.clear(); |
| 612 | |
| 613 | m_wait_called_mutex.destroy(); |
| 614 | m_wait_called_cond.destroy(); |
| 615 | } |
| 616 | |
| 617 | Gcs_xcom_node_address *xcom_node_address; |
| 618 | std::vector<Gcs_xcom_node_address *> peers; |
| 619 | |
| 620 | Gcs_group_identifier *group_id; |
| 621 | |
| 622 | NiceMock<mock_gcs_xcom_proxy> proxy; |
| 623 | NiceMock<mock_gcs_control_event_listener> mock_ev_listener; |
| 624 | |
| 625 | NiceMock<mock_gcs_xcom_control> *xcom_control_if; |
| 626 | NiceMock<mock_gcs_xcom_state_exchange_interface> *mock_se; |
| 627 | NiceMock<mock_gcs_xcom_view_change_control_interface> *mock_vce; |
| 628 | NiceMock<mock_my_xp_socket_util> *mock_socket_util; |
| 629 | mock_gcs_network_provider_operations_interface *mock_net_ops; |
| 630 | std::unique_ptr<mock_gcs_network_provider_operations_interface> |
| 631 | mock_net_ops_for_interface; |
| 632 | |
| 633 | bool m_wait_called; |
| 634 | My_xp_mutex_impl m_wait_called_mutex; |
| 635 | My_xp_cond_impl m_wait_called_cond; |
| 636 | |
| 637 | Gcs_xcom_engine gcs_engine; |
| 638 | |
| 639 | Gcs_xcom_group_management *xcom_group_mgm; |
| 640 | |
| 641 | public: |
| 642 | Gcs_view *create_fake_view() { |
| 643 | string address = xcom_node_address->get_member_address(); |
| 644 | Gcs_member_identifier local_member_information(address); |
| 645 | |
| 646 | std::vector<Gcs_member_identifier> members; |
| 647 | members.push_back(local_member_information); |
| 648 | |
| 649 | Gcs_xcom_view_identifier view_id(111111, 1); |
| 650 | std::vector<Gcs_member_identifier> leaving; |
| 651 | std::vector<Gcs_member_identifier> joined; |
| 652 | |
| 653 | Gcs_group_identifier fake_group_id(group_id->get_group_id()); |
| 654 | |
| 655 | Gcs_view *fake_view = |
| 656 | new Gcs_view(members, view_id, leaving, joined, fake_group_id); |
| 657 | return fake_view; |
| 658 | } |
| 659 | }; |
| 660 | |
| 661 | /** |
| 662 | * @brief This test exercices a basic leave-join scenario. |
| 663 | * |
| 664 | */ |
| 665 | TEST_F(XComControlTest, JoinLeaveTest) { |
| 666 | EXPECT_CALL(proxy, xcom_input_connect(_, _)).Times(1); |
| 667 | EXPECT_CALL(proxy, test_xcom_tcp_connection(_, _)).Times(1); |
| 668 | EXPECT_CALL(proxy, xcom_client_boot(_, _)).Times(1); |
| 669 | EXPECT_CALL(proxy, xcom_client_remove_node(_, _)).Times(1); |
| 670 | /*EXPECT_CALL(proxy, xcom_wait_for_xcom_comms_status_change(_)) |
| 671 | .Times(1) |
| 672 | .WillOnce(SetArgReferee<0>(GCS_OK));*/ |
| 673 | EXPECT_CALL(proxy, xcom_wait_ready()).Times(1); |
| 674 | EXPECT_CALL(proxy, xcom_wait_exit()).Times(1); |
| 675 | EXPECT_CALL(proxy, xcom_init(_)).Times(1); |
| 676 | EXPECT_CALL(proxy, xcom_exit()).Times(0); |
| 677 | |
| 678 | enum_gcs_error result = xcom_control_if->join(create_fake_view()); |
| 679 | ASSERT_EQ(GCS_OK, result); |
| 680 | ASSERT_TRUE(xcom_control_if->is_xcom_running()); |
| 681 | |
| 682 | result = xcom_control_if->leave(); |
| 683 | ASSERT_EQ(GCS_OK, result); |
| 684 | ASSERT_FALSE(xcom_control_if->is_xcom_running()); |
| 685 | } |
| 686 | |
| 687 | /** |
| 688 | * @brief This test exercises a join() after a successful join() |
| 689 | * |
| 690 | */ |
| 691 | TEST_F(XComControlTest, JoinTestFailedMultipleJoins) { |
| 692 | EXPECT_CALL(proxy, xcom_input_connect(_, _)).Times(1); |
| 693 | EXPECT_CALL(proxy, test_xcom_tcp_connection(_, _)).Times(1); |
| 694 | EXPECT_CALL(proxy, xcom_client_boot(_, _)).Times(1); |
| 695 | EXPECT_CALL(proxy, xcom_client_remove_node(_, _)).Times(1); |
| 696 | |
| 697 | EXPECT_CALL(proxy, xcom_wait_ready()).Times(1); |
| 698 | EXPECT_CALL(proxy, xcom_wait_exit()).Times(1); |
| 699 | EXPECT_CALL(proxy, xcom_init(_)).Times(1); |
| 700 | EXPECT_CALL(proxy, xcom_exit()).Times(0); |
| 701 | |
| 702 | enum_gcs_error result = xcom_control_if->join(create_fake_view()); |
| 703 | ASSERT_EQ(GCS_OK, result); |
| 704 | ASSERT_TRUE(xcom_control_if->is_xcom_running()); |
| 705 | |
| 706 | result = xcom_control_if->join(); |
| 707 | ASSERT_EQ(GCS_NOK, result); |
| 708 | ASSERT_TRUE(xcom_control_if->is_xcom_running()); |
| 709 | |
| 710 | result = xcom_control_if->leave(); |
| 711 | ASSERT_EQ(GCS_OK, result); |
| 712 | ASSERT_FALSE(xcom_control_if->is_xcom_running()); |
| 713 | } |
| 714 | |
| 715 | /** |
| 716 | * @brief This test exercises a failed |
| 717 | * |
| 718 | */ |
| 719 | TEST_F(XComControlTest, JoinTestFailedToStartComms) { |
| 720 | EXPECT_CALL(proxy, xcom_client_boot(_, _)).Times(0); |
| 721 | EXPECT_CALL(proxy, xcom_wait_ready()).Times(0); |
| 722 | EXPECT_CALL(proxy, xcom_init(_)).Times(1); |
| 723 | |
| 724 | /* |
| 725 | The join is forced to wait until the XCOM's tread is running. |
| 726 | In this test case though, we make the operation fail. |
| 727 | */ |
| 728 | EXPECT_CALL(proxy, xcom_wait_for_xcom_comms_status_change(_)) |
| 729 | .Times(1) |
| 730 | .WillOnce(SetArgReferee<0>(XCOM_COMMS_OTHER)); |
| 731 | |
| 732 | enum_gcs_error result = xcom_control_if->join(); |
| 733 | ASSERT_EQ(GCS_NOK, result); |
| 734 | ASSERT_FALSE(xcom_control_if->is_xcom_running()); |
| 735 | } |
| 736 | |
| 737 | /* GCS communicates with the local XCom instance via a queue. Whenever GCS |
| 738 | * pushes a command into this queue, GCS signals XCom that the queue has |
| 739 | * commands. |
| 740 | * When GCS sets up XCom, GCS connects to the XCom signalling mechanism. |
| 741 | * This tests ensures that GCS handles a failure to connect to the signalling |
| 742 | * mechanism by failing to join the group. */ |
| 743 | TEST_F(XComControlTest, JoinTestFailedToConnectToXComQueueSignallingMechanism) { |
| 744 | /* We fail to connect to XCom's local server. */ |
| 745 | EXPECT_CALL(proxy, xcom_input_connect(_, _)).Times(1).WillOnce(Return(false)); |
| 746 | EXPECT_CALL(proxy, xcom_client_boot(_, _)).Times(0); |
| 747 | EXPECT_CALL(proxy, xcom_wait_ready()).Times(0); |
| 748 | EXPECT_CALL(proxy, xcom_init(_)).Times(1); |
| 749 | EXPECT_CALL(proxy, xcom_exit()).Times(1); |
| 750 | |
| 751 | enum_gcs_error result = xcom_control_if->join(); |
| 752 | ASSERT_EQ(GCS_NOK, result); |
| 753 | ASSERT_FALSE(xcom_control_if->is_xcom_running()); |
| 754 | } |
| 755 | |
| 756 | /* This tests ensures that GCS handles a failure to send the boot command to |
| 757 | * XCom by failing to join the group. */ |
| 758 | TEST_F(XComControlTest, JoinTestFailedToSendBootToXCom) { |
| 759 | EXPECT_CALL(proxy, xcom_input_connect(_, _)).Times(1); |
| 760 | /* We fail to send the boot command. */ |
| 761 | EXPECT_CALL(proxy, xcom_client_boot(_, _)).Times(1).WillOnce(Return(false)); |
| 762 | EXPECT_CALL(proxy, xcom_wait_ready()).Times(0); |
| 763 | EXPECT_CALL(proxy, xcom_init(_)).Times(1); |
| 764 | EXPECT_CALL(proxy, xcom_exit()).Times(1); |
| 765 | |
| 766 | enum_gcs_error result = xcom_control_if->join(); |
| 767 | ASSERT_EQ(GCS_NOK, result); |
| 768 | ASSERT_FALSE(xcom_control_if->is_xcom_running()); |
| 769 | } |
| 770 | |
| 771 | TEST_F(XComControlTest, JoinTestFailedToStartXCom) { |
| 772 | EXPECT_CALL(proxy, xcom_input_connect(_, _)).Times(1); |
| 773 | EXPECT_CALL(proxy, test_xcom_tcp_connection(_, _)).Times(1); |
| 774 | EXPECT_CALL(proxy, xcom_client_boot(_, _)).Times(1); |
| 775 | EXPECT_CALL(proxy, xcom_wait_ready()).Times(1).WillOnce(Return(GCS_NOK)); |
| 776 | EXPECT_CALL(proxy, xcom_init(_)).Times(1); |
| 777 | EXPECT_CALL(proxy, xcom_exit()).Times(1); |
| 778 | |
| 779 | enum_gcs_error result = xcom_control_if->join(); |
| 780 | |
| 781 | ASSERT_EQ(GCS_NOK, result); |
| 782 | } |
| 783 | |
| 784 | TEST_F(XComControlTest, JoinTestTimeoutStartingXCom) { |
| 785 | Gcs_xcom_proxy *my_proxy = new Gcs_xcom_proxy_impl(); |
| 786 | |
| 787 | EXPECT_CALL(proxy, xcom_input_connect(_, _)).Times(1); |
| 788 | EXPECT_CALL(proxy, test_xcom_tcp_connection(_, _)).Times(1); |
| 789 | EXPECT_CALL(proxy, xcom_client_boot(_, _)).Times(1); |
| 790 | EXPECT_CALL(proxy, xcom_wait_ready()) |
| 791 | .Times(1) |
| 792 | .WillOnce(Invoke(my_proxy, &Gcs_xcom_proxy::xcom_wait_ready)); |
| 793 | EXPECT_CALL(proxy, xcom_init(_)).Times(1); |
| 794 | EXPECT_CALL(proxy, xcom_exit()).Times(1); |
| 795 | |
| 796 | enum_gcs_error result = xcom_control_if->join(); |
| 797 | |
| 798 | ASSERT_EQ(GCS_NOK, result); |
| 799 | |
| 800 | delete my_proxy; |
| 801 | } |
| 802 | |
| 803 | TEST_F(XComControlTest, JoinTestWithoutBootNorPeers) { |
| 804 | EXPECT_CALL(proxy, new_node_address_uuid(_, _, _)).Times(0); |
| 805 | EXPECT_CALL(proxy, xcom_input_connect(_, _)).Times(0); |
| 806 | EXPECT_CALL(proxy, xcom_client_boot(_, _)).Times(0); |
| 807 | EXPECT_CALL(proxy, xcom_wait_ready()).Times(0); |
| 808 | EXPECT_CALL(proxy, xcom_init(_)).Times(0); |
| 809 | EXPECT_CALL(proxy, xcom_exit()).Times(0); |
| 810 | EXPECT_CALL(proxy, delete_node_address(_, _)).Times(0); |
| 811 | |
| 812 | xcom_control_if->set_boot_node(false); |
| 813 | std::vector<Gcs_xcom_node_address *> peers; |
| 814 | xcom_control_if->set_peer_nodes(peers); |
| 815 | |
| 816 | enum_gcs_error result = xcom_control_if->join(); |
| 817 | ASSERT_EQ(GCS_NOK, result); |
| 818 | ASSERT_FALSE(xcom_control_if->is_xcom_running()); |
| 819 | } |
| 820 | |
| 821 | TEST_F(XComControlTest, JoinTestSkipOwnNodeAndCycleThroughPeerNodes) { |
| 822 | connection_descriptor *con = |
| 823 | (connection_descriptor *)malloc(sizeof(connection_descriptor)); |
| 824 | con->fd = 0; |
| 825 | |
| 826 | /*connection_descriptor *failed_con = |
| 827 | (connection_descriptor *)malloc(sizeof(connection_descriptor *)); |
| 828 | failed_con->fd = -1;*/ |
| 829 | |
| 830 | auto create_failed_con_lambda = [](std::string a, xcom_port b) { |
| 831 | (void)a; |
| 832 | (void)b; |
| 833 | |
| 834 | connection_descriptor *failed_con = |
| 835 | (connection_descriptor *)malloc(sizeof(connection_descriptor)); |
| 836 | failed_con->fd = -1; |
| 837 | |
| 838 | return failed_con; |
| 839 | }; |
| 840 | |
| 841 | auto create_good_con_lambda = [](std::string a, xcom_port b) { |
| 842 | (void)a; |
| 843 | (void)b; |
| 844 | |
| 845 | connection_descriptor *failed_con = |
| 846 | (connection_descriptor *)malloc(sizeof(connection_descriptor)); |
| 847 | failed_con->fd = 0; |
| 848 | |
| 849 | return failed_con; |
| 850 | }; |
| 851 | |
| 852 | EXPECT_CALL(proxy, xcom_input_connect(_, _)).Times(1); |
| 853 | EXPECT_CALL(proxy, test_xcom_tcp_connection(_, _)).Times(1); |
| 854 | EXPECT_CALL(proxy, xcom_client_boot(_, _)).Times(0); |
| 855 | EXPECT_CALL(proxy, xcom_wait_ready()).Times(1); |
| 856 | EXPECT_CALL(proxy, xcom_init(_)).Times(1); |
| 857 | EXPECT_CALL(proxy, xcom_exit()).Times(0); |
| 858 | // Fail to connect to the peer every time. |
| 859 | EXPECT_CALL(proxy, xcom_client_open_connection(Eq("127.0.0.1"), Eq(12346))) |
| 860 | .Times(3) |
| 861 | .WillRepeatedly(create_failed_con_lambda); |
| 862 | |
| 863 | /* |
| 864 | Fail to connect on the first attempt. |
| 865 | Connect on the second attempt, but fail to disable the Nagle algorithm. |
| 866 | Succeed on the third attempt. |
| 867 | */ |
| 868 | EXPECT_CALL(proxy, xcom_client_open_connection(Eq("127.0.0.1"), Eq(12347))) |
| 869 | .Times(3) |
| 870 | .WillOnce(create_failed_con_lambda) |
| 871 | .WillRepeatedly(create_good_con_lambda); |
| 872 | EXPECT_CALL(*mock_socket_util, disable_nagle_in_socket(_)) |
| 873 | .Times(2) |
| 874 | .WillOnce(Return(-1)) |
| 875 | .WillOnce(Return(0)); |
| 876 | |
| 877 | EXPECT_CALL(proxy, xcom_client_add_node(_, _, _)) |
| 878 | .Times(1) |
| 879 | .WillOnce(Return(1)); |
| 880 | EXPECT_CALL(proxy, xcom_client_remove_node(_, _)).Times(1); |
| 881 | EXPECT_CALL(proxy, xcom_client_close_connection(_)) |
| 882 | .Times(2) |
| 883 | .WillRepeatedly(Return(0)); |
| 884 | |
| 885 | xcom_control_if->set_boot_node(false); |
| 886 | enum_gcs_error result = xcom_control_if->join(create_fake_view()); |
| 887 | ASSERT_EQ(GCS_OK, result); |
| 888 | ASSERT_TRUE(xcom_control_if->is_xcom_running()); |
| 889 | |
| 890 | result = xcom_control_if->leave(); |
| 891 | ASSERT_EQ(GCS_OK, result); |
| 892 | ASSERT_FALSE(xcom_control_if->is_xcom_running()); |
| 893 | } |
| 894 | |
| 895 | TEST_F(XComControlTest, JoinTestAllPeersUnavailable) { |
| 896 | auto create_failed_con_lambda = [](std::string a, xcom_port b) { |
| 897 | (void)a; |
| 898 | (void)b; |
| 899 | |
| 900 | connection_descriptor *failed_con = |
| 901 | (connection_descriptor *)malloc(sizeof(connection_descriptor)); |
| 902 | failed_con->fd = -1; |
| 903 | |
| 904 | return failed_con; |
| 905 | }; |
| 906 | |
| 907 | EXPECT_CALL(proxy, xcom_input_connect(_, _)).Times(1); |
| 908 | EXPECT_CALL(proxy, test_xcom_tcp_connection(_, _)).Times(1); |
| 909 | EXPECT_CALL(proxy, xcom_client_boot(_, _)).Times(0); |
| 910 | EXPECT_CALL(proxy, xcom_wait_ready()).Times(0); |
| 911 | EXPECT_CALL(proxy, xcom_init(_)).Times(1); |
| 912 | EXPECT_CALL(proxy, xcom_exit()).Times(1); |
| 913 | |
| 914 | /* |
| 915 | Fail to connect to all peers every time. |
| 916 | peers.size() - 1 because we skip our own address. |
| 917 | */ |
| 918 | EXPECT_CALL(proxy, xcom_client_open_connection(_, _)) |
| 919 | .Times((peers.size() - 1) * Gcs_xcom_control::CONNECTION_ATTEMPTS) |
| 920 | .WillRepeatedly(create_failed_con_lambda); |
| 921 | EXPECT_CALL(*mock_socket_util, disable_nagle_in_socket(_)).Times(0); |
| 922 | EXPECT_CALL(proxy, xcom_client_add_node(_, _, _)).Times(0); |
| 923 | EXPECT_CALL(proxy, xcom_client_remove_node(_, _, _)).Times(0); |
| 924 | EXPECT_CALL(proxy, xcom_client_close_connection(_)).Times(0); |
| 925 | |
| 926 | xcom_control_if->set_boot_node(false); |
| 927 | enum_gcs_error result = xcom_control_if->join(); |
| 928 | ASSERT_EQ(GCS_NOK, result); |
| 929 | } |
| 930 | |
| 931 | TEST_F(XComControlTest, LeaveTestWithoutJoin) { |
| 932 | EXPECT_CALL(proxy, new_node_address_uuid(_, _, _)).Times(0); |
| 933 | EXPECT_CALL(proxy, xcom_input_connect(_, _)).Times(0); |
| 934 | EXPECT_CALL(proxy, xcom_client_boot(_, _)).Times(0); |
| 935 | EXPECT_CALL(proxy, xcom_wait_ready()).Times(0); |
| 936 | EXPECT_CALL(proxy, xcom_init(_)).Times(0); |
| 937 | EXPECT_CALL(proxy, xcom_exit()).Times(0); |
| 938 | EXPECT_CALL(proxy, xcom_client_remove_node(_, _)).Times(0); |
| 939 | |
| 940 | enum_gcs_error result = xcom_control_if->leave(); |
| 941 | ASSERT_EQ(GCS_NOK, result); |
| 942 | ASSERT_FALSE(xcom_control_if->is_xcom_running()); |
| 943 | } |
| 944 | |
| 945 | TEST_F(XComControlTest, LeaveTestMultiMember) { |
| 946 | EXPECT_CALL(proxy, xcom_input_connect(_, _)).Times(1); |
| 947 | EXPECT_CALL(proxy, test_xcom_tcp_connection(_, _)).Times(1); |
| 948 | EXPECT_CALL(proxy, xcom_client_boot(_, _)).Times(1); |
| 949 | EXPECT_CALL(proxy, xcom_client_remove_node(_, _)).Times(1); |
| 950 | EXPECT_CALL(proxy, xcom_wait_ready()).Times(1); |
| 951 | EXPECT_CALL(proxy, xcom_init(_)).Times(1); |
| 952 | EXPECT_CALL(proxy, xcom_exit()).Times(0); |
| 953 | |
| 954 | string member_id_1 = xcom_node_address->get_member_address(); |
| 955 | Gcs_member_identifier local_member_information_1(member_id_1); |
| 956 | |
| 957 | string member_id_2("127.0.0.1:12343"); |
| 958 | Gcs_member_identifier local_member_information_2(member_id_2); |
| 959 | |
| 960 | std::vector<Gcs_member_identifier> members; |
| 961 | members.push_back(local_member_information_1); |
| 962 | members.push_back(local_member_information_2); |
| 963 | |
| 964 | Gcs_xcom_view_identifier view_id(111111, 1); |
| 965 | std::vector<Gcs_member_identifier> leaving; |
| 966 | std::vector<Gcs_member_identifier> joined; |
| 967 | |
| 968 | Gcs_group_identifier fake_group_id(group_id->get_group_id()); |
| 969 | |
| 970 | Gcs_view *fake_old_view = |
| 971 | new Gcs_view(members, view_id, leaving, joined, fake_group_id); |
| 972 | |
| 973 | enum_gcs_error result = xcom_control_if->join(fake_old_view); |
| 974 | ASSERT_EQ(GCS_OK, result); |
| 975 | |
| 976 | result = xcom_control_if->leave(); |
| 977 | ASSERT_EQ(GCS_OK, result); |
| 978 | } |
| 979 | |
| 980 | TEST_F(XComControlTest, GetLocalInformationTest) { |
| 981 | Gcs_member_identifier result = xcom_control_if->get_local_member_identifier(); |
| 982 | std::string address = xcom_node_address->get_member_address(); |
| 983 | ASSERT_EQ(address, result.get_member_id()); |
| 984 | } |
| 985 | |
| 986 | TEST_F(XComControlTest, SetEventListenerTest) { |
| 987 | mock_gcs_control_event_listener control_listener; |
| 988 | |
| 989 | int reference = xcom_control_if->add_event_listener(control_listener); |
| 990 | |
| 991 | ASSERT_NE(0, reference); |
| 992 | ASSERT_EQ((long unsigned int)1, |
| 993 | xcom_control_if->get_event_listeners()->count(reference)); |
| 994 | ASSERT_EQ((long unsigned int)1, |
| 995 | xcom_control_if->get_event_listeners()->size()); |
| 996 | } |
| 997 | |
| 998 | TEST_F(XComControlTest, SetEventListenersTest) { |
| 999 | mock_gcs_control_event_listener control_listener; |
| 1000 | mock_gcs_control_event_listener another_control_listener; |
| 1001 | |
| 1002 | int reference = xcom_control_if->add_event_listener(control_listener); |
| 1003 | int another_reference = |
| 1004 | xcom_control_if->add_event_listener(another_control_listener); |
| 1005 | |
| 1006 | ASSERT_NE(0, reference); |
| 1007 | ASSERT_NE(0, another_reference); |
| 1008 | ASSERT_EQ((long unsigned int)1, |
| 1009 | xcom_control_if->get_event_listeners()->count(reference)); |
| 1010 | ASSERT_EQ((long unsigned int)1, |
| 1011 | xcom_control_if->get_event_listeners()->count(another_reference)); |
| 1012 | ASSERT_EQ((long unsigned int)2, |
| 1013 | xcom_control_if->get_event_listeners()->size()); |
| 1014 | ASSERT_NE(reference, another_reference); |
| 1015 | } |
| 1016 | |
| 1017 | TEST_F(XComControlTest, RemoveEventListenerTest) { |
| 1018 | mock_gcs_control_event_listener control_listener; |
| 1019 | mock_gcs_control_event_listener another_control_listener; |
| 1020 | |
| 1021 | int reference = xcom_control_if->add_event_listener(control_listener); |
| 1022 | int another_reference = |
| 1023 | xcom_control_if->add_event_listener(another_control_listener); |
| 1024 | |
| 1025 | xcom_control_if->remove_event_listener(reference); |
| 1026 | |
| 1027 | ASSERT_NE(0, reference); |
| 1028 | ASSERT_NE(0, another_reference); |
| 1029 | ASSERT_EQ((long unsigned int)0, |
| 1030 | xcom_control_if->get_event_listeners()->count(reference)); |
| 1031 | ASSERT_EQ((long unsigned int)1, |
| 1032 | xcom_control_if->get_event_listeners()->count(another_reference)); |
| 1033 | ASSERT_EQ((long unsigned int)1, |
| 1034 | xcom_control_if->get_event_listeners()->size()); |
| 1035 | ASSERT_NE(reference, another_reference); |
| 1036 | } |
| 1037 | |
| 1038 | Gcs_message *create_state_exchange_msg(Gcs_member_identifier &member_id, |
| 1039 | Gcs_group_identifier &group_id, |
| 1040 | Stored_States *out_stored_states) { |
| 1041 | Gcs_message_data *dummy = new Gcs_message_data(0, 3); |
| 1042 | uchar to_append = (uchar)1; |
| 1043 | |
| 1044 | dummy->append_to_payload(&to_append, 1); |
| 1045 | dummy->append_to_payload(&to_append, 1); |
| 1046 | dummy->append_to_payload(&to_append, 1); |
| 1047 | |
| 1048 | const Gcs_xcom_view_identifier view_id(999999, 1); |
| 1049 | synode_no configuration_id = null_synode; |
| 1050 | Gcs_xcom_synode_set snapshot; |
| 1051 | Xcom_member_state *member_state = new Xcom_member_state( |
| 1052 | view_id, configuration_id, Gcs_protocol_version::HIGHEST_KNOWN, snapshot, |
| 1053 | nullptr, 0); |
| 1054 | |
| 1055 | (*out_stored_states)[member_id] = member_state; |
| 1056 | |
| 1057 | uint64_t buffer_len = member_state->get_encode_header_size() + |
| 1058 | dummy->get_encode_size() + |
| 1059 | member_state->get_encode_snapshot_size(); |
| 1060 | uchar *buffer = static_cast<uchar *>(malloc(buffer_len * sizeof(uchar))); |
| 1061 | uchar *slider = buffer; |
| 1062 | |
| 1063 | auto header_len = member_state->get_encode_header_size(); |
| 1064 | member_state->encode_header(slider, &header_len); |
| 1065 | slider += header_len; |
| 1066 | |
| 1067 | auto payload_len = dummy->get_encode_size(); |
| 1068 | dummy->encode(slider, &payload_len); |
| 1069 | slider += payload_len; |
| 1070 | |
| 1071 | auto snapshot_len = member_state->get_encode_snapshot_size(); |
| 1072 | member_state->encode_snapshot(slider, &snapshot_len); |
| 1073 | slider += snapshot_len; |
| 1074 | |
| 1075 | Gcs_message *msg = |
| 1076 | new Gcs_message(member_id, group_id, new Gcs_message_data(0, buffer_len)); |
| 1077 | msg->get_message_data().append_to_payload(buffer, buffer_len); |
| 1078 | |
| 1079 | delete dummy; |
| 1080 | free(buffer); |
| 1081 | |
| 1082 | return msg; |
| 1083 | } |
| 1084 | |
| 1085 | TEST_F(XComControlTest, ViewChangedJoiningTest) { |
| 1086 | Gcs_xcom_uuid uuid_1 = Gcs_xcom_uuid::create_uuid(); |
| 1087 | blob blob_1 = {{0, static_cast<char *>(malloc(uuid_1.actual_value.size()))}}; |
| 1088 | uuid_1.encode(reinterpret_cast<uchar **>(&blob_1.data.data_val), |
| 1089 | &blob_1.data.data_len); |
| 1090 | |
| 1091 | Gcs_xcom_uuid uuid_2 = Gcs_xcom_uuid::create_uuid(); |
| 1092 | blob blob_2 = {{0, static_cast<char *>(malloc(uuid_2.actual_value.size()))}}; |
| 1093 | uuid_2.encode(reinterpret_cast<uchar **>(&blob_2.data.data_val), |
| 1094 | &blob_2.data.data_len); |
| 1095 | |
| 1096 | node_address node_addrs[2] = {{const_cast<char *>("127.0.0.1:12345"), |
| 1097 | blob_1, |
| 1098 | {x_1_0, x_1_2}, |
| 1099 | P_PROP | P_ACC | P_LEARN}, |
| 1100 | {const_cast<char *>("127.0.0.1:12346"), |
| 1101 | blob_2, |
| 1102 | {x_1_0, x_1_2}, |
| 1103 | P_PROP | P_ACC | P_LEARN}}; |
| 1104 | |
| 1105 | // Common unit test data |
| 1106 | Gcs_xcom_view_identifier *view_id = new Gcs_xcom_view_identifier(999999, 27); |
| 1107 | |
| 1108 | string member_addr_1(node_addrs[0].address); |
| 1109 | Gcs_member_identifier *node1_member_id = |
| 1110 | new Gcs_member_identifier(member_addr_1); |
| 1111 | |
| 1112 | string member_addr_2(node_addrs[1].address); |
| 1113 | Gcs_member_identifier *node2_member_id = |
| 1114 | new Gcs_member_identifier(member_addr_2); |
| 1115 | |
| 1116 | std::set<Gcs_member_identifier *> *total_set = |
| 1117 | new std::set<Gcs_member_identifier *>(); |
| 1118 | std::set<Gcs_member_identifier *> *join_set = |
| 1119 | new std::set<Gcs_member_identifier *>(); |
| 1120 | std::set<Gcs_member_identifier *> *left_set = |
| 1121 | new std::set<Gcs_member_identifier *>(); |
| 1122 | |
| 1123 | total_set->insert(node1_member_id); |
| 1124 | total_set->insert(node2_member_id); |
| 1125 | |
| 1126 | join_set->insert(node2_member_id); |
| 1127 | |
| 1128 | site_def *site_config = new_site_def(); |
| 1129 | init_site_def(2, node_addrs, site_config); |
| 1130 | site_config->x_proto = static_cast<xcom_proto>(1); |
| 1131 | site_config->nodeno = 1; |
| 1132 | |
| 1133 | node_set nodes; |
| 1134 | alloc_node_set(&nodes, 2); |
| 1135 | set_node_set(&nodes); |
| 1136 | |
| 1137 | Stored_States stored_states; |
| 1138 | Gcs_message *state_message1 = |
| 1139 | create_state_exchange_msg(*node1_member_id, *group_id, &stored_states); |
| 1140 | Gcs_message *state_message2 = |
| 1141 | create_state_exchange_msg(*node2_member_id, *group_id, &stored_states); |
| 1142 | |
| 1143 | EXPECT_CALL(proxy, find_site_def(_)).Times(0); |
| 1144 | EXPECT_CALL(mock_ev_listener, on_view_changed(_, _)).Times(1); |
| 1145 | EXPECT_CALL(*mock_se, state_exchange(_, _, _, _, _, _, _, _, _)).Times(1); |
| 1146 | EXPECT_CALL(*mock_se, process_member_state(_, _, _, _)).Times(2); |
| 1147 | EXPECT_CALL(*mock_se, process_recovery_state()) |
| 1148 | .Times(1) |
| 1149 | .WillOnce(Return(true)); |
| 1150 | EXPECT_CALL(*mock_se, get_new_view_id()).Times(1).WillOnce(Return(view_id)); |
| 1151 | EXPECT_CALL(*mock_se, get_joined()).Times(1).WillOnce(Return(join_set)); |
| 1152 | EXPECT_CALL(*mock_se, get_left()).Times(1).WillOnce(Return(left_set)); |
| 1153 | EXPECT_CALL(*mock_se, get_total()).Times(1).WillOnce(Return(total_set)); |
| 1154 | EXPECT_CALL(*mock_vce, start_view_exchange()).Times(1); |
| 1155 | EXPECT_CALL(*mock_vce, end_view_exchange()).Times(1); |
| 1156 | EXPECT_CALL(*mock_se, reset()).Times(0); |
| 1157 | EXPECT_CALL(*mock_se, reset_with_flush()).Times(1); |
| 1158 | EXPECT_CALL(*mock_se, end()).Times(1); |
| 1159 | EXPECT_CALL(*mock_se, get_member_states()) |
| 1160 | .Times(1) |
| 1161 | .WillOnce(Return(&stored_states)); |
| 1162 | EXPECT_CALL(*mock_vce, is_view_changing()).WillRepeatedly(Return(true)); |
| 1163 | |
| 1164 | xcom_control_if->add_event_listener(mock_ev_listener); |
| 1165 | |
| 1166 | /* |
| 1167 | Initially the node does nto belong to a group and has not |
| 1168 | installed any view. |
| 1169 | */ |
| 1170 | ASSERT_FALSE(xcom_control_if->belongs_to_group()); |
| 1171 | ASSERT_TRUE(xcom_control_if->get_current_view() == nullptr); |
| 1172 | |
| 1173 | synode_no message_id; |
| 1174 | message_id.group_id = Gcs_xcom_utils::build_xcom_group_id(*this->group_id); |
| 1175 | message_id.msgno = 4; |
| 1176 | message_id.node = 0; |
| 1177 | |
| 1178 | Gcs_xcom_nodes *xcom_nodes = new Gcs_xcom_nodes(site_config, nodes); |
| 1179 | |
| 1180 | /* |
| 1181 | Process a global view message delivered by XCOM but say |
| 1182 | that a view with such information was never installed. |
| 1183 | |
| 1184 | Note that nodes are freed by the caller. |
| 1185 | */ |
| 1186 | bool view_accepted = !xcom_control_if->xcom_receive_global_view( |
| 1187 | null_synode, message_id, xcom_nodes, false, null_synode); |
| 1188 | ASSERT_TRUE(view_accepted); |
| 1189 | |
| 1190 | /* |
| 1191 | Process a global view message delivered by XCOM but say |
| 1192 | that a view with such information was already installed. |
| 1193 | |
| 1194 | Note that nodes are freed by the caller. |
| 1195 | */ |
| 1196 | view_accepted = !xcom_control_if->xcom_receive_global_view( |
| 1197 | null_synode, message_id, xcom_nodes, true, null_synode); |
| 1198 | ASSERT_FALSE(view_accepted); |
| 1199 | |
| 1200 | /* |
| 1201 | Process the state exchange messages so that the new |
| 1202 | view can be installed. |
| 1203 | */ |
| 1204 | xcom_control_if->process_control_message( |
| 1205 | state_message1, Gcs_protocol_version::V1, Gcs_protocol_version::V1); |
| 1206 | xcom_control_if->process_control_message( |
| 1207 | state_message2, Gcs_protocol_version::V1, Gcs_protocol_version::V1); |
| 1208 | |
| 1209 | Gcs_view *current_view = xcom_control_if->get_current_view(); |
| 1210 | ASSERT_TRUE(xcom_control_if->belongs_to_group()); |
| 1211 | ASSERT_TRUE(current_view != nullptr); |
| 1212 | |
| 1213 | const Gcs_xcom_view_identifier ¤t_view_id = |
| 1214 | down_cast<const Gcs_xcom_view_identifier &>(current_view->get_view_id()); |
| 1215 | ASSERT_EQ(typeid(Gcs_xcom_view_identifier).name(), |
| 1216 | typeid(current_view_id).name()); |
| 1217 | |
| 1218 | Gcs_xcom_view_identifier *xcom_view_id = |
| 1219 | const_cast<Gcs_xcom_view_identifier *>(¤t_view_id); |
| 1220 | |
| 1221 | ASSERT_EQ(view_id->get_fixed_part(), xcom_view_id->get_fixed_part()); |
| 1222 | ASSERT_EQ(view_id->get_monotonic_part() + 1, |
| 1223 | xcom_view_id->get_monotonic_part()); |
| 1224 | ASSERT_EQ((size_t)2, current_view->get_members().size()); |
| 1225 | ASSERT_EQ((size_t)1, current_view->get_joined_members().size()); |
| 1226 | |
| 1227 | delete view_id; |
| 1228 | delete node2_member_id; |
| 1229 | delete node1_member_id; |
| 1230 | delete total_set; |
| 1231 | delete join_set; |
| 1232 | delete left_set; |
| 1233 | delete current_view; |
| 1234 | delete xcom_nodes; |
| 1235 | mock_vce->set_current_view(nullptr); |
| 1236 | |
| 1237 | // TODO: replace the following with free_site_def(site_config) once |
| 1238 | // the header file in site_def.h is fixed |
| 1239 | homemade_free_site_def(2, site_config, node_addrs); |
| 1240 | free_node_set(&nodes); |
| 1241 | |
| 1242 | // reclaim Xcom_member_states |
| 1243 | Stored_States::iterator it; |
| 1244 | for (it = stored_states.begin(); it != stored_states.end(); it++) |
| 1245 | delete (*it).second; |
| 1246 | } |
| 1247 | |
| 1248 | TEST_F(XComControlTest, FailedNodeRemovalTest) { |
| 1249 | // Setting Expectations and Return Values |
| 1250 | // First the node joins the group. |
| 1251 | EXPECT_CALL(proxy, xcom_input_connect(_, _)).Times(1); |
| 1252 | EXPECT_CALL(proxy, test_xcom_tcp_connection(_, _)).Times(1); |
| 1253 | EXPECT_CALL(proxy, xcom_client_boot(_, _)).Times(1); |
| 1254 | EXPECT_CALL(proxy, xcom_wait_ready()).Times(1); |
| 1255 | EXPECT_CALL(proxy, xcom_init(_)).Times(1); |
| 1256 | EXPECT_CALL(proxy, xcom_exit()).Times(0); |
| 1257 | EXPECT_CALL(proxy, delete_node_address(_, _)).Times(4); |
| 1258 | EXPECT_CALL(mock_ev_listener, on_view_changed(_, _)).Times(1); |
| 1259 | EXPECT_CALL(proxy, xcom_client_remove_node(_, _)).Times(3); |
| 1260 | |
| 1261 | // Get suspicions manager and enable majority |
| 1262 | Gcs_suspicions_manager *mgr = xcom_control_if->get_suspicions_manager(); |
| 1263 | mgr->set_suspicions_processing_period(15u); |
| 1264 | mgr->set_non_member_expel_timeout_seconds(60ul); |
| 1265 | mgr->set_member_expel_timeout_seconds(0ul); |
| 1266 | |
| 1267 | Gcs_xcom_uuid uuid_1 = Gcs_xcom_uuid::create_uuid(); |
| 1268 | blob blob_1 = {{0, static_cast<char *>(malloc(uuid_1.actual_value.size()))}}; |
| 1269 | uuid_1.encode(reinterpret_cast<uchar **>(&blob_1.data.data_val), |
| 1270 | &blob_1.data.data_len); |
| 1271 | |
| 1272 | Gcs_xcom_uuid uuid_2 = Gcs_xcom_uuid::create_uuid(); |
| 1273 | blob blob_2 = {{0, static_cast<char *>(malloc(uuid_2.actual_value.size()))}}; |
| 1274 | uuid_2.encode(reinterpret_cast<uchar **>(&blob_2.data.data_val), |
| 1275 | &blob_2.data.data_len); |
| 1276 | |
| 1277 | Gcs_xcom_uuid uuid_3 = Gcs_xcom_uuid::create_uuid(); |
| 1278 | blob blob_3 = {{0, static_cast<char *>(malloc(uuid_3.actual_value.size()))}}; |
| 1279 | uuid_3.encode(reinterpret_cast<uchar **>(&blob_3.data.data_val), |
| 1280 | &blob_3.data.data_len); |
| 1281 | |
| 1282 | node_address node_addrs[3] = {{const_cast<char *>("127.0.0.1:12345"), |
| 1283 | blob_1, |
| 1284 | {x_1_0, x_1_2}, |
| 1285 | P_PROP | P_ACC | P_LEARN}, |
| 1286 | {const_cast<char *>("127.0.0.1:12343"), |
| 1287 | blob_2, |
| 1288 | {x_1_0, x_1_2}, |
| 1289 | P_PROP | P_ACC | P_LEARN}, |
| 1290 | {const_cast<char *>("127.0.0.1:12341"), |
| 1291 | blob_3, |
| 1292 | {x_1_0, x_1_2}, |
| 1293 | P_PROP | P_ACC | P_LEARN}}; |
| 1294 | |
| 1295 | site_def *site_config = new_site_def(); |
| 1296 | init_site_def(3, node_addrs, site_config); |
| 1297 | site_config->x_proto = static_cast<xcom_proto>(1); |
| 1298 | site_config->nodeno = 0; |
| 1299 | |
| 1300 | node_set nodes; |
| 1301 | alloc_node_set(&nodes, 3); |
| 1302 | set_node_set(&nodes); |
| 1303 | nodes.node_set_val[1] = 0; |
| 1304 | |
| 1305 | EXPECT_CALL(proxy, find_site_def(_)).Times(0); |
| 1306 | |
| 1307 | // Setting fake values |
| 1308 | string member_id_1("127.0.0.1:12345"); |
| 1309 | Gcs_member_identifier local_member_information_1(member_id_1); |
| 1310 | |
| 1311 | string member_id_2("127.0.0.1:12343"); |
| 1312 | Gcs_member_identifier local_member_information_2(member_id_2); |
| 1313 | |
| 1314 | string member_id_3("127.0.0.1:12341"); |
| 1315 | Gcs_member_identifier local_member_information_3(member_id_3); |
| 1316 | |
| 1317 | std::vector<Gcs_member_identifier> members; |
| 1318 | members.push_back(local_member_information_1); |
| 1319 | members.push_back(local_member_information_2); |
| 1320 | members.push_back(local_member_information_3); |
| 1321 | |
| 1322 | Gcs_xcom_view_identifier view_id(111111, 1); |
| 1323 | std::vector<Gcs_member_identifier> leaving; |
| 1324 | std::vector<Gcs_member_identifier> joined; |
| 1325 | |
| 1326 | Gcs_group_identifier fake_group_id(group_id->get_group_id()); |
| 1327 | |
| 1328 | Gcs_view *fake_old_view = |
| 1329 | new Gcs_view(members, view_id, leaving, joined, fake_group_id); |
| 1330 | |
| 1331 | // registering the listener |
| 1332 | int listener_handle = xcom_control_if->add_event_listener(mock_ev_listener); |
| 1333 | |
| 1334 | // Test |
| 1335 | enum_gcs_error result = xcom_control_if->join(fake_old_view); |
| 1336 | ASSERT_EQ(GCS_OK, result); |
| 1337 | |
| 1338 | synode_no message_id; |
| 1339 | message_id.group_id = Gcs_xcom_utils::build_xcom_group_id(*this->group_id); |
| 1340 | message_id.msgno = 3; |
| 1341 | message_id.node = 0; |
| 1342 | |
| 1343 | Gcs_xcom_nodes *xcom_nodes = new Gcs_xcom_nodes(site_config, nodes); |
| 1344 | |
| 1345 | bool view_accepted = xcom_control_if->xcom_receive_global_view( |
| 1346 | null_synode, message_id, xcom_nodes, false, null_synode); |
| 1347 | ASSERT_TRUE(view_accepted); |
| 1348 | |
| 1349 | // Run thread to remove failed node |
| 1350 | mgr->run_process_suspicions(true); |
| 1351 | |
| 1352 | // Check if the manager kept the majority enabled |
| 1353 | ASSERT_EQ(mgr->has_majority(), true); |
| 1354 | |
| 1355 | // Process a local view. |
| 1356 | // Define nodes and emulate the failure of the second node. |
| 1357 | std::vector<Gcs_member_identifier> unreachable; |
| 1358 | unreachable.push_back(local_member_information_2); |
| 1359 | |
| 1360 | EXPECT_CALL(mock_ev_listener, on_suspicions(members, unreachable)).Times(1); |
| 1361 | xcom_control_if->xcom_receive_local_view(null_synode, xcom_nodes, |
| 1362 | null_synode); |
| 1363 | |
| 1364 | // Run thread to remove failed node |
| 1365 | mgr->run_process_suspicions(true); |
| 1366 | |
| 1367 | // Check if the manager has kept the majority |
| 1368 | ASSERT_EQ(mgr->has_majority(), true); |
| 1369 | |
| 1370 | result = xcom_control_if->leave(); |
| 1371 | ASSERT_EQ(GCS_OK, result); |
| 1372 | |
| 1373 | xcom_control_if->remove_event_listener(listener_handle); |
| 1374 | |
| 1375 | homemade_free_site_def(3, site_config, node_addrs); |
| 1376 | delete xcom_nodes; |
| 1377 | free_node_set(&nodes); |
| 1378 | } |
| 1379 | |
| 1380 | void check_view_ok(const Gcs_view &view, const Exchanged_data &) { |
| 1381 | ASSERT_EQ(view.get_error_code(), Gcs_view::OK); |
| 1382 | } |
| 1383 | |
| 1384 | void check_view_expelled(const Gcs_view &view) { |
| 1385 | ASSERT_EQ(view.get_error_code(), Gcs_view::MEMBER_EXPELLED); |
| 1386 | } |
| 1387 | |
| 1388 | TEST_F(XComControlTest, FailedNodeGlobalViewTest) { |
| 1389 | // Setting Expectations and Return Values |
| 1390 | // First the node joins the group. |
| 1391 | EXPECT_CALL(proxy, xcom_input_connect(_, _)).Times(1); |
| 1392 | EXPECT_CALL(proxy, test_xcom_tcp_connection(_, _)).Times(1); |
| 1393 | EXPECT_CALL(proxy, xcom_client_boot(_, _)).Times(1); |
| 1394 | EXPECT_CALL(proxy, xcom_wait_ready()).Times(1); |
| 1395 | EXPECT_CALL(proxy, xcom_init(_)).Times(1); |
| 1396 | EXPECT_CALL(proxy, xcom_exit()).Times(0); |
| 1397 | EXPECT_CALL(mock_ev_listener, on_suspicions(_, _)).Times(0); |
| 1398 | EXPECT_CALL(proxy, xcom_client_remove_node(_, _)).Times(1); |
| 1399 | |
| 1400 | EXPECT_CALL(mock_ev_listener, on_view_changed(_, _)) |
| 1401 | .Times(1) |
| 1402 | .WillOnce(Invoke(check_view_ok)); |
| 1403 | |
| 1404 | Gcs_xcom_uuid uuid_1 = Gcs_xcom_uuid::create_uuid(); |
| 1405 | blob blob_1 = {{0, static_cast<char *>(malloc(uuid_1.actual_value.size()))}}; |
| 1406 | uuid_1.encode(reinterpret_cast<uchar **>(&blob_1.data.data_val), |
| 1407 | &blob_1.data.data_len); |
| 1408 | |
| 1409 | Gcs_xcom_uuid uuid_2 = Gcs_xcom_uuid::create_uuid(); |
| 1410 | blob blob_2 = {{0, static_cast<char *>(malloc(uuid_2.actual_value.size()))}}; |
| 1411 | uuid_2.encode(reinterpret_cast<uchar **>(&blob_2.data.data_val), |
| 1412 | &blob_2.data.data_len); |
| 1413 | |
| 1414 | node_address node_addrs[2] = {{const_cast<char *>("127.0.0.1:12345"), |
| 1415 | blob_1, |
| 1416 | {x_1_0, x_1_2}, |
| 1417 | P_PROP | P_ACC | P_LEARN}, |
| 1418 | {const_cast<char *>("127.0.0.1:12343"), |
| 1419 | blob_2, |
| 1420 | {x_1_0, x_1_2}, |
| 1421 | P_PROP | P_ACC | P_LEARN}}; |
| 1422 | |
| 1423 | site_def *site_config = new_site_def(); |
| 1424 | init_site_def(2, node_addrs, site_config); |
| 1425 | site_config->x_proto = static_cast<xcom_proto>(1); |
| 1426 | site_config->nodeno = 0; |
| 1427 | |
| 1428 | node_set nodes; |
| 1429 | alloc_node_set(&nodes, 2); |
| 1430 | set_node_set(&nodes); |
| 1431 | nodes.node_set_val[0] = 0; |
| 1432 | |
| 1433 | EXPECT_CALL(proxy, find_site_def(_)).Times(0); |
| 1434 | |
| 1435 | // Setting fake values |
| 1436 | string address_1("127.0.0.1:12343"); |
| 1437 | Gcs_member_identifier local_member_information_1(address_1); |
| 1438 | |
| 1439 | string address_2("127.0.0.1:12343"); |
| 1440 | Gcs_member_identifier local_member_information_2(address_2); |
| 1441 | |
| 1442 | std::vector<Gcs_member_identifier> members; |
| 1443 | members.push_back(local_member_information_1); |
| 1444 | members.push_back(local_member_information_2); |
| 1445 | |
| 1446 | Gcs_xcom_view_identifier view_id(111111, 1); |
| 1447 | std::vector<Gcs_member_identifier> leaving; |
| 1448 | std::vector<Gcs_member_identifier> joined; |
| 1449 | |
| 1450 | Gcs_group_identifier fake_group_id(group_id->get_group_id()); |
| 1451 | |
| 1452 | Gcs_view *fake_old_view = |
| 1453 | new Gcs_view(members, view_id, leaving, joined, fake_group_id); |
| 1454 | |
| 1455 | // registering the listener |
| 1456 | int listener_handle = xcom_control_if->add_event_listener(mock_ev_listener); |
| 1457 | |
| 1458 | // Test |
| 1459 | enum_gcs_error result = xcom_control_if->join(fake_old_view); |
| 1460 | ASSERT_EQ(GCS_OK, result); |
| 1461 | |
| 1462 | synode_no message_id; |
| 1463 | message_id.group_id = Gcs_xcom_utils::build_xcom_group_id(*this->group_id); |
| 1464 | message_id.msgno = 2; |
| 1465 | message_id.node = 0; |
| 1466 | |
| 1467 | Gcs_xcom_nodes *xcom_nodes = new Gcs_xcom_nodes(site_config, nodes); |
| 1468 | |
| 1469 | bool view_accepted = xcom_control_if->xcom_receive_global_view( |
| 1470 | null_synode, message_id, xcom_nodes, true, null_synode); |
| 1471 | ASSERT_TRUE(view_accepted); |
| 1472 | |
| 1473 | result = xcom_control_if->leave(); |
| 1474 | ASSERT_EQ(GCS_OK, result); |
| 1475 | |
| 1476 | xcom_control_if->remove_event_listener(listener_handle); |
| 1477 | |
| 1478 | homemade_free_site_def(2, site_config, node_addrs); |
| 1479 | delete xcom_nodes; |
| 1480 | free_node_set(&nodes); |
| 1481 | } |
| 1482 | |
| 1483 | TEST_F(XComControlTest, SuspectMembersRemoval) { |
| 1484 | EXPECT_CALL(proxy, xcom_input_connect(_, _)).Times(1); |
| 1485 | EXPECT_CALL(proxy, test_xcom_tcp_connection(_, _)).Times(1); |
| 1486 | EXPECT_CALL(proxy, xcom_client_boot(_, _)).Times(1); |
| 1487 | EXPECT_CALL(proxy, xcom_client_remove_node(_, _)).Times(2); |
| 1488 | EXPECT_CALL(proxy, delete_node_address(_, _)).Times(3); |
| 1489 | |
| 1490 | enum_gcs_error result = xcom_control_if->join(create_fake_view()); |
| 1491 | ASSERT_EQ(GCS_OK, result); |
| 1492 | ASSERT_TRUE(xcom_control_if->is_xcom_running()); |
| 1493 | |
| 1494 | // Get Gcs_suspicions_manager and set default parameters values |
| 1495 | Gcs_suspicions_manager *mgr = xcom_control_if->get_suspicions_manager(); |
| 1496 | Gcs_xcom_nodes xcom_nodes; |
| 1497 | mgr->set_suspicions_processing_period(15u); |
| 1498 | mgr->set_non_member_expel_timeout_seconds(60ul); |
| 1499 | mgr->set_member_expel_timeout_seconds(0ul); |
| 1500 | |
| 1501 | // Build vector with suspect nodes |
| 1502 | std::vector<Gcs_member_identifier *> no_nodes, member_suspect_nodes; |
| 1503 | member_suspect_nodes.push_back(new Gcs_member_identifier("127.0.0.1:12346")); |
| 1504 | member_suspect_nodes.push_back(new Gcs_member_identifier("127.0.0.1:12347")); |
| 1505 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12345", false)); |
| 1506 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12346", false)); |
| 1507 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12347", false)); |
| 1508 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12348", false)); |
| 1509 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12349", false)); |
| 1510 | |
| 1511 | // Insert suspicions into manager |
| 1512 | mgr->process_view(null_synode, &xcom_nodes, no_nodes, no_nodes, |
| 1513 | member_suspect_nodes, no_nodes, true, null_synode); |
| 1514 | |
| 1515 | // Run thread to remove failed node |
| 1516 | mgr->run_process_suspicions(true); |
| 1517 | |
| 1518 | // Check if suspicions list is empty |
| 1519 | const Gcs_xcom_nodes &suspicions_list = mgr->get_suspicions(); |
| 1520 | long unsigned int number_suspects = suspicions_list.get_size(); |
| 1521 | ASSERT_EQ(0ul, number_suspects); |
| 1522 | MYSQL_GCS_LOG_TRACE("List has %lu suspects.", number_suspects); |
| 1523 | |
| 1524 | // Check if the manager has kept the majority |
| 1525 | ASSERT_EQ(mgr->has_majority(), true); |
| 1526 | |
| 1527 | result = xcom_control_if->leave(); |
| 1528 | ASSERT_EQ(GCS_OK, result); |
| 1529 | ASSERT_FALSE(xcom_control_if->is_xcom_running()); |
| 1530 | |
| 1531 | std::vector<Gcs_member_identifier *>::iterator it; |
| 1532 | for (it = member_suspect_nodes.begin(); it != member_suspect_nodes.end(); |
| 1533 | ++it) |
| 1534 | delete (*it); |
| 1535 | member_suspect_nodes.clear(); |
| 1536 | } |
| 1537 | |
| 1538 | TEST_F(XComControlTest, SuspectMemberFailedRemovalDueToMajorityLoss) { |
| 1539 | EXPECT_CALL(proxy, xcom_input_connect(_, _)).Times(1); |
| 1540 | EXPECT_CALL(proxy, test_xcom_tcp_connection(_, _)).Times(1); |
| 1541 | EXPECT_CALL(proxy, xcom_client_boot(_, _)).Times(1); |
| 1542 | EXPECT_CALL(proxy, xcom_client_remove_node(_, _)).Times(2); |
| 1543 | EXPECT_CALL(proxy, delete_node_address(_, _)).Times(3); |
| 1544 | |
| 1545 | enum_gcs_error result = xcom_control_if->join(create_fake_view()); |
| 1546 | ASSERT_EQ(GCS_OK, result); |
| 1547 | ASSERT_TRUE(xcom_control_if->is_xcom_running()); |
| 1548 | |
| 1549 | // Get Gcs_suspicions_manager and set default parameters values |
| 1550 | Gcs_suspicions_manager *mgr = xcom_control_if->get_suspicions_manager(); |
| 1551 | Gcs_xcom_nodes xcom_nodes; |
| 1552 | mgr->set_suspicions_processing_period(15u); |
| 1553 | mgr->set_non_member_expel_timeout_seconds(60ul); |
| 1554 | mgr->set_member_expel_timeout_seconds(0ul); |
| 1555 | |
| 1556 | // Build vector with suspect nodes |
| 1557 | std::vector<Gcs_member_identifier *> no_nodes, member_suspect_nodes; |
| 1558 | member_suspect_nodes.push_back(new Gcs_member_identifier("127.0.0.1:12346")); |
| 1559 | member_suspect_nodes.push_back(new Gcs_member_identifier("127.0.0.1:12347")); |
| 1560 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12345", false)); |
| 1561 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12346", false)); |
| 1562 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12347", false)); |
| 1563 | |
| 1564 | // Insert suspicions into manager |
| 1565 | mgr->process_view(null_synode, &xcom_nodes, no_nodes, no_nodes, |
| 1566 | member_suspect_nodes, no_nodes, true, null_synode); |
| 1567 | |
| 1568 | // Check that the majority is disabled |
| 1569 | ASSERT_EQ(mgr->has_majority(), false); |
| 1570 | |
| 1571 | // Run thread to remove failed node |
| 1572 | mgr->run_process_suspicions(true); |
| 1573 | |
| 1574 | // Check if the manager has kept the majority disabled |
| 1575 | ASSERT_EQ(mgr->has_majority(), false); |
| 1576 | |
| 1577 | // Check if suspicions list is empty as they've timed out |
| 1578 | const Gcs_xcom_nodes &suspicions_list = mgr->get_suspicions(); |
| 1579 | long unsigned int number_suspects = suspicions_list.get_size(); |
| 1580 | ASSERT_EQ(0ul, number_suspects); |
| 1581 | MYSQL_GCS_LOG_TRACE("List has %lu suspects.", number_suspects); |
| 1582 | |
| 1583 | // Add two additional members in order to enable majority |
| 1584 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12348", false)); |
| 1585 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12349", false)); |
| 1586 | |
| 1587 | // Process same view but with more group members |
| 1588 | mgr->process_view(null_synode, &xcom_nodes, no_nodes, no_nodes, |
| 1589 | member_suspect_nodes, no_nodes, true, null_synode); |
| 1590 | |
| 1591 | // Check if the manager has kept the majority |
| 1592 | ASSERT_EQ(mgr->has_majority(), true); |
| 1593 | |
| 1594 | // Run thread to remove failed nodes |
| 1595 | mgr->run_process_suspicions(true); |
| 1596 | |
| 1597 | // Check if suspicions list is empty |
| 1598 | number_suspects = suspicions_list.get_size(); |
| 1599 | ASSERT_EQ(0ul, number_suspects); |
| 1600 | MYSQL_GCS_LOG_TRACE("List has %lu suspects.", number_suspects); |
| 1601 | |
| 1602 | result = xcom_control_if->leave(); |
| 1603 | ASSERT_EQ(GCS_OK, result); |
| 1604 | ASSERT_FALSE(xcom_control_if->is_xcom_running()); |
| 1605 | |
| 1606 | std::vector<Gcs_member_identifier *>::iterator it; |
| 1607 | for (it = member_suspect_nodes.begin(); it != member_suspect_nodes.end(); |
| 1608 | ++it) |
| 1609 | delete (*it); |
| 1610 | member_suspect_nodes.clear(); |
| 1611 | } |
| 1612 | |
| 1613 | TEST_F(XComControlTest, ThreeSuspectNodesRemoval) { |
| 1614 | EXPECT_CALL(proxy, xcom_input_connect(_, _)).Times(1); |
| 1615 | EXPECT_CALL(proxy, test_xcom_tcp_connection(_, _)).Times(1); |
| 1616 | EXPECT_CALL(proxy, xcom_client_boot(_, _)).Times(1); |
| 1617 | EXPECT_CALL(proxy, xcom_client_remove_node(_, _)).Times(3); |
| 1618 | EXPECT_CALL(proxy, delete_node_address(_, _)).Times(4); |
| 1619 | |
| 1620 | enum_gcs_error result = xcom_control_if->join(create_fake_view()); |
| 1621 | ASSERT_EQ(GCS_OK, result); |
| 1622 | ASSERT_TRUE(xcom_control_if->is_xcom_running()); |
| 1623 | |
| 1624 | // Get Gcs_suspicions_manager |
| 1625 | Gcs_suspicions_manager *mgr = xcom_control_if->get_suspicions_manager(); |
| 1626 | Gcs_xcom_nodes xcom_nodes; |
| 1627 | mgr->set_suspicions_processing_period(2u); |
| 1628 | mgr->set_non_member_expel_timeout_seconds(1ul); |
| 1629 | mgr->set_member_expel_timeout_seconds(5ul); |
| 1630 | |
| 1631 | // Build vector with suspect nodes |
| 1632 | std::vector<Gcs_member_identifier *> no_nodes, non_member_suspect_nodes, |
| 1633 | member_suspect_nodes; |
| 1634 | non_member_suspect_nodes.push_back( |
| 1635 | new Gcs_member_identifier("127.0.0.1:12346")); |
| 1636 | non_member_suspect_nodes.push_back( |
| 1637 | new Gcs_member_identifier("127.0.0.1:12347")); |
| 1638 | member_suspect_nodes.push_back(new Gcs_member_identifier("127.0.0.1:12348")); |
| 1639 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12342", false)); |
| 1640 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12343", false)); |
| 1641 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12344", false)); |
| 1642 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12345", false)); |
| 1643 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12346", false)); |
| 1644 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12347", false)); |
| 1645 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12348", false)); |
| 1646 | |
| 1647 | // Insert suspicions into manager |
| 1648 | mgr->process_view(null_synode, &xcom_nodes, no_nodes, no_nodes, |
| 1649 | member_suspect_nodes, non_member_suspect_nodes, true, |
| 1650 | null_synode); |
| 1651 | |
| 1652 | // Check that the majority is enabled |
| 1653 | ASSERT_EQ(mgr->has_majority(), true); |
| 1654 | |
| 1655 | const Gcs_xcom_nodes &suspicions_list = mgr->get_suspicions(); |
| 1656 | long unsigned int number_suspects = suspicions_list.get_size(); |
| 1657 | ASSERT_EQ(non_member_suspect_nodes.size() + member_suspect_nodes.size(), |
| 1658 | number_suspects); |
| 1659 | MYSQL_GCS_LOG_TRACE("List has %lu suspects.", number_suspects); |
| 1660 | |
| 1661 | // Verify if duplicate suspicions aren't inserted |
| 1662 | mgr->process_view(null_synode, &xcom_nodes, no_nodes, no_nodes, |
| 1663 | member_suspect_nodes, non_member_suspect_nodes, true, |
| 1664 | null_synode); |
| 1665 | |
| 1666 | // Check if the manager has kept the majority |
| 1667 | ASSERT_EQ(mgr->has_majority(), true); |
| 1668 | |
| 1669 | number_suspects = suspicions_list.get_size(); |
| 1670 | ASSERT_EQ(non_member_suspect_nodes.size() + member_suspect_nodes.size(), |
| 1671 | number_suspects); |
| 1672 | MYSQL_GCS_LOG_TRACE("List has %lu suspects.", number_suspects); |
| 1673 | |
| 1674 | /* |
| 1675 | Wait the period to and wake the thread to be sure nodes in |
| 1676 | non_member_suspect_nodes were expelled. |
| 1677 | */ |
| 1678 | My_xp_util::sleep_seconds(mgr->get_suspicions_processing_period()); |
| 1679 | |
| 1680 | // Run thread to remove failed node |
| 1681 | mgr->run_process_suspicions(true); |
| 1682 | |
| 1683 | // Check if suspicions list is empty |
| 1684 | number_suspects = suspicions_list.get_size(); |
| 1685 | ASSERT_EQ(member_suspect_nodes.size(), number_suspects); |
| 1686 | MYSQL_GCS_LOG_TRACE("List has %lu suspects.", number_suspects); |
| 1687 | |
| 1688 | // Wait for twice the period to be sure all suspect nodes were expelled |
| 1689 | My_xp_util::sleep_seconds(2 * mgr->get_suspicions_processing_period()); |
| 1690 | |
| 1691 | // Run thread to remove failed node |
| 1692 | mgr->run_process_suspicions(true); |
| 1693 | |
| 1694 | // Check if suspicions list is empty |
| 1695 | number_suspects = suspicions_list.get_size(); |
| 1696 | ASSERT_EQ(0ul, number_suspects); |
| 1697 | MYSQL_GCS_LOG_TRACE("List has %lu suspects.", number_suspects); |
| 1698 | |
| 1699 | result = xcom_control_if->leave(); |
| 1700 | ASSERT_EQ(GCS_OK, result); |
| 1701 | ASSERT_FALSE(xcom_control_if->is_xcom_running()); |
| 1702 | |
| 1703 | std::vector<Gcs_member_identifier *>::iterator it; |
| 1704 | for (it = non_member_suspect_nodes.begin(); |
| 1705 | it != non_member_suspect_nodes.end(); ++it) |
| 1706 | delete (*it); |
| 1707 | non_member_suspect_nodes.clear(); |
| 1708 | |
| 1709 | for (it = member_suspect_nodes.begin(); it != member_suspect_nodes.end(); |
| 1710 | ++it) |
| 1711 | delete (*it); |
| 1712 | member_suspect_nodes.clear(); |
| 1713 | } |
| 1714 | |
| 1715 | TEST_F(XComControlTest, FalseThreeSuspectNodesWithdrawn) { |
| 1716 | EXPECT_CALL(proxy, xcom_client_boot(_, _)).Times(1); |
| 1717 | EXPECT_CALL(proxy, xcom_client_remove_node(_, _)).Times(1); |
| 1718 | EXPECT_CALL(proxy, delete_node_address(_, _)).Times(2); |
| 1719 | |
| 1720 | enum_gcs_error result = xcom_control_if->join(create_fake_view()); |
| 1721 | ASSERT_EQ(GCS_OK, result); |
| 1722 | ASSERT_TRUE(xcom_control_if->is_xcom_running()); |
| 1723 | |
| 1724 | // Get Gcs_suspicions_manager |
| 1725 | Gcs_suspicions_manager *mgr = xcom_control_if->get_suspicions_manager(); |
| 1726 | Gcs_xcom_nodes xcom_nodes; |
| 1727 | mgr->set_suspicions_processing_period(15u); |
| 1728 | mgr->set_non_member_expel_timeout_seconds(5ul); |
| 1729 | mgr->set_member_expel_timeout_seconds(5ul); |
| 1730 | |
| 1731 | // Build vector with suspect nodes |
| 1732 | std::vector<Gcs_member_identifier *> no_nodes, non_member_suspect_nodes, |
| 1733 | member_suspect_nodes; |
| 1734 | non_member_suspect_nodes.push_back( |
| 1735 | new Gcs_member_identifier("127.0.0.1:12346")); |
| 1736 | non_member_suspect_nodes.push_back( |
| 1737 | new Gcs_member_identifier("127.0.0.1:12347")); |
| 1738 | member_suspect_nodes.push_back(new Gcs_member_identifier("127.0.0.1:12348")); |
| 1739 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12346", false)); |
| 1740 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12347", false)); |
| 1741 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12348", false)); |
| 1742 | |
| 1743 | // Insert suspicions into manager |
| 1744 | mgr->process_view(null_synode, &xcom_nodes, no_nodes, no_nodes, |
| 1745 | member_suspect_nodes, non_member_suspect_nodes, true, |
| 1746 | null_synode); |
| 1747 | |
| 1748 | // Check that the majority is disabled |
| 1749 | ASSERT_EQ(mgr->has_majority(), false); |
| 1750 | |
| 1751 | // Enable majority |
| 1752 | mgr->inform_on_majority(true); |
| 1753 | |
| 1754 | const Gcs_xcom_nodes &suspicions_list = mgr->get_suspicions(); |
| 1755 | long unsigned int number_suspects = suspicions_list.get_size(); |
| 1756 | ASSERT_EQ(3ul, number_suspects); |
| 1757 | MYSQL_GCS_LOG_TRACE("List has %lu suspects.", number_suspects); |
| 1758 | |
| 1759 | // Wait for less than timeout and period in order to remove suspicions |
| 1760 | My_xp_util::sleep_seconds(3); |
| 1761 | |
| 1762 | std::vector<Gcs_member_identifier *> alive_nodes; |
| 1763 | alive_nodes.push_back(new Gcs_member_identifier("127.0.0.1:12346")); |
| 1764 | alive_nodes.push_back(new Gcs_member_identifier("127.0.0.1:12347")); |
| 1765 | alive_nodes.push_back(new Gcs_member_identifier("127.0.0.1:12348")); |
| 1766 | xcom_nodes.remove_node(Gcs_xcom_node_information("127.0.0.1:12346", false)); |
| 1767 | xcom_nodes.remove_node(Gcs_xcom_node_information("127.0.0.1:12347", false)); |
| 1768 | xcom_nodes.remove_node(Gcs_xcom_node_information("127.0.0.1:12348", false)); |
| 1769 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12346", true)); |
| 1770 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12347", true)); |
| 1771 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12348", true)); |
| 1772 | |
| 1773 | // Remove suspicions in alive_nodes |
| 1774 | mgr->process_view(null_synode, &xcom_nodes, alive_nodes, no_nodes, no_nodes, |
| 1775 | no_nodes, true, null_synode); |
| 1776 | |
| 1777 | // Check if the manager has kept the majority |
| 1778 | ASSERT_EQ(mgr->has_majority(), true); |
| 1779 | |
| 1780 | // Run thread to remove failed node |
| 1781 | mgr->run_process_suspicions(true); |
| 1782 | |
| 1783 | result = xcom_control_if->leave(); |
| 1784 | ASSERT_EQ(GCS_OK, result); |
| 1785 | ASSERT_FALSE(xcom_control_if->is_xcom_running()); |
| 1786 | |
| 1787 | std::vector<Gcs_member_identifier *>::iterator it; |
| 1788 | for (it = non_member_suspect_nodes.begin(); |
| 1789 | it != non_member_suspect_nodes.end(); ++it) |
| 1790 | delete (*it); |
| 1791 | non_member_suspect_nodes.clear(); |
| 1792 | |
| 1793 | for (it = alive_nodes.begin(); it != alive_nodes.end(); ++it) delete (*it); |
| 1794 | alive_nodes.clear(); |
| 1795 | |
| 1796 | for (it = member_suspect_nodes.begin(); it != member_suspect_nodes.end(); |
| 1797 | ++it) |
| 1798 | delete (*it); |
| 1799 | member_suspect_nodes.clear(); |
| 1800 | } |
| 1801 | |
| 1802 | TEST_F(XComControlTest, ThreeSuspectNodesRemovalAndWithdrawn) { |
| 1803 | EXPECT_CALL(proxy, xcom_input_connect(_, _)).Times(1); |
| 1804 | EXPECT_CALL(proxy, test_xcom_tcp_connection(_, _)).Times(1); |
| 1805 | EXPECT_CALL(proxy, xcom_client_boot(_, _)).Times(1); |
| 1806 | EXPECT_CALL(proxy, xcom_client_remove_node(_, _)).Times(2); |
| 1807 | EXPECT_CALL(proxy, delete_node_address(_, _)).Times(3); |
| 1808 | |
| 1809 | enum_gcs_error result = xcom_control_if->join(create_fake_view()); |
| 1810 | ASSERT_EQ(GCS_OK, result); |
| 1811 | ASSERT_TRUE(xcom_control_if->is_xcom_running()); |
| 1812 | |
| 1813 | // Get Gcs_suspicions_manager |
| 1814 | Gcs_suspicions_manager *mgr = xcom_control_if->get_suspicions_manager(); |
| 1815 | Gcs_xcom_nodes xcom_nodes; |
| 1816 | mgr->set_suspicions_processing_period(11u); |
| 1817 | mgr->set_non_member_expel_timeout_seconds(5ul); |
| 1818 | mgr->set_member_expel_timeout_seconds(5ul); |
| 1819 | |
| 1820 | // Build vector with suspect nodes |
| 1821 | std::vector<Gcs_member_identifier *> no_nodes, non_member_suspect_nodes, |
| 1822 | member_suspect_nodes; |
| 1823 | non_member_suspect_nodes.push_back( |
| 1824 | new Gcs_member_identifier("127.0.0.1:12346")); |
| 1825 | non_member_suspect_nodes.push_back( |
| 1826 | new Gcs_member_identifier("127.0.0.1:12347")); |
| 1827 | member_suspect_nodes.push_back(new Gcs_member_identifier("127.0.0.1:12348")); |
| 1828 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12346", false)); |
| 1829 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12347", false)); |
| 1830 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12348", false)); |
| 1831 | |
| 1832 | // Insert suspicions into manager |
| 1833 | mgr->process_view(null_synode, &xcom_nodes, no_nodes, no_nodes, |
| 1834 | member_suspect_nodes, non_member_suspect_nodes, true, |
| 1835 | null_synode); |
| 1836 | |
| 1837 | // Check that the majority is disabled |
| 1838 | ASSERT_EQ(mgr->has_majority(), false); |
| 1839 | |
| 1840 | // Enable majority |
| 1841 | mgr->inform_on_majority(true); |
| 1842 | |
| 1843 | const Gcs_xcom_nodes &suspicions_list = mgr->get_suspicions(); |
| 1844 | long unsigned int number_suspects = suspicions_list.get_size(); |
| 1845 | ASSERT_EQ(non_member_suspect_nodes.size() + member_suspect_nodes.size(), |
| 1846 | number_suspects); |
| 1847 | MYSQL_GCS_LOG_DEBUG("List has %lu suspects.", number_suspects); |
| 1848 | |
| 1849 | // Wait for less than timeout and period in order to remove suspicions |
| 1850 | My_xp_util::sleep_seconds(3); |
| 1851 | |
| 1852 | std::vector<Gcs_member_identifier *> alive_nodes; |
| 1853 | alive_nodes.push_back(new Gcs_member_identifier("127.0.0.1:12346")); |
| 1854 | xcom_nodes.remove_node(Gcs_xcom_node_information("127.0.0.1:12346", false)); |
| 1855 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12346", true)); |
| 1856 | |
| 1857 | mgr->process_view(null_synode, &xcom_nodes, alive_nodes, no_nodes, no_nodes, |
| 1858 | no_nodes, true, null_synode); |
| 1859 | |
| 1860 | // Check if the manager has kept the majority |
| 1861 | ASSERT_EQ(mgr->has_majority(), true); |
| 1862 | |
| 1863 | /* |
| 1864 | Check if the list still contains the two suspicions since they haven't |
| 1865 | timed out. |
| 1866 | */ |
| 1867 | number_suspects = suspicions_list.get_size(); |
| 1868 | ASSERT_EQ(2ul, number_suspects); |
| 1869 | MYSQL_GCS_LOG_TRACE("List has %lu number_suspects", number_suspects); |
| 1870 | |
| 1871 | alive_nodes.push_back(new Gcs_member_identifier("127.0.0.1:12348")); |
| 1872 | xcom_nodes.remove_node(Gcs_xcom_node_information("127.0.0.1:12348", false)); |
| 1873 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12348", true)); |
| 1874 | |
| 1875 | mgr->process_view(null_synode, &xcom_nodes, alive_nodes, no_nodes, no_nodes, |
| 1876 | no_nodes, true, null_synode); |
| 1877 | |
| 1878 | // Run thread to remove failed node |
| 1879 | mgr->run_process_suspicions(true); |
| 1880 | |
| 1881 | // Check if the manager has kept the majority |
| 1882 | ASSERT_EQ(mgr->has_majority(), true); |
| 1883 | |
| 1884 | /* |
| 1885 | Check if the suspicions list still contains the remaining suspicion which |
| 1886 | still hasn't timed out. |
| 1887 | */ |
| 1888 | number_suspects = suspicions_list.get_size(); |
| 1889 | ASSERT_EQ(1ul, number_suspects); |
| 1890 | MYSQL_GCS_LOG_TRACE("List has %lu number_suspects", number_suspects); |
| 1891 | |
| 1892 | // Wait for period to be sure suspect nodes are expelled |
| 1893 | My_xp_util::sleep_seconds(mgr->get_suspicions_processing_period()); |
| 1894 | |
| 1895 | // Run thread to remove failed node |
| 1896 | mgr->run_process_suspicions(true); |
| 1897 | |
| 1898 | /* |
| 1899 | Check if the suspicions list is empty, since the contained suspicion |
| 1900 | should already have timed out. |
| 1901 | */ |
| 1902 | number_suspects = suspicions_list.get_size(); |
| 1903 | ASSERT_EQ(0ul, number_suspects); |
| 1904 | |
| 1905 | MYSQL_GCS_LOG_TRACE("List has %lu number_suspects", number_suspects); |
| 1906 | |
| 1907 | result = xcom_control_if->leave(); |
| 1908 | ASSERT_EQ(GCS_OK, result); |
| 1909 | ASSERT_FALSE(xcom_control_if->is_xcom_running()); |
| 1910 | |
| 1911 | std::vector<Gcs_member_identifier *>::iterator it; |
| 1912 | for (it = non_member_suspect_nodes.begin(); |
| 1913 | it != non_member_suspect_nodes.end(); ++it) |
| 1914 | delete (*it); |
| 1915 | non_member_suspect_nodes.clear(); |
| 1916 | |
| 1917 | for (it = member_suspect_nodes.begin(); it != member_suspect_nodes.end(); |
| 1918 | ++it) |
| 1919 | delete (*it); |
| 1920 | member_suspect_nodes.clear(); |
| 1921 | |
| 1922 | for (it = alive_nodes.begin(); it != alive_nodes.end(); ++it) delete (*it); |
| 1923 | alive_nodes.clear(); |
| 1924 | } |
| 1925 | |
| 1926 | TEST_F(XComControlTest, ThreeSuspectNodesRemovalAfterTimeoutReset) { |
| 1927 | EXPECT_CALL(proxy, xcom_input_connect(_, _)).Times(1); |
| 1928 | EXPECT_CALL(proxy, test_xcom_tcp_connection(_, _)).Times(1); |
| 1929 | EXPECT_CALL(proxy, xcom_client_boot(_, _)).Times(1); |
| 1930 | EXPECT_CALL(proxy, xcom_client_remove_node(_, _)).Times(2); |
| 1931 | EXPECT_CALL(proxy, delete_node_address(_, _)).Times(3); |
| 1932 | |
| 1933 | enum_gcs_error result = xcom_control_if->join(create_fake_view()); |
| 1934 | ASSERT_EQ(GCS_OK, result); |
| 1935 | ASSERT_TRUE(xcom_control_if->is_xcom_running()); |
| 1936 | |
| 1937 | // Get Gcs_suspicions_manager |
| 1938 | Gcs_suspicions_manager *mgr = xcom_control_if->get_suspicions_manager(); |
| 1939 | Gcs_xcom_nodes xcom_nodes; |
| 1940 | mgr->set_suspicions_processing_period(1u); |
| 1941 | mgr->set_non_member_expel_timeout_seconds(60ul); |
| 1942 | mgr->set_member_expel_timeout_seconds(30ul); |
| 1943 | |
| 1944 | // Build vector with suspect nodes |
| 1945 | std::vector<Gcs_member_identifier *> no_nodes, non_member_suspect_nodes, |
| 1946 | member_suspect_nodes; |
| 1947 | non_member_suspect_nodes.push_back( |
| 1948 | new Gcs_member_identifier("127.0.0.1:12346")); |
| 1949 | non_member_suspect_nodes.push_back( |
| 1950 | new Gcs_member_identifier("127.0.0.1:12347")); |
| 1951 | member_suspect_nodes.push_back(new Gcs_member_identifier("127.0.0.1:12348")); |
| 1952 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12346", false)); |
| 1953 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12347", false)); |
| 1954 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12348", false)); |
| 1955 | |
| 1956 | // Insert suspicions into manager |
| 1957 | mgr->process_view(null_synode, &xcom_nodes, no_nodes, no_nodes, |
| 1958 | member_suspect_nodes, non_member_suspect_nodes, true, |
| 1959 | null_synode); |
| 1960 | |
| 1961 | // Check that the majority is disabled |
| 1962 | ASSERT_EQ(mgr->has_majority(), false); |
| 1963 | |
| 1964 | // Enable majority |
| 1965 | mgr->inform_on_majority(true); |
| 1966 | |
| 1967 | const Gcs_xcom_nodes &suspicions_list = mgr->get_suspicions(); |
| 1968 | long unsigned int number_suspects = suspicions_list.get_size(); |
| 1969 | ASSERT_EQ(non_member_suspect_nodes.size() + member_suspect_nodes.size(), |
| 1970 | number_suspects); |
| 1971 | MYSQL_GCS_LOG_TRACE("List has %lu suspects.", number_suspects); |
| 1972 | |
| 1973 | // Check if suspicions list contains all the suspect nodes |
| 1974 | number_suspects = suspicions_list.get_size(); |
| 1975 | ASSERT_EQ(non_member_suspect_nodes.size() + member_suspect_nodes.size(), |
| 1976 | number_suspects); |
| 1977 | MYSQL_GCS_LOG_TRACE("List has %lu suspects.", number_suspects); |
| 1978 | |
| 1979 | // Check if the manager kept the majority enabled |
| 1980 | ASSERT_EQ(mgr->has_majority(), true); |
| 1981 | |
| 1982 | // Update the value of the timeout parameters |
| 1983 | mgr->set_non_member_expel_timeout_seconds(0ul); |
| 1984 | mgr->set_member_expel_timeout_seconds(0ul); |
| 1985 | |
| 1986 | // Wait for twice the period to be sure all suspect nodes were expelled |
| 1987 | My_xp_util::sleep_seconds(2 * mgr->get_suspicions_processing_period()); |
| 1988 | |
| 1989 | // Check if suspicions list is empty |
| 1990 | number_suspects = suspicions_list.get_size(); |
| 1991 | ASSERT_EQ(0ul, number_suspects); |
| 1992 | MYSQL_GCS_LOG_TRACE("List has %lu suspects.", number_suspects); |
| 1993 | |
| 1994 | // Check if the manager kept the majority enabled |
| 1995 | ASSERT_EQ(mgr->has_majority(), true); |
| 1996 | |
| 1997 | result = xcom_control_if->leave(); |
| 1998 | ASSERT_EQ(GCS_OK, result); |
| 1999 | ASSERT_FALSE(xcom_control_if->is_xcom_running()); |
| 2000 | |
| 2001 | std::vector<Gcs_member_identifier *>::iterator it; |
| 2002 | for (it = non_member_suspect_nodes.begin(); |
| 2003 | it != non_member_suspect_nodes.end(); ++it) |
| 2004 | delete (*it); |
| 2005 | non_member_suspect_nodes.clear(); |
| 2006 | |
| 2007 | for (it = member_suspect_nodes.begin(); it != member_suspect_nodes.end(); |
| 2008 | ++it) |
| 2009 | delete (*it); |
| 2010 | member_suspect_nodes.clear(); |
| 2011 | } |
| 2012 | |
| 2013 | void *parallel_invocation(void *ptr) { |
| 2014 | InvocationHelper *helper = (InvocationHelper *)ptr; |
| 2015 | helper->invokeMethod(); |
| 2016 | |
| 2017 | return nullptr; |
| 2018 | } |
| 2019 | |
| 2020 | TEST_F(XComControlTest, ParallellJoinsTest) { |
| 2021 | EXPECT_CALL(proxy, xcom_input_connect(_, _)).Times(1); |
| 2022 | EXPECT_CALL(proxy, test_xcom_tcp_connection(_, _)).Times(1); |
| 2023 | EXPECT_CALL(proxy, xcom_client_boot(_, _)).Times(1); |
| 2024 | EXPECT_CALL(proxy, xcom_client_remove_node(_, _)).Times(1); |
| 2025 | EXPECT_CALL(proxy, xcom_wait_ready()).Times(1); |
| 2026 | EXPECT_CALL(proxy, xcom_init(_)).Times(1); |
| 2027 | EXPECT_CALL(proxy, xcom_exit()).Times(0); |
| 2028 | |
| 2029 | InvocationHelper *helper = new InvocationHelper(xcom_control_if, JJ); |
| 2030 | |
| 2031 | My_xp_thread_impl thread; |
| 2032 | thread.create(PSI_NOT_INSTRUMENTED, nullptr, parallel_invocation, |
| 2033 | (void *)helper); |
| 2034 | |
| 2035 | helper->invokeMethod(); |
| 2036 | |
| 2037 | thread.join(nullptr); |
| 2038 | |
| 2039 | ASSERT_EQ(helper->count_success, 1); |
| 2040 | ASSERT_EQ(helper->count_fail, 1); |
| 2041 | |
| 2042 | // release memory |
| 2043 | delete helper; |
| 2044 | } |
| 2045 | |
| 2046 | TEST_F(XComControlTest, ParallelLeavesTest) { |
| 2047 | EXPECT_CALL(proxy, xcom_input_connect(_, _)).Times(1); |
| 2048 | EXPECT_CALL(proxy, test_xcom_tcp_connection(_, _)).Times(1); |
| 2049 | EXPECT_CALL(proxy, xcom_client_boot(_, _)).Times(1); |
| 2050 | EXPECT_CALL(proxy, xcom_wait_ready()).Times(1); |
| 2051 | EXPECT_CALL(proxy, xcom_init(_)).Times(1); |
| 2052 | EXPECT_CALL(proxy, xcom_exit()).Times(0); |
| 2053 | EXPECT_CALL(proxy, xcom_client_remove_node(_, _)).Times(1); |
| 2054 | EXPECT_CALL(proxy, delete_node_address(_, _)).Times(2); |
| 2055 | |
| 2056 | enum_gcs_error result = xcom_control_if->join(create_fake_view()); |
| 2057 | ASSERT_EQ(GCS_OK, result); |
| 2058 | |
| 2059 | InvocationHelper *helper = new InvocationHelper(xcom_control_if, LL); |
| 2060 | |
| 2061 | My_xp_thread_impl thread; |
| 2062 | thread.create(PSI_NOT_INSTRUMENTED, nullptr, parallel_invocation, |
| 2063 | (void *)helper); |
| 2064 | |
| 2065 | helper->invokeMethod(); |
| 2066 | |
| 2067 | thread.join(nullptr); |
| 2068 | |
| 2069 | ASSERT_EQ(helper->count_success, 1); |
| 2070 | ASSERT_EQ(helper->count_fail, 1); |
| 2071 | |
| 2072 | // release memory |
| 2073 | delete helper; |
| 2074 | } |
| 2075 | |
| 2076 | TEST_F(XComControlTest, ParallelLeaveAndDelayedJoinTest) { |
| 2077 | EXPECT_CALL(proxy, xcom_input_connect(_, _)).Times(2); |
| 2078 | EXPECT_CALL(proxy, test_xcom_tcp_connection(_, _)).Times(2); |
| 2079 | EXPECT_CALL(proxy, xcom_client_boot(_, _)).Times(2); |
| 2080 | EXPECT_CALL(proxy, xcom_client_remove_node(_, _)).Times(2); |
| 2081 | EXPECT_CALL(proxy, xcom_wait_ready()).Times(2); |
| 2082 | EXPECT_CALL(proxy, xcom_init(_)).Times(2); |
| 2083 | EXPECT_CALL(proxy, xcom_exit()).Times(0); |
| 2084 | |
| 2085 | enum_gcs_error result = xcom_control_if->join(create_fake_view()); |
| 2086 | ASSERT_EQ(GCS_OK, result); |
| 2087 | |
| 2088 | InvocationHelper *helper = new InvocationHelper(xcom_control_if, LJ); |
| 2089 | |
| 2090 | My_xp_thread_impl thread; |
| 2091 | thread.create(PSI_NOT_INSTRUMENTED, nullptr, parallel_invocation, |
| 2092 | (void *)helper); |
| 2093 | |
| 2094 | helper->invokeMethod(); |
| 2095 | |
| 2096 | thread.join(nullptr); |
| 2097 | |
| 2098 | ASSERT_EQ(helper->count_success, 2); |
| 2099 | |
| 2100 | // release memory |
| 2101 | delete helper; |
| 2102 | } |
| 2103 | |
| 2104 | TEST_F(XComControlTest, ParallelJoinAndDelayedLeaveTest) { |
| 2105 | EXPECT_CALL(proxy, xcom_input_connect(_, _)).Times(1); |
| 2106 | EXPECT_CALL(proxy, test_xcom_tcp_connection(_, _)).Times(1); |
| 2107 | EXPECT_CALL(proxy, xcom_client_boot(_, _)).Times(1); |
| 2108 | EXPECT_CALL(proxy, xcom_client_remove_node(_, _)).Times(1); |
| 2109 | EXPECT_CALL(proxy, xcom_wait_ready()).Times(1); |
| 2110 | EXPECT_CALL(proxy, xcom_init(_)).Times(1); |
| 2111 | EXPECT_CALL(proxy, xcom_exit()).Times(0); |
| 2112 | EXPECT_CALL(proxy, delete_node_address(_, _)).Times(2); |
| 2113 | |
| 2114 | InvocationHelper *helper = new InvocationHelper(xcom_control_if, JL); |
| 2115 | |
| 2116 | My_xp_thread_impl thread; |
| 2117 | thread.create(PSI_NOT_INSTRUMENTED, nullptr, parallel_invocation, |
| 2118 | (void *)helper); |
| 2119 | |
| 2120 | helper->invokeMethod(); |
| 2121 | |
| 2122 | thread.join(nullptr); |
| 2123 | |
| 2124 | ASSERT_EQ(helper->count_success, 2); |
| 2125 | |
| 2126 | // release memory |
| 2127 | delete helper; |
| 2128 | } |
| 2129 | |
| 2130 | TEST_F(XComControlTest, NodeTooFarMessage) { |
| 2131 | EXPECT_CALL(proxy, xcom_input_connect(_, _)).Times(1); |
| 2132 | EXPECT_CALL(proxy, test_xcom_tcp_connection(_, _)).Times(1); |
| 2133 | EXPECT_CALL(proxy, xcom_client_boot(_, _)).Times(1); |
| 2134 | EXPECT_CALL(proxy, xcom_wait_ready()).Times(1); |
| 2135 | EXPECT_CALL(proxy, xcom_init(_)).Times(1); |
| 2136 | EXPECT_CALL(proxy, xcom_exit()).Times(0); |
| 2137 | EXPECT_CALL(proxy, delete_node_address(_, _)).Times(2); |
| 2138 | EXPECT_CALL(proxy, xcom_client_remove_node(_, _)).Times(1); |
| 2139 | |
| 2140 | enum_gcs_error result = xcom_control_if->join(create_fake_view()); |
| 2141 | ASSERT_EQ(GCS_OK, result); |
| 2142 | |
| 2143 | // Get Gcs_suspicions_manager and set default parameters values |
| 2144 | Gcs_suspicions_manager *mgr = xcom_control_if->get_suspicions_manager(); |
| 2145 | Gcs_xcom_nodes xcom_nodes; |
| 2146 | mgr->set_suspicions_processing_period(15u); |
| 2147 | mgr->set_non_member_expel_timeout_seconds(60ul); |
| 2148 | mgr->set_member_expel_timeout_seconds(60ul); |
| 2149 | |
| 2150 | // Build vector with suspect nodes |
| 2151 | std::vector<Gcs_member_identifier *> no_nodes, member_suspect_nodes; |
| 2152 | member_suspect_nodes.push_back(new Gcs_member_identifier("127.0.0.1:12346")); |
| 2153 | member_suspect_nodes.push_back(new Gcs_member_identifier("127.0.0.1:12347")); |
| 2154 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12345", false)); |
| 2155 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12346", false)); |
| 2156 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12347", false)); |
| 2157 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12348", false)); |
| 2158 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12349", false)); |
| 2159 | |
| 2160 | std::vector<Gcs_xcom_node_information>::iterator node_it; |
| 2161 | std::vector<Gcs_xcom_node_information> nodes = xcom_nodes.get_nodes(); |
| 2162 | for (node_it = nodes.begin(); node_it != nodes.end(); ++node_it) { |
| 2163 | ASSERT_FALSE(node_it->has_lost_messages()); |
| 2164 | ASSERT_TRUE(synode_eq(node_it->get_max_synode(), null_synode)); |
| 2165 | } |
| 2166 | |
| 2167 | synode_no suspicion_synode = {1, 100, 0}; |
| 2168 | // Insert suspicions into manager |
| 2169 | mgr->process_view(null_synode, &xcom_nodes, no_nodes, no_nodes, |
| 2170 | member_suspect_nodes, no_nodes, true, suspicion_synode); |
| 2171 | |
| 2172 | // Run thread and check that messages have not yet been lost since nothing |
| 2173 | // has yet been removed from the cache. |
| 2174 | mgr->run_process_suspicions(true); |
| 2175 | |
| 2176 | std::vector<Gcs_member_identifier *>::iterator it; |
| 2177 | for (it = member_suspect_nodes.begin(); it != member_suspect_nodes.end(); |
| 2178 | ++it) { |
| 2179 | ASSERT_FALSE(mgr->get_suspicions().get_node(*(*it))->has_lost_messages()); |
| 2180 | ASSERT_TRUE( |
| 2181 | synode_eq(mgr->get_suspicions().get_node(*(*it))->get_max_synode(), |
| 2182 | suspicion_synode)); |
| 2183 | } |
| 2184 | |
| 2185 | synode_no last_removed = {1, 200, 0}; |
| 2186 | // Do it again with a higher last_removed_from_cache value |
| 2187 | mgr->update_last_removed(last_removed); |
| 2188 | mgr->run_process_suspicions(true); |
| 2189 | |
| 2190 | Gcs_xcom_node_information *node = nullptr; |
| 2191 | for (it = member_suspect_nodes.begin(); it != member_suspect_nodes.end(); |
| 2192 | ++it) { |
| 2193 | node = const_cast<Gcs_xcom_node_information *>( |
| 2194 | mgr->get_suspicions().get_node(*(*it))); |
| 2195 | ASSERT_TRUE(node->has_lost_messages()); |
| 2196 | ASSERT_TRUE(synode_eq(node->get_max_synode(), suspicion_synode)); |
| 2197 | } |
| 2198 | |
| 2199 | // Clear current suspicions... |
| 2200 | mgr->clear_suspicions(); |
| 2201 | // ...and add them again to see if the message related vars are cleared. |
| 2202 | mgr->process_view(null_synode, &xcom_nodes, no_nodes, no_nodes, |
| 2203 | member_suspect_nodes, no_nodes, true, last_removed); |
| 2204 | |
| 2205 | for (it = member_suspect_nodes.begin(); it != member_suspect_nodes.end(); |
| 2206 | ++it) { |
| 2207 | node = const_cast<Gcs_xcom_node_information *>( |
| 2208 | mgr->get_suspicions().get_node(*(*it))); |
| 2209 | ASSERT_FALSE(node->has_lost_messages()); |
| 2210 | ASSERT_TRUE(synode_eq(node->get_max_synode(), last_removed)); |
| 2211 | } |
| 2212 | |
| 2213 | result = xcom_control_if->leave(); |
| 2214 | ASSERT_EQ(GCS_OK, result); |
| 2215 | |
| 2216 | for (it = member_suspect_nodes.begin(); it != member_suspect_nodes.end(); |
| 2217 | ++it) |
| 2218 | delete (*it); |
| 2219 | member_suspect_nodes.clear(); |
| 2220 | } |
| 2221 | |
| 2222 | /** |
| 2223 | This test verifies that local views are never delivered to the application |
| 2224 | after a node is expelled from the group. |
| 2225 | */ |
| 2226 | TEST_F(XComControlTest, LocalViewAfterExpel) { |
| 2227 | // Two calls: expel and leave |
| 2228 | EXPECT_CALL(mock_ev_listener, on_view_changed(_, _)).Times(2); |
| 2229 | /* |
| 2230 | on_suspicion is never called because the local view is ignored |
| 2231 | after the node is expelled |
| 2232 | */ |
| 2233 | EXPECT_CALL(mock_ev_listener, on_suspicions(_, _)).Times(0); |
| 2234 | xcom_control_if->add_event_listener(mock_ev_listener); |
| 2235 | |
| 2236 | // Create fake view with two members |
| 2237 | string member_id_1 = xcom_node_address->get_member_address(); |
| 2238 | Gcs_member_identifier local_member_information_1(member_id_1); |
| 2239 | string member_id_2("127.0.0.1:12343"); |
| 2240 | Gcs_member_identifier local_member_information_2(member_id_2); |
| 2241 | std::vector<Gcs_member_identifier> members; |
| 2242 | members.push_back(local_member_information_1); |
| 2243 | members.push_back(local_member_information_2); |
| 2244 | Gcs_xcom_view_identifier view_id(111111, 1); |
| 2245 | std::vector<Gcs_member_identifier> leaving; |
| 2246 | std::vector<Gcs_member_identifier> joined; |
| 2247 | Gcs_group_identifier fake_group_id(group_id->get_group_id()); |
| 2248 | Gcs_view *fake_old_view = |
| 2249 | new Gcs_view(members, view_id, leaving, joined, fake_group_id); |
| 2250 | |
| 2251 | // Join the group with fake view |
| 2252 | enum_gcs_error result = xcom_control_if->join(fake_old_view); |
| 2253 | ASSERT_EQ(GCS_OK, result); |
| 2254 | ASSERT_TRUE(xcom_control_if->is_xcom_running()); |
| 2255 | |
| 2256 | // Create view without the local node to simulate expel |
| 2257 | synode_no message_id; |
| 2258 | message_id.group_id = Gcs_xcom_utils::build_xcom_group_id(*this->group_id); |
| 2259 | message_id.msgno = 4; |
| 2260 | message_id.node = 0; |
| 2261 | |
| 2262 | Gcs_xcom_uuid uuid = Gcs_xcom_uuid::create_uuid(); |
| 2263 | blob blob = {{0, static_cast<char *>(malloc(uuid.actual_value.size()))}}; |
| 2264 | uuid.encode(reinterpret_cast<uchar **>(&blob.data.data_val), |
| 2265 | &blob.data.data_len); |
| 2266 | |
| 2267 | node_address node_addrs[1] = {{const_cast<char *>(member_id_2.c_str()), |
| 2268 | blob, |
| 2269 | {x_1_0, x_1_2}, |
| 2270 | P_PROP | P_ACC | P_LEARN}}; |
| 2271 | |
| 2272 | site_def *site_config = new_site_def(); |
| 2273 | init_site_def(1, node_addrs, site_config); |
| 2274 | site_config->x_proto = static_cast<xcom_proto>(1); |
| 2275 | site_config->nodeno = 0; |
| 2276 | |
| 2277 | node_set nodes; |
| 2278 | alloc_node_set(&nodes, 1); |
| 2279 | set_node_set(&nodes); |
| 2280 | |
| 2281 | Gcs_xcom_nodes *xcom_nodes = new Gcs_xcom_nodes(site_config, nodes); |
| 2282 | |
| 2283 | // Install expel view and verify that it succeeded |
| 2284 | bool expel_view_accepted = xcom_control_if->xcom_receive_global_view( |
| 2285 | null_synode, message_id, xcom_nodes, false, null_synode); |
| 2286 | ASSERT_TRUE(expel_view_accepted); |
| 2287 | Gcs_view *current_view = mock_vce->get_current_view(); |
| 2288 | check_view_expelled(*current_view); |
| 2289 | |
| 2290 | // Try to Install local view and verify that it fails |
| 2291 | bool local_view_accepted = xcom_control_if->xcom_receive_local_view( |
| 2292 | null_synode, xcom_nodes, null_synode); |
| 2293 | ASSERT_FALSE(local_view_accepted); |
| 2294 | |
| 2295 | // Leave and cleanup |
| 2296 | result = xcom_control_if->leave(); |
| 2297 | ASSERT_EQ(GCS_OK, result); |
| 2298 | |
| 2299 | homemade_free_site_def(1, site_config, node_addrs); |
| 2300 | free_node_set(&nodes); |
| 2301 | delete xcom_nodes; |
| 2302 | delete current_view; |
| 2303 | mock_vce->set_current_view(nullptr); |
| 2304 | } |
| 2305 | |
| 2306 | /* This test validates that GCS, given the right sequence of views, does not |
| 2307 | expel the entire group. |
| 2308 | |
| 2309 | The test mimics the following scenario. |
| 2310 | |
| 2311 | Members: |
| 2312 | {me, suspect_1, suspect_2} |
| 2313 | |
| 2314 | Views, composed of a sequence of alive members A, and a set of failed |
| 2315 | members F: |
| 2316 | V1: A=(me, suspect_1) F={suspect_2} |
| 2317 | V2: A=(me, suspect_2) F={suspect_1} |
| 2318 | V3: A=(suspect_1, suspect_2) F={me} |
| 2319 | |
| 2320 | The test mimics the delivery of V1, V2, and V3 on member `me` before any |
| 2321 | expel takes effect, i.e. any expelled member is actually removed from the |
| 2322 | view. The test verifies that `me` does the following. |
| 2323 | |
| 2324 | Upon receiving V1, expels `suspect_2`. |
| 2325 | |
| 2326 | Upon receiving V2, does NOT expel `suspect_1`. The only reason to expel |
| 2327 | `suspect_1` is because the view states that there is a majority of members |
| 2328 | alive, `(me, suspect_2)` vs. `{suspect_1}`. But we have already ordered |
| 2329 | the expel of `suspect_2`, so it should be counted as if it is failed. In |
| 2330 | this case, there is no majority, `(me)` vs. `{suspect_1, suspect_2}`, so |
| 2331 | `suspect_1` should not be expelled. |
| 2332 | |
| 2333 | Upon receiving V3, expels `me`, due to the `force_remove` flag in the |
| 2334 | suspicion manager. |
| 2335 | */ |
| 2336 | TEST_F(XComControlTest, DoNotDisbandEntireGroup) { |
| 2337 | auto me = std::make_unique<Gcs_member_identifier>("127.0.0.1:12345"); |
| 2338 | auto suspect_1 = std::make_unique<Gcs_member_identifier>("127.0.0.1:12346"); |
| 2339 | auto suspect_2 = std::make_unique<Gcs_member_identifier>("127.0.0.1:12347"); |
| 2340 | |
| 2341 | Gcs_xcom_nodes xcom_nodes; |
| 2342 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12345", true)); |
| 2343 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12346", true)); |
| 2344 | xcom_nodes.add_node(Gcs_xcom_node_information("127.0.0.1:12347", true)); |
| 2345 | |
| 2346 | auto suspect_is_me = [suspect = me.get()](node_list *nl, uint32_t) -> bool { |
| 2347 | EXPECT_EQ(nl->node_list_len, 1); |
| 2348 | EXPECT_EQ(std::string{nl->node_list_val[0].address}, |
| 2349 | suspect->get_member_id()); |
| 2350 | return true; |
| 2351 | }; |
| 2352 | auto suspect_is_2 = [suspect = suspect_2.get()](node_list *nl, |
| 2353 | uint32_t) -> bool { |
| 2354 | EXPECT_EQ(nl->node_list_len, 1); |
| 2355 | EXPECT_EQ(std::string{nl->node_list_val[0].address}, |
| 2356 | suspect->get_member_id()); |
| 2357 | return true; |
| 2358 | }; |
| 2359 | |
| 2360 | EXPECT_CALL(proxy, xcom_input_connect(_, _)).Times(1); |
| 2361 | EXPECT_CALL(proxy, test_xcom_tcp_connection(_, _)).Times(1); |
| 2362 | EXPECT_CALL(proxy, xcom_client_boot(_, _)).Times(1); |
| 2363 | EXPECT_CALL(proxy, xcom_client_remove_node(_, _)) |
| 2364 | .Times(2) // once for expulsion, once for leave |
| 2365 | .WillOnce(Invoke(suspect_is_2)) |
| 2366 | .WillOnce(Invoke(suspect_is_me)); |
| 2367 | EXPECT_CALL(proxy, delete_node_address(_, _)) |
| 2368 | .Times(3); // once for boot, once for expulsion, once for leave |
| 2369 | |
| 2370 | enum_gcs_error result = xcom_control_if->join(create_fake_view()); |
| 2371 | ASSERT_EQ(GCS_OK, result); |
| 2372 | ASSERT_TRUE(xcom_control_if->is_xcom_running()); |
| 2373 | |
| 2374 | // Get Gcs_suspicions_manager and set default parameters values |
| 2375 | Gcs_suspicions_manager *mgr = xcom_control_if->get_suspicions_manager(); |
| 2376 | mgr->set_suspicions_processing_period(15u); |
| 2377 | mgr->set_non_member_expel_timeout_seconds(60ul); |
| 2378 | mgr->set_member_expel_timeout_seconds(0ul); |
| 2379 | |
| 2380 | /* Test expulsion of suspect_2. |
| 2381 | We should issue the expel, as specified in the EXPECT_CALL(proxy, |
| 2382 | xcom_client_remove_node(_, _)) above. */ |
| 2383 | // Build vector with suspect nodes |
| 2384 | std::vector<Gcs_member_identifier *> no_nodes; |
| 2385 | std::vector<Gcs_member_identifier *> member_suspect_nodes{suspect_2.get()}; |
| 2386 | |
| 2387 | // Insert suspicions into manager |
| 2388 | mgr->process_view(null_synode, &xcom_nodes, no_nodes, no_nodes, |
| 2389 | member_suspect_nodes, no_nodes, true, null_synode); |
| 2390 | |
| 2391 | // Run thread to remove failed node suspect_2. |
| 2392 | mgr->run_process_suspicions(true); |
| 2393 | |
| 2394 | // Check that the manager has majority |
| 2395 | ASSERT_EQ(mgr->has_majority(), true); |
| 2396 | |
| 2397 | /* Test expulsion of suspect_1. |
| 2398 | We should NOT issue the expel, as specified in the EXPECT_CALL(proxy, |
| 2399 | xcom_client_remove_node(_, _)) above. */ |
| 2400 | // Build vector with suspect nodes |
| 2401 | member_suspect_nodes = std::vector<Gcs_member_identifier *>{suspect_1.get()}; |
| 2402 | |
| 2403 | // Insert suspicions into manager |
| 2404 | mgr->process_view(null_synode, &xcom_nodes, no_nodes, no_nodes, |
| 2405 | member_suspect_nodes, no_nodes, true, null_synode); |
| 2406 | |
| 2407 | // Run thread to try to remove failed node me. |
| 2408 | mgr->run_process_suspicions(true); |
| 2409 | |
| 2410 | // Check that the manager does not have majority |
| 2411 | ASSERT_EQ(mgr->has_majority(), false); |
| 2412 | |
| 2413 | /* Test expulsion of me. |
| 2414 | We should NOT issue the expel, as specified in the EXPECT_CALL(proxy, |
| 2415 | xcom_client_remove_node(_, _)) above. */ |
| 2416 | // Build vector with suspect nodes |
| 2417 | member_suspect_nodes = std::vector<Gcs_member_identifier *>{me.get()}; |
| 2418 | |
| 2419 | // Insert suspicions into manager |
| 2420 | mgr->process_view(null_synode, &xcom_nodes, no_nodes, no_nodes, |
| 2421 | member_suspect_nodes, no_nodes, true, null_synode); |
| 2422 | |
| 2423 | // Run thread to try to remove failed node me. |
| 2424 | mgr->run_process_suspicions(true); |
| 2425 | |
| 2426 | // Check that the manager does not have majority |
| 2427 | ASSERT_EQ(mgr->has_majority(), false); |
| 2428 | |
| 2429 | result = xcom_control_if->leave(); |
| 2430 | ASSERT_EQ(GCS_OK, result); |
| 2431 | ASSERT_FALSE(xcom_control_if->is_xcom_running()); |
| 2432 | } |
| 2433 | |
| 2434 | } // namespace gcs_xcom_control_unittest |