harbour 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/TODO CHANGED
@@ -1,3 +1,5 @@
1
1
  Add timeout to pid, since the port may be open but no pid from lsof. Doing this will also allow the sleep(2) to be removed from start_server! in specs.
2
2
  add a self.all_ports which lists all open ports, maybe returning Port objects.
3
3
  raise exceptions instead of returning nil
4
+ add commands for linux, it should allow other OS's to be added as well.
5
+ Work out if there is a better way to test
@@ -4,6 +4,8 @@ module Harbour
4
4
  class Port
5
5
  attr_reader :port
6
6
 
7
+ class PortFailedToClose < StandardError; end
8
+
7
9
  def initialize(port)
8
10
  @port = port
9
11
  end
@@ -12,30 +14,40 @@ module Harbour
12
14
  command "nc -z localhost #{port}"
13
15
  end
14
16
 
17
+ def closed?
18
+ !open?
19
+ end
20
+
15
21
  def pid
16
22
  return nil unless open?
17
- `lsof -P | grep ':#{port}' | grep 'LISTEN' | awk '{print $2}'`
23
+ `lsof -P | grep ':#{port}' | grep 'LISTEN' | awk '{print $2}'`.to_i
18
24
  end
19
25
 
20
26
  def term!(options = {})
21
27
  return nil unless open?
22
- command "kill #{pid}"
23
- ensure_open(options[:timeout] || 10)
28
+ Process.kill('TERM', pid)
29
+ ensure_closed!(options[:timeout] || 10)
30
+ rescue PortFailedToClose
31
+ raise unless options[:try_kill] == true
32
+ kill!(options)
24
33
  end
25
34
 
26
35
  def kill!(options = {})
27
36
  return nil unless open?
28
- command "kill -9 #{pid}"
29
- ensure_open(options[:timeout] || 10)
37
+ Process.kill('KILL', pid)
38
+ ensure_closed!(options[:timeout] || 10)
30
39
  end
31
40
 
32
41
  private
33
42
 
34
- def ensure_open(within = 10)
43
+ def ensure_closed!(within = 10, step = 1)
44
+ _elasped = within
35
45
  within.times do
36
- break if open?
37
- sleep(1)
46
+ break if closed?
47
+ sleep(step)
48
+ _elasped =- step
38
49
  end
50
+ raise PortFailedToClose, "Port #{port} did not close within #{within} seconds" if _elasped < 1
39
51
  end
40
52
 
41
53
  def command(_command)
@@ -1,3 +1,3 @@
1
1
  module Harbour
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -9,7 +9,7 @@ describe Harbour::Port do
9
9
 
10
10
  it '#open? returns true if port is open' do
11
11
  with_server(port) do
12
- Harbour::Port.new(3000).open?.should be_true
12
+ Harbour::Port.new(port).open?.should be_true
13
13
  end
14
14
  end
15
15
 
@@ -19,16 +19,34 @@ describe Harbour::Port do
19
19
  end
20
20
  end
21
21
 
22
- it '#term! terminates process listening on port' do
23
- with_server(port) do
24
- Harbour::Port.new(3000).term!
25
- port_open?(port).should be_false
22
+ describe '#term!' do
23
+ pending 'terminates process listening on port' do
24
+ # Webrick does not die on TERM
25
+ with_server(port) do
26
+ Harbour::Port.new(port).term!
27
+ port_open?(port).should be_false
28
+ end
29
+ end
30
+
31
+ it 'raises if port fails to close' do
32
+ # this is fragile, Webrick does not die on TERM, but may do in the future
33
+ with_server(port) do
34
+ expect { Harbour::Port.new(port).term! }.to raise_error(Harbour::Port::PortFailedToClose)
35
+ end
36
+ end
37
+
38
+ it 'trys to kill process if term fails' do
39
+ # this is fragile, Webrick does not die on TERM, but may do in the future
40
+ with_server(port) do
41
+ Harbour::Port.new(port, :try_kill => true).term!
42
+ port_open?(port).should be_false
43
+ end
26
44
  end
27
45
  end
28
46
 
29
47
  it '#kill! terminates process listening on port' do
30
48
  with_server(port) do
31
- Harbour::Port.new(3000).kill!
49
+ Harbour::Port.new(port).kill!
32
50
  port_open?(port).should be_false
33
51
  end
34
52
  end
@@ -4,27 +4,27 @@ require 'harbour'
4
4
 
5
5
  RSpec.configure do |config|
6
6
  config.treat_symbols_as_metadata_keys_with_true_values = true
7
- config.run_all_when_everything_filtered = true
8
- config.filter_run :focus
9
- config.order = 'random'
7
+ # config.run_all_when_everything_filtered = true
8
+ # config.filter_run :focus
9
+ # config.order = 'random'
10
10
  end
11
11
 
12
- def with_server(port = '8000', &block)
12
+ def with_server(port = 8000, &block)
13
13
  start_server!(port)
14
14
  yield
15
15
  stop_server!(port)
16
16
  end
17
17
 
18
18
  def start_server!(port)
19
- return if system "nc -z localhost #{port}"
19
+ return if system "nc -z localhost #{port} > /dev/null"
20
20
  server_root = File.join(File.dirname(__FILE__), 'server')
21
- system "cd #{server_root} && rackup -d -p #{port} &"
22
- sleep(2)
21
+ system "cd #{server_root} && rackup -D -s webrick -p #{port}"
22
+ sleep(1)
23
23
  end
24
24
 
25
25
  def stop_server!(port)
26
- pid = `lsof -P | grep ':#{port}' | grep 'LISTEN' | awk '{print $2}'`
27
- system "kill -9 #{pid}"
26
+ pid = `lsof -P | grep ':#{port}' | grep 'LISTEN' | awk '{print $2}'`.to_i
27
+ Process.kill('KILL', pid)
28
28
  end
29
29
 
30
30
  def port_open?(port)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: harbour
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-06 00:00:00.000000000 Z
12
+ date: 2012-10-10 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: !ruby/object:Gem::Requirement
16
+ requirement: &2161129300 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,15 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
24
+ version_requirements: *2161129300
30
25
  - !ruby/object:Gem::Dependency
31
26
  name: rack
32
- requirement: !ruby/object:Gem::Requirement
27
+ requirement: &2161127440 !ruby/object:Gem::Requirement
33
28
  none: false
34
29
  requirements:
35
30
  - - ! '>='
@@ -37,12 +32,7 @@ dependencies:
37
32
  version: '0'
38
33
  type: :development
39
34
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: '0'
35
+ version_requirements: *2161127440
46
36
  description: Query and terminate processes listening on ports
47
37
  email:
48
38
  - kris.leech@interkonect.com
@@ -83,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
73
  version: '0'
84
74
  requirements: []
85
75
  rubyforge_project:
86
- rubygems_version: 1.8.23
76
+ rubygems_version: 1.8.10
87
77
  signing_key:
88
78
  specification_version: 3
89
79
  summary: Query and terminate processes listening on ports