1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
|
require 'spec_helper'
RSpec.describe Mysql2::Client do # rubocop:disable Metrics/BlockLength
let(:performance_schema_enabled) do
performance_schema = @client.query "SHOW VARIABLES LIKE 'performance_schema'"
performance_schema.any? { |x| x['Value'] == 'ON' }
end
context "using defaults file" do
let(:cnf_file) { File.expand_path('../../my.cnf', __FILE__) }
it "should not raise an exception for valid defaults group" do
expect do
new_client(default_file: cnf_file, default_group: "test")
end.not_to raise_error
end
it "should not raise an exception without default group" do
expect do
new_client(default_file: cnf_file)
end.not_to raise_error
end
end
it "should raise a Mysql::Error::ConnectionError upon connection failure" do
expect do
# The odd local host IP address forces the mysql client library to
# use a TCP socket rather than a domain socket.
new_client('host' => '127.0.0.2', 'port' => 999999)
end.to raise_error(Mysql2::Error::ConnectionError)
end
it "should raise an exception on create for invalid encodings" do
expect do
new_client(encoding: "fake")
end.to raise_error(Mysql2::Error)
end
it "should raise an exception on non-string encodings" do
expect do
new_client(encoding: :fake)
end.to raise_error(TypeError)
end
it "should not raise an exception on create for a valid encoding" do
expect do
new_client(encoding: "utf8")
end.not_to raise_error
expect do
new_client(DatabaseCredentials['root'].merge(encoding: "big5"))
end.not_to raise_error
end
Klient = Class.new(Mysql2::Client) do
attr_reader :connect_args
def connect(*args)
@connect_args ||= []
@connect_args << args
end
end
it "should accept connect flags and pass them to #connect" do
client = Klient.new flags: Mysql2::Client::FOUND_ROWS
expect(client.connect_args.last[6] & Mysql2::Client::FOUND_ROWS).to be > 0
end
it "should parse flags array" do
client = Klient.new flags: %w[FOUND_ROWS -PROTOCOL_41]
expect(client.connect_args.last[6] & Mysql2::Client::FOUND_ROWS).to eql(Mysql2::Client::FOUND_ROWS)
expect(client.connect_args.last[6] & Mysql2::Client::PROTOCOL_41).to eql(0)
end
it "should parse flags string" do
client = Klient.new flags: "FOUND_ROWS -PROTOCOL_41"
expect(client.connect_args.last[6] & Mysql2::Client::FOUND_ROWS).to eql(Mysql2::Client::FOUND_ROWS)
expect(client.connect_args.last[6] & Mysql2::Client::PROTOCOL_41).to eql(0)
end
it "should default flags to (REMEMBER_OPTIONS, LONG_PASSWORD, LONG_FLAG, TRANSACTIONS, PROTOCOL_41, SECURE_CONNECTION)" do
client = Klient.new
client_flags = Mysql2::Client::REMEMBER_OPTIONS |
Mysql2::Client::LONG_PASSWORD |
Mysql2::Client::LONG_FLAG |
Mysql2::Client::TRANSACTIONS |
Mysql2::Client::PROTOCOL_41 |
Mysql2::Client::SECURE_CONNECTION |
Mysql2::Client::CONNECT_ATTRS
expect(client.connect_args.last[6]).to eql(client_flags)
end
it "should execute init command" do
options = DatabaseCredentials['root'].dup
options[:init_command] = "SET @something = 'setting_value';"
client = new_client(options)
result = client.query("SELECT @something;")
expect(result.first['@something']).to eq('setting_value')
end
it "should send init_command after reconnect" do
options = DatabaseCredentials['root'].dup
options[:init_command] = "SET @something = 'setting_value';"
options[:reconnect] = true
client = new_client(options)
result = client.query("SELECT @something;")
expect(result.first['@something']).to eq('setting_value')
# get the current connection id
result = client.query("SELECT CONNECTION_ID()")
first_conn_id = result.first['CONNECTION_ID()']
# break the current connection
expect { client.query("KILL #{first_conn_id}") }.to raise_error(Mysql2::Error)
client.ping # reconnect now
# get the new connection id
result = client.query("SELECT CONNECTION_ID()")
second_conn_id = result.first['CONNECTION_ID()']
# confirm reconnect by checking the new connection id
expect(first_conn_id).not_to eq(second_conn_id)
# At last, check that the init command executed
result = client.query("SELECT @something;")
expect(result.first['@something']).to eq('setting_value')
end
it "should have a global default_query_options hash" do
expect(Mysql2::Client).to respond_to(:default_query_options)
end
context "SSL" do
before(:example) do
ssl = @client.query "SHOW VARIABLES LIKE 'have_ssl'"
ssl_uncompiled = ssl.any? { |x| x['Value'] == 'OFF' }
ssl_disabled = ssl.any? { |x| x['Value'] == 'DISABLED' }
if ssl_uncompiled
skip("DON'T WORRY, THIS TEST PASSES - but SSL is not compiled into your MySQL daemon.")
elsif ssl_disabled
skip("DON'T WORRY, THIS TEST PASSES - but SSL is not enabled in your MySQL daemon.")
else
%i[sslkey sslcert sslca].each do |item|
unless File.exist?(option_overrides[item])
skip("DON'T WORRY, THIS TEST PASSES - but #{option_overrides[item]} does not exist.")
break
end
end
end
end
let(:option_overrides) do
{
'host' => 'mysql2gem.example.com', # must match the certificates
:sslkey => '/etc/mysql/client-key.pem',
:sslcert => '/etc/mysql/client-cert.pem',
:sslca => '/etc/mysql/ca-cert.pem',
:sslcipher => 'DHE-RSA-AES256-SHA',
:sslverify => true,
}
end
let(:ssl_client) do
new_client(option_overrides)
end
# 'preferred' or 'verify_ca' are only in MySQL 5.6.36+, 5.7.11+, 8.0+
version = Mysql2::Client.info
ssl_modes = case version
when 50636...50700, 50711...50800, 80000...90000
%i[disabled preferred required verifa_ca verify_identity]
else
%i[disabled required verify_identity]
end
# MySQL and MariaDB and all versions of Connector/C
ssl_modes.each do |ssl_mode|
it "should set ssl_mode option #{ssl_mode}" do
options = {
ssl_mode: ssl_mode,
}
options.merge!(option_overrides)
expect do
expect do
new_client(options)
end.not_to output(/does not support ssl_mode/).to_stderr
end.not_to raise_error
end
end
it "should be able to connect via SSL options" do
# You may need to adjust the lines below to match your SSL certificate paths
results = Hash[ssl_client.query('SHOW STATUS WHERE Variable_name LIKE "Ssl_%"').map { |x| x.values_at('Variable_name', 'Value') }]
expect(results['Ssl_cipher']).not_to be_empty
expect(results['Ssl_version']).not_to be_empty
expect(ssl_client.ssl_cipher).not_to be_empty
expect(results['Ssl_cipher']).to eql(ssl_client.ssl_cipher)
end
end
def run_gc
if defined?(Rubinius)
GC.run(true)
else
GC.start
end
sleep(0.5)
end
it "should terminate connections when calling close" do
# rubocop:disable Lint/AmbiguousBlockAssociation
expect do
client = Mysql2::Client.new(DatabaseCredentials['root'])
connection_id = client.thread_id
client.close
# mysql_close sends a quit command without waiting for a response
# so give the server some time to handle the detect the closed connection
closed = false
10.times do
closed = @client.query("SHOW PROCESSLIST").none? { |row| row['Id'] == connection_id }
break if closed
sleep(0.1)
end
expect(closed).to eq(true)
end.to_not change {
@client.query("SHOW STATUS LIKE 'Aborted_%'").to_a
}
# rubocop:enable Lint/AmbiguousBlockAssociation
end
it "should not leave dangling connections after garbage collection" do
run_gc
# rubocop:disable Lint/AmbiguousBlockAssociation
expect do
expect do
10.times do
Mysql2::Client.new(DatabaseCredentials['root']).query('SELECT 1')
end
end.to change {
@client.query("SHOW STATUS LIKE 'Threads_connected'").first['Value'].to_i
}.by(10)
run_gc
end.to_not change {
@client.query("SHOW STATUS LIKE 'Aborted_%'").to_a +
@client.query("SHOW STATUS LIKE 'Threads_connected'").to_a
}
# rubocop:enable Lint/AmbiguousBlockAssociation
end
context "#set_server_option" do
let(:client) do
new_client.tap do |client|
client.set_server_option(Mysql2::Client::OPTION_MULTI_STATEMENTS_ON)
end
end
it 'returns true when multi_statements is enable' do
expect(client.set_server_option(Mysql2::Client::OPTION_MULTI_STATEMENTS_ON)).to be true
end
it 'returns true when multi_statements is disable' do
expect(client.set_server_option(Mysql2::Client::OPTION_MULTI_STATEMENTS_OFF)).to be true
end
it 'returns false when multi_statements is neither OPTION_MULTI_STATEMENTS_OFF or OPTION_MULTI_STATEMENTS_ON' do
expect(client.set_server_option(344)).to be false
end
it 'enables multiple-statement' do
client.query("SELECT 1;SELECT 2;")
expect(client.next_result).to be true
expect(client.store_result.first).to eql('2' => 2)
expect(client.next_result).to be false
end
it 'disables multiple-statement' do
client.set_server_option(Mysql2::Client::OPTION_MULTI_STATEMENTS_OFF)
expect { client.query("SELECT 1;SELECT 2;") }.to raise_error(Mysql2::Error)
end
end
context "#automatic_close" do
it "is enabled by default" do
expect(new_client.automatic_close?).to be(true)
end
if RUBY_PLATFORM =~ /mingw|mswin/
it "cannot be disabled" do
expect do
client = new_client(automatic_close: false)
expect(client.automatic_close?).to be(true)
end.to output(/always closed by garbage collector/).to_stderr
expect do
client = new_client(automatic_close: true)
expect(client.automatic_close?).to be(true)
end.to_not output(/always closed by garbage collector/).to_stderr
expect do
client = new_client(automatic_close: true)
client.automatic_close = false
expect(client.automatic_close?).to be(true)
end.to output(/always closed by garbage collector/).to_stderr
end
else
it "can be configured" do
client = new_client(automatic_close: false)
expect(client.automatic_close?).to be(false)
end
it "can be assigned" do
client = new_client
client.automatic_close = false
expect(client.automatic_close?).to be(false)
client.automatic_close = true
expect(client.automatic_close?).to be(true)
client.automatic_close = nil
expect(client.automatic_close?).to be(false)
client.automatic_close = 9
expect(client.automatic_close?).to be(true)
end
it "should not close connections when running in a child process" do
run_gc
client = Mysql2::Client.new(DatabaseCredentials['root'])
client.automatic_close = false
child = fork do
client.query('SELECT 1')
client = nil
run_gc
end
Process.wait(child)
# this will throw an error if the underlying socket was shutdown by the
# child's GC
expect { client.query('SELECT 1') }.to_not raise_exception
client.close
end
end
end
it "should be able to connect to database with numeric-only name" do
database = 1235
@client.query "CREATE DATABASE IF NOT EXISTS `#{database}`"
expect do
new_client('database' => database)
end.not_to raise_error
@client.query "DROP DATABASE IF EXISTS `#{database}`"
end
it "should respond to #close" do
expect(@client).to respond_to(:close)
end
it "should be able to close properly" do
expect(@client.close).to be_nil
expect do
@client.query "SELECT 1"
end.to raise_error(Mysql2::Error)
end
context "#closed?" do
it "should return false when connected" do
expect(@client.closed?).to eql(false)
end
it "should return true after close" do
@client.close
expect(@client.closed?).to eql(true)
end
end
it "should not try to query closed mysql connection" do
client = new_client(reconnect: true)
expect(client.close).to be_nil
expect do
client.query "SELECT 1"
end.to raise_error(Mysql2::Error)
end
it "should respond to #query" do
expect(@client).to respond_to(:query)
end
it "should respond to #warning_count" do
expect(@client).to respond_to(:warning_count)
end
context "#warning_count" do
context "when no warnings" do
it "should 0" do
@client.query('select 1')
expect(@client.warning_count).to eq(0)
end
end
context "when has a warnings" do
it "should > 0" do
# "the statement produces extra information that can be viewed by issuing a SHOW WARNINGS"
# https://dev.mysql.com/doc/refman/5.7/en/show-warnings.html
@client.query('DROP TABLE IF EXISTS test.no_such_table')
expect(@client.warning_count).to be > 0
end
end
end
it "should respond to #query_info" do
expect(@client).to respond_to(:query_info)
end
context "#query_info" do
context "when no info present" do
it "should 0" do
@client.query('select 1')
expect(@client.query_info).to be_empty
expect(@client.query_info_string).to be_nil
end
end
context "when has some info" do
it "should retrieve it" do
@client.query "USE test"
@client.query "CREATE TABLE IF NOT EXISTS infoTest (`id` int(11) NOT NULL AUTO_INCREMENT, blah INT(11), PRIMARY KEY (`id`))"
# http://dev.mysql.com/doc/refman/5.0/en/mysql-info.html says
# # Note that mysql_info() returns a non-NULL value for INSERT ... VALUES only for the multiple-row form of the statement (that is, only if multiple value lists are specified).
@client.query("INSERT INTO infoTest (blah) VALUES (1234),(4535)")
expect(@client.query_info).to eql(records: 2, duplicates: 0, warnings: 0)
expect(@client.query_info_string).to eq('Records: 2 Duplicates: 0 Warnings: 0')
@client.query "DROP TABLE infoTest"
end
end
end
context ":local_infile" do
before(:context) do
new_client(local_infile: true) do |client|
local = client.query "SHOW VARIABLES LIKE 'local_infile'"
local_enabled = local.any? { |x| x['Value'] == 'ON' }
skip("DON'T WORRY, THIS TEST PASSES - but LOCAL INFILE is not enabled in your MySQL daemon.") unless local_enabled
client.query %[
CREATE TABLE IF NOT EXISTS infileTest (
id MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
foo VARCHAR(10),
bar MEDIUMTEXT
)
]
end
end
after(:context) do
new_client do |client|
client.query "DROP TABLE IF EXISTS infileTest"
end
end
it "should raise an error when local_infile is disabled" do
client = new_client(local_infile: false)
expect do
client.query "LOAD DATA LOCAL INFILE 'spec/test_data' INTO TABLE infileTest"
end.to raise_error(Mysql2::Error, /command is not allowed/)
end
it "should raise an error when a non-existent file is loaded" do
client = new_client(local_infile: true)
expect do
client.query "LOAD DATA LOCAL INFILE 'this/file/is/not/here' INTO TABLE infileTest"
end.to raise_error(Mysql2::Error, 'No such file or directory: this/file/is/not/here')
end
it "should LOAD DATA LOCAL INFILE" do
client = new_client(local_infile: true)
client.query "LOAD DATA LOCAL INFILE 'spec/test_data' INTO TABLE infileTest"
info = client.query_info
expect(info).to eql(records: 1, deleted: 0, skipped: 0, warnings: 0)
result = client.query "SELECT * FROM infileTest"
expect(result.first).to eql('id' => 1, 'foo' => 'Hello', 'bar' => 'World')
end
end
it "should expect connect_timeout to be a positive integer" do
expect do
new_client(connect_timeout: -1)
end.to raise_error(Mysql2::Error)
end
it "should expect read_timeout to be a positive integer" do
expect do
new_client(read_timeout: -1)
end.to raise_error(Mysql2::Error)
end
it "should expect write_timeout to be a positive integer" do
expect do
new_client(write_timeout: -1)
end.to raise_error(Mysql2::Error)
end
it "should allow nil read_timeout" do
client = new_client(read_timeout: nil)
expect(client.read_timeout).to be_nil
end
xit "should set default program_name in connect_attrs" do
skip("DON'T WORRY, THIS TEST PASSES - but PERFORMANCE SCHEMA is not enabled in your MySQL daemon.") unless performance_schema_enabled
client = new_client
if Mysql2::Client::CONNECT_ATTRS.zero? || client.server_info[:version].match(/10.[01].\d+-MariaDB/)
pending('Both client and server versions must be MySQL 5.6 or MariaDB 10.2 or later.')
end
result = client.query("SELECT attr_value FROM performance_schema.session_account_connect_attrs WHERE processlist_id = connection_id() AND attr_name = 'program_name'")
expect(result.first['attr_value']).to eq($PROGRAM_NAME)
end
xit "should set custom connect_attrs" do
skip("DON'T WORRY, THIS TEST PASSES - but PERFORMANCE SCHEMA is not enabled in your MySQL daemon.") unless performance_schema_enabled
client = new_client(connect_attrs: { program_name: 'my_program_name', foo: 'fooval', bar: 'barval' })
if Mysql2::Client::CONNECT_ATTRS.zero? || client.server_info[:version].match(/10.[01].\d+-MariaDB/)
pending('Both client and server versions must be MySQL 5.6 or MariaDB 10.2 or later.')
end
results = Hash[client.query("SELECT * FROM performance_schema.session_account_connect_attrs WHERE processlist_id = connection_id()").map { |x| x.values_at('ATTR_NAME', 'ATTR_VALUE') }]
expect(results['program_name']).to eq('my_program_name')
expect(results['foo']).to eq('fooval')
expect(results['bar']).to eq('barval')
end
context "#query" do
it "should let you query again if iterating is finished when streaming" do
@client.query("SELECT 1 UNION SELECT 2", stream: true, cache_rows: false).each.to_a
expect do
@client.query("SELECT 1 UNION SELECT 2", stream: true, cache_rows: false)
end.to_not raise_error
end
it "should not let you query again if iterating is not finished when streaming" do
@client.query("SELECT 1 UNION SELECT 2", stream: true, cache_rows: false).first
expect do
@client.query("SELECT 1 UNION SELECT 2", stream: true, cache_rows: false)
end.to raise_exception(Mysql2::Error)
end
it "should only accept strings as the query parameter" do
expect do
@client.query ["SELECT 'not right'"]
end.to raise_error(TypeError)
end
it "should not retain query options set on a query for subsequent queries, but should retain it in the result" do
result = @client.query "SELECT 1", something: :else
expect(@client.query_options[:something]).to be_nil
expect(result.instance_variable_get('@query_options')).to eql(@client.query_options.merge(something: :else))
expect(@client.instance_variable_get('@current_query_options')).to eql(@client.query_options.merge(something: :else))
result = @client.query "SELECT 1"
expect(result.instance_variable_get('@query_options')).to eql(@client.query_options)
expect(@client.instance_variable_get('@current_query_options')).to eql(@client.query_options)
end
it "should allow changing query options for subsequent queries" do
@client.query_options[:something] = :else
result = @client.query "SELECT 1"
expect(@client.query_options[:something]).to eql(:else)
expect(result.instance_variable_get('@query_options')[:something]).to eql(:else)
# Clean up after this test
@client.query_options.delete(:something)
expect(@client.query_options[:something]).to be_nil
end
it "should return results as a hash by default" do
expect(@client.query("SELECT 1").first).to be_an_instance_of(Hash)
end
it "should be able to return results as an array" do
expect(@client.query("SELECT 1", as: :array).first).to be_an_instance_of(Array)
@client.query("SELECT 1").each(as: :array)
end
it "should be able to return results with symbolized keys" do
expect(@client.query("SELECT 1", symbolize_keys: true).first.keys[0]).to be_an_instance_of(Symbol)
end
it "should require an open connection" do
@client.close
expect do
@client.query "SELECT 1"
end.to raise_error(Mysql2::Error)
end
it "should detect closed connection on query read error" do
connection_id = @client.thread_id
Thread.new do
sleep(0.1)
Mysql2::Client.new(DatabaseCredentials['root']).tap do |supervisor|
supervisor.query("KILL #{connection_id}")
end.close
end
expect do
@client.query("SELECT SLEEP(1)")
end.to raise_error(Mysql2::Error, /Lost connection/)
if RUBY_PLATFORM !~ /mingw|mswin/
expect do
@client.socket
end.to raise_error(Mysql2::Error, 'MySQL client is not connected')
end
end
if RUBY_PLATFORM !~ /mingw|mswin/
it "should not allow another query to be sent without fetching a result first" do
@client.query("SELECT 1", async: true)
expect do
@client.query("SELECT 1")
end.to raise_error(Mysql2::Error)
end
it "should describe the thread holding the active query" do
thr = Thread.new do
@client.query("SELECT 1", async: true)
Fiber.current
end
fiber = thr.value
expect { @client.query('SELECT 1') }.to raise_error(Mysql2::Error, Regexp.new(Regexp.escape(fiber.inspect)))
end
it "should timeout if we wait longer than :read_timeout" do
client = new_client(read_timeout: 0)
expect do
client.query('SELECT SLEEP(0.1)')
end.to raise_error(Mysql2::Error::TimeoutError)
end
# XXX this test is not deterministic (because Unix signal handling is not)
# and may fail on a loaded system
it "should run signal handlers while waiting for a response" do
kill_time = 0.25
query_time = 4 * kill_time
mark = {}
begin
trap(:USR1) { mark.store(:USR1, clock_time) }
pid = fork do
sleep kill_time # wait for client query to start
Process.kill(:USR1, Process.ppid)
sleep # wait for explicit kill to prevent GC disconnect
end
mark.store(:QUERY_START, clock_time)
@client.query("SELECT SLEEP(#{query_time})")
mark.store(:QUERY_END, clock_time)
ensure
Process.kill(:TERM, pid)
Process.waitpid2(pid)
trap(:USR1, 'DEFAULT')
end
# the query ran uninterrupted
expect(mark.fetch(:QUERY_END) - mark.fetch(:QUERY_START)).to be_within(0.2).of(query_time)
# signals fired while the query was running
expect(mark.fetch(:USR1)).to be_between(mark.fetch(:QUERY_START), mark.fetch(:QUERY_END))
end
it "#socket should return a Fixnum (file descriptor from C)" do
expect(@client.socket).to be_an_instance_of(0.class)
expect(@client.socket).not_to eql(0)
end
it "#socket should require an open connection" do
@client.close
expect do
@client.socket
end.to raise_error(Mysql2::Error)
end
it 'should be impervious to connection-corrupting timeouts in #execute' do
# attempt to break the connection
stmt = @client.prepare('SELECT SLEEP(?)')
expect { Timeout.timeout(0.1) { stmt.execute(0.2) } }.to raise_error(Timeout::Error)
stmt.close
# expect the connection to not be broken
expect { @client.query('SELECT 1') }.to_not raise_error
end if false # SKIP this test fails randomly
context 'when a non-standard exception class is raised' do
it "should close the connection when an exception is raised" do
expect { Timeout.timeout(0.1, ArgumentError) { @client.query('SELECT SLEEP(1)') } }.to raise_error(ArgumentError)
expect { @client.query('SELECT 1') }.to raise_error(Mysql2::Error, 'MySQL client is not connected')
end
it "should handle Timeouts without leaving the connection hanging if reconnect is true" do
if RUBY_PLATFORM.include?('darwin') && @client.server_info.fetch(:version).start_with?('5.5')
pending('MySQL 5.5 on OSX is afflicted by an unknown bug that breaks this test. See #633 and #634.')
end
client = new_client(reconnect: true)
expect { Timeout.timeout(0.1, ArgumentError) { client.query('SELECT SLEEP(1)') } }.to raise_error(ArgumentError)
expect { client.query('SELECT 1') }.to_not raise_error
end
it "should handle Timeouts without leaving the connection hanging if reconnect is set to true after construction" do
if RUBY_PLATFORM.include?('darwin') && @client.server_info.fetch(:version).start_with?('5.5')
pending('MySQL 5.5 on OSX is afflicted by an unknown bug that breaks this test. See #633 and #634.')
end
client = new_client
expect { Timeout.timeout(0.1, ArgumentError) { client.query('SELECT SLEEP(1)') } }.to raise_error(ArgumentError)
expect { client.query('SELECT 1') }.to raise_error(Mysql2::Error)
client.reconnect = true
expect { Timeout.timeout(0.1, ArgumentError) { client.query('SELECT SLEEP(1)') } }.to raise_error(ArgumentError)
expect { client.query('SELECT 1') }.to_not raise_error
end
end
it "threaded queries should be supported" do
sleep_time = 0.5
# Note that each thread opens its own database connection
start = clock_time
threads = Array.new(5) do
Thread.new do
new_client do |client|
client.query("SELECT SLEEP(#{sleep_time})")
end
Thread.current.object_id
end
end
values = threads.map(&:value)
stop = clock_time
# This check demonstrates that the threads are sleeping concurrently:
# In the serial case, the difference would be a multiple of sleep time
expect(stop - start).to be_within(0.2).of(sleep_time)
expect(values).to match_array(threads.map(&:object_id))
end
it "evented async queries should be supported" do
# should immediately return nil
expect(@client.query("SELECT sleep(0.1)", async: true)).to eql(nil)
io_wrapper = IO.for_fd(@client.socket, autoclose: false)
loops = 0
loops += 1 until IO.select([io_wrapper], nil, nil, 0.05)
# make sure we waited some period of time
expect(loops >= 1).to be true
result = @client.async_result
expect(result).to be_an_instance_of(Mysql2::Result)
end
end
context "Multiple results sets" do
before(:example) do
@multi_client = new_client(flags: Mysql2::Client::MULTI_STATEMENTS)
end
it "should raise an exception when one of multiple statements fails" do
result = @multi_client.query("SELECT 1 AS 'set_1'; SELECT * FROM invalid_table_name; SELECT 2 AS 'set_2';")
expect(result.first['set_1']).to be(1)
expect do
@multi_client.next_result
end.to raise_error(Mysql2::Error)
expect(@multi_client.next_result).to be false
end
it "returns multiple result sets" do
expect(@multi_client.query("SELECT 1 AS 'set_1'; SELECT 2 AS 'set_2'").first).to eql('set_1' => 1)
expect(@multi_client.next_result).to be true
expect(@multi_client.store_result.first).to eql('set_2' => 2)
expect(@multi_client.next_result).to be false
end
it "does not interfere with other statements" do
@multi_client.query("SELECT 1 AS 'set_1'; SELECT 2 AS 'set_2'")
@multi_client.store_result while @multi_client.next_result
expect(@multi_client.query("SELECT 3 AS 'next'").first).to eq('next' => 3)
end
it "will raise on query if there are outstanding results to read" do
@multi_client.query("SELECT 1; SELECT 2; SELECT 3")
expect do
@multi_client.query("SELECT 4")
end.to raise_error(Mysql2::Error)
end
it "#abandon_results! should work" do
@multi_client.query("SELECT 1; SELECT 2; SELECT 3")
@multi_client.abandon_results!
expect do
@multi_client.query("SELECT 4")
end.not_to raise_error
end
it "#more_results? should work" do
@multi_client.query("SELECT 1 AS 'set_1'; SELECT 2 AS 'set_2'")
expect(@multi_client.more_results?).to be true
@multi_client.next_result
@multi_client.store_result
expect(@multi_client.more_results?).to be false
end
it "#more_results? should work with stored procedures" do
@multi_client.query("DROP PROCEDURE IF EXISTS test_proc")
@multi_client.query("CREATE PROCEDURE test_proc() BEGIN SELECT 1 AS 'set_1'; SELECT 2 AS 'set_2'; END")
expect(@multi_client.query("CALL test_proc()").first).to eql('set_1' => 1)
expect(@multi_client.more_results?).to be true
@multi_client.next_result
expect(@multi_client.store_result.first).to eql('set_2' => 2)
@multi_client.next_result
expect(@multi_client.store_result).to be_nil # this is the result from CALL itself
expect(@multi_client.more_results?).to be false
end
end
end
it "should respond to #socket" do
expect(@client).to respond_to(:socket)
end
if RUBY_PLATFORM =~ /mingw|mswin/
it "#socket should raise as it's not supported" do
expect do
@client.socket
end.to raise_error(Mysql2::Error, /Raw access to the mysql file descriptor isn't supported on Windows/)
end
end
it "should respond to escape" do
expect(Mysql2::Client).to respond_to(:escape)
end
context "escape" do
it "should return a new SQL-escape version of the passed string" do
expect(Mysql2::Client.escape("abc'def\"ghi\0jkl%mno")).to eql("abc\\'def\\\"ghi\\0jkl%mno")
end
it "should return the passed string if nothing was escaped" do
str = "plain"
expect(Mysql2::Client.escape(str).object_id).to eql(str.object_id)
end
it "should not overflow the thread stack" do
expect do
Thread.new { Mysql2::Client.escape("'" * 256 * 1024) }.join
end.not_to raise_error
end
it "should not overflow the process stack" do
expect do
Thread.new { Mysql2::Client.escape("'" * 1024 * 1024 * 4) }.join
end.not_to raise_error
end
it "should carry over the original string's encoding" do
str = "abc'def\"ghi\0jkl%mno"
escaped = Mysql2::Client.escape(str)
expect(escaped.encoding).to eql(str.encoding)
str.encode!('us-ascii')
escaped = Mysql2::Client.escape(str)
expect(escaped.encoding).to eql(str.encoding)
end
end
it "should respond to #escape" do
expect(@client).to respond_to(:escape)
end
context "#escape" do
it "should return a new SQL-escape version of the passed string" do
expect(@client.escape("abc'def\"ghi\0jkl%mno")).to eql("abc\\'def\\\"ghi\\0jkl%mno")
end
it "should return the passed string if nothing was escaped" do
str = "plain"
expect(@client.escape(str).object_id).to eql(str.object_id)
end
it "should not overflow the thread stack" do
expect do
Thread.new { @client.escape("'" * 256 * 1024) }.join
end.not_to raise_error
end
it "should not overflow the process stack" do
expect do
Thread.new { @client.escape("'" * 1024 * 1024 * 4) }.join
end.not_to raise_error
end
it "should require an open connection" do
@client.close
expect do
@client.escape ""
end.to raise_error(Mysql2::Error)
end
context 'when mysql encoding is not utf8' do
let(:client) { new_client(encoding: "ujis") }
it 'should return a internal encoding string if Encoding.default_internal is set' do
with_internal_encoding Encoding::UTF_8 do
expect(client.escape("\u{30C6}\u{30B9}\u{30C8}")).to eq "\u{30C6}\u{30B9}\u{30C8}"
expect(client.escape("\u{30C6}'\u{30B9}\"\u{30C8}")).to eq "\u{30C6}\\'\u{30B9}\\\"\u{30C8}"
end
end
end
end
it "should respond to #info" do
expect(@client).to respond_to(:info)
end
it "#info should return a hash containing the client version ID and String" do
info = @client.info
expect(info).to be_an_instance_of(Hash)
expect(info).to have_key(:id)
expect(info[:id]).to be_an_instance_of(0.class)
expect(info).to have_key(:version)
expect(info[:version]).to be_an_instance_of(String)
end
context "strings returned by #info" do
it "should be tagged as ascii" do
expect(@client.info[:version].encoding).to eql(Encoding::US_ASCII)
expect(@client.info[:header_version].encoding).to eql(Encoding::US_ASCII)
end
end
context "strings returned by .info" do
it "should be tagged as ascii" do
expect(Mysql2::Client.info[:version].encoding).to eql(Encoding::US_ASCII)
expect(Mysql2::Client.info[:header_version].encoding).to eql(Encoding::US_ASCII)
end
end
it "should respond to #server_info" do
expect(@client).to respond_to(:server_info)
end
it "#server_info should return a hash containing the client version ID and String" do
server_info = @client.server_info
expect(server_info).to be_an_instance_of(Hash)
expect(server_info).to have_key(:id)
expect(server_info[:id]).to be_an_instance_of(0.class)
expect(server_info).to have_key(:version)
expect(server_info[:version]).to be_an_instance_of(String)
end
it "#server_info should require an open connection" do
@client.close
expect do
@client.server_info
end.to raise_error(Mysql2::Error)
end
context "strings returned by #server_info" do
it "should default to the connection's encoding if Encoding.default_internal is nil" do
with_internal_encoding nil do
expect(@client.server_info[:version].encoding).to eql(Encoding::UTF_8)
client2 = new_client(encoding: 'ascii')
expect(client2.server_info[:version].encoding).to eql(Encoding::ASCII)
end
end
it "should use Encoding.default_internal" do
with_internal_encoding Encoding::UTF_8 do
expect(@client.server_info[:version].encoding).to eql(Encoding.default_internal)
end
with_internal_encoding Encoding::ASCII do
expect(@client.server_info[:version].encoding).to eql(Encoding.default_internal)
end
end
end
it "should raise a Mysql2::Error::ConnectionError exception upon connection failure due to invalid credentials" do
expect do
new_client(host: 'localhost', username: 'asdfasdf8d2h', password: 'asdfasdfw42')
end.to raise_error(Mysql2::Error::ConnectionError)
expect do
new_client(DatabaseCredentials['root'])
end.not_to raise_error
end
context 'write operations api' do
before(:example) do
@client.query "USE test"
@client.query "CREATE TABLE IF NOT EXISTS lastIdTest (`id` BIGINT NOT NULL AUTO_INCREMENT, blah INT(11), PRIMARY KEY (`id`))"
end
after(:example) do
@client.query "DROP TABLE lastIdTest"
end
it "should respond to #last_id" do
expect(@client).to respond_to(:last_id)
end
it "#last_id should return a Fixnum, the from the last INSERT/UPDATE" do
expect(@client.last_id).to eql(0)
@client.query "INSERT INTO lastIdTest (blah) VALUES (1234)"
expect(@client.last_id).to eql(1)
end
it "should respond to #last_id" do
expect(@client).to respond_to(:last_id)
end
it "#last_id should return a Fixnum, the from the last INSERT/UPDATE" do
@client.query "INSERT INTO lastIdTest (blah) VALUES (1234)"
expect(@client.affected_rows).to eql(1)
@client.query "UPDATE lastIdTest SET blah=4321 WHERE id=1"
expect(@client.affected_rows).to eql(1)
end
it "#last_id should handle BIGINT auto-increment ids above 32 bits" do
# The id column type must be BIGINT. Surprise: INT(x) is limited to 32-bits for all values of x.
# Insert a row with a given ID, this should raise the auto-increment state
@client.query "INSERT INTO lastIdTest (id, blah) VALUES (5000000000, 5000)"
expect(@client.last_id).to eql(5000000000)
@client.query "INSERT INTO lastIdTest (blah) VALUES (5001)"
expect(@client.last_id).to eql(5000000001)
end
end
it "should respond to #thread_id" do
expect(@client).to respond_to(:thread_id)
end
it "#thread_id should be a Fixnum" do
expect(@client.thread_id).to be_an_instance_of(0.class)
end
it "should respond to #ping" do
expect(@client).to respond_to(:ping)
end
context "session_track" do
before(:example) do
unless Mysql2::Client.const_defined?(:SESSION_TRACK)
skip('Server versions must be MySQL 5.7 later.')
end
@client.query("SET @@SESSION.session_track_system_variables='*';")
end
it "returns changes system variables for SESSION_TRACK_SYSTEM_VARIABLES" do
@client.query("SET @@SESSION.session_track_state_change=ON;")
res = @client.session_track(Mysql2::Client::SESSION_TRACK_SYSTEM_VARIABLES)
expect(res).to eq(%w[session_track_state_change ON])
end
it "returns database name for SESSION_TRACK_SCHEMA" do
@client.query("USE information_schema")
res = @client.session_track(Mysql2::Client::SESSION_TRACK_SCHEMA)
expect(res).to eq(["information_schema"])
end
it "returns multiple session track type values when available" do
@client.query("SET @@SESSION.session_track_transaction_info='CHARACTERISTICS';")
res = @client.session_track(Mysql2::Client::SESSION_TRACK_SYSTEM_VARIABLES)
expect(res).to eq(%w[session_track_transaction_info CHARACTERISTICS])
res = @client.session_track(Mysql2::Client::SESSION_TRACK_STATE_CHANGE)
expect(res).to be_nil
res = @client.session_track(Mysql2::Client::SESSION_TRACK_TRANSACTION_CHARACTERISTICS)
expect(res).to eq([""])
end
it "returns valid transaction state inside a transaction" do
@client.query("SET @@SESSION.session_track_transaction_info='CHARACTERISTICS'")
@client.query("START TRANSACTION")
res = @client.session_track(Mysql2::Client::SESSION_TRACK_TRANSACTION_STATE)
expect(res).to eq(["T_______"])
end
it "returns empty array if session track type not found" do
@client.query("SET @@SESSION.session_track_state_change=ON;")
res = @client.session_track(Mysql2::Client::SESSION_TRACK_TRANSACTION_CHARACTERISTICS)
expect(res).to be_nil
end
end
context "select_db" do
before(:example) do
2.times do |i|
@client.query("CREATE DATABASE test_selectdb_#{i}")
@client.query("USE test_selectdb_#{i}")
@client.query("CREATE TABLE test#{i} (`id` int NOT NULL PRIMARY KEY)")
end
end
after(:example) do
2.times do |i|
@client.query("DROP DATABASE test_selectdb_#{i}")
end
end
it "should respond to #select_db" do
expect(@client).to respond_to(:select_db)
end
it "should switch databases" do
@client.select_db("test_selectdb_0")
expect(@client.query("SHOW TABLES").first.values.first).to eql("test0")
@client.select_db("test_selectdb_1")
expect(@client.query("SHOW TABLES").first.values.first).to eql("test1")
@client.select_db("test_selectdb_0")
expect(@client.query("SHOW TABLES").first.values.first).to eql("test0")
end
it "should raise a Mysql2::Error when the database doesn't exist" do
expect do
@client.select_db("nopenothere")
end.to raise_error(Mysql2::Error)
end
it "should return the database switched to" do
expect(@client.select_db("test_selectdb_1")).to eq("test_selectdb_1")
end
end
it "#thread_id should return a boolean" do
expect(@client.ping).to eql(true)
@client.close
expect(@client.ping).to eql(false)
end
it "should be able to connect using plaintext password" do
client = new_client(enable_cleartext_plugin: true)
client.query('SELECT 1')
end
it "should respond to #encoding" do
expect(@client).to respond_to(:encoding)
end
end
|