sanford 0.8.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +1 -0
- data/lib/sanford/cli.rb +1 -270
- data/lib/sanford/host.rb +12 -14
- data/lib/sanford/host_data.rb +7 -7
- data/lib/sanford/manager.rb +275 -0
- data/lib/sanford/runner.rb +39 -32
- data/lib/sanford/server.rb +1 -0
- data/lib/sanford/service_handler.rb +54 -41
- data/lib/sanford/test_helpers.rb +19 -0
- data/lib/sanford/test_runner.rb +17 -12
- data/lib/sanford/version.rb +1 -1
- data/lib/sanford.rb +19 -17
- data/sanford.gemspec +5 -5
- data/test/support/service_handlers.rb +69 -43
- data/test/support/services.rb +2 -2
- data/test/support/simple_client.rb +2 -2
- data/test/system/request_handling_tests.rb +2 -2
- data/test/unit/error_handler_tests.rb +13 -12
- data/test/unit/host_data_tests.rb +26 -14
- data/test/unit/host_tests.rb +105 -16
- data/test/unit/manager_tests.rb +103 -50
- data/test/unit/runner_tests.rb +6 -3
- data/test/unit/sanford_tests.rb +70 -0
- data/test/unit/server_tests.rb +8 -6
- data/test/unit/service_handler_tests.rb +179 -68
- data/test/unit/worker_tests.rb +6 -5
- metadata +32 -74
- data/test/unit/config_tests.rb +0 -12
- data/test/unit/host_configuration_tests.rb +0 -37
- data/test/unit/hosts_tests.rb +0 -56
- data/test/unit/manager_pid_file_tests.rb +0 -60
@@ -1,8 +1,9 @@
|
|
1
1
|
require 'assert'
|
2
|
+
require 'sanford/error_handler'
|
2
3
|
|
3
4
|
class Sanford::ErrorHandler
|
4
5
|
|
5
|
-
class
|
6
|
+
class UnitTests < Assert::Context
|
6
7
|
desc "Sanford::ErrorHandler"
|
7
8
|
setup do
|
8
9
|
@exception = RuntimeError.new('test')
|
@@ -11,7 +12,7 @@ class Sanford::ErrorHandler
|
|
11
12
|
end
|
12
13
|
subject{ @error_handler }
|
13
14
|
|
14
|
-
should
|
15
|
+
should have_imeths :exception, :host_data, :request, :run
|
15
16
|
|
16
17
|
should "return a Sanford::Protocol::Response with `run`" do
|
17
18
|
assert_instance_of Sanford::Protocol::Response, subject.run
|
@@ -28,7 +29,7 @@ class Sanford::ErrorHandler
|
|
28
29
|
|
29
30
|
end
|
30
31
|
|
31
|
-
class ResponseFromProcTests <
|
32
|
+
class ResponseFromProcTests < UnitTests
|
32
33
|
desc "generating a respone from an error proc"
|
33
34
|
setup do
|
34
35
|
@host_defaults = { :ip => "localhost", :port => 8000 }
|
@@ -43,9 +44,9 @@ class Sanford::ErrorHandler
|
|
43
44
|
}))
|
44
45
|
response = Sanford::ErrorHandler.new(@exception, host_data).run
|
45
46
|
|
46
|
-
assert_equal 567,
|
47
|
-
assert_equal 'custom message',
|
48
|
-
assert_equal 'custom data',
|
47
|
+
assert_equal 567, response.code
|
48
|
+
assert_equal 'custom message', response.status.message
|
49
|
+
assert_equal 'custom data', response.data
|
49
50
|
end
|
50
51
|
|
51
52
|
should "use an integer returned by the error proc to generate a protocol response" do
|
@@ -76,12 +77,12 @@ class Sanford::ErrorHandler
|
|
76
77
|
}))
|
77
78
|
response = Sanford::ErrorHandler.new(@exception, host_data).run
|
78
79
|
|
79
|
-
assert_equal 500,
|
80
|
+
assert_equal 500, response.code
|
80
81
|
assert_equal 'An unexpected error occurred.', response.status.message
|
81
82
|
end
|
82
83
|
|
83
84
|
should "use the default behavior for an exception raised by the error proc " \
|
84
|
-
|
85
|
+
"and ignore the original exception" do
|
85
86
|
host_data = Sanford::HostData.new(EmptyHost, @host_defaults.merge({
|
86
87
|
:error_procs => [ proc{ raise Sanford::NotFoundError } ]
|
87
88
|
}))
|
@@ -94,14 +95,14 @@ class Sanford::ErrorHandler
|
|
94
95
|
|
95
96
|
end
|
96
97
|
|
97
|
-
class ResponseFromExceptionTests <
|
98
|
+
class ResponseFromExceptionTests < UnitTests
|
98
99
|
desc "generating a respone from an exception"
|
99
100
|
|
100
101
|
should "build a 400 response with a protocol BadMessageError" do
|
101
102
|
exception = generate_exception(Sanford::Protocol::BadMessageError, 'bad message')
|
102
103
|
response = Sanford::ErrorHandler.new(exception, @host_data).run
|
103
104
|
|
104
|
-
assert_equal 400,
|
105
|
+
assert_equal 400, response.code
|
105
106
|
assert_equal 'bad message', response.status.message
|
106
107
|
end
|
107
108
|
|
@@ -109,7 +110,7 @@ class Sanford::ErrorHandler
|
|
109
110
|
exception = generate_exception(Sanford::Protocol::BadRequestError, 'bad request')
|
110
111
|
response = Sanford::ErrorHandler.new(exception, @host_data).run
|
111
112
|
|
112
|
-
assert_equal 400,
|
113
|
+
assert_equal 400, response.code
|
113
114
|
assert_equal 'bad request', response.status.message
|
114
115
|
end
|
115
116
|
|
@@ -124,7 +125,7 @@ class Sanford::ErrorHandler
|
|
124
125
|
should "build a 500 response with all other exceptions" do
|
125
126
|
response = Sanford::ErrorHandler.new(RuntimeError.new('test'), @host_data).run
|
126
127
|
|
127
|
-
assert_equal 500,
|
128
|
+
assert_equal 500, response.code
|
128
129
|
assert_equal 'An unexpected error occurred.', response.status.message
|
129
130
|
end
|
130
131
|
|
@@ -1,8 +1,11 @@
|
|
1
1
|
require 'assert'
|
2
|
+
require 'sanford/host_data'
|
3
|
+
|
4
|
+
require 'test/support/services'
|
2
5
|
|
3
6
|
class Sanford::HostData
|
4
7
|
|
5
|
-
class
|
8
|
+
class UnitTests < Assert::Context
|
6
9
|
desc "Sanford::HostData"
|
7
10
|
setup do
|
8
11
|
TestHost.init_has_been_called = false
|
@@ -13,14 +16,27 @@ class Sanford::HostData
|
|
13
16
|
end
|
14
17
|
subject{ @host_data }
|
15
18
|
|
16
|
-
should
|
17
|
-
|
19
|
+
should have_readers :name, :logger, :verbose, :keep_alive, :runner, :error_procs
|
20
|
+
should have_imeths :handler_class_for, :run
|
18
21
|
|
19
|
-
should "
|
22
|
+
should "call the init procs" do
|
23
|
+
assert_equal true, TestHost.init_has_been_called
|
24
|
+
end
|
25
|
+
|
26
|
+
should "default its attrs from the host configuration" do
|
27
|
+
assert_equal TestHost.configuration.name, subject.name
|
28
|
+
assert_equal TestHost.configuration.logger.class, subject.logger.class
|
29
|
+
assert_equal TestHost.configuration.verbose_logging, subject.verbose
|
30
|
+
assert_equal TestHost.configuration.receives_keep_alive, subject.keep_alive
|
31
|
+
assert_equal TestHost.configuration.runner.class, subject.runner.class
|
32
|
+
assert_equal TestHost.configuration.error_procs, subject.error_procs
|
33
|
+
end
|
34
|
+
|
35
|
+
should "allow overriding host configuration attrs" do
|
20
36
|
host_data = Sanford::HostData.new(TestHost, :verbose_logging => false)
|
21
37
|
|
38
|
+
assert_false host_data.verbose
|
22
39
|
assert_equal TestHost.receives_keep_alive, host_data.keep_alive
|
23
|
-
assert_equal false, host_data.verbose
|
24
40
|
end
|
25
41
|
|
26
42
|
should "ignore nil values passed as overrides" do
|
@@ -28,17 +44,13 @@ class Sanford::HostData
|
|
28
44
|
assert_not_nil host_data.verbose
|
29
45
|
end
|
30
46
|
|
31
|
-
should "have called the setup proc" do
|
32
|
-
assert_equal true, TestHost.init_has_been_called
|
33
|
-
end
|
34
|
-
|
35
47
|
should "constantize a host's handlers" do
|
36
48
|
handlers = subject.instance_variable_get("@handlers")
|
37
|
-
assert_equal TestHost::Authorized,
|
38
|
-
assert_equal TestHost::Bad,
|
39
|
-
assert_equal TestHost::Echo,
|
40
|
-
assert_equal TestHost::HaltIt,
|
41
|
-
assert_equal TestHost::Multiply,
|
49
|
+
assert_equal TestHost::Authorized, handlers['authorized']
|
50
|
+
assert_equal TestHost::Bad, handlers['bad']
|
51
|
+
assert_equal TestHost::Echo, handlers['echo']
|
52
|
+
assert_equal TestHost::HaltIt, handlers['halt_it']
|
53
|
+
assert_equal TestHost::Multiply, handlers['multiply']
|
42
54
|
end
|
43
55
|
|
44
56
|
should "look up handler classes with #handler_class_for" do
|
data/test/unit/host_tests.rb
CHANGED
@@ -1,35 +1,96 @@
|
|
1
1
|
require 'assert'
|
2
|
+
require 'sanford/host'
|
3
|
+
|
4
|
+
require 'sanford/logger'
|
5
|
+
require 'sanford/runner'
|
2
6
|
|
3
7
|
module Sanford::Host
|
4
8
|
|
5
|
-
class
|
9
|
+
class UnitTests < Assert::Context
|
6
10
|
desc "Sanford::Host"
|
7
11
|
setup do
|
8
|
-
@
|
12
|
+
@host_class = Class.new{ include Sanford::Host }
|
9
13
|
end
|
10
|
-
|
11
|
-
|
14
|
+
subject{ @host_class.instance }
|
15
|
+
|
16
|
+
should have_readers :configuration, :services
|
17
|
+
should have_imeths :name, :ip, :port, :pid_file, :logger, :verbose_logging
|
18
|
+
should have_imeths :runner, :error, :init, :service_handler_ns, :service
|
19
|
+
|
20
|
+
should "know its configuration" do
|
21
|
+
assert_kind_of Configuration, subject.configuration
|
12
22
|
end
|
13
|
-
subject{ MyHost.instance }
|
14
23
|
|
15
|
-
should
|
16
|
-
|
17
|
-
|
24
|
+
should "have no services by default" do
|
25
|
+
assert_empty subject.services
|
26
|
+
end
|
18
27
|
|
19
|
-
should "get
|
28
|
+
should "get/set its configuration options" do
|
20
29
|
subject.name 'my_awesome_host'
|
30
|
+
assert_equal 'my_awesome_host', subject.name
|
31
|
+
assert_equal subject.name, subject.configuration.name
|
32
|
+
|
33
|
+
subject.ip '127.0.0.1'
|
34
|
+
assert_equal '127.0.0.1', subject.ip
|
35
|
+
assert_equal subject.ip, subject.configuration.ip
|
36
|
+
|
37
|
+
subject.port '10100'
|
38
|
+
assert_equal 10100, subject.port
|
39
|
+
assert_equal subject.port, subject.configuration.port
|
40
|
+
|
41
|
+
subject.pid_file '/path/to/file'
|
42
|
+
assert_equal Pathname.new('/path/to/file'), subject.pid_file
|
43
|
+
assert_equal subject.pid_file, subject.configuration.pid_file
|
44
|
+
|
45
|
+
logger = Sanford::NullLogger.new
|
46
|
+
subject.logger logger
|
47
|
+
assert_equal logger, subject.logger
|
48
|
+
assert_equal subject.logger, subject.configuration.logger
|
49
|
+
|
50
|
+
subject.verbose_logging false
|
51
|
+
assert_equal false, subject.verbose_logging
|
52
|
+
assert_equal subject.verbose_logging, subject.configuration.verbose_logging
|
53
|
+
|
54
|
+
subject.receives_keep_alive true
|
55
|
+
assert_equal true, subject.receives_keep_alive
|
56
|
+
assert_equal subject.receives_keep_alive, subject.configuration.receives_keep_alive
|
57
|
+
|
58
|
+
subject.runner Sanford::DefaultRunner
|
59
|
+
assert_equal Sanford::DefaultRunner, subject.runner
|
60
|
+
assert_equal subject.runner, subject.configuration.runner
|
61
|
+
end
|
62
|
+
|
63
|
+
should "add error procs to the configuration" do
|
64
|
+
assert_empty subject.configuration.error_procs
|
65
|
+
subject.error &proc{}
|
66
|
+
assert_not_empty subject.configuration.error_procs
|
67
|
+
end
|
21
68
|
|
22
|
-
|
23
|
-
|
69
|
+
should "add init procs to the configuration" do
|
70
|
+
assert_empty subject.configuration.init_procs
|
71
|
+
subject.init &proc{}
|
72
|
+
assert_not_empty subject.configuration.init_procs
|
24
73
|
end
|
25
74
|
|
26
|
-
should "
|
75
|
+
should "get/set its service_handler_ns" do
|
76
|
+
assert_nil subject.service_handler_ns
|
77
|
+
subject.service_handler_ns 'a-ns'
|
78
|
+
assert_equal 'a-ns', subject.service_handler_ns
|
79
|
+
end
|
80
|
+
|
81
|
+
should "add services" do
|
27
82
|
subject.service_handler_ns 'MyNamespace'
|
28
83
|
subject.service('test', 'MyServiceHandler')
|
29
84
|
|
30
85
|
assert_equal 'MyNamespace::MyServiceHandler', subject.services['test']
|
31
86
|
end
|
32
87
|
|
88
|
+
should "force string names when adding services" do
|
89
|
+
subject.service(:another_service, 'MyServiceHandler')
|
90
|
+
assert_nil subject.services[:another_service]
|
91
|
+
assert_equal 'MyServiceHandler', subject.services['another_service']
|
92
|
+
end
|
93
|
+
|
33
94
|
should "ignore a namespace when a service class has leading colons" do
|
34
95
|
subject.service_handler_ns 'MyNamespace'
|
35
96
|
subject.service('test', '::MyServiceHandler')
|
@@ -39,11 +100,11 @@ module Sanford::Host
|
|
39
100
|
|
40
101
|
end
|
41
102
|
|
42
|
-
class ClassMethodsTests <
|
43
|
-
desc "
|
44
|
-
subject{
|
103
|
+
class ClassMethodsTests < UnitTests
|
104
|
+
desc "class"
|
105
|
+
subject{ @host_class }
|
45
106
|
|
46
|
-
should "proxy
|
107
|
+
should "proxy its method to its instance" do
|
47
108
|
assert_equal subject.instance.name, subject.name
|
48
109
|
assert subject.respond_to?(:pid_file)
|
49
110
|
end
|
@@ -54,4 +115,32 @@ module Sanford::Host
|
|
54
115
|
|
55
116
|
end
|
56
117
|
|
118
|
+
class ConfigurationTests < UnitTests
|
119
|
+
desc "Configuration"
|
120
|
+
setup do
|
121
|
+
@configuration = Configuration.new(@host_class.instance)
|
122
|
+
end
|
123
|
+
subject{ @configuration }
|
124
|
+
|
125
|
+
should have_imeths :name, :ip, :port, :pid_file, :logger, :verbose_logging
|
126
|
+
should have_imeths :receives_keep_alive, :runner, :error_procs, :init_procs
|
127
|
+
|
128
|
+
should "default name to the class name of the host" do
|
129
|
+
assert_equal @host_class.name, subject.name
|
130
|
+
end
|
131
|
+
|
132
|
+
should "default its other attrs" do
|
133
|
+
assert_equal '0.0.0.0', subject.ip
|
134
|
+
assert_nil subject.port
|
135
|
+
assert_nil subject.pid_file
|
136
|
+
assert_equal Sanford.config.logger, subject.logger
|
137
|
+
assert_true subject.verbose_logging
|
138
|
+
assert_false subject.receives_keep_alive
|
139
|
+
assert_equal Sanford.config.runner, subject.runner
|
140
|
+
assert_empty subject.error_procs
|
141
|
+
assert_empty subject.init_procs
|
142
|
+
end
|
143
|
+
|
144
|
+
end
|
145
|
+
|
57
146
|
end
|
data/test/unit/manager_tests.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
require 'assert'
|
2
|
-
require 'sanford/
|
2
|
+
require 'sanford/manager'
|
3
3
|
|
4
4
|
module Sanford::Manager
|
5
5
|
|
6
|
-
class
|
6
|
+
class UnitTests < Assert::Context
|
7
7
|
desc "Sanford::Manager"
|
8
8
|
subject{ Sanford::Manager }
|
9
9
|
|
10
|
-
should
|
10
|
+
should have_imeths :call, :get_handler_class
|
11
11
|
|
12
12
|
should "return ServerHandler or SignalHandler with get_handler_class" do
|
13
13
|
assert_equal Sanford::Manager::ServerHandler, subject.get_handler_class('run')
|
@@ -18,56 +18,16 @@ module Sanford::Manager
|
|
18
18
|
|
19
19
|
end
|
20
20
|
|
21
|
-
class
|
22
|
-
desc "ServerHandler"
|
23
|
-
setup do
|
24
|
-
@handler = Sanford::Manager::ServerHandler.new({ :host => 'TestHost' })
|
25
|
-
end
|
26
|
-
subject{ @handler }
|
27
|
-
|
28
|
-
should have_instance_methods :run, :start
|
29
|
-
|
30
|
-
should "raise an error when a host can't be found matching the `host` option" do
|
31
|
-
assert_raises(Sanford::NoHostError) do
|
32
|
-
Sanford::Manager::ServerHandler.new({ :host => 'poop' })
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
should "raise an error when a host is invalid for running a server" do
|
37
|
-
assert_raises(Sanford::InvalidHostError) do
|
38
|
-
Sanford::Manager::ServerHandler.new({ :host => 'InvalidHost' })
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
end
|
43
|
-
|
44
|
-
class SignalHandlertests < BaseTests
|
45
|
-
desc "SignalHandler"
|
46
|
-
setup do
|
47
|
-
@handler = Sanford::Manager::SignalHandler.new({ :pid => -1 })
|
48
|
-
end
|
49
|
-
subject{ @handler }
|
50
|
-
|
51
|
-
should have_instance_methods :stop, :restart
|
52
|
-
|
53
|
-
should "raise an error when a pid can't be found" do
|
54
|
-
assert_raises(Sanford::NoPIDError) do
|
55
|
-
Sanford::Manager::SignalHandler.new
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
class ConfigTests < BaseTests
|
21
|
+
class ConfigTests < UnitTests
|
61
22
|
desc "Config"
|
62
23
|
setup do
|
63
24
|
@config = Sanford::Manager::Config.new({ :host => 'TestHost' })
|
64
25
|
end
|
65
26
|
subject{ @config }
|
66
27
|
|
67
|
-
should
|
68
|
-
should
|
69
|
-
should
|
70
|
-
should have_instance_methods :listen_args, :has_listen_args?, :found_host?
|
28
|
+
should have_readers :host_name, :host, :ip, :port, :pid, :pid_file, :restart_dir
|
29
|
+
should have_readers :file_descriptor, :client_file_descriptors
|
30
|
+
should have_imeths :listen_args, :has_listen_args?, :found_host?
|
71
31
|
|
72
32
|
should "find a host based on the `host` option" do
|
73
33
|
assert_equal TestHost, subject.host
|
@@ -81,8 +41,8 @@ module Sanford::Manager
|
|
81
41
|
end
|
82
42
|
|
83
43
|
should "use the first host if no host option is provided" do
|
84
|
-
|
85
|
-
assert_equal Sanford.hosts.first,
|
44
|
+
config = Sanford::Manager::Config.new({ :port => 1 })
|
45
|
+
assert_equal Sanford.hosts.first, config.host
|
86
46
|
end
|
87
47
|
|
88
48
|
should "return the file descriptor or ip and port with listen_args" do
|
@@ -103,7 +63,7 @@ module Sanford::Manager
|
|
103
63
|
|
104
64
|
should "build a NullHost when a host can't be found" do
|
105
65
|
config = Sanford::Manager::Config.new({ :host => 'poop' })
|
106
|
-
assert_instance_of Sanford::Manager::NullHost, config.host
|
66
|
+
assert_instance_of Sanford::Manager::Config::NullHost, config.host
|
107
67
|
assert_equal false, config.found_host?
|
108
68
|
end
|
109
69
|
|
@@ -139,4 +99,97 @@ module Sanford::Manager
|
|
139
99
|
|
140
100
|
end
|
141
101
|
|
102
|
+
class PIDFileTests < ConfigTests
|
103
|
+
desc "PIDFile"
|
104
|
+
setup do
|
105
|
+
@pid_file_path = File.join(ROOT, "tmp/my.pid")
|
106
|
+
@pid_file = Sanford::Manager::Config::PIDFile.new(@pid_file_path)
|
107
|
+
end
|
108
|
+
teardown do
|
109
|
+
FileUtils.rm_rf(@pid_file_path)
|
110
|
+
end
|
111
|
+
subject{ @pid_file }
|
112
|
+
|
113
|
+
should have_imeths :pid, :to_s, :write, :remove
|
114
|
+
|
115
|
+
should "return it's path with #to_s" do
|
116
|
+
assert_equal @pid_file_path, subject.to_s
|
117
|
+
end
|
118
|
+
|
119
|
+
should "write the pid file with #write" do
|
120
|
+
subject.write
|
121
|
+
|
122
|
+
assert_file_exists @pid_file_path
|
123
|
+
assert_equal "#{Process.pid}\n", File.read(@pid_file_path)
|
124
|
+
end
|
125
|
+
|
126
|
+
should "return the value stored in the pid value with #pid" do
|
127
|
+
subject.write
|
128
|
+
|
129
|
+
assert_equal Process.pid, subject.pid
|
130
|
+
end
|
131
|
+
|
132
|
+
should "remove the file with #remove" do
|
133
|
+
subject.write
|
134
|
+
subject.remove
|
135
|
+
|
136
|
+
assert_not File.exists?(@pid_file_path)
|
137
|
+
end
|
138
|
+
|
139
|
+
should "complain nicely if it can't write the pid file" do
|
140
|
+
pid_file_path = 'does/not/exist.pid'
|
141
|
+
pid_file = Sanford::Manager::Config::PIDFile.new(pid_file_path)
|
142
|
+
|
143
|
+
err = nil
|
144
|
+
begin
|
145
|
+
pid_file.write
|
146
|
+
rescue Exception => err
|
147
|
+
end
|
148
|
+
|
149
|
+
assert err
|
150
|
+
assert_kind_of RuntimeError, err
|
151
|
+
assert_includes File.dirname(pid_file_path), err.message
|
152
|
+
end
|
153
|
+
|
154
|
+
end
|
155
|
+
|
156
|
+
class ServerHandlerTests < UnitTests
|
157
|
+
desc "ServerHandler"
|
158
|
+
setup do
|
159
|
+
@handler = Sanford::Manager::ServerHandler.new({ :host => 'TestHost' })
|
160
|
+
end
|
161
|
+
subject{ @handler }
|
162
|
+
|
163
|
+
should have_imeths :run, :start
|
164
|
+
|
165
|
+
should "raise an error when a host can't be found" do
|
166
|
+
assert_raises(Sanford::NoHostError) do
|
167
|
+
Sanford::Manager::ServerHandler.new({ :host => 'not_found' })
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
should "raise an error when a host is invalid for running a server" do
|
172
|
+
assert_raises(Sanford::InvalidHostError) do
|
173
|
+
Sanford::Manager::ServerHandler.new({ :host => 'InvalidHost' })
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
class SignalHandlertests < UnitTests
|
180
|
+
desc "SignalHandler"
|
181
|
+
setup do
|
182
|
+
@handler = Sanford::Manager::SignalHandler.new({ :pid => -1 })
|
183
|
+
end
|
184
|
+
subject{ @handler }
|
185
|
+
|
186
|
+
should have_imeths :stop, :restart
|
187
|
+
|
188
|
+
should "raise an error when a pid can't be found" do
|
189
|
+
assert_raises(Sanford::NoPIDError) do
|
190
|
+
Sanford::Manager::SignalHandler.new
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
142
195
|
end
|
data/test/unit/runner_tests.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
require 'assert'
|
2
|
+
require 'sanford/runner'
|
3
|
+
|
4
|
+
require 'test/support/services'
|
2
5
|
|
3
6
|
module Sanford::Runner
|
4
7
|
|
5
|
-
class
|
8
|
+
class UnitTests < Assert::Context
|
6
9
|
desc "Sanford::Runner"
|
7
10
|
setup do
|
8
11
|
request = Sanford::Protocol::Request.new('test', {})
|
@@ -10,8 +13,8 @@ module Sanford::Runner
|
|
10
13
|
end
|
11
14
|
subject{ @runner }
|
12
15
|
|
13
|
-
should
|
14
|
-
should
|
16
|
+
should have_cmeths :run
|
17
|
+
should have_readers :handler_class, :request, :logger, :run
|
15
18
|
|
16
19
|
should "run the handler and return the response it generates when `run` is called" do
|
17
20
|
response = subject.run
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'assert'
|
2
|
+
require 'sanford'
|
3
|
+
|
4
|
+
require 'ns-options/proxy'
|
5
|
+
|
6
|
+
module Sanford
|
7
|
+
|
8
|
+
class UnitTests < Assert::Context
|
9
|
+
desc "Sanford"
|
10
|
+
subject{ Sanford }
|
11
|
+
|
12
|
+
should have_imeths :config, :configure, :init, :register, :hosts
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
class ConfigTests < UnitTests
|
17
|
+
desc "Config"
|
18
|
+
subject{ Sanford::Config }
|
19
|
+
|
20
|
+
should have_imeths :services_file, :logger, :runner
|
21
|
+
|
22
|
+
should "be an NsOptions::Proxy" do
|
23
|
+
assert_includes NsOptions::Proxy, subject
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
class HostsTests < UnitTests
|
29
|
+
desc "Hosts"
|
30
|
+
setup do
|
31
|
+
@hosts = Sanford::Hosts.new
|
32
|
+
end
|
33
|
+
subject{ @hosts }
|
34
|
+
|
35
|
+
should have_instance_methods :add, :first, :find
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
class FindTests < HostsTests
|
40
|
+
desc "find method"
|
41
|
+
setup do
|
42
|
+
@hosts.add ::NotNamedHost
|
43
|
+
@hosts.add ::NamedHost
|
44
|
+
@hosts.add ::BadlyNamedHost
|
45
|
+
end
|
46
|
+
|
47
|
+
should "allow finding hosts by their class name or configured name" do
|
48
|
+
assert_includes ::NotNamedHost, subject
|
49
|
+
assert_includes ::NamedHost, subject
|
50
|
+
|
51
|
+
assert_equal ::NotNamedHost, subject.find('NotNamedHost')
|
52
|
+
assert_equal ::NamedHost, subject.find('NamedHost')
|
53
|
+
assert_equal ::NamedHost, subject.find('named_host')
|
54
|
+
end
|
55
|
+
|
56
|
+
should "prefer hosts with a matching class name over configured name" do
|
57
|
+
assert_includes ::BadlyNamedHost, subject
|
58
|
+
assert_equal NotNamedHost, subject.find('NotNamedHost')
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
# Using this syntax because these classes need to be defined as top-level
|
64
|
+
# constants for ease in using their class names in the tests
|
65
|
+
|
66
|
+
::NotNamedHost = Class.new{ include Sanford::Host }
|
67
|
+
::NamedHost = Class.new{ include Sanford::Host; name 'named_host' }
|
68
|
+
::BadlyNamedHost = Class.new{ include Sanford::Host; name 'NotNamedHost' }
|
69
|
+
|
70
|
+
end
|
data/test/unit/server_tests.rb
CHANGED
@@ -1,20 +1,22 @@
|
|
1
1
|
require 'assert'
|
2
2
|
require 'sanford/server'
|
3
3
|
|
4
|
+
require 'test/support/services'
|
5
|
+
|
4
6
|
class Sanford::Server
|
5
7
|
|
6
|
-
class
|
8
|
+
class UnitTests < Assert::Context
|
7
9
|
desc "Sanford::Server"
|
8
10
|
setup do
|
9
|
-
@server = Sanford::Server.new(TestHost,
|
11
|
+
@server = Sanford::Server.new(TestHost, :keep_alive => true)
|
10
12
|
end
|
11
13
|
subject{ @server }
|
12
14
|
|
13
|
-
should
|
14
|
-
should
|
15
|
+
should have_readers :sanford_host, :sanford_host_data, :sanford_host_options
|
16
|
+
should have_imeths :on_run
|
15
17
|
|
16
18
|
should "include DatTCP::Server" do
|
17
|
-
assert_includes DatTCP::Server, subject.class
|
19
|
+
assert_includes DatTCP::Server, subject.class
|
18
20
|
end
|
19
21
|
|
20
22
|
should "save it's host and host options but not initialize a host data yet" do
|
@@ -25,7 +27,7 @@ class Sanford::Server
|
|
25
27
|
|
26
28
|
end
|
27
29
|
|
28
|
-
class RunTests <
|
30
|
+
class RunTests < UnitTests
|
29
31
|
desc "run"
|
30
32
|
setup do
|
31
33
|
@server.listen(TestHost.ip, TestHost.port)
|