communist 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ 0.2.0 (Aug , 2012)
2
+ ===================
3
+
4
+ * Add NullServer.
5
+ * Add Constants.
6
+ * Add server specs.
7
+ * Change code logic for setting up server ports.
8
+ * Change server start up logic.
9
+ * Change code logic for stopping a server.
data/README.md CHANGED
@@ -4,7 +4,9 @@ The Manifesto:
4
4
 
5
5
  >It is Communist prime role to serve community by providing Cucumber steps for
6
6
  >testing external API calls from command line applications. To meet this aim
7
- >the Communist employees Sinatra DSL for mocking server responses.
7
+ >the Communist employees Sinatra DSL for mocking server responses. The Communist
8
+ >server receives requests from CLI and provides an API to respond to those
9
+ >requests. Canned answers/expectations have to be provided upfront.
8
10
 
9
11
  ## Installation
10
12
 
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+
3
+ module Communist
4
+ module Const
5
+
6
+ DEFAULT_TIMEOUT = 30
7
+ DEFAULT_HOST = '127.0.0.1'
8
+
9
+ end # Const
10
+ end # Communist
@@ -2,6 +2,7 @@ require 'communist/server'
2
2
 
3
3
  Before do
4
4
  ENV['TEST_HOST'] = '127.0.0.1:0'
5
+ puts 'RUNNING LOCALLY'
5
6
  end
6
7
 
7
8
  After do
@@ -0,0 +1,12 @@
1
+ # encoding: utf-8
2
+
3
+ module Communist
4
+
5
+ # Provides a dummy server.
6
+ class NullServer
7
+ def method_missing(*)
8
+ return nil
9
+ end
10
+ end
11
+
12
+ end # Communist
@@ -7,6 +7,8 @@ require 'communist'
7
7
  module Communist
8
8
  class Server
9
9
 
10
+ include Communist::Const
11
+
10
12
  class ServerError < StandardError; end
11
13
 
12
14
  class Identify
@@ -35,10 +37,13 @@ module Communist
35
37
  @app = app
36
38
  @host = options[:host]
37
39
  @server_thread = nil
40
+ @port = options[:port] || Communist.server_port
41
+ @port ||= Communist::Server.ports[@app.object_id]
42
+ @port ||= find_available_port
38
43
  end
39
44
 
40
45
  def host
41
- "127.0.0.1"
46
+ Communist.server_host || DEFAULT_HOST
42
47
  end
43
48
 
44
49
  def responsive?
@@ -47,28 +52,26 @@ module Communist
47
52
  res = Net::HTTP.start(host, @port) { |http| http.get('/__identify__') }
48
53
 
49
54
  if res.is_a?(Net::HTTPSuccess) or res.is_a?(Net::HTTPRedirection)
50
- return res.body == @app.object_id.to_s
55
+ return res.body == app.object_id.to_s
51
56
  end
52
57
  rescue Errno::ECONNREFUSED, Errno::EBADF
53
58
  return false
54
59
  end
55
60
 
56
61
  def start(&block)
57
- if @app
58
- @port = Server.ports[@app.object_id]
62
+ raise ArgumentError, 'app required' unless app
59
63
 
60
- if not @port or not responsive?
61
- @port = find_available_port
62
- Server.ports[@pp.object_id] = @port
64
+ unless responsive?
65
+ Communist::Server.ports[app.object_id] = port
63
66
 
64
- @server_thread = Thread.new do
65
- Communist.run_default_server(Identify.new(app), @port) do |server|
66
- Communist.server = server
67
- end
67
+ @server_thread = Thread.new do
68
+ Communist.run_default_server(Identify.new(app), port) do |server|
69
+ Communist.servers[app.object_id] = server
70
+ trap "INT" do server.shutdown end
68
71
  end
69
-
70
- Timeout.timeout(5) { @server_thread.join(0.1) until responsive? }
71
72
  end
73
+
74
+ Timeout.timeout(DEFAULT_TIMEOUT) { @server_thread.join(0.1) until responsive? }
72
75
  end
73
76
  rescue TimeoutError
74
77
  raise ServerError, "Rack application timed out during start"
@@ -76,13 +79,24 @@ module Communist
76
79
  self
77
80
  end
78
81
 
82
+ # Stops the server after handling the connection.
83
+ # Attempts to stop the server gracefully, otherwise
84
+ # shuts current connection right away.
79
85
  def stop
80
- Communist.server.respond_to?(:stop!) ? Communist.server.stop! : Communist.server.stop
86
+ server = Communist.servers.delete(app.object_id) { |s| NullServer.new }
87
+ if Communist.server.respond_to?(:shutdown)
88
+ server.shutdown
89
+ elsif Communist.server.respond_to?(:stop!)
90
+ server.stop!
91
+ else
92
+ server.stop
93
+ end
81
94
  @server_thread.join
82
95
  end
83
96
 
84
97
  def self.run(&block)
85
98
  app = Sinatra.new do
99
+ set :show_exceptions, true
86
100
  set :environment, :test
87
101
  disable :protection
88
102
 
@@ -102,7 +116,7 @@ module Communist
102
116
  private
103
117
 
104
118
  def find_available_port
105
- server = TCPServer.new('127.0.0.1', 0)
119
+ server = TCPServer.new(DEFAULT_HOST, 0)
106
120
  server.addr[Socket::Constants::SOCK_STREAM]
