Skip to content

Commit 78151d4

Browse files
authored
Merge pull request #387 from sparklemotion/flavorjones-test-valgrind-rake-task
Add a valgrind test task, and clean up resource management in the tests
2 parents 69c9fa7 + a9bddc7 commit 78151d4

File tree

10 files changed

+132
-26
lines changed

10 files changed

+132
-26
lines changed

.github/workflows/sqlite3-ruby.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,16 @@ jobs:
119119
vcpkg: sqlcipher
120120
- run: bundle exec rake compile -- --with-sqlcipher
121121
- run: bundle exec rake test
122+
123+
valgrind:
124+
runs-on: ubuntu-latest
125+
steps:
126+
- uses: actions/checkout@v3
127+
- uses: ruby/setup-ruby-pkgs@v1
128+
with:
129+
ruby-version: "3.2"
130+
bundler-cache: true
131+
apt-get: libsqlite3-dev valgrind
132+
- run: bundle install
133+
- run: bundle exec rake compile
134+
- run: bundle exec rake test:valgrind

Gemfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
source "https://rubygems.org"
22

33
gemspec
4+
5+
gem("minitest", "~> 5.15")
6+
gem("rake-compiler", "~> 1.2.0")
7+
gem("rake-compiler-dock", "1.3.0")
8+
gem("rdoc", ">= 4.0", "< 7")
9+
gem("psych", "~> 4.0") # psych 5 doesn't build on some CI platforms yet
10+
11+
gem("ruby_memcheck") if Gem::Platform.local.os == "linux"

rakelib/test.rake

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
require "rake/testtask"
2-
3-
Rake::TestTask.new(:test) do |t|
2+
test_config = lambda do |t|
43
t.libs << "test"
54
t.libs << "lib"
65
t.test_files = FileList["test/**/test_*.rb"]
76
end
7+
8+
Rake::TestTask.new(:test, &test_config)
9+
10+
begin
11+
require "ruby_memcheck"
12+
13+
RubyMemcheck.config(binary_name: "sqlite3_native")
14+
15+
namespace :test do
16+
RubyMemcheck::TestTask.new(:valgrind, &test_config)
17+
end
18+
rescue LoadError => e
19+
warn("NOTE: ruby_memcheck is not available in this environment: #{e}")
20+
end

sqlite3.gemspec

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,5 @@ Gem::Specification.new do |s|
108108

109109
s.add_dependency("mini_portile2", "~> 2.8.0")
110110

111-
s.add_development_dependency("minitest", "~> 5.15")
112-
s.add_development_dependency("rake-compiler", "~> 1.2.0")
113-
s.add_development_dependency("rake-compiler-dock", "1.3.0")
114-
s.add_development_dependency("rdoc", ">= 4.0", "< 7")
115-
s.add_development_dependency("psych", "~> 4.0") # psych 5 doesn't build on some CI platforms yet
116-
117111
s.extensions << "ext/sqlite3/extconf.rb"
118112
end

test/test_database.rb

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ def setup
1111
super
1212
end
1313

14+
def teardown
15+
@db.close unless @db.closed?
16+
end
17+
1418
def test_segv
1519
assert_raises { SQLite3::Database.new 1 }
1620
end
@@ -54,6 +58,7 @@ def test_filename_to_path
5458
assert_equal pn.realdirpath.to_s, File.realdirpath(db.filename)
5559
ensure
5660
tf.close! if tf
61+
db.close if db
5762
end
5863

5964

@@ -189,6 +194,8 @@ def test_execute_batch2
189194
def test_new
190195
db = SQLite3::Database.new(':memory:')
191196
assert db
197+
ensure
198+
db.close if db
192199
end
193200

194201
def test_new_yields_self
@@ -210,6 +217,8 @@ def test_new_with_options
210217
:utf16 => true)
211218
end
212219
assert db
220+
ensure
221+
db.close if db
213222
end
214223

