Permalink
Browse files

mock redis

  • Loading branch information...
Sean McDaniel
Sean McDaniel committed Jan 11, 2012
1 parent 5f33dee commit 30d1b50317000e066df835510b047e03231bb63e
Showing with 32 additions and 25 deletions.
  1. +1 −0 .travis.yml
  2. +1 −1 Gemfile
  3. +2 −12 lib/redmon.rb
  4. +6 −1 lib/redmon/app.rb
  5. +10 −4 lib/redmon/worker.rb
  6. +12 −7 spec/app_spec.rb
View
@@ -0,0 +1 @@
+rvm: 1.9.3
View
@@ -14,7 +14,7 @@ gem "yajl-ruby", :git => "git://github.com/brianmario/yajl-ruby.git"
gem "thin"
gem "activesupport"
-group :development do
+group :test do
gem "rake"
gem "rspec", "~> 2.6.0"
gem "shoulda", ">= 0"
View
@@ -24,14 +24,8 @@ def start_em(opts)
trap("INT", &method(:shutdown))
@opts = DEFAULT_OPTS.merge opts
-
- if @opts[:worker]
- Worker.new(@opts).run!
- end
-
- if @opts[:web_interface]
- start_app
- end
+ Worker.new(@opts).run! if @opts[:worker]
+ start_app if @opts[:web_interface]
end
end
@@ -57,10 +51,6 @@ def shutdown
EM.stop
end
- def info_key
- "#{@opts[:namespace]}:redis.info"
- end
-
def log(msg)
puts "[#{Time.now.strftime("%y-%m-%d %H:%M:%S")}] #{msg}"
end
View
@@ -28,7 +28,7 @@ def initialize(opts)
get '/info' do
content_type :json
- @redis.zrange(Redmon.info_key, count, -1).to_json
+ @redis.zrange(key, count, -1).to_json
end
get '/cli' do
@@ -47,4 +47,9 @@ def initialize(opts)
end
end
+ private
+ def key
+ "#{@opts[:namespace]}:redis.info"
+ end
+
end
View
@@ -1,15 +1,21 @@
class Redmon::Worker
def initialize(opts)
- @opts = opts
+ @ns = opts[:namespace]
+ @url = opts[:redis_url]
+ @interval = opts[:poll_interval]
end
def run!
- redis = EM::Hiredis.connect(@opts[:redis_url])
- EM::PeriodicTimer.new(@opts[:poll_interval]) do
+ redis = EM::Hiredis.connect(@url)
+ EM::PeriodicTimer.new(@interval) do
redis.info do |info|
info[:time] = ts = Time.now.to_i * 1000
- redis.zadd(Redmon.info_key, ts, info.to_json)
+ redis.zadd(key, ts, info.to_json)
end
end
end
+
+ def key
+ @key ||= "#{@ns}:redis.info"
+ end
end
View
@@ -5,7 +5,7 @@
describe "app" do
def app
- Redmon::App.new(Redmon::DEFAULT_OPTS)
+ Redmon::App.new Redmon::DEFAULT_OPTS
end
describe "GET /" do
@@ -17,18 +17,23 @@ def app
end
describe "GET /info" do
- it "should render json" do
+ it "should render a single json result" do
+ stub_redis ['redmon:redis.info', -1, -1]
get "/info"
last_response.should be_ok
last_response.headers["Content-Type"].should == "application/json;charset=utf-8"
end
- it "should render the correct # of results" do
- get "/info?count=1"
- JSON.parse(last_response.body).length.should == 1
+ it "should request the correct # of historical info records from redis" do
+ stub_redis ['redmon:redis.info', -666, -1]
+ get "/info?count=666"
+ last_response.should be_ok
+ end
- get "/info?count=2"
- JSON.parse(last_response.body).length.should == 2
+ def stub_redis(args)
+ redis = Redis.new
+ redis.should_receive(:zrange).with(*args).and_return({})
+ Redis.stub(:new).and_return(redis)
end
end

0 comments on commit 30d1b50

Please sign in to comment.