Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate unique, temporary names for socket files in a more robust way. #6

Merged
merged 4 commits into from
Jan 24, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Next Release
============

* Improve generation of temporary filename for socket - [@joeyAghion](https://github.com/joeyAghion).

0.3.0
=====

Expand Down
1 change: 1 addition & 0 deletions lib/heroku/forward.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
require 'heroku/forward/errors'
require 'heroku/forward/backends'
require 'heroku/forward/proxy'
require 'heroku/forward/utils/dir'
16 changes: 5 additions & 11 deletions lib/heroku/forward/backends/thin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Thin
# ssl_verify: activated with ssl_verify
def initialize(options = {})
@application = options[:application]
@socket = options[:socket] || new_socket
@socket = options[:socket] || Heroku::Forward::Utils::Dir.tmp_filename('thin', '.sock')
@env = options[:env] || :development

@ssl = options[:ssl] || false
Expand Down Expand Up @@ -62,16 +62,10 @@ def spawned?

private

def new_socket
Tempfile.open 'thin' do |file|
return file.path
end
end

def check!
raise Heroku::Forward::Errors::MissingBackendOptionError.new('application') unless @application && @application.length > 0
raise Heroku::Forward::Errors::MissingBackendApplicationError.new(@application) unless File.exists?(@application)
end
def check!
raise Heroku::Forward::Errors::MissingBackendOptionError.new('application') unless @application && @application.length > 0
raise Heroku::Forward::Errors::MissingBackendApplicationError.new(@application) unless File.exists?(@application)
end

end
end
Expand Down
7 changes: 1 addition & 6 deletions lib/heroku/forward/backends/unicorn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Unicorn

def initialize(options = {})
@application = options[:application]
@socket = options[:socket] || tmp_filename('unicorn', '.sock')
@socket = options[:socket] || Heroku::Forward::Utils::Dir.tmp_filename('unicorn', '.sock')
@env = options[:env] || 'development'
@config_file = options[:config_file]
end
Expand Down Expand Up @@ -38,11 +38,6 @@ def spawned?

private

# Borrowed from ruby 1.9's Dir::Tmpname.make_tmpname for 1.8.7-compatibility.
def tmp_filename(prefix, suffix)
File.join Dir.tmpdir, "#{prefix}#{Time.now.strftime("%Y%m%d")}-#{$$}-#{rand(0x100000000).to_s(36)}#{suffix}"
end

def check!
raise Heroku::Forward::Errors::MissingBackendOptionError.new('application') unless @application && @application.length > 0
raise Heroku::Forward::Errors::MissingBackendApplicationError.new(@application) unless File.exists?(@application)
Expand Down
16 changes: 16 additions & 0 deletions lib/heroku/forward/utils/dir.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Heroku
module Forward
module Utils

module Dir

# Partially borrowed from ruby 1.9's Dir::Tmpname.make_tmpname for 1.8.7-compatibility.
def self.tmp_filename(prefix, suffix)
File.join ::Dir.tmpdir, "#{prefix}#{Time.now.strftime("%Y%m%d")}-#{$$}-#{rand(0x100000000).to_s(36)}#{suffix}"
end

end

end
end
end
55 changes: 0 additions & 55 deletions spec/lib/heroku/backends/unicorn_spec.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@
let(:backend) do
Heroku::Forward::Backends::Thin.new
end

after do
backend.terminate!
end

it "#spawned?" do
backend.spawned?.should be_false
end

it "doesn't generate socket file" do
File.exists?(backend.socket).should_not be_true
end

context "checks" do
it "checks for application" do
Expand All @@ -27,13 +35,26 @@
end

end

it "#spawn!" do
backend.application = "spec/support/app.ru"
backend.spawn!.should_not == 0
sleep 2
backend.terminate!.should be_true

describe "#spawn!" do
before do
backend.application = "spec/support/app.ru"
end

it "starts successfully" do
backend.spawn!.should_not == 0
sleep 2
backend.terminate!.should be_true
end

it "generates socket file that outlives garbage-collection" do
backend.spawn!
sleep 2
lambda { GC.start }.should_not raise_error
File.exists?(backend.socket).should be_true
end
end

end

describe "with SSL" do
Expand Down
61 changes: 61 additions & 0 deletions spec/lib/heroku/forward/backends/unicorn_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require 'spec_helper'
require 'heroku/forward/backends/unicorn'

describe Heroku::Forward::Backends::Unicorn do

describe "with defaults" do
let(:backend) do
Heroku::Forward::Backends::Unicorn.new
end

after do
backend.terminate!
end

it "#spawned?" do
backend.spawned?.should be_false
end

context "checks" do
it "checks for application" do
expect {
backend.spawn!
}.to raise_error Heroku::Forward::Errors::MissingBackendOptionError
end

it "checks that the application file exists" do
expect {
backend.application = 'spec/foobar'
backend.spawn!
}.to raise_error Heroku::Forward::Errors::MissingBackendApplicationError
end

end

it "#spawn!" do
backend.application = "spec/support/app.ru"
backend.spawn!.should_not == 0
sleep 2
backend.terminate!.should be_true
end
end

context "constructs command" do

let(:backend) do
Heroku::Forward::Backends::Unicorn.new(
:application => 'spec/support/app.ru',
:env => 'test',
:socket => '/tmp/unicorn.sock',
:config_file => 'spec/support/unicorn.rb'
)
end

it "forwards arguments to spawner" do
Spoon.should_receive(:spawnp).with(*%w{unicorn --env test --config-file spec/support/unicorn.rb --listen /tmp/unicorn.sock spec/support/app.ru}).and_return(0)
backend.spawn!
end

end
end

18 changes: 18 additions & 0 deletions spec/lib/heroku/forward/utils/dir_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'spec_helper'

describe Heroku::Forward::Utils::Dir do

context "#tmp_filename" do
let(:filename) { Heroku::Forward::Utils::Dir.tmp_filename('foo', '.bar') }

it "doesn't create file" do
File.exists?(filename).should_not be_true
end

it "names file using prefix and suffix" do
filename.should match(/foo.*\.bar/)
end
end

end