Skip to content

Commit

Permalink
local redis test environment
Browse files Browse the repository at this point in the history
  • Loading branch information
levicook committed Feb 14, 2010
1 parent d5ed1d6 commit fa26de3
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ rdoc
pkg

## PROJECT::SPECIFIC
test/config
test/log
test/pid
53 changes: 53 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Important Moving Parts
gem.authors = ["levicook@gmail.com"]
gem.add_development_dependency "riot", ">= 0"
gem.add_development_dependency "reek", ">= 0"
gem.add_development_dependency "daemon_controller", ">= 0"
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
Jeweler::GemcutterTasks.new
Expand Down Expand Up @@ -91,3 +92,55 @@ Rake::RDocTask.new do |rdoc|
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb')
end



# redis test environment...

def write_redis_config(confile, port, slaveof=nil)
pidfile = File.expand_path("test/pid/#{ File.basename(confile, '.*') }.pid")
logfile = File.expand_path("test/log/#{ File.basename(confile, '.*') }.log")
open(confile, 'w') do |f|
f.puts "daemonize yes"
f.puts "port #{port}"
f.puts "pidfile #{pidfile}"
f.puts "bind 127.0.0.1"
f.puts "timeout 0"
f.puts "dir ./test"
f.puts "loglevel notice"
f.puts "logfile #{logfile}"
f.puts slaveof unless slaveof.nil?
f.puts "databases 2"
f.puts "maxmemory 6291456"
f.puts "glueoutputbuf yes"
f.puts "shareobjects no"
f.puts "shareobjectspoolsize 1024"
end
end

directory 'test/config'
directory 'test/log'
directory 'test/pid'

(16379..16380).each do |port|
config_file = "test/config/redis-server:#{port}.conf"
file config_file => %w(test/config test/log test/pid) do
write_redis_config(config_file, port)
end
namespace :test do
task :config => config_file
end
end

(16389..16394).each do |port|
config_file = "test/config/redis-server:#{port}.conf"
file config_file => %w(test/config test/log test/pid) do
master_port = port.even? ? 16379 : 16380
write_redis_config(config_file, port, "slaveof 127.0.0.1 #{master_port}")
end
namespace :test do
task :config => config_file
end
end

task :test => 'test:config'
17 changes: 10 additions & 7 deletions examples/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,29 @@
require 'rubygems'
require 'hitimes'

def fib( num )
def fib(num)
return num if num < 2
fib(num - 1) + fib(num - 2)
end

print 'Before memoize: '
puts Hitimes::Interval.measure { fib(33) }
puts Hitimes::Interval.measure { fib(30) }


extend Memodis
memoize :fib

print ' After memoize: '
puts Hitimes::Interval.measure { fib(33) }
puts Hitimes::Interval.measure { fib(1000) }


# Memodis::SimpleCluster.new(
# :master => DistRedis.new(),
# :slaves =>,
# )
Memodis::SimpleCluster.new(
:master => '127.0.0.1:16379 127.0.0.1:16380'.split,
:slaves => '127.0.0.1:16389 127.0.0.1:16390
127.0.0.1:16391 127.0.0.1:16392
127.0.0.1:16393 127.0.0.1:16394'.split
)


# memoize :fib, Memodis::RedisCache.new(
# :namespace => :fib,
Expand Down
4 changes: 2 additions & 2 deletions lib/memodis.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'pathname'
load Pathname.new(__FILE__).parent+'dependencies.rb'

require 'dist_redis'

module Memodis

# slurp cool vendor goodies into our namespace. would declare them
Expand All @@ -13,6 +15,4 @@ module Memodis

include Memodis::Memoizable



end
60 changes: 60 additions & 0 deletions lib/memodis/local_redis_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require 'daemon_controller'
require 'socket'

module Memodis
class LocalRedisController

def self.start(config_file)
new(config_file).start
end

attr_reader :config, :controller

def initialize(config_file)
config = IO.readlines(config_file)
config.reject! { |l| l !~ /^(bind|port|pidfile|logfile)[ \t]?/ }
config.collect! { |l| l.chomp.split(/[ \t]/) }
config.flatten!
config = Hash[*config]
config['file'] = config_file
@config = config
@config.freeze

@controller = DaemonController.new({
:identifier => identifier,
:start_command => start_command,
:ping_command => ping_command,
:pid_file => pid_file,
:log_file => log_file
})
end

def connect
@controller.connect { TCPSocket.new(@config['bind'], @config['port']) }
end
alias :start :connect

private

def identifier
"redis-server #{@config['file']}"
end

def start_command
"redis-server #{@config['file']}"
end

def ping_command
lambda { TCPSocket.new(@config['bind'], @config['port']) }
end

def pid_file
@config['pidfile']
end

def log_file
@config['logfile']
end

end
end
6 changes: 6 additions & 0 deletions test/teststrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@
require 'riot'
require 'memodis'
Riot.dots


require 'memodis/local_redis_controller'
Dir['test/config/redis-server*.conf'].each do |config_file|
Memodis::LocalRedisController.start(config_file)
end

0 comments on commit fa26de3

Please sign in to comment.