215224
def test_close
@@ -243,6 +252,8 @@ def test_prepare
243252
db = SQLite3::Database.new(':memory:')
244253
stmt = db.prepare('select "hello world"')
245254
assert_instance_of(SQLite3::Statement, stmt)
255+
ensure
256+
stmt.close if stmt
246257
end
247258

248259
def test_block_prepare_does_not_double_close
@@ -459,15 +470,19 @@ def step a
459470
end
460471

461472
def test_authorizer_ok
473+
statements = []
474+
462475
@db.authorizer = Class.new {
463476
def call action, a, b, c, d; true end
464477
}.new
465-
@db.prepare("select 'fooooo'")
478+
statements << @db.prepare("select 'fooooo'")
466479

467480
@db.authorizer = Class.new {
468481
def call action, a, b, c, d; 0 end
469482
}.new
470-
@db.prepare("select 'fooooo'")
483+
statements << @db.prepare("select 'fooooo'")
484+
ensure
485+
statements.each(&:close)
471486
end
472487

473488
def test_authorizer_ignore
@@ -476,6 +491,8 @@ def call action, a, b, c, d; nil end
476491
}.new
477492
stmt = @db.prepare("select 'fooooo'")
478493
assert_nil stmt.step
494+
ensure
495+
stmt.close if stmt
479496
end
480497

481498
def test_authorizer_fail
@@ -496,22 +513,29 @@ def call action, a, b, c, d; false end
496513
end
497514

498515
@db.authorizer = nil
499-
@db.prepare("select 'fooooo'")
516+
s = @db.prepare("select 'fooooo'")
517+
ensure
518+
s.close if s
500519
end
501520

502521
def test_close_with_open_statements
503-
@db.prepare("select 'foo'")
522+
s = @db.prepare("select 'foo'")
504523
assert_raises(SQLite3::BusyException) do
505524
@db.close
506525
end
526+
ensure
527+
s.close if s
507528
end
508529

509530
def test_execute_with_empty_bind_params
510531
assert_equal [['foo']], @db.execute("select 'foo'", [])
511532
end
512533

513534
def test_query_with_named_bind_params
514-
assert_equal [['foo']], @db.query("select :n", {'n' => 'foo'}).to_a
535+
resultset = @db.query("select :n", {'n' => 'foo'})
536+
assert_equal [['foo']], resultset.to_a
537+
ensure
538+
resultset.close if resultset
515539
end
516540

517541
def test_execute_with_named_bind_params

test/test_deprecated.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
module SQLite3
44
class TestDeprecated < SQLite3::TestCase
5-
attr_reader :db
6-
75
def setup
86
super
97
@warn_before = $-w
@@ -15,22 +13,29 @@ def setup
1513
def teardown
1614
super
1715
$-w = @warn_before
16+
@db.close
1817
end
1918

2019
def test_query_with_many_bind_params_not_nil
21-
assert_equal [[1, 2]], db.query('select ?, ?', 1, 2).to_a
20+
rs = @db.query('select ?, ?', 1, 2)
21+
assert_equal [[1, 2]], rs.to_a
22+
rs.close
2223
end
2324

2425
def test_execute_with_many_bind_params_not_nil
2526
assert_equal [[1, 2]], @db.execute("select ?, ?", 1, 2).to_a
2627
end
2728

2829
def test_query_with_many_bind_params
29-
assert_equal [[nil, 1]], @db.query("select ?, ?", nil, 1).to_a
30+
rs = @db.query("select ?, ?", nil, 1)
31+
assert_equal [[nil, 1]], rs.to_a
32+
rs.close
3033
end
3134

3235
def test_query_with_nil_bind_params
33-
assert_equal [['foo']], @db.query("select 'foo'", nil).to_a
36+
rs = @db.query("select 'foo'", nil)
37+
assert_equal [['foo']], rs.to_a
38+
rs.close
3439
end
3540

3641
def test_execute_with_many_bind_params

test/test_encoding.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ def setup
1111
@db.execute(@create);
1212
end
1313

