add replication test and example

This commit is contained in:
Justin Balthrop 2009-11-24 09:38:53 -08:00
parent 0359fe7c49
commit 251eff4063
2 changed files with 76 additions and 0 deletions

29
examples/replication.rb Normal file
View file

@ -0,0 +1,29 @@
require 'rubygems'
require 'bdb/database'
require 'fileutils'
role = ARGV.shift || 'client'
MASTER = 'localhost:8888'
CLIENT = 'localhost:9999'
DIR = "/tmp/#{role}"
FileUtils.rmtree DIR
FileUtils.mkdir DIR
puts "Starting #{role}..."
Bdb::Environment.replicate DIR, :from => MASTER, :to => CLIENT, :host => role == 'master' ? MASTER : CLIENT
db = Bdb::Database.new('foo', :path => DIR)
loop do
100.times do |i|
if role == 'master'
db.set(i, db[i].to_i + 1)
puts "#{i}: #{db[i]}"
else
puts "#{i}: #{db[i]}"
end
sleep 0.1
end
end

47
test/replication_test.rb Normal file
View file

@ -0,0 +1,47 @@
require File.dirname(__FILE__) + '/database_test_helper'
MASTER_DIR = '/tmp/bdb_rep_test_master'
CLIENT_DIR = '/tmp/bdb_rep_test_client'
FileUtils.rmtree MASTER_DIR; FileUtils.mkdir MASTER_DIR;
FileUtils.rmtree CLIENT_DIR; FileUtils.mkdir CLIENT_DIR
MASTER = 'localhost:8888'
CLIENT = 'localhost:8889'
N = 100
class ReplicationTest < Test::Unit::TestCase
def setup
Bdb::Environment.replicate CLIENT_DIR, :from => MASTER, :to => CLIENT, :host => CLIENT #, :verbose => true
@pid = Process.fork do
Bdb::Environment.replicate MASTER_DIR, :from => MASTER, :to => CLIENT, :host => MASTER #, :verbose => true
db = Bdb::Database.new('foo', :path => MASTER_DIR)
N.times do |i|
db.set(i, i + 1)
log "m"
sleep 0.1
end
sleep 10
end
end
def teardown
Process.kill(9, @pid)
end
def test_single_process_replication
db = Bdb::Database.new('foo', :path => CLIENT_DIR)
N.times do |i|
sleep 0.2
assert_equal i + 1, db[i]
log "c"
end
end
def log(action)
print action
$stdout.flush
end
end