Skip to content
This repository
Browse code

mock redis

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

0 notes on commit 30d1b50

Please sign in to comment.
Something went wrong with that request. Please try again.