ruby-tdb.git  about / heads / tags
Trivial Database bindings for Ruby
blob 0fbe09fd652e583c9e8159bc7d83a5f991c40cb0 2284 bytes (raw)
$ git show v0.2.0:test/test_tdb_mt.rb	# shows this blob on the CLI

 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
 
# -*- encoding: binary -*-
$stdout.sync = $stderr.sync = true
require 'test/unit'
require 'tempfile'
$-w = true
require 'tdb'

class Test_TDB_MT < Test::Unit::TestCase
  def setup
    @tdb = @tmp = nil
    @start_pid = $$
  end

  def teardown
    return if @start_pid != $$
    @tmp.close! if @tmp.respond_to?(:close!)
    @tdb.close if @tdb && ! @tdb.closed?
  end

  def test_make_threadsafe
    @tdb = TDB.new(nil)
    assert_kind_of TDB, @tdb
    assert ! @tdb.threadsafe?
    assert_nothing_raised { @tdb.threadsafe! }
    assert @tdb.threadsafe?
    @tdb.each { |k,v| assert_equal v, @tdb[k] }
  end

  def test_init_threadsafe
    @tdb = TDB.new(nil, :threadsafe => true)
    assert @tdb.threadsafe?
    @tdb.close
    @tdb = TDB.new(nil, :threadsafe => false)
    assert ! @tdb.threadsafe?
    @tdb.close
    @tdb = TDB.new(nil)
    assert ! @tdb.threadsafe?
    @tdb.close
  end

  def test_thread_safe_torture_test
    @tdb = TDB.new(nil)
    assert_nothing_raised { @tdb.threadsafe! }
    pid = fork do
      Thread.abort_on_exception = true
      threads = []
      blob = 'foo' * 1000
      nr = 10000
      t = Thread.new do
        while true
          Thread.pass
          @tdb.to_a
        end
      end
      threads << Thread.new { nr.times { |i| @tdb[i.to_s] = blob } }
      threads << Thread.new { nr.times { |i| @tdb[i.to_s] = blob } }
      threads << Thread.new { nr.times { |i| @tdb[i.to_s] = blob } }
      threads << Thread.new { nr.times { |i| @tdb[i.to_s] = blob } }
      threads << t

      t.kill
      threads.each { |t| t.join }
    end
    _, status = Process.waitpid2(pid)
    assert status.success?, status.inspect
  end

  def test_check_methods
    m = TDB.instance_methods.sort
    m -= Object.instance_methods
    m -= Enumerable.instance_methods
    m.map! { |x| x.to_sym }
    mt = TDB::MT.instance_methods.sort
    m -= [ :threadsafe! ]
    m += [ :include?, :member? ]
    m.sort!
    unwrapped = ( (m - mt) | (mt - m)).uniq
    assert unwrapped.empty?, "unwrapped methods: #{unwrapped.inspect}"
    @tdb = TDB.new(nil)
    respond_to?(:refute_match) and
      m.each { |meth| refute_match(/\bTDB::MT\b/, @tdb.method(meth).to_s) }
    @tdb.threadsafe!
    m.each { |meth| assert_match(/\bTDB::MT\b/, @tdb.method(meth).to_s) }
  end
end

git clone https://yhbt.net/ruby-tdb.git