Permalink
Browse files

moved tests over

  • Loading branch information...
1 parent 70236cd commit 25b186b19d9c360a748ef6a92d6fd22425798337 Jordan Glasner committed Apr 4, 2010
View
51 lib/nimble_nodes/app.rb
@@ -0,0 +1,51 @@
+module NimbleNodes
+
+ module App
+
+ #= Rails
+
+ def self.rails?
+ defined?(RAILS_GEM_VERSION) ? true : false
+ end
+
+ def self.rails_version
+ RAILS_GEM_VERSION.slice(0..2).to_f
+ end
+
+ #== Check Rails version
+ # returns true if gem is loaded in a pre 2.3 version of rails
+ def self.use_rails_filter?
+ NimbleNodes::App.rails? and NimbleNodes::App.rails_version < 2.3
+ end
+
+ # set through Heroku API when app is created
+ def self.name
+ ENV['NIMBLE_NODES_APP_NAME']
+ end
+
+ # set through Heroku API when app is created at server
+ def self.token
+ ENV['NIMBLE_NODES_APP_TOKEN']
+ end
+
+ # returns true if app has been setup at server correctly
+ def self.token?
+ not NimbleNodes::App.token.nil?
+ end
+
+ # path at server for calling app resource and subresources
+ def self.path(subpath='/')
+ name = NimbleNodes::App.name
+ return nil if name.nil?
+ '/' + name + subpath
+ end
+
+ # returns path to platform specific report implementation
+ def self.reporter
+ path = NimbleNodes::App.use_rails_filter? ? '/rails/filter' : '/middleware'
+ NimbleNodes.lib_path(path)
+ end
+
+ end
+
+end
View
51 lib/nimble_nodes/settings.rb
@@ -0,0 +1,51 @@
+module NimbleNodes
+
+ class Settings
+
+ #= Setting accessor
+ #= allows NimbleNodes::Settings to be treated as a hash
+ # shortcut for accessing NimbleNodes.settings cache
+ def self.[](key)
+ cache[key]
+ end
+
+ private
+
+ #= Cache
+ # settings are fetched once from server and cached in global constant
+
+ # returns hash of settings from cache or fetched from nimblenodes.com
+ def self.cache
+ return CACHE if cache? and fresh_cache?
+ const_set('CACHE',NimbleNodes::Settings.fetch_from_server)
+ end
+
+ #== Fetch from server
+
+ # returns hash from parsed server response
+ def self.fetch_from_server
+ response = NimbleNodes::Server.get("/settings.json")
+ return {} if response.nil?
+ JSON[response].merge('fetched_at' => Time.now)
+ end
+
+ #== Cache Inspectors
+
+ # returns true if local settings have been set
+ def self.cache?
+ defined?(NimbleNodes::Settings::CACHE) and !NimbleNodes::Settings::CACHE.nil? and !NimbleNodes::Settings::CACHE.empty?
+ end
+
+ def self.fresh_cache?
+ fetched_at = CACHE['fetched_at']
+ return false if fetched_at.nil?
+ age = Time.now.to_i - fetched_at.to_i
+ five_minutes = 300
+ age > five_minutes
+ end
+
+
+
+ end
+
+end
View
35 lib/nimble_nodes/workers.rb
@@ -0,0 +1,35 @@
+module NimbleNodes
+
+ module Workers
+
+ def self.active?
+ ENV.has_key?('NN_WORKERS_POOL_ACTIVE')
+ end
+
+ def self.size
+ ENV['NN_WORKERS_POOL_SIZE']
+ end
+
+ def self.min
+ ENV['NN_WORKERS_POOL_MIN']
+ end
+
+ def self.max
+ ENV['NN_WORKERS_POOL_MAX']
+ end
+
+ def self.min
+ ENV['NN_WORKERS_POOL_MIN']
+ end
+
+ def self.min_job_queue
+ ENV['NN_WORKERS_JOB_QUEUE_MIN']
+ end
+
+ def self.max_job_queue
+ ENV['NN_WORKERS_JOB_QUEUE_MAX']
+ end
+
+ end
+
+end
View
102 test/unit/test_app.rb
@@ -0,0 +1,102 @@
+require File.expand_path(File.dirname(__FILE__)) + '/../helper'
+
+class TestApp < Test::Unit::TestCase
+
+ context "An App" do
+
+ setup { @module = NimbleNodes::App }
+
+ context "on Rails" do
+
+ setup { @module.stubs(:rails?).returns(true) }
+
+ context "2.3 or later" do
+ setup { @module.stubs(:rails_version).returns(2.3) }
+
+ should "not use rails before_filter" do
+ assert(!@module.use_rails_filter?)
+ end
+
+ should "return NimbleNodes::Middleware as reporter" do
+ assert_equal 'middleware', @module.reporter.split('/').last
+ end
+
+ end
+
+ context "prior to 2.3" do
+ setup { NimbleNodes::App.stubs(:rails_version).returns(2.2) }
+
+ should "use rails before_filter" do
+ assert(@module.use_rails_filter?)
+ end
+
+ should "return NimbleNodes::Filter as reporter" do
+ assert_equal 'filter', @module.reporter.split('/').last
+ end
+
+ end
+
+
+ end
+
+ context "off Rails" do
+
+ should "return false for rails?" do
+ # we're not in Rails so this works
+ assert_equal false, @module.rails?
+ end
+
+ should "return NimbleNodes::Middleware as reporter" do
+ assert_equal 'middleware', @module.reporter.split('/').last
+ end
+
+ end
+
+
+ context "installed at NimbleNodes" do
+
+ setup {
+ ENV['NIMBLE_NODES_APP_NAME'] = 'nimble-nodes-test'
+ ENV['NIMBLE_NODES_APP_TOKEN'] = 'test'
+ }
+
+ should "return name from ENV" do
+ assert_equal ENV['NIMBLE_NODES_APP_NAME'], @module.name
+ end
+
+ should "return token from ENV" do
+ assert_equal ENV['NIMBLE_NODES_APP_TOKEN'], @module.token
+ end
+
+ should "return true for token?" do
+ assert @module.token?
+ end
+
+ should "return relative path at NN server" do
+ assert_equal '/nimble-nodes-test/', @module.path
+ end
+
+ should "return relative path to app resource for given path" do
+ assert_equal '/nimble-nodes-test/dynos', @module.path('/dynos')
+ end
+
+
+ end
+
+ context "not installed at NimbleNodes" do
+
+ setup {
+ ENV['NIMBLE_NODES_APP_NAME'] = nil
+ ENV['NIMBLE_NODES_APP_TOKEN'] = nil
+ }
+
+ should "return false for token?" do
+ assert_equal false, @module.token?
+ end
+
+ end
+
+
+ end
+
+end
View
97 test/unit/test_dynos.rb
@@ -0,0 +1,97 @@
+require File.expand_path(File.dirname(__FILE__)) + '/../helper'
+
+class Test::Unit::TestCase
+
+ def self.should_not_raise_anything_when_monitored
+ should "not raise any exceptions when sent :monitored?" do
+ assert_nothing_raised { @module.monitor? }
+ NimbleNodes::Server.stubs(:post).returns(nil)
+ assert_nothing_raised { @module.monitor({}) }
+ end
+ end
+
+ def self.should_not_be_monitored
+ should "not be monitored" do
+ assert_equal false, @module.monitor?
+ end
+ end
+
+
+end
+
+
+class TestDynos < Test::Unit::TestCase
+
+ context "Dynos" do
+
+ setup {
+ @module = NimbleNodes::Dynos
+ }
+
+ context "App setup at NimbleNodes" do
+
+ setup {
+ NimbleNodes::App.stubs({
+ :name => 'nimble-nodes-test',
+ :token => 'token'
+ })
+ @module.stubs(:settings).returns({
+ 'paused_at' => nil,
+ 'min' => 1,
+ 'max' => 2
+ })
+ }
+
+ context "and App not paused" do
+ setup { NimbleNodes.stubs(:paused?).returns(false) }
+
+ should "be monitored" do
+ assert @module.monitor?
+ end
+
+ should_not_raise_anything_when_monitored
+
+ context "and Dynos paused" do
+ setup {
+ @module.stubs(:settings).returns({
+ 'paused_at' => Time.now,
+ 'min' => 1,
+ 'max' => 2
+ })
+ }
+
+ should "be paused" do
+ assert @module.paused?
+ end
+
+ should_not_be_monitored
+ should_not_raise_anything_when_monitored
+
+ end
+
+
+ end
+
+ context "and App paused" do
+ setup { NimbleNodes.stubs(:paused?).returns(true) }
+ should_not_be_monitored
+ should_not_raise_anything_when_monitored
+
+ end
+
+
+ end
+
+ context "App *not* setup at NimbleNodes.com" do
+
+ should_not_be_monitored
+
+ should_not_raise_anything_when_monitored
+
+
+ end
+
+
+ end
+
+end
View
80 test/unit/test_nimble_nodes.rb
@@ -0,0 +1,80 @@
+require File.expand_path(File.dirname(__FILE__)) + '/../helper'
+
+class TestNimbleNodes < Test::Unit::TestCase
+
+
+ context "NimbleNodes module" do
+ setup { @module = NimbleNodes }
+
+ context "in a Rails app" do
+
+ setup { NimbleNodes::App.stubs(:rails?).returns(true) }
+
+ context "pre 2.3" do
+ setup { NimbleNodes::App.stubs(:rails_version).returns(2.2) }
+
+ should "use NimbleNodes::Filter as reporter" do
+ assert_equal 'filter', @module.reporter.split('/').last
+ end
+
+ end
+
+ context "2.3 or later" do
+ setup { NimbleNodes::App.stubs(:rails_version).returns(2.3) }
+
+ should "use NimbleNodes::Middleware as reporter" do
+ assert_equal 'middleware', @module.reporter.split('/').last
+ end
+
+ end
+
+ end
+
+ context "in a non-Rails app" do
+ setup { NimbleNodes::App.stubs(:rails?).returns(false) }
+
+ should "use NimbleNodes::Middleware" do
+ assert_equal 'middleware8', @module.reporter.split('/').last
+ end
+
+ end
+
+ context "App installed at NimbleNodes" do
+
+ setup {
+ NimbleNodes::App.stubs({
+ :name => 'nimble-nodes-test',
+ :token => 'token'
+ })
+ }
+
+ context "not paused" do
+
+ setup {
+ @module.stubs(:paused?).returns(false)
+ }
+
+ should "be active" do
+ assert @module.active?
+ end
+ end
+
+ context "paused" do
+
+ setup {
+ @module.stubs(:paused?).returns(true)
+ }
+
+ should "be not active" do
+ assert(!@module.active?)
+ end
+ end
+
+ end
+
+
+ end
+
+
+
+end
View
93 test/unit/test_settings.rb
@@ -0,0 +1,93 @@
+require File.expand_path(File.dirname(__FILE__)) + '/../helper'
+
+class Test::Unit::TestCase
+
+ def self.should_not_raise_any_errors_when_fetching_from_server
+ should "not raise any errors when fetching from server" do
+ assert_nothing_raised { @module.fetch_from_server }
+ end
+ end
+
+ def self.should_not_raise_any_errors_on_unknown_keys
+ should "not raise errors on any unknown keys" do
+ assert_nothing_raised { @module.cache['bogus-key'] }
+ end
+ end
+
+
+end
+
+class TestSettings < Test::Unit::TestCase
+
+ context "Settings" do
+
+ setup {
+ @module = NimbleNodes::Settings
+ @module.send(:remove_const,'CACHE') if defined?(@module::CACHE)
+ }
+
+ context "App is installed at NimbleNodes.com" do
+ setup do
+ NimbleNodes::App.stubs({
+ :name => "nimble-nodes-test",
+ :token => 'test'
+ })
+ end
+
+ context "server responds with JSON" do
+
+ setup {
+ @json = '{"paused_at":null,"dynos_pool":{"size":1,"paused_at":null,"max":1,"request_queue":{"max":5,"min":0},"min":1}}'
+ @parsed = JSON[@json]
+ NimbleNodes::Server.expects(:get).returns(@json)
+ }
+
+ should "only fetch from server once" do
+ @module.expects(:fetch_from_server).once
+ @module['paused_at']
+ @module['dynos_pool']
+ end
+
+ should "save parsed parsed response in cache" do
+ cached = @module.cache
+ cached.delete('fetched_at')
+ assert_equal @parsed, cached
+ end
+
+ should "add fetched_at to server response" do
+ assert_not_nil @module['fetched_at']
+ end
+
+ should_not_raise_any_errors_on_unknown_keys
+
+ should_not_raise_any_errors_when_fetching_from_server
+
+ end
+
+ context "server responds with error" do
+ setup { NimbleNodes::Server.expects(:get).once.returns(nil) }
+ should_not_raise_any_errors_on_unknown_keys
+ should_not_raise_any_errors_when_fetching_from_server
+ end
+
+ end
+
+ context "App is *not* installed at NimbleNodes.com" do
+
+ setup {
+ NimbleNodes::App.stubs({
+ :name => nil,
+ :token => nil
+ })
+ NimbleNodes::Server.expects(:get).returns(nil)
+ }
+
+ should_not_raise_any_errors_on_unknown_keys
+ should_not_raise_any_errors_when_fetching_from_server
+
+ end
+
+
+ end
+
+end

0 comments on commit 25b186b

Please sign in to comment.