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 1 commit
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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're missing a dot (.) at the end of this sentence ;)


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'
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.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.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
12 changes: 12 additions & 0 deletions lib/heroku/forward/utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Heroku
module Forward
module Utils

# 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
33 changes: 27 additions & 6 deletions spec/lib/heroku/backends/thin_spec.rb
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
54 changes: 30 additions & 24 deletions spec/lib/heroku/backends/unicorn_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,41 @@

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

let(:backend) do
Heroku::Forward::Backends::Unicorn.new
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
describe "with defaults" do
let(:backend) do
Heroku::Forward::Backends::Unicorn.new
end

after do
backend.terminate!
end

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

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

it "#spawn!" do
backend.application = "spec/support/app.ru"
backend.spawn!.should_not == 0
sleep 2
backend.terminate!.should be_true
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
Expand Down
18 changes: 18 additions & 0 deletions spec/lib/heroku/utils_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'spec_helper'

describe Heroku::Forward::Utils do

context "#tmp_filename" do
let(:filename) { Heroku::Forward::Utils.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