14+
def teardown
15+
@db.close
16+
end
17+
1418
def test_select_encoding_on_utf_16
1519
str = "foo"
1620
utf16 = ([1].pack("I") == [1].pack("N")) ? "UTF-16BE" : "UTF-16LE"
@@ -24,6 +28,7 @@ def test_select_encoding_on_utf_16
2428
assert_equal 1, stmt.to_a.length
2529
stmt.reset!
2630
end
31+
stmt.close
2732
end
2833

2934
def test_insert_encoding
@@ -39,6 +44,7 @@ def test_insert_encoding
3944
stmt.to_a
4045
stmt.reset!
4146
end
47+
stmt.close
4248

4349
db.execute('select data from ex').flatten.each do |s|
4450
assert_equal str, s
@@ -55,6 +61,7 @@ def test_default_internal_is_honored
5561
stmt = @db.prepare('insert into ex(data) values (?)')
5662
stmt.bind_param 1, str
5763
stmt.step
64+
stmt.close
5865

5966
Encoding.default_internal = 'EUC-JP'
6067
string = @db.execute('select data from ex').first.first
@@ -73,6 +80,7 @@ def test_blob_is_binary
7380
stmt = @db.prepare('insert into foo(data) values (?)')
7481
stmt.bind_param(1, SQLite3::Blob.new(str))
7582
stmt.step
83+
stmt.close
7684

7785
string = @db.execute('select data from foo').first.first
7886
assert_equal Encoding.find('ASCII-8BIT'), string.encoding
@@ -85,6 +93,7 @@ def test_blob_is_ascii8bit
8593
stmt = @db.prepare('insert into foo(data) values (?)')
8694
stmt.bind_param(1, str.dup.force_encoding("ASCII-8BIT"))
8795
stmt.step
96+
stmt.close
8897

8998
string = @db.execute('select data from foo').first.first
9099
assert_equal Encoding.find('ASCII-8BIT'), string.encoding
@@ -97,6 +106,7 @@ def test_blob_with_eucjp
97106
stmt = @db.prepare('insert into foo(data) values (?)')
98107
stmt.bind_param(1, SQLite3::Blob.new(str))
99108
stmt.step
109+
stmt.close
100110

101111
string = @db.execute('select data from foo').first.first
102112
assert_equal Encoding.find('ASCII-8BIT'), string.encoding

test/test_result_set.rb

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,46 @@
22

33
module SQLite3
44
class TestResultSet < SQLite3::TestCase
5+
def setup
6+
@db = SQLite3::Database.new ':memory:'
7+
super
8+
end
9+
10+
def teardown
11+
super
12+
@db.close
13+
end
14+
515
def test_each_hash
6-
db = SQLite3::Database.new ':memory:'
7-
db.execute "create table foo ( a integer primary key, b text )"
16+
@db.execute "create table foo ( a integer primary key, b text )"
817
list = ('a'..'z').to_a
918
list.each do |t|
10-
db.execute "insert into foo (b) values (\"#{t}\")"
19+
@db.execute "insert into foo (b) values (\"#{t}\")"
1120
end
1221

13-
rs = db.prepare('select * from foo').execute
22+
rs = @db.prepare('select * from foo').execute
1423
rs.each_hash do |hash|
1524
assert_equal list[hash['a'] - 1], hash['b']
1625
end
26+
rs.close
1727
end
1828

1929
def test_next_hash
20-
db = SQLite3::Database.new ':memory:'
21-
db.execute "create table foo ( a integer primary key, b text )"
30+
@db.execute "create table foo ( a integer primary key, b text )"
2231
list = ('a'..'z').to_a
2332
list.each do |t|
24-
db.execute "insert into foo (b) values (\"#{t}\")"
33+
@db.execute "insert into foo (b) values (\"#{t}\")"
2534
end
2635

27-
rs = db.prepare('select * from foo').execute
36+
rs = @db.prepare('select * from foo').execute
2837
rows = []
2938
while row = rs.next_hash
3039
rows << row
3140
end
3241
rows.each do |hash|
3342
assert_equal list[hash['a'] - 1], hash['b']
3443
end
44+
rs.close
3545
end
3646
end
3747
end

0 commit comments

Comments
 (0)