2009-08-07 22:19:23 +02:00
|
|
|
require 'test_helper'
|
|
|
|
require File.dirname(__FILE__) + '/../lib/bdb/simple'
|
|
|
|
|
|
|
|
class SimpleTest < Test::Unit::TestCase
|
|
|
|
def setup
|
2009-10-17 02:57:12 +02:00
|
|
|
@path = File.join(File.dirname(__FILE__), 'tmp')
|
|
|
|
rm_rf @path
|
|
|
|
mkdir @path
|
|
|
|
open
|
2009-08-07 22:19:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2009-10-17 02:57:12 +02:00
|
|
|
close
|
|
|
|
rm_rf @path
|
|
|
|
end
|
|
|
|
|
|
|
|
def open
|
|
|
|
@db = Bdb::Simple.new(@path)
|
|
|
|
@dbd = Bdb::Simple.new(@path, :name => 'dup', :dup => true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
2009-08-07 22:19:23 +02:00
|
|
|
@db.close
|
|
|
|
@dbd.close
|
|
|
|
end
|
2009-10-17 02:57:12 +02:00
|
|
|
|
2009-08-07 22:19:23 +02:00
|
|
|
def test_put_and_get
|
|
|
|
@db['key'] = 'data'
|
|
|
|
assert_equal 'data', @db['key']
|
|
|
|
|
|
|
|
@dbd['key'] = 'data1'
|
|
|
|
@dbd['key'] = 'data2'
|
|
|
|
assert_equal ['data1', 'data2'], @dbd['key'].to_a
|
|
|
|
end
|
|
|
|
|
2009-10-17 02:57:12 +02:00
|
|
|
def test_update
|
|
|
|
@db[:key] = 0
|
|
|
|
close
|
|
|
|
|
|
|
|
pids = []
|
|
|
|
5.times do
|
|
|
|
pids << Process.fork do
|
|
|
|
db = Bdb::Simple.new(@path)
|
|
|
|
10.times do
|
|
|
|
db.update(:key) do |v|
|
|
|
|
sleep(0.1)
|
|
|
|
v + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
db.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
pids.each {|pid| Process.wait(pid)}
|
|
|
|
|
|
|
|
open
|
|
|
|
assert_equal 50, @db[:key]
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2009-08-07 22:19:23 +02:00
|
|
|
def test_delete
|
|
|
|
@db['key'] = 'data'
|
|
|
|
assert_equal 'data', @db['key']
|
|
|
|
|
|
|
|
@db.delete('key')
|
|
|
|
assert_nil @db['key']
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_range
|
|
|
|
(1..10).each {|i| @db[i] = "data#{i}"}
|
|
|
|
|
|
|
|
assert_equal (3..7).collect {|i| "data#{i}"}, @db[3..7].to_a
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_compare_absolute
|
|
|
|
list = [5, 6, "foo", :bar, "bar", :foo, [1,2,4], true, [1,2,3], false, [1], [2], nil, {}, {:b => 1, :a => 1}, {:b => 2, :a => 1}]
|
|
|
|
|
2009-08-07 23:43:40 +02:00
|
|
|
expected = [nil, false, true, 5, 6, :bar, :foo, "bar", "foo", [1], [1, 2, 3], [1, 2, 4], [2], {}, {:a=>1, :b=>1}, {:a=>1, :b=>2}]
|
2009-08-07 22:19:23 +02:00
|
|
|
assert_equal expected, list.sort {|a,b| Bdb::Simple.compare_absolute(a,b)}
|
|
|
|
100.times do
|
|
|
|
assert_equal expected, list.shuffle.sort {|a,b| Bdb::Simple.compare_absolute(a,b)}
|
|
|
|
end
|
|
|
|
end
|
2009-10-17 02:57:12 +02:00
|
|
|
|
|
|
|
def parallel(n)
|
|
|
|
threads = []
|
|
|
|
n.times do |i|
|
|
|
|
threads << Thread.new do
|
|
|
|
yield(i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
threads.each { |thread| thread.join }
|
|
|
|
end
|
2009-08-07 22:19:23 +02:00
|
|
|
end
|