From 3107a78110309a203b0c2ef7cc4cd9d18d294a46 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 17 Jan 2011 03:05:42 +0000 Subject: fetch and delete may be passed destination buffer This allows apps to reduce GC thrashing by reusing a string buffer. --- test/test_tdb.rb | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'test') diff --git a/test/test_tdb.rb b/test/test_tdb.rb index d7a6a8a..7e77f14 100644 --- a/test/test_tdb.rb +++ b/test/test_tdb.rb @@ -275,6 +275,50 @@ class TestTdb < Test::Unit::TestCase assert ! @tdb.include?("hello") end + def test_fetch_reuse_buf + assert_nothing_raised do + @tmp = Tempfile.new('tdb') + File.unlink(@tmp.path) + end + @tdb = TDB.new(@tmp.path) + @tdb["HELLO"] = "WORLD" + rbuf = "HI" + rv = @tdb.fetch("HELLO", rbuf) + assert_equal rbuf.object_id, rv.object_id + + assert_equal "WORLD", rbuf + assert_nil @tdb.fetch("HELLO!", rbuf) + assert_equal "", rbuf + + @tdb["HELLO"] = "WORLD" * 100 + rv = @tdb.fetch("HELLO", rbuf) + assert_equal rbuf.object_id, rv.object_id + assert_equal "WORLD" * 100, rbuf + end + + def test_delete_reuse_buf + assert_nothing_raised do + @tmp = Tempfile.new('tdb') + File.unlink(@tmp.path) + end + @tdb = TDB.new(@tmp.path) + @tdb["HELLO"] = "WORLD" + rbuf = "HI" + rv = @tdb.delete("HELLO", rbuf) + assert_equal rbuf.object_id, rv.object_id + assert_equal "WORLD", rbuf + assert ! @tdb.include?("HELLO") + + assert_nil @tdb.delete("HELLO!", rbuf) + assert_equal "", rbuf + + @tdb["HELLO"] = "WORLD" * 100 + rv = @tdb.delete("HELLO", rbuf) + assert_equal rbuf.object_id, rv.object_id + assert_equal "WORLD" * 100, rbuf + assert ! @tdb.include?("HELLO") + end + def test_repack_file assert_nothing_raised do @tmp = Tempfile.new('tdb') -- cgit v1.2.3-24-ge0c7