Skip to content

Commit 0fc4e0e

Browse files
committed
clean up connections in specs where we can
1 parent 46f4fcd commit 0fc4e0e

File tree

5 files changed

+25
-12
lines changed

5 files changed

+25
-12
lines changed

spec/em/em_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
defer1 = client1.query "SELECT sleep(0.1) as first_query"
1313
defer1.callback do |result|
1414
results << result.first
15+
client1.close
1516
EM.stop_event_loop
1617
end
1718

1819
client2 = Mysql2::EM::Client.new DatabaseCredentials['root']
1920
defer2 = client2.query "SELECT sleep(0.025) second_query"
2021
defer2.callback do |result|
2122
results << result.first
23+
client2.close
2224
end
2325
end
2426

@@ -36,6 +38,7 @@
3638
defer2 = client.query "SELECT sleep(0.025) as second_query"
3739
defer2.callback do |r|
3840
results << r.first
41+
client.close
3942
EM.stop_event_loop
4043
end
4144
end
@@ -51,6 +54,7 @@
5154
client = Mysql2::EM::Client.new DatabaseCredentials['root']
5255
defer = client.query "SELECT sleep(0.1) as first_query"
5356
defer.callback do |result|
57+
client.close
5458
raise 'some error'
5559
end
5660
defer.errback do |err|

spec/mysql2/client_spec.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
require 'spec_helper'
33

44
describe Mysql2::Client do
5-
before(:each) do
6-
@client = Mysql2::Client.new DatabaseCredentials['root']
7-
end
8-
95
it "should raise an exception upon connection failure" do
106
lambda {
117
# The odd local host IP address forces the mysql client library to
@@ -88,6 +84,8 @@ def connect *args
8884
results[1]['Value'].should_not be_nil
8985
results[1]['Value'].should be_kind_of(String)
9086
results[1]['Value'].should_not be_empty
87+
88+
ssl_client.close
9189
end
9290

9391
it "should respond to #close" do

spec/mysql2/error_spec.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,22 @@
33

44
describe Mysql2::Error do
55
before(:each) do
6-
@client = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => "utf8"))
76
begin
8-
@client.query("HAHAHA")
7+
client = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => "big5"))
8+
client.query("HAHAHA")
99
rescue Mysql2::Error => e
1010
@error = e
11+
ensure
12+
client.close
1113
end
1214

13-
@client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => "big5"))
1415
begin
15-
@client2.query("HAHAHA")
16+
client = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => "big5"))
17+
client.query("HAHAHA")
1618
rescue Mysql2::Error => e
1719
@error2 = e
20+
ensure
21+
client.close
1822
end
1923
end
2024

spec/mysql2/result_spec.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
require 'spec_helper'
33

44
describe Mysql2::Result do
5-
before(:each) do
6-
@client = Mysql2::Client.new DatabaseCredentials['root']
7-
end
8-
95
before(:each) do
106
@result = @client.query "SELECT 1"
117
end
@@ -330,6 +326,7 @@
330326
client2.query "USE test"
331327
result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
332328
result['enum_test'].encoding.should eql(Encoding.find('us-ascii'))
329+
client2.close
333330
end
334331

335332
it "should use Encoding.default_internal" do
@@ -359,6 +356,7 @@
359356
client2.query "USE test"
360357
result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
361358
result['set_test'].encoding.should eql(Encoding.find('us-ascii'))
359+
client2.close
362360
end
363361

364362
it "should use Encoding.default_internal" do
@@ -441,6 +439,7 @@
441439
client2.query "USE test"
442440
result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
443441
result[field].encoding.should eql(Encoding.find('us-ascii'))
442+
client2.close
444443
end
445444

446445
it "should use Encoding.default_internal" do

spec/spec_helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
DatabaseCredentials = YAML.load_file('spec/configuration.yml')
88

99
RSpec.configure do |config|
10+
config.before :each do
11+
@client = Mysql2::Client.new DatabaseCredentials['root']
12+
end
13+
14+
config.after :each do
15+
@client.close
16+
end
17+
1018
config.before(:all) do
1119
client = Mysql2::Client.new DatabaseCredentials['root']
1220
client.query %[

0 commit comments

Comments
 (0)