| 1 | /* Copyright (c) 2017, 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 | |
| 24 | #include "gcs_base_test.h" |
| 25 | |
| 26 | #include "gcs_xcom_group_member_information.h" |
| 27 | #include "gcs_xcom_utils.h" |
| 28 | |
| 29 | void homemade_free_site_def(unsigned int n, site_def *s, |
| 30 | node_address *node_addrs) { |
| 31 | // TODO: replace the following with free_site_def(site_config) once |
| 32 | // the header file in site_def.h is fixed |
| 33 | for (unsigned int i = 0; i < n; i++) free(node_addrs[i].uuid.data.data_val); |
| 34 | free_node_set(&s->global_node_set); |
| 35 | free_node_set(&s->local_node_set); |
| 36 | remove_node_list(n, node_addrs, &s->nodes); |
| 37 | free(s->nodes.node_list_val); |
| 38 | free(s); |
| 39 | } |
| 40 | |
| 41 | namespace gcs_parameters_unittest { |
| 42 | |
| 43 | class GcsNodeAddressTest : public GcsBaseTest { |
| 44 | protected: |
| 45 | GcsNodeAddressTest() = default; |
| 46 | |
| 47 | static void SetUpTestCase() {} |
| 48 | }; |
| 49 | |
| 50 | TEST_F(GcsNodeAddressTest, TestNodeAddress) { |
| 51 | /* |
| 52 | Check address "localhost:1030". |
| 53 | */ |
| 54 | std::string hostname = "localhost"; |
| 55 | xcom_port port = 1030; |
| 56 | std::string address = "localhost:1030"; |
| 57 | |
| 58 | Gcs_xcom_node_address local_addr_1(address); |
| 59 | ASSERT_EQ(local_addr_1.get_member_address(), address); |
| 60 | ASSERT_EQ(local_addr_1.get_member_ip(), hostname); |
| 61 | ASSERT_EQ(local_addr_1.get_member_port(), port); |
| 62 | std::string *rep = local_addr_1.get_member_representation(); |
| 63 | ASSERT_EQ(*rep, address); |
| 64 | delete rep; |
| 65 | |
| 66 | /* |
| 67 | Check address "127.0.0.1:1030". |
| 68 | */ |
| 69 | hostname = "127.0.0.1"; |
| 70 | port = 1030; |
| 71 | address = "127.0.0.1:1030"; |
| 72 | Gcs_xcom_node_address local_addr_2(address); |
| 73 | |
| 74 | ASSERT_EQ(local_addr_2.get_member_address(), address); |
| 75 | ASSERT_EQ(local_addr_2.get_member_ip(), hostname); |
| 76 | ASSERT_EQ(local_addr_2.get_member_port(), port); |
| 77 | rep = local_addr_2.get_member_representation(); |
| 78 | ASSERT_EQ(*rep, address); |
| 79 | delete rep; |
| 80 | |
| 81 | /* |
| 82 | Check address "127.0.0.1". |
| 83 | */ |
| 84 | hostname = ""; |
| 85 | port = 0; |
| 86 | address = "127.0.0.1"; |
| 87 | Gcs_xcom_node_address invalid_addr_1(address); |
| 88 | |
| 89 | ASSERT_EQ(invalid_addr_1.get_member_address(), address); |
| 90 | ASSERT_EQ(invalid_addr_1.get_member_ip(), hostname); |
| 91 | ASSERT_EQ(invalid_addr_1.get_member_port(), port); |
| 92 | ASSERT_FALSE(invalid_addr_1.is_valid()); |
| 93 | rep = invalid_addr_1.get_member_representation(); |
| 94 | ASSERT_EQ(*rep, address); |
| 95 | delete rep; |
| 96 | |
| 97 | /* |
| 98 | Check address "127.0.0.1:". |
| 99 | */ |
| 100 | hostname = ""; |
| 101 | port = 0; |
| 102 | address = "127.0.0.1:"; |
| 103 | Gcs_xcom_node_address invalid_addr_2(address); |
| 104 | |
| 105 | ASSERT_EQ(invalid_addr_2.get_member_address(), address); |
| 106 | ASSERT_EQ(invalid_addr_2.get_member_ip(), hostname); |
| 107 | ASSERT_EQ(invalid_addr_2.get_member_port(), port); |
| 108 | ASSERT_FALSE(invalid_addr_2.is_valid()); |
| 109 | rep = invalid_addr_2.get_member_representation(); |
| 110 | ASSERT_EQ(*rep, address); |
| 111 | delete rep; |
| 112 | |
| 113 | /* |
| 114 | Check address "127.0.0.1:invalid". |
| 115 | */ |
| 116 | hostname = ""; |
| 117 | port = 0; |
| 118 | address = "127.0.0.1:invalid"; |
| 119 | Gcs_xcom_node_address invalid_addr_3(address); |
| 120 | |
| 121 | ASSERT_EQ(invalid_addr_3.get_member_address(), address); |
| 122 | ASSERT_EQ(invalid_addr_3.get_member_ip(), hostname); |
| 123 | ASSERT_EQ(invalid_addr_3.get_member_port(), port); |
| 124 | ASSERT_FALSE(invalid_addr_3.is_valid()); |
| 125 | rep = invalid_addr_3.get_member_representation(); |
| 126 | ASSERT_EQ(*rep, address); |
| 127 | delete rep; |
| 128 | } |
| 129 | |
| 130 | TEST_F(GcsNodeAddressTest, TestNodeAddressV6) { |
| 131 | /* |
| 132 | Check address "[::1]:1030". |
| 133 | */ |
| 134 | std::string hostname = "::1"; |
| 135 | xcom_port port = 1030; |
| 136 | std::string address = "[::1]:1030"; |
| 137 | Gcs_xcom_node_address local_addr_2(address); |
| 138 | |
| 139 | ASSERT_EQ(local_addr_2.get_member_address(), address); |
| 140 | ASSERT_EQ(local_addr_2.get_member_ip(), hostname); |
| 141 | ASSERT_EQ(local_addr_2.get_member_port(), port); |
| 142 | std::string *rep = local_addr_2.get_member_representation(); |
| 143 | ASSERT_EQ(*rep, address); |
| 144 | delete rep; |
| 145 | |
| 146 | /* |
| 147 | Check address "[::1]". |
| 148 | */ |
| 149 | hostname = ""; |
| 150 | port = 0; |
| 151 | address = "[::1]"; |
| 152 | Gcs_xcom_node_address invalid_addr_1(address); |
| 153 | |
| 154 | ASSERT_EQ(invalid_addr_1.get_member_address(), address); |
| 155 | ASSERT_EQ(invalid_addr_1.get_member_ip(), hostname); |
| 156 | ASSERT_EQ(invalid_addr_1.get_member_port(), port); |
| 157 | ASSERT_FALSE(invalid_addr_1.is_valid()); |
| 158 | rep = invalid_addr_1.get_member_representation(); |
| 159 | ASSERT_EQ(*rep, address); |
| 160 | delete rep; |
| 161 | |
| 162 | /* |
| 163 | Check address "[::1]:". |
| 164 | */ |
| 165 | hostname = ""; |
| 166 | port = 0; |
| 167 | address = "[::1]:"; |
| 168 | Gcs_xcom_node_address invalid_addr_2(address); |
| 169 | |
| 170 | ASSERT_EQ(invalid_addr_2.get_member_address(), address); |
| 171 | ASSERT_EQ(invalid_addr_2.get_member_ip(), hostname); |
| 172 | ASSERT_EQ(invalid_addr_2.get_member_port(), port); |
| 173 | ASSERT_FALSE(invalid_addr_2.is_valid()); |
| 174 | rep = invalid_addr_2.get_member_representation(); |
| 175 | ASSERT_EQ(*rep, address); |
| 176 | delete rep; |
| 177 | |
| 178 | /* |
| 179 | Check address "[::1:1]:". |
| 180 | */ |
| 181 | hostname = ""; |
| 182 | port = 0; |
| 183 | address = "[::1:1]:"; |
| 184 | Gcs_xcom_node_address invalid_addr_3(address); |
| 185 | |
| 186 | ASSERT_EQ(invalid_addr_3.get_member_address(), address); |
| 187 | ASSERT_EQ(invalid_addr_3.get_member_ip(), hostname); |
| 188 | ASSERT_EQ(invalid_addr_3.get_member_port(), port); |
| 189 | ASSERT_FALSE(invalid_addr_3.is_valid()); |
| 190 | rep = invalid_addr_3.get_member_representation(); |
| 191 | ASSERT_EQ(*rep, address); |
| 192 | delete rep; |
| 193 | |
| 194 | /* |
| 195 | Check address "::1". |
| 196 | */ |
| 197 | hostname = ""; |
| 198 | port = 0; |
| 199 | address = "::1"; |
| 200 | Gcs_xcom_node_address invalid_addr_4(address); |
| 201 | |
| 202 | ASSERT_EQ(invalid_addr_4.get_member_address(), address); |
| 203 | ASSERT_EQ(invalid_addr_4.get_member_ip(), hostname); |
| 204 | ASSERT_EQ(invalid_addr_4.get_member_port(), port); |
| 205 | ASSERT_FALSE(invalid_addr_4.is_valid()); |
| 206 | rep = invalid_addr_4.get_member_representation(); |
| 207 | ASSERT_EQ(*rep, address); |
| 208 | delete rep; |
| 209 | |
| 210 | /* |
| 211 | Check address "[::1". |
| 212 | */ |
| 213 | hostname = ""; |
| 214 | port = 0; |
| 215 | address = "[::1"; |
| 216 | Gcs_xcom_node_address invalid_addr_5(address); |
| 217 | |
| 218 | ASSERT_EQ(invalid_addr_5.get_member_address(), address); |
| 219 | ASSERT_EQ(invalid_addr_5.get_member_ip(), hostname); |
| 220 | ASSERT_EQ(invalid_addr_5.get_member_port(), port); |
| 221 | ASSERT_FALSE(invalid_addr_5.is_valid()); |
| 222 | rep = invalid_addr_5.get_member_representation(); |
| 223 | ASSERT_EQ(*rep, address); |
| 224 | delete rep; |
| 225 | |
| 226 | /* |
| 227 | Check address "::1]". |
| 228 | */ |
| 229 | hostname = ""; |
| 230 | port = 0; |
| 231 | address = "2606:b400:85c:1048:221:f6ff:fe2e:972b]:10301"; |
| 232 | Gcs_xcom_node_address invalid_addr_6(address); |
| 233 | |
| 234 | ASSERT_EQ(invalid_addr_6.get_member_address(), address); |
| 235 | ASSERT_EQ(invalid_addr_6.get_member_ip(), hostname); |
| 236 | ASSERT_EQ(invalid_addr_6.get_member_port(), port); |
| 237 | ASSERT_FALSE(invalid_addr_6.is_valid()); |
| 238 | rep = invalid_addr_6.get_member_representation(); |
| 239 | ASSERT_EQ(*rep, address); |
| 240 | delete rep; |
| 241 | |
| 242 | /* |
| 243 | Check address "[hostname]". |
| 244 | */ |
| 245 | hostname = ""; |
| 246 | port = 0; |
| 247 | address = "[hostname]:10301"; |
| 248 | Gcs_xcom_node_address invalid_addr_7(address); |
| 249 | |
| 250 | ASSERT_EQ(invalid_addr_7.get_member_address(), address); |
| 251 | ASSERT_EQ(invalid_addr_7.get_member_ip(), hostname); |
| 252 | ASSERT_EQ(invalid_addr_7.get_member_port(), port); |
| 253 | ASSERT_FALSE(invalid_addr_7.is_valid()); |
| 254 | rep = invalid_addr_7.get_member_representation(); |
| 255 | ASSERT_EQ(*rep, address); |
| 256 | delete rep; |
| 257 | } |
| 258 | |
| 259 | class GcsUUIDTest : public GcsBaseTest { |
| 260 | protected: |
| 261 | GcsUUIDTest() = default; |
| 262 | |
| 263 | static void SetUpTestCase() {} |
| 264 | }; |
| 265 | |
| 266 | TEST_F(GcsUUIDTest, TestGcsUUID) { |
| 267 | /* |
| 268 | Check that the UUIDs are different as expected. |
| 269 | */ |
| 270 | uchar *buffer = nullptr; |
| 271 | u_int size = 0; |
| 272 | Gcs_xcom_uuid uuid_1 = Gcs_xcom_uuid::create_uuid(); |
| 273 | |
| 274 | /* |
| 275 | Check that we cannot encode the UUID if the buffer is NULL. |
| 276 | */ |
| 277 | ASSERT_FALSE(uuid_1.encode(&buffer, &size)); |
| 278 | |
| 279 | /* |
| 280 | Check that we can encode the UUID if the buffer is not NULL. |
| 281 | */ |
| 282 | buffer = static_cast<uchar *>(malloc(uuid_1.actual_value.size())); |
| 283 | ASSERT_TRUE(uuid_1.encode(&buffer, &size)); |
| 284 | |
| 285 | /* |
| 286 | Try to check that if we decode the buffer objects will have the |
| 287 | same value. Note, howerver, that the parameters will make the |
| 288 | operation fail. |
| 289 | |
| 290 | |
| 291 | We are using the machine clock as UUID and nodes are created |
| 292 | one after the other and the UUIDs may be equal. So we let the |
| 293 | time pass by for just one second. |
| 294 | */ |
| 295 | My_xp_util::sleep_seconds(1); |
| 296 | Gcs_xcom_uuid uuid_2 = Gcs_xcom_uuid::create_uuid(); |
| 297 | ASSERT_FALSE(uuid_2.decode(nullptr, size)); |
| 298 | |
| 299 | /* |
| 300 | Check that if we decode the buffer objects will have the same value. |
| 301 | */ |
| 302 | ASSERT_TRUE(uuid_1.actual_value != uuid_2.actual_value); |
| 303 | uuid_2.decode(buffer, size); |
| 304 | ASSERT_TRUE(uuid_1.actual_value == uuid_2.actual_value); |
| 305 | |
| 306 | free(buffer); |
| 307 | } |
| 308 | |
| 309 | class GcsNodeInformationTest : public GcsBaseTest { |
| 310 | protected: |
| 311 | GcsNodeInformationTest() = default; |
| 312 | |
| 313 | static void SetUpTestCase() {} |
| 314 | }; |
| 315 | |
| 316 | TEST_F(GcsNodeInformationTest, TestGcsNodeInformation) { |
| 317 | std::string id_1("localhost:13001"); |
| 318 | std::string id_2("invalid"); |
| 319 | |
| 320 | Gcs_xcom_node_information node_1(id_1); |
| 321 | Gcs_xcom_node_information node_2(id_1, true); |
| 322 | Gcs_xcom_node_information node_3(id_1, false); |
| 323 | Gcs_xcom_node_information node_4(id_2, false); |
| 324 | Gcs_xcom_uuid uuid_5 = Gcs_xcom_uuid::create_uuid(); |
| 325 | Gcs_xcom_node_information node_5(id_1, uuid_5, 1, false); |
| 326 | |
| 327 | /* |
| 328 | Check whether the constructor: |
| 329 | Gcs_xcom_node_information(const std::string &member_id, |
| 330 | const bool alive |
| 331 | ); |
| 332 | is working fine. Note that the first parameter defines the |
| 333 | regular identifier. |
| 334 | */ |
| 335 | ASSERT_EQ(node_1.get_member_id().get_member_id(), id_1); |
| 336 | ASSERT_EQ(node_1.is_alive(), true); |
| 337 | |
| 338 | ASSERT_EQ(node_2.get_member_id().get_member_id(), id_1); |
| 339 | ASSERT_EQ(node_2.is_alive(), true); |
| 340 | |
| 341 | ASSERT_EQ(node_3.get_member_id().get_member_id(), id_1); |
| 342 | ASSERT_EQ(node_3.is_alive(), false); |
| 343 | |
| 344 | ASSERT_EQ(node_4.get_member_id().get_member_id(), id_2); |
| 345 | ASSERT_EQ(node_4.is_alive(), false); |
| 346 | |
| 347 | /* |
| 348 | Check information on node_no which must be initially invalid. |
| 349 | */ |
| 350 | ASSERT_EQ(node_1.get_node_no(), VOID_NODE_NO); |
| 351 | node_1.set_node_no(1); |
| 352 | ASSERT_EQ(node_1.get_node_no(), 1); |
| 353 | |
| 354 | ASSERT_EQ(node_2.get_node_no(), VOID_NODE_NO); |
| 355 | node_2.set_node_no(2); |
| 356 | ASSERT_EQ(node_2.get_node_no(), 2); |
| 357 | |
| 358 | ASSERT_EQ(node_3.get_node_no(), VOID_NODE_NO); |
| 359 | node_3.set_node_no(3); |
| 360 | ASSERT_EQ(node_3.get_node_no(), 3); |
| 361 | |
| 362 | ASSERT_EQ(node_4.get_node_no(), VOID_NODE_NO); |
| 363 | node_4.set_node_no(4); |
| 364 | ASSERT_EQ(node_4.get_node_no(), 4); |
| 365 | |
| 366 | /* |
| 367 | Set the timestamp and check whether it has timed out: |
| 368 | (m_suspicion_creation_timestamp + suspicion_timeout) < now_ts |
| 369 | */ |
| 370 | node_1.set_suspicion_creation_timestamp(1 /* Set the m_timestamp */); |
| 371 | ASSERT_TRUE(node_1.has_timed_out(3 /* now_ts */, 1 /* timeout */)); |
| 372 | ASSERT_EQ(node_1.get_suspicion_creation_timestamp(), 1); |
| 373 | |
| 374 | /* |
| 375 | Check whether the constructor: |
| 376 | Gcs_xcom_node_information(const std::string &member_id, |
| 377 | const Gcs_xcom_uuid &uuid, |
| 378 | const unsigned int node_no, |
| 379 | const bool alive |
| 380 | ); |
| 381 | is working fine. Note that the first parameter defines the |
| 382 | regular identifier. |
| 383 | */ |
| 384 | ASSERT_EQ(node_5.get_node_no(), 1); |
| 385 | ASSERT_TRUE(node_5.get_member_uuid().actual_value == uuid_5.actual_value); |
| 386 | /* |
| 387 | We are using the machine clock as UUID and nodes are created |
| 388 | one after the other and the UUIDs may be equal. So we let the |
| 389 | time pass by for just one second. |
| 390 | */ |
| 391 | My_xp_util::sleep_seconds(1); |
| 392 | node_5.regenerate_member_uuid(); |
| 393 | ASSERT_TRUE(node_5.get_member_uuid().actual_value != uuid_5.actual_value); |
| 394 | } |
| 395 | |
| 396 | class GcsNodesTest : public GcsBaseTest { |
| 397 | protected: |
| 398 | GcsNodesTest() = default; |
| 399 | |
| 400 | static void SetUpTestCase() {} |
| 401 | }; |
| 402 | |
| 403 | TEST_F(GcsNodesTest, TestGcsNodesBasicProperties) { |
| 404 | Gcs_xcom_nodes nodes; |
| 405 | |
| 406 | Gcs_xcom_node_information node_1("localhost:1031"); |
| 407 | node_1.set_node_no(1); |
| 408 | |
| 409 | Gcs_xcom_node_information node_2("localhost:1032"); |
| 410 | node_2.set_node_no(2); |
| 411 | |
| 412 | Gcs_xcom_node_information node_3("localhost:1033"); |
| 413 | node_3.set_node_no(3); |
| 414 | |
| 415 | const Gcs_xcom_node_information *ret = nullptr; |
| 416 | |
| 417 | /* |
| 418 | Initially the node's number is void and check whether the |
| 419 | set_node_no() function is working properly. |
| 420 | */ |
| 421 | ASSERT_FALSE(nodes.is_valid()); |
| 422 | ASSERT_EQ(nodes.get_node_no(), VOID_NODE_NO); |
| 423 | nodes.set_node_no(0); |
| 424 | ASSERT_EQ(nodes.get_node_no(), 0); |
| 425 | ASSERT_TRUE(nodes.is_valid()); |
| 426 | |
| 427 | /* |
| 428 | Initially the number of nodes is zero. |
| 429 | */ |
| 430 | ASSERT_EQ(nodes.get_size(), 0); |
| 431 | ASSERT_TRUE(nodes.empty()); |
| 432 | |
| 433 | /* |
| 434 | Adding and removing nodes. Note that it allows duplicated nodes. |
| 435 | */ |
| 436 | nodes.add_node(node_1); |
| 437 | ASSERT_EQ(nodes.get_size(), 1); |
| 438 | ASSERT_FALSE(nodes.empty()); |
| 439 | |
| 440 | nodes.add_node(node_1); |
| 441 | ASSERT_EQ(nodes.get_size(), 2); |
| 442 | |
| 443 | nodes.remove_node(node_1); |
| 444 | ASSERT_EQ(nodes.get_size(), 1); |
| 445 | |
| 446 | nodes.add_node(node_2); |
| 447 | ASSERT_EQ(nodes.get_size(), 2); |
| 448 | |
| 449 | /* |
| 450 | Trying to get a node using get_node(const Gcs_member_identifier &member_id). |
| 451 | */ |
| 452 | ret = nodes.get_node(node_1.get_member_id()); |
| 453 | ASSERT_EQ(ret->get_member_id(), node_1.get_member_id()); |
| 454 | |
| 455 | ret = nodes.get_node(node_2.get_member_id()); |
| 456 | ASSERT_EQ(ret->get_member_id(), node_2.get_member_id()); |
| 457 | |
| 458 | ret = nodes.get_node(node_3.get_member_id()); |
| 459 | ASSERT_TRUE(ret == nullptr); |
| 460 | |
| 461 | /* |
| 462 | Trying to get a node using get_node(const std::string &member_id). |
| 463 | */ |
| 464 | ret = nodes.get_node(node_1.get_member_id().get_member_id()); |
| 465 | ASSERT_EQ(ret->get_member_id(), node_1.get_member_id()); |
| 466 | |
| 467 | ret = nodes.get_node(node_2.get_member_id().get_member_id()); |
| 468 | ASSERT_EQ(ret->get_member_id(), node_2.get_member_id()); |
| 469 | |
| 470 | ret = nodes.get_node(node_3.get_member_id().get_member_id()); |
| 471 | ASSERT_TRUE(ret == nullptr); |
| 472 | |
| 473 | /* |
| 474 | Trying to get a node using get_node(unsigned int node_no). |
| 475 | */ |
| 476 | ret = nodes.get_node(node_1.get_member_id().get_member_id()); |
| 477 | ASSERT_EQ(ret->get_member_id(), node_1.get_member_id()); |
| 478 | |
| 479 | ret = nodes.get_node(node_2.get_member_id().get_member_id()); |
| 480 | ASSERT_EQ(ret->get_member_id(), node_2.get_member_id()); |
| 481 | |
| 482 | ret = nodes.get_node(node_3.get_member_id().get_member_id()); |
| 483 | ASSERT_TRUE(ret == nullptr); |
| 484 | |
| 485 | /* |
| 486 | Trying to get a node using get_node(const Gcs_xcom_uuid &uuid). |
| 487 | |
| 488 | We are only looking for node_1 because we are using the machine |
| 489 | clock as UUID and nodes are created one after the other and the |
| 490 | UUIDs may be equal. |
| 491 | */ |
| 492 | ret = nodes.get_node(node_1.get_member_uuid()); |
| 493 | ASSERT_EQ(ret->get_member_id(), node_1.get_member_id()); |
| 494 | } |
| 495 | |
| 496 | TEST_F(GcsNodesTest, TestGcsNodesCopyingNodes) { |
| 497 | Gcs_xcom_nodes nodes; |
| 498 | Gcs_xcom_nodes copied_nodes; |
| 499 | |
| 500 | Gcs_xcom_node_information node_1("localhost:1031"); |
| 501 | node_1.set_node_no(1); |
| 502 | nodes.add_node(node_1); |
| 503 | |
| 504 | Gcs_xcom_node_information node_2("localhost:1032"); |
| 505 | node_2.set_node_no(2); |
| 506 | nodes.add_node(node_2); |
| 507 | |
| 508 | const Gcs_xcom_node_information *ret = nullptr; |
| 509 | Gcs_xcom_node_information *ret_1 = nullptr; |
| 510 | Gcs_xcom_node_information *ret_2 = nullptr; |
| 511 | |
| 512 | /* |
| 513 | Copying nodes through the add_nodes method which cleans up |
| 514 | the previous data and copies the set of nodes provided as |
| 515 | parameter. |
| 516 | */ |
| 517 | copied_nodes.add_nodes(nodes); |
| 518 | const std::vector<Gcs_xcom_node_information> &node_list = |
| 519 | copied_nodes.get_nodes(); |
| 520 | std::vector<Gcs_xcom_node_information>::const_iterator nodes_it; |
| 521 | for (nodes_it = node_list.begin(); nodes_it != node_list.end(); ++nodes_it) { |
| 522 | ret = nodes.get_node((*nodes_it).get_member_id()); |
| 523 | ASSERT_EQ(ret->get_member_id(), (*nodes_it).get_member_id()); |
| 524 | } |
| 525 | ASSERT_EQ(copied_nodes.get_size(), nodes.get_size()); |
| 526 | |
| 527 | /* |
| 528 | Check if the information in the different sets are copies and |
| 529 | not references or pointers. |
| 530 | */ |
| 531 | ret_1 = const_cast<Gcs_xcom_node_information *>( |
| 532 | nodes.get_node(node_1.get_member_id())); |
| 533 | ASSERT_TRUE(ret_1->get_member_id() == node_1.get_member_id()); |
| 534 | ASSERT_TRUE(ret_1->get_member_uuid().actual_value == |
| 535 | node_1.get_member_uuid().actual_value); |
| 536 | ASSERT_TRUE(ret_1->get_node_no() == node_1.get_node_no()); |
| 537 | |
| 538 | ret_2 = const_cast<Gcs_xcom_node_information *>( |
| 539 | copied_nodes.get_node(node_1.get_member_id())); |
| 540 | ASSERT_TRUE(ret_2->get_member_id() == node_1.get_member_id()); |
| 541 | ASSERT_TRUE(ret_2->get_member_uuid().actual_value == |
| 542 | node_1.get_member_uuid().actual_value); |
| 543 | ASSERT_TRUE(ret_2->get_node_no() == node_1.get_node_no()); |
| 544 | |
| 545 | ret_1->set_node_no(10); |
| 546 | ret_2->set_node_no(20); |
| 547 | ret_1 = const_cast<Gcs_xcom_node_information *>( |
| 548 | nodes.get_node(node_1.get_member_id())); |
| 549 | ret_2 = const_cast<Gcs_xcom_node_information *>( |
| 550 | copied_nodes.get_node(node_1.get_member_id())); |
| 551 | ASSERT_FALSE(ret_1->get_node_no() == node_1.get_node_no()); |
| 552 | ASSERT_FALSE(ret_2->get_node_no() == node_1.get_node_no()); |
| 553 | ASSERT_FALSE(ret_1->get_node_no() == ret_2->get_node_no()); |
| 554 | ASSERT_EQ(node_1.get_node_no(), 1); |
| 555 | ASSERT_EQ(ret_1->get_node_no(), 10); |
| 556 | ASSERT_EQ(ret_2->get_node_no(), 20); |
| 557 | } |
| 558 | |
| 559 | TEST_F(GcsNodesTest, TestGcsNodesEncoding) { |
| 560 | Gcs_xcom_nodes nodes; |
| 561 | |
| 562 | Gcs_xcom_node_information node_1("localhost:1031"); |
| 563 | node_1.set_node_no(1); |
| 564 | nodes.add_node(node_1); |
| 565 | |
| 566 | /* |
| 567 | We are using the machine clock as UUID and nodes are created |
| 568 | one after the other and the UUIDs may be equal. So we let the |
| 569 | time pass by for just one second. |
| 570 | */ |
| 571 | My_xp_util::sleep_seconds(1); |
| 572 | Gcs_xcom_node_information node_2("localhost:1032"); |
| 573 | node_2.set_node_no(2); |
| 574 | nodes.add_node(node_2); |
| 575 | |
| 576 | /* |
| 577 | Enconding the list of nodes in a format that can be used |
| 578 | by XCOM and checking if the encode method is producing the |
| 579 | expected result: |
| 580 | |
| 581 | length -> Has the number of addresses and uuids. |
| 582 | addrs[n] -> Pointer to the member identifier as string. |
| 583 | uuids[n] -> data.data_len and data.data_val. |
| 584 | */ |
| 585 | const Gcs_xcom_node_information *ret_1 = nullptr; |
| 586 | const Gcs_xcom_node_information *ret_2 = nullptr; |
| 587 | |
| 588 | Gcs_xcom_uuid uuid_1; |
| 589 | Gcs_xcom_uuid uuid_2; |
| 590 | |
| 591 | unsigned int length = 0; |
| 592 | char const **addrs = nullptr; |
| 593 | blob *uuids = nullptr; |
| 594 | |
| 595 | nodes.encode(&length, &addrs, &uuids); |
| 596 | |
| 597 | ASSERT_EQ(length, 2); |
| 598 | ret_1 = nodes.get_node(addrs[0]); |
| 599 | ret_2 = nodes.get_node(addrs[1]); |
| 600 | ASSERT_FALSE(ret_1->get_member_id() == ret_2->get_member_id()); |
| 601 | |
| 602 | uuid_1.decode(reinterpret_cast<uchar *>(uuids[0].data.data_val), |
| 603 | uuids[0].data.data_len); |
| 604 | uuid_2.decode(reinterpret_cast<uchar *>(uuids[1].data.data_val), |
| 605 | uuids[1].data.data_len); |
| 606 | ret_1 = nodes.get_node(uuid_1); |
| 607 | ret_2 = nodes.get_node(uuid_2); |
| 608 | ASSERT_EQ(uuids[0].data.data_len, uuid_1.actual_value.size()); |
| 609 | ASSERT_EQ(uuids[1].data.data_len, uuid_2.actual_value.size()); |
| 610 | ASSERT_FALSE(ret_1->get_member_id() == ret_2->get_member_id()); |
| 611 | |
| 612 | /* |
| 613 | There is no problem if encoded is called twice. The nodes |
| 614 | object will be responsible for deallocating any previously |
| 615 | allocated memory without causing any leak. |
| 616 | */ |
| 617 | nodes.encode(&length, &addrs, &uuids); |
| 618 | } |
| 619 | |
| 620 | TEST_F(GcsNodesTest, TestGcsNodesConstructor) { |
| 621 | const Gcs_xcom_node_information *ret = nullptr; |
| 622 | |
| 623 | Gcs_xcom_uuid uuid_1 = Gcs_xcom_uuid::create_uuid(); |
| 624 | blob blob_1 = {{0, static_cast<char *>(malloc(uuid_1.actual_value.size()))}}; |
| 625 | uuid_1.encode(reinterpret_cast<uchar **>(&blob_1.data.data_val), |
| 626 | &blob_1.data.data_len); |
| 627 | |
| 628 | Gcs_xcom_uuid uuid_2 = Gcs_xcom_uuid::create_uuid(); |
| 629 | blob blob_2 = {{0, static_cast<char *>(malloc(uuid_2.actual_value.size()))}}; |
| 630 | uuid_2.encode(reinterpret_cast<uchar **>(&blob_2.data.data_val), |
| 631 | &blob_2.data.data_len); |
| 632 | |
| 633 | node_address node_addrs[2] = {{const_cast<char *>("127.0.0.1:12345"), |
| 634 | blob_1, |
| 635 | {x_1_0, x_1_2}, |
| 636 | P_PROP | P_ACC | P_LEARN}, |
| 637 | {const_cast<char *>("127.0.0.1:12343"), |
| 638 | blob_2, |
| 639 | {x_1_0, x_1_2}, |
| 640 | P_PROP | P_ACC | P_LEARN}}; |
| 641 | |
| 642 | site_def *site_config = new_site_def(); |
| 643 | init_site_def(2, node_addrs, site_config); |
| 644 | site_config->nodeno = 0; |
| 645 | site_config->x_proto = static_cast<xcom_proto>(1); |
| 646 | |
| 647 | node_set nodes; |
| 648 | alloc_node_set(&nodes, 2); |
| 649 | set_node_set(&nodes); |
| 650 | nodes.node_set_val[0] = 0; |
| 651 | |
| 652 | /* |
| 653 | Check if Gcs_xcom_nodes(const site_def *site, node_set &nodes) is |
| 654 | working properly. |
| 655 | */ |
| 656 | Gcs_xcom_nodes xcom_nodes(site_config, nodes); |
| 657 | |
| 658 | ret = xcom_nodes.get_node("127.0.0.1:12345"); |
| 659 | ASSERT_EQ(ret->get_member_id().get_member_id(), "127.0.0.1:12345"); |
| 660 | ASSERT_EQ(ret->get_node_no(), 0); |
| 661 | ASSERT_FALSE(ret->is_alive()); |
| 662 | ASSERT_EQ(ret->get_member_uuid().actual_value, uuid_1.actual_value); |
| 663 | |
| 664 | ret = xcom_nodes.get_node("127.0.0.1:12343"); |
| 665 | ASSERT_EQ(ret->get_member_id().get_member_id(), "127.0.0.1:12343"); |
| 666 | ASSERT_EQ(ret->get_node_no(), 1); |
| 667 | ASSERT_TRUE(ret->is_alive()); |
| 668 | ASSERT_EQ(ret->get_member_uuid().actual_value, uuid_2.actual_value); |
| 669 | |
| 670 | homemade_free_site_def(2, site_config, node_addrs); |
| 671 | free_node_set(&nodes); |
| 672 | } |
| 673 | |
| 674 | } // namespace gcs_parameters_unittest |