sanford 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bench/report.txt +34 -4
- data/bench/runner.rb +122 -1
- data/bench/services.rb +5 -2
- data/lib/sanford/error_handler.rb +60 -0
- data/lib/sanford/exceptions.rb +11 -10
- data/lib/sanford/host.rb +79 -101
- data/lib/sanford/host_data.rb +55 -0
- data/lib/sanford/logger.rb +23 -0
- data/lib/sanford/manager.rb +13 -22
- data/lib/sanford/rake.rb +1 -0
- data/lib/sanford/runner.rb +50 -0
- data/lib/sanford/server.rb +31 -15
- data/lib/sanford/service_handler.rb +34 -43
- data/lib/sanford/test_runner.rb +47 -0
- data/lib/sanford/version.rb +1 -1
- data/lib/sanford/worker.rb +124 -0
- data/lib/sanford.rb +49 -6
- data/sanford.gemspec +1 -1
- data/test/helper.rb +1 -0
- data/test/support/fake_connection.rb +18 -0
- data/test/support/helpers.rb +6 -10
- data/test/support/service_handlers.rb +56 -68
- data/test/support/services.rb +55 -10
- data/test/system/managing_test.rb +18 -18
- data/test/system/request_handling_test.rb +10 -100
- data/test/unit/config_test.rb +1 -43
- data/test/unit/error_handler_test.rb +133 -0
- data/test/unit/host_configuration_test.rb +41 -0
- data/test/unit/host_data_test.rb +65 -0
- data/test/unit/host_test.rb +20 -112
- data/test/unit/{host/version_group_test.rb → host_version_group_test.rb} +0 -0
- data/test/unit/hosts_test.rb +56 -0
- data/test/unit/manager_test.rb +3 -3
- data/test/unit/runner_test.rb +26 -0
- data/test/unit/server_test.rb +10 -2
- data/test/unit/service_handler_test.rb +126 -115
- data/test/unit/worker_test.rb +195 -0
- metadata +28 -16
- data/lib/sanford/config.rb +0 -33
- data/lib/sanford/connection.rb +0 -70
- data/lib/sanford/exception_handler.rb +0 -43
- data/test/unit/connection_test.rb +0 -23
- data/test/unit/exception_handler_test.rb +0 -69
data/lib/sanford/connection.rb
DELETED
@@ -1,70 +0,0 @@
|
|
1
|
-
# Sanford's connection class is an extesion of the connection class provided by
|
2
|
-
# Sanford-Protocol. It provides the main process of reading a request, routing
|
3
|
-
# it and writing a response. All requests are benchmarked and logged. The
|
4
|
-
# connection's `process` method should always try to return a response, so that
|
5
|
-
# clients do not have to timeout.
|
6
|
-
#
|
7
|
-
# Notes:
|
8
|
-
# * This class is separated from `Sanford::Server` to help with thread safety.
|
9
|
-
# The server creates a new instance of this class per connection, which means
|
10
|
-
# there is a separate connection per thread.
|
11
|
-
#
|
12
|
-
require 'benchmark'
|
13
|
-
require 'sanford-protocol'
|
14
|
-
|
15
|
-
require 'sanford/exceptions'
|
16
|
-
|
17
|
-
module Sanford
|
18
|
-
|
19
|
-
class Connection < Sanford::Protocol::Connection
|
20
|
-
|
21
|
-
DEFAULT_TIMEOUT = 1
|
22
|
-
|
23
|
-
attr_reader :service_host, :logger, :exception_handler, :timeout
|
24
|
-
|
25
|
-
def initialize(service_host, client_socket)
|
26
|
-
@service_host = service_host
|
27
|
-
@exception_handler = self.service_host.exception_handler
|
28
|
-
@logger = self.service_host.logger
|
29
|
-
@timeout = (ENV['SANFORD_TIMEOUT'] || DEFAULT_TIMEOUT).to_f
|
30
|
-
super(client_socket)
|
31
|
-
end
|
32
|
-
|
33
|
-
def process
|
34
|
-
response = nil
|
35
|
-
self.logger.info("Received request")
|
36
|
-
benchmark = Benchmark.measure do
|
37
|
-
begin
|
38
|
-
request = Sanford::Protocol::Request.parse(self.read(self.timeout))
|
39
|
-
self.log_request(request)
|
40
|
-
response = Sanford::Protocol::Response.new(*self.run(request))
|
41
|
-
rescue Exception => exception
|
42
|
-
handler = self.exception_handler.new(exception, self.logger)
|
43
|
-
response = handler.response
|
44
|
-
ensure
|
45
|
-
self.write(response.to_hash)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
time_taken = self.round_time(benchmark.real)
|
49
|
-
self.logger.info("Completed in #{time_taken}ms #{response.status}\n")
|
50
|
-
end
|
51
|
-
|
52
|
-
protected
|
53
|
-
|
54
|
-
def run(request)
|
55
|
-
self.service_host.run(request)
|
56
|
-
end
|
57
|
-
|
58
|
-
def log_request(request)
|
59
|
-
self.logger.info(" Version: #{request.version.inspect}")
|
60
|
-
self.logger.info(" Service: #{request.name.inspect}")
|
61
|
-
self.logger.info(" Parameters: #{request.params.inspect}")
|
62
|
-
end
|
63
|
-
|
64
|
-
def round_time(time_in_seconds)
|
65
|
-
((time_in_seconds * 1000.to_f) + 0.5).to_i
|
66
|
-
end
|
67
|
-
|
68
|
-
end
|
69
|
-
|
70
|
-
end
|
@@ -1,43 +0,0 @@
|
|
1
|
-
# Sanford's exception handler class takes an exception and builds a valid
|
2
|
-
# response. For certain exceptions, Sanford will use special response codes and
|
3
|
-
# for all others it will classify them as generic error requests.
|
4
|
-
#
|
5
|
-
require 'sanford-protocol'
|
6
|
-
|
7
|
-
require 'sanford/exceptions'
|
8
|
-
|
9
|
-
module Sanford
|
10
|
-
|
11
|
-
class ExceptionHandler
|
12
|
-
attr_reader :exception, :logger
|
13
|
-
|
14
|
-
def initialize(exception, logger)
|
15
|
-
@exception = exception
|
16
|
-
@logger = logger
|
17
|
-
end
|
18
|
-
|
19
|
-
def response
|
20
|
-
self.logger.error("#{exception.class}: #{exception.message}")
|
21
|
-
self.logger.error(exception.backtrace.join("\n"))
|
22
|
-
status = Sanford::Protocol::ResponseStatus.new(*self.determine_code_and_message)
|
23
|
-
Sanford::Protocol::Response.new(status)
|
24
|
-
end
|
25
|
-
|
26
|
-
protected
|
27
|
-
|
28
|
-
def determine_code_and_message
|
29
|
-
case(self.exception)
|
30
|
-
when Sanford::Protocol::BadMessageError, Sanford::Protocol::BadRequestError
|
31
|
-
[ :bad_request, self.exception.message ]
|
32
|
-
when Sanford::NotFoundError
|
33
|
-
[ :not_found ]
|
34
|
-
when Sanford::Protocol::TimeoutError
|
35
|
-
[ :timeout ]
|
36
|
-
when Exception
|
37
|
-
[ :error, "An unexpected error occurred." ]
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
require 'assert'
|
2
|
-
|
3
|
-
require 'sanford-protocol/test/helpers'
|
4
|
-
|
5
|
-
class Sanford::Connection
|
6
|
-
|
7
|
-
class BaseTest < Assert::Context
|
8
|
-
include Sanford::Protocol::Test::Helpers
|
9
|
-
|
10
|
-
desc "Sanford::Connection"
|
11
|
-
setup do
|
12
|
-
@fake_socket = self.fake_socket_with_request('v1', 'echo', { :message => 'test' })
|
13
|
-
@connection = Sanford::Connection.new(DummyHost.new, @fake_socket)
|
14
|
-
end
|
15
|
-
subject{ @connection }
|
16
|
-
|
17
|
-
should have_instance_methods :service_host, :exception_handler, :logger, :process, :timeout
|
18
|
-
end
|
19
|
-
|
20
|
-
# The system test `test/system/request_handling_test.rb`, covers all the
|
21
|
-
# special requests that can occur when given all sorts of invalid requests.
|
22
|
-
|
23
|
-
end
|
@@ -1,69 +0,0 @@
|
|
1
|
-
require 'assert'
|
2
|
-
|
3
|
-
class Sanford::ExceptionHandler
|
4
|
-
|
5
|
-
class BaseTest < Assert::Context
|
6
|
-
desc "Sanford::Server::ExceptionHandler"
|
7
|
-
setup do
|
8
|
-
@exception = nil
|
9
|
-
begin
|
10
|
-
raise "test"
|
11
|
-
rescue Exception => @exception
|
12
|
-
end
|
13
|
-
@logger = Sanford::NullLogger.new
|
14
|
-
@exception_handler = Sanford::ExceptionHandler.new(@exception, @logger)
|
15
|
-
end
|
16
|
-
subject{ @exception_handler }
|
17
|
-
|
18
|
-
should have_instance_methods :exception
|
19
|
-
|
20
|
-
should "have built a 500 Sanford::Response" do
|
21
|
-
response = subject.response
|
22
|
-
|
23
|
-
assert_instance_of Sanford::Protocol::Response, response
|
24
|
-
assert_equal 500, response.status.code
|
25
|
-
assert_equal "An unexpected error occurred.", response.status.message
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
class BadRequestTest < BaseTest
|
30
|
-
desc "with a Sanford::BadRequest exception"
|
31
|
-
setup do
|
32
|
-
@exception = nil
|
33
|
-
begin
|
34
|
-
raise Sanford::Protocol::BadMessageError, "test"
|
35
|
-
rescue Exception => @exception
|
36
|
-
end
|
37
|
-
@exception_handler = Sanford::ExceptionHandler.new(@exception, @logger)
|
38
|
-
end
|
39
|
-
|
40
|
-
should "have built a 400 Sanford::Response" do
|
41
|
-
response = subject.response
|
42
|
-
|
43
|
-
assert_instance_of Sanford::Protocol::Response, response
|
44
|
-
assert_equal 400, response.status.code
|
45
|
-
assert_equal "test", response.status.message
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
class NotFoundTest < BaseTest
|
50
|
-
desc "with a Sanford::NotFound exception"
|
51
|
-
setup do
|
52
|
-
@exception = nil
|
53
|
-
begin
|
54
|
-
raise Sanford::NotFoundError, "test"
|
55
|
-
rescue Exception => @exception
|
56
|
-
end
|
57
|
-
@exception_handler = Sanford::ExceptionHandler.new(@exception, @logger)
|
58
|
-
end
|
59
|
-
|
60
|
-
should "have built a 404 Sanford::Response" do
|
61
|
-
response = subject.response
|
62
|
-
|
63
|
-
assert_instance_of Sanford::Protocol::Response, response
|
64
|
-
assert_equal 404, response.status.code
|
65
|
-
assert_equal nil, response.status.message
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
end
|