sanford 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,6 +1,3 @@
1
1
  require "bundler/gem_tasks"
2
2
 
3
3
  require 'bench/tasks'
4
-
5
- require "assert/rake_tasks"
6
- Assert::RakeTasks.install
data/lib/sanford/host.rb CHANGED
@@ -25,6 +25,7 @@ module Sanford
25
25
  option :logger, :default => proc{ Sanford.config.logger }
26
26
  option :verbose_logging, :default => true
27
27
  option :receives_keep_alive, :default => false
28
+ option :runner, :default => proc{ Sanford.config.runner }
28
29
  option :error_proc, Proc, :default => proc{ }
29
30
  option :init_proc, Proc, :default => proc{ }
30
31
 
@@ -77,6 +78,10 @@ module Sanford
77
78
  self.configuration.receives_keep_alive *args
78
79
  end
79
80
 
81
+ def runner(*args)
82
+ self.configuration.runner *args
83
+ end
84
+
80
85
  def error(&block)
81
86
  self.configuration.error_proc = block
82
87
  end
@@ -11,7 +11,7 @@ module Sanford
11
11
 
12
12
  # NOTE: The `name` attribute shouldn't be removed, it is used to identify
13
13
  # a `HostData`, particularly in error handlers
14
- attr_reader :name, :logger, :verbose, :keep_alive, :error_proc
14
+ attr_reader :name, :logger, :verbose, :keep_alive, :runner, :error_proc
15
15
 
16
16
  def initialize(service_host, options = nil)
17
17
  service_host.configuration.init_proc.call
@@ -23,6 +23,7 @@ module Sanford
23
23
  @logger = configuration[:logger]
24
24
  @verbose = configuration[:verbose_logging]
25
25
  @keep_alive = configuration[:receives_keep_alive]
26
+ @runner = configuration[:runner]
26
27
  @error_proc = configuration[:error_proc]
27
28
 
28
29
  @handlers = service_host.versioned_services.inject({}) do |hash, (version, services)|
@@ -30,6 +31,10 @@ module Sanford
30
31
  end
31
32
  end
32
33
 
34
+ def run(handler_class, request)
35
+ self.runner.new(handler_class, request, self.logger).run
36
+ end
37
+
33
38
  def handler_class_for(version, service)
34
39
  version_group = @handlers[version] || {}
35
40
  version_group[service] || raise(Sanford::NotFoundError)
@@ -3,50 +3,72 @@ require 'sanford-protocol'
3
3
 
4
4
  module Sanford
5
5
 
6
- class Runner
6
+ module Runner
7
7
 
8
8
  ResponseArgs = Struct.new(:status, :data)
9
9
 
10
10
  attr_reader :handler_class, :request, :logger
11
11
 
12
- def self.run(handler_class, params = nil, logger = nil)
13
- request = Sanford::Protocol::Request.new('version', 'name', params || {})
14
- self.new(handler_class, request, logger).run
12
+ def self.included(klass)
13
+ klass.class_eval{ extend ClassMethods }
15
14
  end
16
15
 
17
16
  def initialize(handler_class, request, logger = nil)
18
17
  @handler_class, @request = handler_class, request
19
18
  @logger = logger || Sanford.config.logger
20
19
  @handler = @handler_class.new(self)
20
+ self.init
21
+ end
22
+
23
+ def init
24
+ self.init!
25
+ end
26
+
27
+ def init!
21
28
  end
22
29
 
23
30
  def run
24
- response_args = catch_halt do
25
- @handler.init
26
- @handler.run
27
- end
31
+ response_args = catch_halt{ self.run!(@handler) }
28
32
  Sanford::Protocol::Response.new(response_args.status, response_args.data)
29
33
  end
30
34
 
31
- module HaltMethods
35
+ def run!
36
+ raise NotImplementedError
37
+ end
32
38
 
33
- # It's best to keep what `halt` and `catch_halt` return in the same format.
34
- # Currently this is a `ResponseArgs` object. This is so no matter how the
35
- # block returns (either by throwing or running normally), you get the same
36
- # thing kind of object.
39
+ # It's best to keep what `halt` and `catch_halt` return in the same format.
40
+ # Currently this is a `ResponseArgs` object. This is so no matter how the
41
+ # block returns (either by throwing or running normally), you get the same
42
+ # thing kind of object.
37
43
 
