about summary refs log tree commit homepage
path: root/test
diff options
context:
space:
mode:
authorEric Wong <e@yhbt.net>2011-01-17 03:05:42 +0000
committerEric Wong <normalperson@yhbt.net>2011-01-17 08:28:47 +0000
commit3107a78110309a203b0c2ef7cc4cd9d18d294a46 (patch)
tree470795dcd99d3f90af83d83549f1796e5a4fc019 /test
parent069d047d3dcb0eefe59babbd295cfee922aa1476 (diff)
downloadruby-tdb-3107a78110309a203b0c2ef7cc4cd9d18d294a46.tar.gz
This allows apps to reduce GC thrashing by reusing
a string buffer.
Diffstat (limited to 'test')
-rw-r--r--test/test_tdb.rb44
1 files changed, 44 insertions, 0 deletions
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')