-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathclient_test.rb
More file actions
1155 lines (885 loc) · 32.7 KB
/
client_test.rb
File metadata and controls
1155 lines (885 loc) · 32.7 KB
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
require "test_helper"
class ClientTest < TrilogyTest
def test_trilogy_connected_host
client = new_tcp_client
# Since this method depends on the hostname of the machine,
# and therefore isn't constant, we just assert that it's set.
assert client.connected_host
ensure
ensure_closed client
end
def test_trilogy_connect_tcp
client = new_tcp_client
refute_nil client
ensure
ensure_closed client
end
def test_trilogy_connect_tcp_string_host
assert_raises TypeError do
new_tcp_client host: :localhost
end
end
def test_trilogy_connect_tcp_to_wrong_port
e = assert_raises Trilogy::ConnectionError do
new_tcp_client port: 13307
end
assert_equal "Connection refused - trilogy_connect - unable to connect to #{DEFAULT_HOST}:13307", e.message
end
def test_trilogy_connect_unix_socket
return skip unless ["127.0.0.1", "localhost"].include?(DEFAULT_HOST)
socket = new_tcp_client.query("SHOW VARIABLES LIKE 'socket'").to_a[0][1]
if !File.exist?(socket)
skip "cound not find socket at #{socket}"
end
client = new_unix_client(socket)
refute_nil client
ensure
ensure_closed client
end
def test_trilogy_connect_unix_socket_string_path
assert_raises TypeError do
new_unix_client socket: :opt_boxen_data_mysql_socket
end
end
def test_trilogy_connection_options
client = new_tcp_client
expected_connection_options = {
host: DEFAULT_HOST,
port: DEFAULT_PORT,
username: DEFAULT_USER,
password: DEFAULT_PASS,
ssl: true,
ssl_mode: 4,
tls_min_version: 3,
}
assert_equal expected_connection_options, client.connection_options
end
def test_trilogy_ping
client = new_tcp_client
assert client.ping
ensure
ensure_closed client
end
def test_trilogy_ping_after_close_raises
client = new_tcp_client
assert client.ping
client.close
assert_raises Trilogy::ConnectionClosed do
client.ping
end
ensure
ensure_closed client
end
def test_trilogy_change_db
client = new_tcp_client
assert client.change_db "test"
ensure
ensure_closed client
end
# select_db is just an alias for change_db
# and is tested here to ensure it works.
def test_trilogy_select_db
client = new_tcp_client
assert client.select_db "test"
ensure
ensure_closed client
end
def test_trilogy_change_db_after_close_raises
client = new_tcp_client
assert client.change_db "test"
client.close
assert_raises Trilogy::ConnectionClosed do
refute client.change_db "test"
end
ensure
ensure_closed client
end
def test_trilogy_query
client = new_tcp_client
assert client.query "SELECT 1"
ensure
ensure_closed client
end
def test_trilogy_query_values_vs_query_allocations
client = new_tcp_client
client.query_with_flags("SELECT 1", client.query_flags) # warm up
row_count = 1000
sql = (1..row_count).map{|i| "SELECT #{i}" }.join(" UNION ")
query_allocations = allocations { client.query_with_flags(sql, client.query_flags) }
flatten_rows_allocations = allocations { client.query_with_flags(sql, client.query_flags | Trilogy::QUERY_FLAGS_FLATTEN_ROWS) }
assert_equal query_allocations - row_count, flatten_rows_allocations
end
def test_trilogy_more_results_exist?
client = new_tcp_client(multi_statement: true)
create_test_table(client)
refute_predicate client, :more_results_exist?
client.query("INSERT INTO trilogy_test (int_test) VALUES ('4')")
refute_predicate client, :more_results_exist?
client.query("INSERT INTO trilogy_test (int_test) VALUES ('4'); INSERT INTO trilogy_test (int_test) VALUES ('1')")
assert_predicate client, :more_results_exist?
end
def test_trilogy_next_result
client = new_tcp_client(multi_statement: true)
create_test_table(client)
client.query("INSERT INTO trilogy_test (int_test) VALUES ('4')")
client.query("INSERT INTO trilogy_test (int_test) VALUES ('3')")
client.query("INSERT INTO trilogy_test (int_test) VALUES ('1')")
results = []
results << client.query("SELECT id, int_test FROM trilogy_test WHERE id = 1; SELECT id, int_test FROM trilogy_test WHERE id IN (2, 3); SELECT id, int_test FROM trilogy_test")
while (client.more_results_exist?) do
results << client.next_result
end
assert_equal 3, results.length
rs1, rs2, rs3 = results
assert_equal [[1, 4]], rs1.rows
assert_equal [[2, 3], [3, 1]], rs2.rows
assert_equal [[1, 4], [2, 3], [3, 1]], rs3.rows
end
def test_trilogy_next_result_when_no_more_results_exist
client = new_tcp_client(multi_statement: true)
create_test_table(client)
client.query("INSERT INTO trilogy_test (int_test) VALUES ('4')")
result = client.query("SELECT id, int_test FROM trilogy_test")
next_result = client.next_result
assert_equal [{ "id" => 1, "int_test" => 4 }], result.each_hash.to_a
assert_nil next_result
end
def test_trilogy_multiple_results
client = new_tcp_client
create_test_table(client)
client.query("DROP PROCEDURE IF EXISTS test_proc")
client.query("CREATE PROCEDURE test_proc() BEGIN SELECT 1 AS 'set_1'; SELECT 2 AS 'set_2'; END")
result = client.query("CALL test_proc()")
assert_equal([{ 'set_1' => 1 }], result.each_hash.to_a)
assert client.more_results_exist?
result = client.next_result
assert_equal([{ 'set_2' => 2 }], result.each_hash.to_a)
result = client.next_result
assert_equal([], result.each_hash.to_a)
refute client.more_results_exist?
end
def test_trilogy_multiple_results_doesnt_allow_multi_statement_queries
client = new_tcp_client
create_test_table(client)
assert_raises(Trilogy::QueryError) do
# Multi statement queries are not supported
client.query("SELECT 1 AS 'set_1'; SELECT 2 AS 'set_2';")
end
end
def test_trilogy_multiple_results_disabled
client = new_tcp_client(multi_result: false)
create_test_table(client)
client.query("DROP PROCEDURE IF EXISTS test_proc")
client.query("CREATE PROCEDURE test_proc() BEGIN SELECT 1 AS 'set_1'; SELECT 2 AS 'set_2'; END")
assert_raises(Trilogy::ProtocolError) do
client.query("CALL test_proc()")
end
end
def test_trilogy_next_result_raises_when_response_has_error
client = new_tcp_client(multi_statement: true)
create_test_table(client)
client.query("INSERT INTO trilogy_test (int_test) VALUES ('4')")
_rs1 = client.query("SELECT id, int_test FROM trilogy_test; SELECT non_existent_column FROM trilogy_test")
assert_raises(Trilogy::ProtocolError) do
client.next_result
end
end
def test_trilogy_query_values
client = new_tcp_client
create_test_table(client)
client.query("INSERT INTO trilogy_test (int_test) VALUES ('4')")
client.query("INSERT INTO trilogy_test (int_test) VALUES ('3')")
client.query("INSERT INTO trilogy_test (int_test) VALUES ('1')")
result = client.query_with_flags("SELECT id, int_test FROM trilogy_test", client.query_flags | Trilogy::QUERY_FLAGS_FLATTEN_ROWS)
assert_equal ["id", "int_test"], result.fields
assert_equal [1, 4, 2, 3, 3, 1], result.rows
end
def test_trilogy_set_server_option
client = new_tcp_client
create_test_table(client)
client.set_server_option(Trilogy::SET_SERVER_MULTI_STATEMENTS_ON)
client.set_server_option(Trilogy::SET_SERVER_MULTI_STATEMENTS_OFF)
end
def test_trilogy_set_server_option_with_invalid_option
client = new_tcp_client
create_test_table(client)
e = assert_raises do
client.set_server_option(42)
end
assert_instance_of(Trilogy::ProtocolError, e)
assert_match(/1047: Unknown command/, e.message)
assert_match(/trilogy_set_option_recv/, e.message)
end
def test_trilogy_set_server_option_multi_statement
# Start with multi_statement disabled, enable it during connection
client = new_tcp_client
create_test_table(client)
e = assert_raises do
client.query("INSERT INTO trilogy_test (int_test) VALUES ('4'); INSERT INTO trilogy_test (int_test) VALUES ('1')")
end
assert_instance_of(Trilogy::QueryError, e)
assert_match(/1064: You have an error in your SQL syntax/, e.message)
assert_match(/trilogy_query_recv/, e.message)
client.set_server_option(Trilogy::SET_SERVER_MULTI_STATEMENTS_ON)
client.query("INSERT INTO trilogy_test (int_test) VALUES ('4'); INSERT INTO trilogy_test (int_test) VALUES ('1')")
client.next_result while client.more_results_exist?
# Start with multi_statement enabled, disable it during connection
client = new_tcp_client(multi_statement: true)
create_test_table(client)
client.query("INSERT INTO trilogy_test (int_test) VALUES ('4'); INSERT INTO trilogy_test (int_test) VALUES ('1')")
client.next_result while client.more_results_exist?
client.set_server_option(Trilogy::SET_SERVER_MULTI_STATEMENTS_OFF)
e = assert_raises do
client.query("INSERT INTO trilogy_test (int_test) VALUES ('4'); INSERT INTO trilogy_test (int_test) VALUES ('1')")
end
assert_instance_of(Trilogy::QueryError, e)
assert_match(/1064: You have an error in your SQL syntax/, e.message)
assert_match(/trilogy_query_recv/, e.message)
end
def test_trilogy_query_result_object
client = new_tcp_client
result = client.query "SELECT 1 AS a, 2 AS b"
assert_equal ["a", "b"], result.fields
assert_equal [[1, 2]], result.rows
assert_equal [{ "a" => 1, "b" => 2 }], result.each_hash.to_a
assert_equal [[1, 2]], result.to_a
assert_kind_of Float, result.query_time
assert_in_delta 0.1, result.query_time, 0.1
ensure
ensure_closed client
end
def test_trilogy_query_after_close_raises
client = new_tcp_client
assert client.query "SELECT 1"
client.close
assert_raises Trilogy::ConnectionClosed do
refute client.query "SELECT 1"
end
ensure
ensure_closed client
end
def test_trilogy_last_insert_id
client = new_tcp_client
create_test_table(client)
client.query "TRUNCATE trilogy_test"
result_a = client.query "INSERT INTO trilogy_test (varchar_test) VALUES ('a')"
assert result_a
assert_equal 1, client.last_insert_id
result_b = client.query "INSERT INTO trilogy_test (varchar_test) VALUES ('b')"
assert result_b
assert_equal 2, client.last_insert_id
result_select = client.query("SELECT varchar_test FROM trilogy_test")
assert_equal 0, client.last_insert_id
assert_equal 1, result_a.last_insert_id
assert_equal 2, result_b.last_insert_id
assert_nil result_select.last_insert_id
ensure
ensure_closed client
end
def test_trilogy_affected_rows
client = new_tcp_client
create_test_table(client)
client.query("INSERT INTO trilogy_test (varchar_test, int_test) VALUES ('a', 1)")
result_unchanged = client.query("UPDATE trilogy_test SET int_test = 1 WHERE varchar_test = 'a'")
assert_equal 0, client.affected_rows
result_changed = client.query("UPDATE trilogy_test SET int_test = 2 WHERE varchar_test = 'a'")
assert_equal 1, client.affected_rows
result_select = client.query("SELECT int_test FROM trilogy_test WHERE varchar_test = 'a'")
assert_equal 0, client.affected_rows
assert_equal 0, result_unchanged.affected_rows
assert_equal 1, result_changed.affected_rows
assert_nil result_select.affected_rows
ensure
ensure_closed client
end
def test_trilogy_affected_rows_in_found_rows_mode
client = new_tcp_client(:found_rows => true)
create_test_table(client)
client.query("INSERT INTO trilogy_test (varchar_test, int_test) VALUES ('a', 1)")
result_unchanged = client.query("UPDATE trilogy_test SET int_test = 1 WHERE varchar_test = 'a'")
assert_equal 1, client.affected_rows
result_changed = client.query("UPDATE trilogy_test SET int_test = 2 WHERE varchar_test = 'a'")
assert_equal 1, client.affected_rows
result_select = client.query("SELECT int_test FROM trilogy_test WHERE varchar_test = 'a'")
assert_equal 0, client.affected_rows
assert_equal 1, result_unchanged.affected_rows
assert_equal 1, result_changed.affected_rows
assert_nil result_select.affected_rows
ensure
ensure_closed client
end
def test_trilogy_warning_count
client = new_tcp_client
create_test_table(client)
result = client.query "INSERT INTO trilogy_test (varchar_test) VALUES ('a')"
assert result
assert_equal 0, client.warning_count
result = client.query "SELECT 1 + 2"
assert result
assert_equal 0, client.warning_count
# this field is only 10 characters wide
longer_val = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
result = client.query "INSERT INTO trilogy_test (varchar_test) VALUES ('#{longer_val}')"
assert result
assert_equal 1, client.warning_count
ensure
ensure_closed client
end
def test_trilogy_close_twice_succeeds
client = new_tcp_client
assert_nil client.close
assert_nil client.close
end
def test_trilogy_escape
client = new_tcp_client
assert_equal "hello", client.escape("hello")
assert_equal "\\\"\\0\\'\\\\\\n\\r\\Z",
client.escape("\"\0\'\\\n\r\x1A")
assert_equal "\xff", client.escape("\xff")
str = "binary string".encode(Encoding::Windows_1252)
assert_equal Encoding::Windows_1252, client.escape(str).encoding
ensure
ensure_closed client
end
def test_trilogy_escape_ascii_compat
client = new_tcp_client
assert_raises Encoding::CompatibilityError do
client.escape("'\"\\".encode("UTF-16LE"))
end
ensure
ensure_closed client
end
def test_trilogy_escape_no_blackslash_escapes
client = new_tcp_client
client.query("SET SQL_MODE=NO_BACKSLASH_ESCAPES")
assert_equal "hello '' world", client.escape("hello ' world")
ensure
ensure_closed client
end
def test_trilogy_closed?
client = new_tcp_client
refute_predicate client, :closed?
client.close
assert_predicate client, :closed?
ensure
ensure_closed client
end
def test_trilogy_check
client = new_tcp_client
assert_equal true, client.check
client.close
assert_raises Trilogy::ConnectionClosed do
client.check
end
ensure
ensure_closed client
end
def test_read_timeout
client = new_tcp_client(read_timeout: 0.1)
assert_raises Trilogy::TimeoutError do
client.query("SELECT SLEEP(1)")
end
ensure
ensure_closed client
end
def test_adjustable_read_timeout
client = new_tcp_client(read_timeout: 5)
assert client.query("SELECT SLEEP(0.2)");
client.read_timeout = 0.1
assert_equal 0.1, client.read_timeout
assert_raises Trilogy::TimeoutError do
client.query("SELECT SLEEP(1)")
end
ensure
ensure_closed client
end
def test_read_timeout_closed_connection
client = new_tcp_client(read_timeout: 5)
client.close
ensure_closed client
assert_raises Trilogy::ConnectionClosed do
client.read_timeout
end
assert_raises Trilogy::ConnectionClosed do
client.read_timeout = 42
end
end
def test_adjustable_write_timeout
client = new_tcp_client(write_timeout: 5)
assert_equal 5.0, client.write_timeout
client.write_timeout = 0.1
assert_equal 0.1, client.write_timeout
ensure
ensure_closed client
end
def test_write_timeout_closed_connection
client = new_tcp_client
client.close
ensure_closed client
assert_raises Trilogy::ConnectionClosed do
client.write_timeout
end
assert_raises Trilogy::ConnectionClosed do
client.write_timeout = 42
end
end
def test_handshake_timeout
serv = TCPServer.new(0)
port = serv.addr[1]
assert_raises Trilogy::TimeoutError do
new_tcp_client(host: "127.0.0.1", port: port, connect_timeout: 0.1)
end
ensure
ensure_closed serv
end
def test_connect_timeout
assert_raises Trilogy::TimeoutError do
# 192.0.2.0/24 is TEST-NET-1 which should only be for docs/examples
new_tcp_client(host: "192.0.2.1", connect_timeout: 0.1)
end
end
def test_connect_timeout_with_only_write_timeout
assert_raises Trilogy::TimeoutError do
# 192.0.2.0/24 is TEST-NET-1 which should only be for docs/examples
new_tcp_client(host: "192.0.2.1", write_timeout: 0.1)
end
end
def test_large_query
client = new_tcp_client
size = 1_000_000
assert_equal size, client.query("SELECT '#{"1" * size}'").to_a[0][0].size
ensure
ensure_closed client
end
def test_cast_exception_during_query_does_not_close_the_connection
client = new_tcp_client
create_test_table(client)
client.query("INSERT INTO trilogy_test (date_time_test) VALUES ('4321-01-01 00:00:00')")
client.query("INSERT INTO trilogy_test (date_time_test) VALUES ('1234-00-00 00:00:00')")
client.query("INSERT INTO trilogy_test (date_time_test) VALUES ('2345-12-31 00:00:00')")
err = assert_raises Trilogy::Error do
client.query("SELECT date_time_test FROM trilogy_test")
end
assert_equal "Invalid date: 1234-00-00 00:00:00", err.message
assert_raises_connection_error do
client.ping
end
end
def test_client_side_timeout_checks_result_set
client = new_tcp_client
create_test_table(client)
client.query("INSERT INTO trilogy_test (varchar_test, int_test) VALUES ('a', 2)")
client.query("INSERT INTO trilogy_test (varchar_test, int_test) VALUES ('b', 2)")
client.query("INSERT INTO trilogy_test (varchar_test, int_test) VALUES ('c', 2)")
assert_raises Timeout::Error do
Timeout::timeout(0.1) do
client.query("SELECT SLEEP(1)")
end
end
assert_raises_connection_error do
client.query("SELECT varchar_test FROM trilogy_test WHERE int_test = 2").to_a
end
end
def assert_elapsed(expected, delta)
start = Process.clock_gettime Process::CLOCK_MONOTONIC
yield
elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
assert_in_delta(expected, elapsed, delta)
end
def test_timeout_deadlines
assert_elapsed(0.1, 0.3) do
client = new_tcp_client
assert_raises Timeout::Error do
Timeout::timeout(0.1) do
client.query("SELECT SLEEP(1)")
end
end
assert_raises_connection_error do
client.query("SELECT 'hello'").to_a
end
end
end
def test_timeout_error
client_1 = new_tcp_client
client_2 = new_tcp_client
create_test_table(client_1)
client_2.change_db("test")
client_1.query("INSERT INTO trilogy_test (varchar_test) VALUES ('a')")
client_1.query("BEGIN")
client_1.query("SELECT * FROM trilogy_test FOR UPDATE")
client_2.query("SET SESSION innodb_lock_wait_timeout = 1;")
assert_raises Trilogy::TimeoutError do
client_2.query("SELECT * FROM trilogy_test FOR UPDATE")
end
ensure
ensure_closed(client_1)
ensure_closed(client_2)
end
def test_connection_closed_error
client = new_tcp_client
client.close
err = assert_raises Trilogy::ConnectionClosed do
client.query("SELECT 1");
end
assert_equal "Attempted to use closed connection", err.message
end
def test_query_error
client = new_tcp_client
err = assert_raises Trilogy::QueryError do
client.query("not legit sqle")
end
assert_equal 1064, err.error_code
assert_includes err.message, "You have an error in your SQL syntax"
# test that the connection is not closed due to 'routine' errors
assert client.ping
ensure
ensure_closed client
end
def test_releases_gvl
client = new_tcp_client
assert_raises Timeout::Error do
Timeout.timeout(0.1) do
client.query("SELECT SLEEP(1)")
end
end
assert_raises_connection_error do
client.ping
end
end
def test_prevent_concurrent_use
client = new_tcp_client
thread = Thread.new { client.query("SELECT SLEEP(1)") }
thread.join(0.2)
assert_raises Trilogy::SynchronizationError do
client.query("SELECT 1")
end
thread.join
client.close
end
USR1 = Class.new(StandardError)
def test_interruptible_when_releasing_gvl
client = new_tcp_client
old_usr1 = trap "USR1" do
raise USR1
end
pid = fork do
sleep 0.1
Process.kill(:USR1, Process.ppid)
end
assert_raises USR1 do
client.query("SELECT SLEEP(1)")
end
ensure
Process.wait(pid)
trap "USR1", old_usr1
ensure_closed client
end
def test_in_transaction_status
client = new_tcp_client
assert !client.in_transaction?
client.query "START TRANSACTION"
assert client.in_transaction?
client.query "COMMIT"
assert !client.in_transaction?
ensure
ensure_closed client
end
def test_server_version
client = new_tcp_client
assert_match %r{\A\d+\.\d+\.\d+}, client.server_version
end
def test_server_info
client = new_tcp_client
server_info = client.server_info
assert_kind_of 0.class, server_info[:id]
assert_kind_of String, server_info[:version]
end
def test_connect_by_multiple_names
return skip unless ["127.0.0.1", "localhost"].include?(DEFAULT_HOST)
Trilogy.new(host: "127.0.0.1")
Trilogy.new(host: "localhost")
end
PADDED_QUERY_TEMPLATE = "SELECT LENGTH('%s')"
PROTOCOL_OVERHEAD = 2 # One byte for the 0x03 (COM_QUERY); one because the packet is actually required to be shorter than the "max"
PADDED_QUERY_OVERHEAD =
PADDED_QUERY_TEMPLATE.size - "%s".size + PROTOCOL_OVERHEAD
def query_for_target_packet_size(size)
PADDED_QUERY_TEMPLATE % ("x" * (size - PADDED_QUERY_OVERHEAD))
end
def set_max_allowed_packet(size)
client = new_tcp_client
client.query "SET GLOBAL max_allowed_packet = #{size}"
ensure
ensure_closed client
end
def test_packet_size_lower_than_trilogy_max_packet_len
set_max_allowed_packet(4 * 1024 * 1024) # TRILOGY_MAX_PACKET_LEN is 16MB
client = new_tcp_client(max_allowed_packet: 4 * 1024 * 1024)
assert client.query query_for_target_packet_size(1 * 1024 * 1024)
assert client.query query_for_target_packet_size(2 * 1024 * 1024)
assert client.query query_for_target_packet_size(4 * 1024 * 1024)
exception = assert_raises Trilogy::QueryError do
client.query query_for_target_packet_size(4 * 1024 * 1024 + 1)
end
assert_equal "trilogy_query_send: TRILOGY_MAX_PACKET_EXCEEDED", exception.message
assert client.ping
ensure
ensure_closed client
end
def test_packet_size_greater_than_trilogy_max_packet_len
set_max_allowed_packet(32 * 1024 * 1024) # TRILOGY_MAX_PACKET_LEN is 16MB
client = new_tcp_client(max_allowed_packet: 32 * 1024 * 1024)
assert client.query query_for_target_packet_size(16 * 1024 * 1024)
assert client.query query_for_target_packet_size(32 * 1024 * 1024)
exception = assert_raises Trilogy::QueryError do
client.query query_for_target_packet_size(32 * 1024 * 1024 + 1)
end
assert_equal "trilogy_query_send: TRILOGY_MAX_PACKET_EXCEEDED", exception.message
assert client.ping
ensure
ensure_closed client
end
def test_configured_max_packet_below_server
set_max_allowed_packet(32 * 1024 * 1024)
client = new_tcp_client(max_allowed_packet: 24 * 1024 * 1024)
assert client.query query_for_target_packet_size(16 * 1024 * 1024)
assert client.query query_for_target_packet_size(24 * 1024 * 1024)
exception = assert_raises Trilogy::QueryError do
client.query query_for_target_packet_size(24 * 1024 * 1024 + 1)
end
assert_equal "trilogy_query_send: TRILOGY_MAX_PACKET_EXCEEDED", exception.message
exception = assert_raises Trilogy::QueryError do
client.query query_for_target_packet_size(32 * 1024 * 1024 + 1)
end
assert_equal "trilogy_query_send: TRILOGY_MAX_PACKET_EXCEEDED", exception.message
assert client.ping
ensure
ensure_closed client
end
def test_configured_max_packet_above_server
set_max_allowed_packet(24 * 1024 * 1024)
client = new_tcp_client(max_allowed_packet: 32 * 1024 * 1024)
assert client.query query_for_target_packet_size(16 * 1024 * 1024)
assert client.query query_for_target_packet_size(24 * 1024 * 1024)
exception = assert_raises Trilogy::QueryError do
client.query query_for_target_packet_size(32 * 1024 * 1024 + 1)
end
assert_equal "trilogy_query_send: TRILOGY_MAX_PACKET_EXCEEDED", exception.message
exception = assert_raises_connection_error do
client.query query_for_target_packet_size(24 * 1024 * 1024 + 1)
end
refute_match(/TRILOGY_MAX_PACKET_EXCEEDED/, exception.message)
assert_raises_connection_error do
client.ping
end
ensure
ensure_closed client
end
def test_absolute_maximum_packet_size
skip unless ENV["CI"]
set_max_allowed_packet(1024 * 1024 * 1024) # 1GB is the highest maximum allowed
client = new_tcp_client(max_allowed_packet: 1024 * 1024 * 1024)
assert client.query query_for_target_packet_size(1024 * 1024 * 1024)
exception = assert_raises Trilogy::QueryError do
client.query query_for_target_packet_size(1024 * 1024 * 1024 + 1)
end
assert_equal "trilogy_query_send: TRILOGY_MAX_PACKET_EXCEEDED", exception.message
assert client.ping
ensure
ensure_closed client
end
def test_too_many_connections
connection_error_packet = [
0x17, 0x00, 0x00, 0x00, 0xff, 0x10, 0x04, 0x54,
0x6f, 0x6f, 0x20, 0x6d, 0x61, 0x6e, 0x79, 0x20,
0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69,
0x6f, 0x6e, 0x73
].pack("c*")
fake_server = TCPServer.new("127.0.0.1", 0)
_, fake_port = fake_server.addr
accept_thread = Thread.new do
# this will block until we connect below this thread creation
write_side = fake_server.accept
write_side.write(connection_error_packet)
write_side.close
end
ex = assert_raises Trilogy::ProtocolError do
new_tcp_client(host: "127.0.0.1", port: fake_port)
end
assert_equal 1040, ex.error_code
assert ex.is_a?(Trilogy::DatabaseError)
ensure
accept_thread.join
fake_server.close
end
def test_no_reconnect_on_error
client = new_tcp_client
connection_id = client.query("SELECT CONNECTION_ID()").to_a.first.first
assert_raises Trilogy::ProtocolError do
client.query("SELECT /*+ MAX_EXECUTION_TIME(10) */ WAIT_FOR_EXECUTED_GTID_SET('01e4737c-9752-11e8-a17a-d40393d98615:1-76747', 1.01)")
end
assert_equal client.query("SELECT CONNECTION_ID()").first.first, connection_id
end
def test_gtid_support
client = new_tcp_client
if client.query("SELECT @@GLOBAL.log_bin").first == [0]
return skip("bin_log needs to be enabled for GTID support")
end
# Run these in case we're still in OFF mode to go step by step to ON
client.query "SET GLOBAL server_id = 1"
client.query "SET GLOBAL gtid_mode = OFF_PERMISSIVE" rescue nil
client.query "SET GLOBAL gtid_mode = ON_PERMISSIVE" rescue nil
client.query "SET GLOBAL enforce_gtid_consistency = ON"
client.query "SET GLOBAL gtid_mode = ON"
client.query "SET SESSION session_track_gtids = OWN_GTID"
create_test_table(client)
client.query "TRUNCATE trilogy_test"
result = client.query "INSERT INTO trilogy_test (varchar_test) VALUES ('a')"
last_gtid = client.last_gtid
result = client.query "SHOW GLOBAL VARIABLES LIKE 'gtid_executed'"
gtid_set = result.rows.first[1]
gtid, set = gtid_set.split(":")
last = set.split("-").last
assert_equal "#{gtid}:#{last}", last_gtid
end
def test_connection_refused
fake_server = TCPServer.new("127.0.0.1", 0)
_, fake_port = fake_server.addr
fake_server.close
assert_raises Trilogy::ConnectionError do
new_tcp_client(host: "127.0.0.1", port: fake_port)
end
end
def test_connection_invalid_dns
ex = assert_raises Trilogy::ConnectionError do
new_tcp_client(host: "mysql.invalid", port: 3306)
end
assert_equal "trilogy_connect - unable to connect to mysql.invalid:3306: TRILOGY_DNS_ERROR", ex.message
end
def test_memsize
require 'objspace'
client = new_tcp_client
assert_kind_of Integer, ObjectSpace.memsize_of(client)
end
def test_trilogy_int_sum_query