38
- def halt(status, options = nil)
39
- options = OpenStruct.new(options || {})
40
- response_status = [ status, options.message ]
41
- throw :halt, ResponseArgs.new(response_status, options.data)
42
- end
44
+ def halt(status, options = nil)
45
+ options = OpenStruct.new(options || {})
46
+ response_status = [ status, options.message ]
47
+ throw :halt, ResponseArgs.new(response_status, options.data)
48
+ end
43
49
 
44
- def catch_halt(&block)
45
- catch(:halt){ ResponseArgs.new(*block.call) }
50
+ def catch_halt(&block)
51
+ catch(:halt){ ResponseArgs.new(*block.call) }
52
+ end
53
+
54
+ module ClassMethods
55
+
56
+ def run(handler_class, params = nil, logger = nil)
57
+ request = Sanford::Protocol::Request.new('version', 'name', params || {})
58
+ self.new(handler_class, request, logger).run
46
59
  end
47
60
 
48
61
  end
49
- include HaltMethods
62
+
63
+ end
64
+
65
+ class DefaultRunner
66
+ include Sanford::Runner
67
+
68
+ def run!(handler)
69
+ handler.init
70
+ handler.run
71
+ end
50
72
 
51
73
  end
52
74
 
@@ -93,7 +93,7 @@ module Sanford
93
93
  module ClassMethods
94
94
 
95
95
  def run(params = nil, logger = nil)
96
- Sanford::Runner.run(self, params || {}, logger)
96
+ Sanford.config.runner.run(self, params || {}, logger)
97
97
  end
98
98
 
99
99
  end
@@ -5,19 +5,20 @@ require 'sanford/runner'
5
5
  module Sanford
6
6
 
7
7
  class TestRunner
8
- include Sanford::Runner::HaltMethods
8
+ include Sanford::Runner
9
9
 
10
- attr_reader :handler, :response, :request, :logger
10
+ attr_reader :handler, :response
11
11
 
12
- def initialize(handler_class, params = {}, logger = nil)
13
- @handler_class = handler_class
14
- @request = params.kind_of?(Sanford::Protocol::Request) ? params : test_request(params)
15
- @logger = logger || Sanford.config.logger
16
-
17
- @handler = @handler_class.new(self)
12
+ def init!
13
+ if !@request.kind_of?(Sanford::Protocol::Request)
14
+ @request = test_request(@request)
15
+ end
18
16
  @response = build_response catch(:halt){ @handler.init; nil }
19
17
  end
20
18
 
19
+ # we override the `run` method because the TestRunner wants to control
20
+ # storing any generated response. If `init` generated a response, we don't
21
+ # want to `run` at all.
21
22
  def run
22
23
  @response ||= build_response catch_halt{ @handler.run }
23
24
  end
@@ -25,7 +26,7 @@ module Sanford
25
26
  protected
26
27
 
27
28
  def test_request(params)
28
- Sanford::Protocol::Request.new('test_version', 'test_service', params)
29
+ Sanford::Protocol::Request.new('version', 'name', params)
29
30
  end
30
31
 
31
32
  def build_response(response_args)
@@ -35,8 +36,8 @@ module Sanford
35
36
  module Helpers
36
37
  module_function
37
38
 
38
- def test_runner(*args)
39
- TestRunner.new(*args)
39
+ def test_runner(handler_class, params = nil, logger = nil)
40
+ TestRunner.new(handler_class, params || {}, logger)
40
41
  end
41
42
 
42
43
  end
@@ -1,3 +1,3 @@
1
1
  module Sanford
2
- VERSION = "0.6.0"
2
+ VERSION = "0.6.1"
3
3
  end
@@ -44,7 +44,7 @@ module Sanford
44
44
  self.log_handler_class(handler_class)
45
45
  service.handler_class = handler_class
46
46
 
47
- response = Sanford::Runner.new(handler_class, request, @host_data.logger).run
47
+ response = @host_data.run(handler_class, request)
48
48
  service.response = response
49
49
  rescue Exception => exception
50
50
  self.handle_exception(service, exception, @host_data)
data/lib/sanford.rb CHANGED
@@ -6,6 +6,7 @@ require 'set'
6
6
 
7
7
  require 'sanford/host'
8
8
  require 'sanford/logger'
9
+ require 'sanford/runner'
9
10
  require 'sanford/server'
10
11
  require 'sanford/service_handler'
11
12
  require 'sanford/version'
@@ -40,6 +41,7 @@ module Sanford
40
41
  include NsOptions::Proxy
41
42
  option :services_file, Pathname, :default => ENV['SANFORD_SERVICES_FILE']
