Skip to content

Commit

Permalink
! specs, + Picky::Rack::Harakiri -> Rack::Harakiri
Browse files Browse the repository at this point in the history
  • Loading branch information
floere committed Nov 13, 2011
1 parent 6943050 commit f114934
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 73 deletions.
23 changes: 23 additions & 0 deletions server/lib/picky/backends/internal.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Picky

module Backends

module Internal

# Nothing needs to be deleted from it.
#
def delete _

end

# It does not need to be cleared.
#
def clear

end

end

end

end
95 changes: 46 additions & 49 deletions server/lib/picky/rack/harakiri.rb
Original file line number Diff line number Diff line change
@@ -1,61 +1,58 @@
module Picky
module Rack # :nodoc:

# Simple Rack Middleware to kill Unicorns after X requests.
#
# Use as follows in e.g. your rackup File:
#
# Rack::Harakiri.after = 100
# use Rack::Harakiri
#
# Then the Unicorn will commit suicide after 100 requests (50 is the default).
#
# The Master Unicorn process forks a new child Unicorn to replace the old one.
#
class Harakiri

# Set the amount of requests before the Unicorn commits Harakiri.
#
class << self
attr_accessor :after
end

module Rack # :nodoc:
def initialize app
@app = app

# Simple Rack Middleware to kill Unicorns after X requests.
@requests = 0
@quit_after_requests = self.class.after || 50
end

# #call interface method.
#
# Use as follows in e.g. your rackup File:
# Harakiri is a middleware, so it delegates the the app or
# the next middleware after checking if it is time to honorably retire.
#
# Rack::Harakiri.after = 100
# use Rack::Harakiri
def call env
harakiri
@app.call env
end

# Checks to see if it is time to honorably retire.
#
# Then the Unicorn will commit suicide after 100 requests (50 is the default).
# If yes, kills itself (Unicorn will answer the request, honorably).
#
# The Master Unicorn process forks a new child Unicorn to replace the old one.
# Note: Sends its process a QUIT signal if it is time.
#
class Harakiri

# Set the amount of requests before the Unicorn commits Harakiri.
#
class << self
attr_accessor :after
end

def initialize app
@app = app

@requests = 0
@quit_after_requests = self.class.after || 50
end

# #call interface method.
#
# Harakiri is a middleware, so it delegates the the app or
# the next middleware after checking if it is time to honorably retire.
#
def call env
harakiri
@app.call env
end

# Checks to see if it is time to honorably retire.
#
# If yes, kills itself (Unicorn will answer the request, honorably).
#
# Note: Sends its process a QUIT signal if it is time.
#
def harakiri
@requests = @requests + 1
Process.kill(:QUIT, Process.pid) if harakiri?
end

# Is it time to honorably retire?
#
def harakiri?
@requests >= @quit_after_requests
end
def harakiri
@requests = @requests + 1
Process.kill(:QUIT, Process.pid) if harakiri?
end

# Is it time to honorably retire?
#
def harakiri?
@requests >= @quit_after_requests
end

end

end
2 changes: 1 addition & 1 deletion server/spec/lib/category_indexing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
category.stub! :indexer => @indexer
end
it "tells the indexer to index" do
@indexer.should_receive(:index).once.with [category]
@indexer.should_receive(:index).once

category.prepare
end
Expand Down
10 changes: 2 additions & 8 deletions server/spec/lib/generators/similarity/none_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,17 @@
before(:each) do
@similarity = described_class.new
end

describe "saved?" do
it "returns the right answer" do
@similarity.saved?.should == false
end
end

describe 'encode' do
it 'should always return nil' do
@similarity.encoded(:whatever).should == nil
end
end

describe 'generate_from' do
it 'should return an empty hash, always' do
@similarity.generate_from(:anything).should == {}
end
end

end
7 changes: 4 additions & 3 deletions server/spec/lib/indexed/bundle_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
@category = Picky::Category.new :some_category, @index

@weights = stub :weights, :saved? => true
@partial = stub :partial
@similarity = stub :similarity
@partial = stub :partial, :saved? => true
@similarity = stub :similarity, :saved? => true
@bundle = described_class.new :some_name, @category, Picky::Backends::Memory.new, @weights, @partial, @similarity
end

Expand Down Expand Up @@ -159,7 +159,8 @@
@category = Picky::Category.new :some_category, @index

@weights = stub :weights, :saved? => true
@bundle = described_class.new :some_name, @category, Picky::Backends::Memory.new, @weights, :partial, :similarity
@partial = stub :partial, :saved? => true
@bundle = described_class.new :some_name, @category, Picky::Backends::Memory.new, @weights, @partial, :similarity
end
it 'should initialize the index correctly' do
@bundle.backend_inverted.should be_kind_of(Picky::Backends::Memory::JSON)
Expand Down
16 changes: 14 additions & 2 deletions server/spec/lib/indexers/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,21 @@

describe 'index' do
it 'processes' do
indexer.should_receive(:process).once.with :categories
categories = stub :categories, :empty => nil, :cache => nil

indexer.index :categories
indexer.should_receive(:process).once.with categories

indexer.index categories
end
it 'calls the right methods on the categories' do
indexer.stub! :process

categories = stub :categories

categories.should_receive(:empty).once.ordered
categories.should_receive(:cache).once.ordered

indexer.index categories
end
end

Expand Down
16 changes: 8 additions & 8 deletions server/spec/lib/rack/harakiri_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# encoding: utf-8
require 'spec_helper'

describe Picky::Rack::Harakiri do
describe Rack::Harakiri do
before(:each) do
@app = stub :app
Process.stub! :kill # not taking any chances
Expand All @@ -16,24 +16,24 @@
describe 'harakiri?' do
it "should be true after 50 harakiri calls" do
50.times { @ronin.harakiri }

@ronin.harakiri?.should == true
end
it "should not be true after just 49 harakiri calls" do
49.times { @ronin.harakiri }

@ronin.harakiri?.should == false
end
end
describe "harakiri" do
it "should kill the process after 50 harakiri calls" do
Process.should_receive(:kill).once

50.times { @ronin.harakiri }
end
it "should not kill the process after 49 harakiri calls" do
Process.should_receive(:kill).never

49.times { @ronin.harakiri }
end
end
Expand All @@ -44,12 +44,12 @@
end
it "calls harakiri" do
@ronin.should_receive(:harakiri).once.with

@ronin.call :env
end
it "calls the app" do
@app.should_receive(:call).once.with :env

@ronin.call :env
end
end
Expand All @@ -66,5 +66,5 @@
@ronin.instance_variable_get(:@quit_after_requests).should == 100
end
end

end
4 changes: 2 additions & 2 deletions server/test_project_sinatra/config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Picky::Indexes.reload
#
# See http://vimeo.com/12614970 for more info.
#
# Rack::Harakiri.after = 1000
# use Rack::Harakiri
# Picky::Rack::Harakiri.after = 1000
# use Picky::Rack::Harakiri

run BookSearch.new

0 comments on commit f114934

Please sign in to comment.