test/test.rb in daybreak-0.2.1 vs test/test.rb in daybreak-0.2.2

- old
+ new

@@ -29,26 +29,26 @@ end it 'should persist values' do @db['1'] = '4' @db['4'] = '1' - assert_equal @db.sync, @db + assert_equal @db.sunrise, @db assert_equal @db['1'], '4' - db2 = Daybreak::DB.new DB_PATH - assert_equal db2['1'], '4' - assert_equal db2['4'], '1' - assert_equal db2.close, nil + db = Daybreak::DB.new DB_PATH + assert_equal db['1'], '4' + assert_equal db['4'], '1' + assert_equal db.close, nil end it 'should persist after batch update' do @db.update!(1 => :a, 2 => :b) - db2 = Daybreak::DB.new DB_PATH - assert_equal db2[1], :a - assert_equal db2[2], :b - assert_equal db2.close, nil + db = Daybreak::DB.new DB_PATH + assert_equal db[1], :a + assert_equal db[2], :b + assert_equal db.close, nil end it 'should persist after clear' do @db['1'] = 'xy' assert_equal @db.clear, @db @@ -82,11 +82,11 @@ assert_equal @db.compact, @db @db['1'] = '4' @db['4'] = '1' assert_equal @db.flush, @db - db.sync + db.sunrise assert_equal db['1'], '4' assert_equal db['4'], '1' db.close end @@ -98,29 +98,30 @@ @db.clear @db['1'] = '4' @db['4'] = '1' @db.flush - db.sync + db.sunrise assert_equal db['1'], '4' assert_equal db['4'], '1' db.close end it 'should compact cleanly' do @db[1] = 1 @db[1] = 1 - @db.sync + @db.sunrise size = File.stat(DB_PATH).size @db.compact assert_equal @db[1], 1 assert size > File.stat(DB_PATH).size end it 'should allow for default values' do db = Daybreak::DB.new(DB_PATH, :default => 0) + assert_equal db.default(1), 0 assert_equal db[1], 0 assert db.include? '1' db[1] = 1 assert_equal db[1], 1 db.default = 42 @@ -128,10 +129,11 @@ db.close end it 'should handle default values that are procs' do db = Daybreak::DB.new(DB_PATH) {|key| set = Set.new; set << key } + assert db.default(:test).include? 'test' assert db['foo'].is_a? Set assert db.include? 'foo' assert db['bar'].include? 'bar' db.default = proc {|key| [key] } assert db[1].is_a? Array @@ -139,66 +141,90 @@ db.close end it 'should be able to sync competing writes' do @db.set! '1', 4 - db2 = Daybreak::DB.new DB_PATH - db2.set! '1', 5 - @db.sync + db = Daybreak::DB.new DB_PATH + db.set! '1', 5 + @db.sunrise assert_equal @db['1'], 5 - db2.close + db.close end it 'should be able to handle another process\'s call to compact' do @db.lock { 20.times {|i| @db[i] = i } } - db2 = Daybreak::DB.new DB_PATH + db = Daybreak::DB.new DB_PATH @db.lock { 20.times {|i| @db[i] = i } } @db.compact - db2.sync - assert_equal 19, db2['19'] - db2.close + db.sunrise + assert_equal 19, db['19'] + db.close end it 'can empty the database' do 20.times {|i| @db[i] = i } @db.clear - db2 = Daybreak::DB.new DB_PATH - assert_equal nil, db2['19'] - db2.close + db = Daybreak::DB.new DB_PATH + assert_equal nil, db['19'] + db.close end it 'should handle deletions' do - @db[1] = 'one' - @db[2] = 'two' + @db['one'] = 1 + @db['two'] = 2 @db.delete! 'two' assert !@db.has_key?('two') assert_equal @db['two'], nil - db2 = Daybreak::DB.new DB_PATH - assert !db2.has_key?('two') - assert_equal db2['two'], nil - db2.close + db = Daybreak::DB.new DB_PATH + assert !db.has_key?('two') + assert_equal db['two'], nil + db.close end + it 'should synchronize deletions after compact' do + @db['one'] = 1 + @db['two'] = 2 + @db.flush + db = Daybreak::DB.new DB_PATH + assert db.has_key?('two') + @db.delete! 'two' + @db.compact + db.sunrise + assert !db.has_key?('two') + assert_equal db['two'], nil + db.close + end + it 'should close and reopen the file when clearing the database' do begin 1000.times {@db.clear} rescue flunk end end it 'should have threadsafe lock' do @db[1] = 0 - inc = proc { 1000.times { @db.lock { @db[1] += 1 } } } + inc = proc { 1000.times { @db.lock {|d| d[1] += 1 } } } a = Thread.new &inc b = Thread.new &inc a.join b.join assert_equal @db[1], 2000 end + it 'should have threadsafe synchronize' do + @db[1] = 0 + inc = proc { 1000.times { @db.synchronize {|d| d[1] += 1 } } } + a = Thread.new &inc + b = Thread.new &inc + a.join + b.join + assert_equal @db[1], 2000 + end + it 'should synchronize across processes' do @db[1] = 0 @db.flush @db.close begin @@ -354,9 +380,13 @@ db = Subclassed.new DB_PATH db[1] = 1 assert_equal db.increment(1), 2 db.clear db.close + end + + it 'should report the bytesize' do + assert @db.bytesize > 0 end after do @db.clear @db.close