42
43
  option :logger, :default => Sanford::NullLogger.new
44
+ option :runner, :default => Sanford::DefaultRunner
43
45
  end
44
46
 
45
47
  class Hosts
data/sanford.gemspec CHANGED
@@ -21,6 +21,6 @@ Gem::Specification.new do |gem|
21
21
  gem.add_dependency("ns-options", ["~>1.0"])
22
22
  gem.add_dependency("sanford-protocol", ["~>0.5"])
23
23
 
24
- gem.add_development_dependency("assert", ["~>1.0"])
24
+ gem.add_development_dependency("assert", ["~>2.0"])
25
25
  gem.add_development_dependency("assert-mocha", ["~>1.0"])
26
26
  end
@@ -1,6 +1,6 @@
1
1
  module Test
2
2
 
3
- module ForkServerHelper
3
+ module SpawnServerHelper
4
4
 
5
5
  def start_server(host, &block)
6
6
  begin
@@ -16,30 +16,4 @@ module Test
16
16
 
17
17
  end
18
18
 
19
- module ManagerHelper
20
-
21
- # start a Sanford server using Sanford's manager in a forked process
22
- def fork_and_call(proc, &block)
23
- pid = fork do
24
- STDOUT.reopen('/dev/null') unless ENV['SANFORD_DEBUG']
25
- manager = proc.call
26
- trap("TERM"){ manager.stop }
27
- end
28
- sleep 0.3 # give time for the command to run
29
- yield
30
- ensure
31
- if pid
32
- Process.kill("TERM", pid)
33
- Process.wait(pid)
34
- end
35
- end
36
-
37
- def open_socket(host, port)
38
- socket = TCPSocket.new(host, port)
39
- ensure
40
- socket.close rescue false
41
- end
42
-
43
- end
44
-
45
19
  end
@@ -233,7 +233,7 @@ class RequestHandlingTest < Assert::Context
233
233
  end
234
234
 
235
235
  class ForkedServerTest < RequestHandlingTest
236
- include Test::ForkServerHelper
236
+ include Test::SpawnServerHelper
237
237
  end
238
238
 
239
239
  # Simple service test that echos back the params sent to it
@@ -13,8 +13,8 @@ class Sanford::HostData
13
13
  end
14
14
  subject{ @host_data }
15
15
 
16
- should have_instance_methods :name, :logger, :verbose, :error_proc,
17
- :handler_class_for
16
+ should have_instance_methods :name, :logger, :verbose, :keep_alive, :runner,
17
+ :error_proc, :run, :handler_class_for
18
18
 
19
19
  should "default it's configuration from the service host, but allow overrides" do
20
20
  host_data = Sanford::HostData.new(TestHost, :verbose_logging => false)
@@ -13,7 +13,8 @@ module Sanford::Host
13
13
  subject{ MyHost.instance }
14
14
 
15
15
  should have_instance_methods :configuration, :name, :ip, :port, :pid_file,
16
- :logger, :verbose_logging, :error, :init, :version, :versioned_services
16
+ :logger, :verbose_logging, :runner, :error, :init, :version,
17
+ :versioned_services
17
18
 
18
19
  should "get and set it's configuration options with their matching methods" do
19
20
  subject.name 'my_awesome_host'
@@ -140,7 +140,4 @@ module Sanford::Manager
140
140
 
141
141
  end
142
142
 
143
- # ServerHandler run/start and SignalHandler stop/restart are tested with
144
- # system tests: test/system/managing_test.rb
145
-
146
143
  end
@@ -1,12 +1,12 @@
1
1
  require 'assert'
2
2
 
3
- class Sanford::Runner
3
+ module Sanford::Runner
4
4
 
5
5
  class BaseTest < Assert::Context
6
6
  desc "Sanford::Runner"
7
7
  setup do
8
8
  request = Sanford::Protocol::Request.new('v1', 'test', {})
9
- @runner = Sanford::Runner.new(BasicServiceHandler, request)
9
+ @runner = Sanford::DefaultRunner.new(BasicServiceHandler, request)
10
10
  end
11
11
  subject{ @runner }
12
12
 
@@ -25,7 +25,7 @@ class Sanford::Runner
25
25
  should "be able to build a runner with a handler class and params" do
26
26
  response = nil
27
27
  assert_nothing_raised do
28
- response = Sanford::Runner.run(BasicServiceHandler, {})
28
+ response = Sanford::DefaultRunner.run(BasicServiceHandler, {})
29
29
  end