107
121
  ensure
108
122
  server.close if server
@@ -1,3 +1,3 @@
1
1
  module Communist
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/communist.rb CHANGED
@@ -2,13 +2,21 @@ require 'communist/version'
2
2
  require 'rack'
3
3
 
4
4
  module Communist
5
+
6
+ class CommunistError < StandardError; end
7
+
5
8
  class << self
6
9
  attr_accessor :app_host
7
10
  attr_accessor :default_host
11
+ attr_accessor :server_host, :server_port
8
12
  attr_accessor :run_server
9
13
  attr_accessor :app
10
14
 
11
- # Possible options
15
+ # Configure Communist options
16
+ #
17
+ # Communist.configure do |config|
18
+ # config.server_host = 'http://localhost'
19
+ # end
12
20
  #
13
21
  def configure
14
22
  yield self
@@ -26,6 +34,12 @@ module Communist
26
34
  end
27
35
  end
28
36
 
37
+ # Stores server instances.
38
+ #
39
+ def servers
40
+ @servers ||= {}
41
+ end
42
+
29
43
  # By default run thin or fall back on webrick
30
44
  #
31
45
  def run_default_server(app, port, &block)
@@ -41,7 +55,9 @@ module Communist
41
55
 
42
56
  end
43
57
 
44
- autoload :Server, 'communist/server'
58
+ autoload :Server, 'communist/server'
59
+ autoload :Const, 'communist/const'
60
+ autoload :NullServer, 'communist/null_server'
45
61
 
46
62
  end # Communist
47
63
 
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe Communist::Server do
4
+
5
+ let(:app) { proc { |env| [200, {}, "Hello Server!"] } }
6
+
7
+ context 'initialization' do
8
+ it 'sets application' do
9
+ server = Communist::Server.new(app)
10
+ server.app.should == app
11
+ end
12
+
13
+ it "sets port" do
14
+ server = Communist::Server.new(app).start
15
+ server.port.should == Communist::Server.ports[app.object_id]
16
+ end
17
+ end
18
+
19
+ it "spins up a server" do
20
+ server = Communist::Server.new(app).start
21
+ res = Net::HTTP.start(server.host, server.port) { |http| http.get('/') }
22
+ res.body.should include("Hello Server!")
23
+ end
24
+
25
+ it "binds to specified host" do
26
+ Communist.server_host = "0.0.0.0"
27
+ server = Communist::Server.new(app).start
28
+ server.host.should == "0.0.0.0"
29
+ Communist.server_host = nil
30
+ end
31
+
32
+ end # Communist::Server
data/spec/spec_helper.rb CHANGED
@@ -4,7 +4,27 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
4
  $LOAD_PATH.unshift(File.dirname(__FILE__))
5
5
 
6
6
  require 'rspec'
7
+ require 'communist'
8
+
9
+ DEFAULT_TEST_HOST = '0.0.0.0'
10
+ DEFAULT_TEST_PORT = 3333
11
+
12
+ module Helpers
13
+ def start_server(app, options, &block)
14
+ @server = Communist::Server.new(app, options)
15
+ @thread = Thread.new { @server.start }
16
+ end
17
+
18
+ def stop_server
19
+ @server.stop!
20
+ @thread.kill
21
+ end
22
+ end
7
23
 
8
24
  RSpec.configure do |config|
9
25
  config.order = :rand
26
+ config.color_enabled = true
27
+ config.tty = true
28
+
29
+ config.include Helpers
10
30
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: communist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
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-07-14 00:00:00.000000000 Z
12
+ date: 2012-08-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
16
- requirement: &2152389780 !ruby/object:Gem::Requirement
16
+ requirement: &2152389520 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2152389780
24
+ version_requirements: *2152389520
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: sinatra
27
- requirement: &2152389340 !ruby/object:Gem::Requirement
27
+ requirement: &2152389080 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2152389340
35
+ version_requirements: *2152389080
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &2152388880 !ruby/object:Gem::Requirement
38
+ requirement: &2152388600 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2152388880
46
+ version_requirements: *2152388600
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rake
49
- requirement: &2152388400 !ruby/object:Gem::Requirement
49
+ requirement: &2152388160 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2152388400
57
+ version_requirements: *2152388160
58
58
  description: Library for mocking CLI calls to external APIs
59
59
  email:
60
60
  - ''
@@ -64,15 +64,19 @@ extra_rdoc_files: []
64
64
  files:
65
65
  - .gitignore
66
66
  - .rvmrc
67
+ - CHANGELOG.md
67
68
  - Gemfile
68
69
  - LICENSE
69
70
  - README.md
70
71
  - Rakefile
71
72
  - communist.gemspec
72
73
  - lib/communist.rb
74
+ - lib/communist/const.rb
73
75
  - lib/communist/cucumber.rb
76
+ - lib/communist/null_server.rb
74
77
  - lib/communist/server.rb
75
78
  - lib/communist/version.rb
79
+ - spec/server_spec.rb
76
80
  - spec/spec_helper.rb
77
81
  homepage: ''
78
82
  licenses: []
@@ -99,4 +103,5 @@ signing_key:
99
103
  specification_version: 3
100
104
  summary: Library for mocking CLI calls to external APIs
101
105
  test_files:
106
+ - spec/server_spec.rb
102
107
  - spec/spec_helper.rb