about summary refs log tree commit homepage
path: root/test/test_tdb.rb
blob: bd25203a210ece4b8899767c9d53d54691f75d8c (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# -*- encoding: binary -*-
$stdout.sync = $stderr.sync = true
require 'test/unit'
require 'tempfile'
$-w = true
require 'tdb'

class TestTdb < Test::Unit::TestCase

  def setup
    @tmp = @tdb = nil
  end

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

  def test_create_file
    assert_nothing_raised do
      @tmp = Tempfile.new('tdb')
      File.unlink(@tmp.path)
    end
    @tdb = TDB.new(@tmp.path)
    assert_kind_of TDB, @tdb
    assert File.exist?(@tmp.path)
  end

  def test_to_a
    @tdb = TDB.new(nil)
    assert_equal [], @tdb.to_a
  end

  def test_each
    @tdb = TDB.new(nil)
    @tdb["X"] = "Y"

    tmp = []
    rc = @tdb.each { |k,v| tmp << [k, v ] }
    assert_equal([ %w(X Y) ], tmp)
    assert_equal @tdb.object_id, rc.object_id

    tmp = []
    assert_raises(EOFError) {
      @tdb.each { |k,v| raise EOFError, "FOO"; tmp << [ k, v ] }
    }
    assert tmp.empty?

    tmp = []
    rc = catch(:zzz) { @tdb.each { |k,v| throw(:zzz, "FOO"); tmp << [ k, v ] } }
    assert_equal rc, "FOO"
    assert tmp.empty?
  end

  def test_each_bigger
    @tdb = TDB.new(nil)
    @tdb["a"] = "A"
    @tdb["b"] = "B"
    @tdb["c"] = "C"

    tmp = []
    rc = @tdb.each { |k,v| tmp << [k, v ] }
    assert_equal 3, tmp.size
    assert_equal @tdb.object_id, rc.object_id

    tmp = []
    assert_raises(EOFError) {
      @tdb.each { |k,v|
        tmp << [ k, v ]
        raise EOFError, "FOO"
      }
    }
    assert_equal 1, tmp.size

    tmp = []
    rc = catch(:zzz) {
      @tdb.each { |k,v|
        tmp << [ k, v ]
        throw(:zzz, "FOO")
      }
    }
    assert_equal rc, "FOO"
    assert_equal 1, tmp.size
  end

  def test_memory
    assert_nothing_raised do
      @tdb = TDB.new(nil)
    end
    assert ! @tdb.closed?
    assert_nil @tdb.close
    assert @tdb.closed?
    assert_raises(IOError) { @tdb.close }
  end

  def test_delete
    @tdb = TDB.new(nil)
    @tdb["hello"] = "X"
    assert_equal "X", @tdb.delete("hello")
    assert_nil @tdb["hello"]
    assert_nil @tdb.fetch("hello")
    assert_nil @tdb.delete("hello")
    @tdb["hello"] = "world"
    assert_equal "world", @tdb.delete("hello")
    assert_nil @tdb.delete("hello")
  end

  def test_nuke!
    @tdb = TDB.new(nil)
    assert_equal false, @tdb.nuke!("hello")
    @tdb["hello"] = "world"
    assert_equal true, @tdb.nuke!("hello")
    assert ! @tdb.include?("hello")
    assert_equal false, @tdb.nuke!("hello")
  end

  def test_exists?
    @tdb = TDB.new(nil)
    assert_equal false, @tdb.key?("hello")
    assert_equal false, @tdb.include?("hello")
    @tdb["hello"] = "world"
    assert_equal true, @tdb.key?("hello")
  end

  def test_store_fetch_mem
    @tdb = TDB.new(nil)
    assert_nothing_raised { @tdb["hello"] = "world" }
    assert_equal "world", @tdb["hello"]
    @tdb.store("hello", "Z")
    assert_equal "Z", @tdb["hello"]
    assert_equal "Z", @tdb.fetch("hello")
  end

  def test_store_modify_mem
    @tdb = TDB.new(nil)
    assert_nothing_raised { @tdb["hello"] = "world" }
    assert_equal "world", @tdb["hello"]
    assert_equal "Z", @tdb.modify("hello", "Z")
    assert_equal "Z", @tdb["hello"]

    assert_nil @tdb.modify("none", "Z")
    assert_raises(TDB::ERR::NOEXIST) { @tdb.modify!("none", "Z") }
  end

  def test_store_insert_mem
    @tdb = TDB.new(nil)
    assert_equal "world", @tdb.insert("hello", "world")
    assert_equal "world", @tdb["hello"]
    assert_nil @tdb.insert("hello", "Z")
    assert_raises(TDB::ERR::EXISTS) { @tdb.insert!("hello", "Z") }
    assert_equal "world", @tdb["hello"]
  end

  def test_gc
    assert_nothing_raised do
      100000.times { TDB.new(nil) }
      100000.times { TDB.new(Tempfile.new('tdb').path) }
    end
  end if ENV["TEST_GC"]

  def test_new_with_hash_size
    assert_nothing_raised { TDB.new(nil, :hash_size => 6) }
    assert_raises(TypeError) { TDB.new(nil, :hash_size => "6") }
  end

  def test_const
    assert_equal 0, TDB::DEFAULT
    assert_equal 1, TDB::CLEAR_IF_FIRST
  end

  def test_new_with_open_flags
    @tmp = Tempfile.new('tdb_excl')
    assert_raises(Errno::EEXIST) {
      TDB.new(@tmp.path, :open_flags => IO::EXCL|IO::CREAT|IO::RDWR)
    }
    File.unlink(@tmp.path)
    assert_nothing_raised {
      @tdb = TDB.new(@tmp.path, :open_flags => IO::EXCL|IO::CREAT|IO::RDWR)
    }
  end

  def test_open_with_tdb_flags
    assert_nothing_raised do
      @tdb = TDB.new("/non/existent/file", :tdb_flags => TDB::INTERNAL)
    end
  end

  def test_alternate_hashes
    results = {}
    expect = TDB::HASHES.to_a.map { |k,v| [ k.to_s, v.to_s ] }.sort
    %w(default jenkins_lookup3 djb2 djb3 fnv1a
       murmur1 murmur1_aligned murmur2 murmur2a murmur2_aligned).each do |h|
      assert_nothing_raised do
        tdb = TDB.new(nil, :hash => h.to_sym)
        TDB::HASHES.each do |k,v|
          tdb[k.to_s] = v.to_s
        end
        assert_equal expect, tdb.to_a.sort
        assert_nil tdb.close
      end
      assert_raises(ArgumentError) do
        TDB.new(nil, :hash => h)
      end
    end
  end

  def test_lock_unlock_all
    @tmp = Tempfile.new('tdb')
    File.unlink(@tmp.path)
    @tdb = TDB.new(@tmp.path)
    assert_equal true, @tdb.lockall
    assert_equal true, @tdb.unlockall
    assert_raises(TDB::ERR::LOCK) { @tdb.unlockall }
  end

  def test_read_locks
    @tmp = Tempfile.new('tdb')
    File.unlink(@tmp.path)
    @tdb = TDB.new(@tmp.path)
    assert_equal true, @tdb.lockall_read
    assert_equal true, @tdb.unlockall_read
    assert_raises(TDB::ERR::LOCK) { @tdb.unlockall_read }
    assert_equal true, @tdb.trylockall_read
    assert_equal true, @tdb.unlockall_read
    assert_raises(TDB::ERR::LOCK) { @tdb.unlockall_read }
  end

  def test_mark_locks
    @tmp = Tempfile.new('tdb')
    File.unlink(@tmp.path)
    @tdb = TDB.new(@tmp.path)
    assert_equal true, @tdb.lockall_mark
    assert_equal true, @tdb.lockall_unmark
    assert_raises(TDB::ERR::LOCK) { @tdb.lockall_unmark }
  end

  def test_trylockall
    @tmp = Tempfile.new('tdb')
    File.unlink(@tmp.path)
    @tdb = TDB.new(@tmp.path)
    ard, awr = IO.pipe
    brd, bwr = IO.pipe
    pid = fork do
      @tdb.close
      ard.close
      bwr.close
      tdb = TDB.new(@tmp.path)
      assert_equal true, tdb.lockall
      awr.close
      brd.read
    end
    awr.close
    brd.close
    assert_equal "", ard.read
    assert_equal false, @tdb.trylockall
    bwr.close
    assert Process.waitpid2(pid)[1].success?
    assert_equal true, @tdb.trylockall
  end

  def test_check_constant_typos
    names = {}
    TDB.constants.each { |const| names[const] = TDB.const_get(const) }
    assert_equal TDB.constants.size, names.size

    values = {}
    TDB.constants.each { |const| values[TDB.const_get(const)] = const }
    assert_equal TDB.constants.size, values.size
  end
end