30
30
 
31
31
  assert_equal 200, response.code
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sanford
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 5
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 6
9
- - 0
10
- version: 0.6.0
9
+ - 1
10
+ version: 0.6.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Collin Redding
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2013-02-21 00:00:00 Z
19
+ date: 2013-02-27 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  prerelease: false
@@ -70,11 +70,11 @@ dependencies:
70
70
  requirements:
71
71
  - - ~>
72
72
  - !ruby/object:Gem::Version
73
- hash: 15
73
+ hash: 3
74
74
  segments:
75
- - 1
75
+ - 2
76
76
  - 0
77
- version: "1.0"
77
+ version: "2.0"
78
78
  requirement: *id004
79
79
  name: assert
80
80
  type: :development
@@ -135,7 +135,6 @@ files:
135
135
  - test/support/service_handlers.rb
136
136
  - test/support/services.rb
137
137
  - test/support/simple_client.rb
138
- - test/system/managing_test.rb
139
138
  - test/system/request_handling_test.rb
140
139
  - test/unit/config_test.rb
141
140
  - test/unit/error_handler_test.rb
@@ -190,7 +189,6 @@ test_files:
190
189
  - test/support/service_handlers.rb
191
190
  - test/support/services.rb
192
191
  - test/support/simple_client.rb
193
- - test/system/managing_test.rb
194
192
  - test/system/request_handling_test.rb
195
193
  - test/unit/config_test.rb
196
194
  - test/unit/error_handler_test.rb
@@ -1,94 +0,0 @@
1
- require 'assert'
2
-
3
- require 'sanford/cli'
4
-
5
- class ManagingTest < Assert::Context
6
- include Test::ManagerHelper
7
- desc "Using Sanford's Manager"
8
- setup do
9
- @start_options = { :host => 'MyHost', :ip => 'localhost', :port => 12345 }
10
- end
11
-
12
- class RunTest < ManagingTest
13
- desc "to run a server"
14
- setup do
15
- @proc = proc{ Sanford::Manager.call(:run, @start_options) }
16
- end
17
-
18
- should "run the server specified and write a PID file" do
19
- self.fork_and_call(@proc) do
20
- assert_nothing_raised{ self.open_socket('localhost', 12345) }
21
- assert File.exists?('tmp/my_host.pid')
22
- end
23
- end
24
-
25
- end
26
-
27
- class StartTest < ManagingTest
28
- desc "to start a daemonized server"
29
- setup do
30
- @proc = proc{ Sanford::Manager.call(:start, @start_options) }
31
- end
32
- teardown do
33
- Sanford::Manager.call(:stop, @start_options)
34
- end
35
-
36
- should "run the server specified and write a PID file" do
37
- self.fork_and_call(@proc) do
38
- assert_nothing_raised{ self.open_socket('localhost', 12345) }
39
- assert File.exists?('tmp/my_host.pid')
40
- end
41
- end
42
-
43
- end
44
-
45
- class StopTest < ManagingTest
46
- desc "to stop a daemonized server"
47
- setup do
48
- @start_proc = proc{ Sanford::Manager.call(:start, @start_options) }
49
- end
50
-
51
- should "stop the server specified and remove the PID file" do
52
- self.fork_and_call(@start_proc) do
53
- Sanford::Manager.call(:stop, @start_options)
54
- sleep 1
55
-
56
- assert_raises{ self.open_socket('localhost', 12345) }
57
- assert_not File.exists?('tmp/my_host_localhost_12345.pid')
58
- end
59
- end
60
-
61
- end
62
-
63
- class RestartTest < ManagingTest
64
- desc "to restart a daemonized server"
65
- setup do
66
- @start_proc = proc{ Sanford::Manager.call(:start, @start_options) }
67
- end
68
-
69
- should "stop the server specified and remove the PID file" do
70
- self.fork_and_call(@start_proc) do
71
- exception = nil
72
- stop = false
73
- thread = Thread.new do
74
- while !stop do
75
- begin
76
- self.open_socket('localhost', 12345)
77
- sleep 0.1
78
- rescue Exception => exception
79
- end
80
- end
81
- end
82
- Sanford::Manager.call(:restart, @start_options)
83
- thread.join(1)
84
-
85
- # make sure we didn't lost the ability to connect
86
- assert_nil exception
87
-
88
- stop = true
89
- thread.join
90
- end
91
- end
92
- end
93
-
94
- end