From 251eff4063549eb3a39f43940c4709d994cb8042 Mon Sep 17 00:00:00 2001 From: Justin Balthrop Date: Tue, 24 Nov 2009 09:38:53 -0800 Subject: [PATCH] add replication test and example --- examples/replication.rb | 29 +++++++++++++++++++++++++ test/replication_test.rb | 47 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 examples/replication.rb create mode 100644 test/replication_test.rb diff --git a/examples/replication.rb b/examples/replication.rb new file mode 100644 index 0000000..84ea9df --- /dev/null +++ b/examples/replication.rb @@ -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 diff --git a/test/replication_test.rb b/test/replication_test.rb new file mode 100644 index 0000000..c3555b2 --- /dev/null +++ b/test/replication_test.rb @@ -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