Skip to content

Commit

Permalink
moving spec and adding licencse and gemspec
Browse files Browse the repository at this point in the history
  • Loading branch information
ajmurmann committed Feb 7, 2011
1 parent 9e1cf23 commit 4152fc1
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 0 deletions.
2 changes: 2 additions & 0 deletions GEMFILE
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ gem 'heroku', '1.11.0'
group :development do
gem "rspec", "~> 2.0"
gem 'rr', '1.0.2'
gem 'echoe'
gem 'rake'
end

20 changes: 20 additions & 0 deletions MIT.LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2011 Alexander Murmann

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 changes: 19 additions & 0 deletions resque-heroku-autoscaler.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
$LOAD_PATH.unshift 'lib'
require 'resque/version'

Gem::Specification.new do |s|
s.name = "resque-heroku-autoscaler"
s.date = Time.now.strftime('%Y-%m-%d')
s.version = "0.1.0"
s.summary = "Resque plugin to autoscale your workers on Heroku"
s.homepage = "https://github.com/ajmurmann/resque-heroku-autoscaler"
s.authors = ["Alexander Murmann"]
s.email = "ajmurmann@gmail.com"

s.files = %w( README.md MIT.LICENSE GEMFILE )
s.files += Dir.glob("lib/**/*")
s.files += Dir.glob("spec/**/*")

s.add_dependency "resque", ">= 1.8"
s.add_dependency "heroku"
end
119 changes: 119 additions & 0 deletions spec/resque_heroku_autoscaler_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
require 'rspec'
require 'heroku'
require 'resque'
require 'resque/plugins/resque_heroku_autoscaler'

class TestJob
extend Resque::Plugins::HerokuAutoscaler

@queue = :test
end

class AnotherJob
extend Resque::Plugins::HerokuAutoscaler

@queue = :test
end

RSpec.configure do |config|
config.mock_with :rr
end

describe Resque::Plugins::HerokuAutoscaler do
it "should be a valid Resque plugin" do
lambda { Resque::Plugin.lint(Resque::Plugins::HerokuAutoscaler) }.should_not raise_error
end

describe ".after_enqueue_scale_workers_up" do
it "should add the hook" do
Resque::Plugin.after_enqueue_hooks(TestJob).should include("after_enqueue_scale_workers_up")
end

it "should take whatever args Resque hands in" do
stub(Heroku::Client).new { stub!.set_workers }

lambda { TestJob.after_enqueue_scale_workers_up("some", "random", "aguments", 42) }.should_not raise_error
end

it "should create one worker" do
stub(TestJob).workers { 0 }
mock(TestJob).set_workers(1)
TestJob.after_enqueue_scale_workers_up
end
end

describe ".after_perform_scale_workers_down" do

it "should add the hook" do
Resque::Plugin.after_hooks(TestJob).should include("after_perform_scale_workers_down")
end

it "should take whatever args Resque hands in" do
Resque::Plugins::HerokuAutoscaler.class_eval("@@heroku_client = nil")
stub(Heroku::Client).new { stub!.set_workers }

lambda { TestJob.after_perform_scale_workers_down("some", "random", "aguments", 42) }.should_not raise_error
end

context "when the queue is empty" do
before do
stub(Resque).info { {:pending => 0} }
end

it "should set workers to 0" do
mock(TestJob).set_workers(0)
TestJob.after_perform_scale_workers_down
end
end

context "when the queue is not empty" do
before do
stub(Resque).info { {:pending => 1} }
end

it "should not change workers" do
dont_allow(TestJob).set_workers
TestJob.after_perform_scale_workers_down
end
end
end

describe ".set_workers" do
it "should use the Heroku client to set the workers" do
ENV['HEROKU_APP'] = 'some app name'
mock(TestJob).heroku_client { mock!.set_workers('some app name', 10) }
TestJob.set_workers(10)
end
end

describe ".heroku_client" do
before do
ENV['HEROKU_USER'] = 'john doe'
ENV['HEROKU_PASS'] = 'password'
end

it "should return a heroku client" do
Resque::Plugins::HerokuAutoscaler.class_eval("@@heroku_client = nil")
TestJob.heroku_client.should be_a(Heroku::Client)
end

it "should use the right username and password" do
Resque::Plugins::HerokuAutoscaler.class_eval("@@heroku_client = nil")
mock(Heroku::Client).new('john doe', 'password')
TestJob.heroku_client
end

it "should return the same client for multiple jobs" do
a = 0
stub(Heroku::Client).new { a += 1 }
TestJob.heroku_client.should == TestJob.heroku_client
end

it "should share the same client across differnet job types" do
a = 0
stub(Heroku::Client).new { a += 1 }

TestJob.heroku_client.should == AnotherJob.heroku_client
end
end
end

0 comments on commit 4152fc1

Please sign in to comment.