sanford 0.10.1 → 0.11.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/Gemfile +1 -1
- data/README.md +41 -56
- data/Rakefile +0 -1
- data/bench/client.rb +8 -3
- data/bench/{services.rb → config.sanford} +11 -6
- data/bench/{runner.rb → report.rb} +2 -2
- data/bench/report.txt +32 -32
- data/lib/sanford/cli.rb +42 -28
- data/lib/sanford/config_file.rb +79 -0
- data/lib/sanford/{worker.rb → connection_handler.rb} +28 -20
- data/lib/sanford/error_handler.rb +7 -7
- data/lib/sanford/pid_file.rb +42 -0
- data/lib/sanford/process.rb +136 -0
- data/lib/sanford/process_signal.rb +20 -0
- data/lib/sanford/route.rb +48 -0
- data/lib/sanford/router.rb +36 -0
- data/lib/sanford/runner.rb +30 -58
- data/lib/sanford/sanford_runner.rb +19 -9
- data/lib/sanford/server.rb +211 -42
- data/lib/sanford/server_data.rb +47 -0
- data/lib/sanford/service_handler.rb +8 -46
- data/lib/sanford/template_source.rb +19 -2
- data/lib/sanford/test_runner.rb +27 -28
- data/lib/sanford/version.rb +1 -1
- data/lib/sanford.rb +1 -23
- data/sanford.gemspec +4 -5
- data/test/helper.rb +3 -20
- data/test/support/app_server.rb +142 -0
- data/test/support/config.sanford +7 -0
- data/test/support/config_invalid_run.sanford +3 -0
- data/test/support/config_no_run.sanford +0 -0
- data/test/support/fake_server_connection.rb +58 -0
- data/test/support/pid_file_spy.rb +19 -0
- data/test/support/template.erb +1 -0
- data/test/system/server_tests.rb +378 -0
- data/test/system/service_handler_tests.rb +224 -0
- data/test/unit/cli_tests.rb +187 -0
- data/test/unit/config_file_tests.rb +59 -0
- data/test/unit/connection_handler_tests.rb +254 -0
- data/test/unit/error_handler_tests.rb +30 -35
- data/test/unit/pid_file_tests.rb +70 -0
- data/test/unit/process_signal_tests.rb +61 -0
- data/test/unit/process_tests.rb +428 -0
- data/test/unit/route_tests.rb +92 -0
- data/test/unit/router_tests.rb +65 -0
- data/test/unit/runner_tests.rb +61 -15
- data/test/unit/sanford_runner_tests.rb +162 -28
- data/test/unit/sanford_tests.rb +0 -8
- data/test/unit/server_data_tests.rb +87 -0
- data/test/unit/server_tests.rb +502 -21
- data/test/unit/service_handler_tests.rb +114 -219
- data/test/unit/template_engine_tests.rb +1 -1
- data/test/unit/template_source_tests.rb +56 -16
- data/test/unit/test_runner_tests.rb +206 -0
- metadata +67 -67
- data/bench/tasks.rb +0 -41
- data/lib/sanford/config.rb +0 -28
- data/lib/sanford/host.rb +0 -129
- data/lib/sanford/host_data.rb +0 -65
- data/lib/sanford/hosts.rb +0 -38
- data/lib/sanford/manager.rb +0 -275
- data/test/support/fake_connection.rb +0 -36
- data/test/support/helpers.rb +0 -17
- data/test/support/service_handlers.rb +0 -154
- data/test/support/services.rb +0 -123
- data/test/support/simple_client.rb +0 -62
- data/test/system/request_handling_tests.rb +0 -306
- data/test/unit/config_tests.rb +0 -56
- data/test/unit/host_data_tests.rb +0 -71
- data/test/unit/host_tests.rb +0 -141
- data/test/unit/hosts_tests.rb +0 -50
- data/test/unit/manager_tests.rb +0 -195
- data/test/unit/worker_tests.rb +0 -24
@@ -1,62 +0,0 @@
|
|
1
|
-
require 'sanford-protocol/fake_socket'
|
2
|
-
|
3
|
-
class SimpleClient
|
4
|
-
|
5
|
-
def self.call_with_request(service_host, name, params)
|
6
|
-
self.new(service_host).call_with_request(name, params)
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.call_with_msg_body(service_host, *args)
|
10
|
-
self.new(service_host).call_with_msg_body(*args)
|
11
|
-
end
|
12
|
-
|
13
|
-
def self.call_with_encoded_msg_body(service_host, *args)
|
14
|
-
self.new(service_host).call_with_encoded_msg_body(*args)
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.call_with(service_host, bytes)
|
18
|
-
self.new(service_host).call(bytes)
|
19
|
-
end
|
20
|
-
|
21
|
-
def initialize(service_host, options = {})
|
22
|
-
@host, @port = service_host.ip, service_host.port
|
23
|
-
@delay = options[:with_delay]
|
24
|
-
end
|
25
|
-
|
26
|
-
def call_with_request(*args)
|
27
|
-
self.call_using_fake_socket(:with_request, *args)
|
28
|
-
end
|
29
|
-
|
30
|
-
def call_with_msg_body(*args)
|
31
|
-
self.call_using_fake_socket(:with_msg_body, *args)
|
32
|
-
end
|
33
|
-
|
34
|
-
def call_with_encoded_msg_body(*args)
|
35
|
-
self.call_using_fake_socket(:with_encoded_msg_body, *args)
|
36
|
-
end
|
37
|
-
|
38
|
-
def call_with_keep_alive
|
39
|
-
socket = TCPSocket.new(@host, @port)
|
40
|
-
ensure
|
41
|
-
socket.close rescue false
|
42
|
-
end
|
43
|
-
|
44
|
-
def call(bytes)
|
45
|
-
socket = TCPSocket.new(@host, @port)
|
46
|
-
socket.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, true)
|
47
|
-
connection = Sanford::Protocol::Connection.new(socket)
|
48
|
-
sleep(@delay) if @delay
|
49
|
-
socket.send(bytes, 0)
|
50
|
-
socket.close_write
|
51
|
-
Sanford::Protocol::Response.parse(connection.read)
|
52
|
-
ensure
|
53
|
-
socket.close rescue false
|
54
|
-
end
|
55
|
-
|
56
|
-
protected
|
57
|
-
|
58
|
-
def call_using_fake_socket(method, *args)
|
59
|
-
self.call(Sanford::Protocol::FakeSocket.send(method, *args).in)
|
60
|
-
end
|
61
|
-
|
62
|
-
end
|
@@ -1,306 +0,0 @@
|
|
1
|
-
require 'assert'
|
2
|
-
require 'sanford-protocol/fake_socket'
|
3
|
-
|
4
|
-
# These tests are intended as a high level test against Sanford's server. They
|
5
|
-
# use fake and real connections to test how Sanford behaves.
|
6
|
-
|
7
|
-
class RequestHandlingTests < Assert::Context
|
8
|
-
desc "Sanford's handling of requests"
|
9
|
-
setup do
|
10
|
-
@env_sanford_protocol_debug = ENV['SANFORD_PROTOCOL_DEBUG']
|
11
|
-
@env_sanford_debug = ENV['SANFORD_DEBUG']
|
12
|
-
ENV.delete('SANFORD_PROTOCOL_DEBUG')
|
13
|
-
ENV['SANFORD_DEBUG'] = '1'
|
14
|
-
end
|
15
|
-
teardown do
|
16
|
-
ENV['SANFORD_DEBUG'] = @env_sanford_debug
|
17
|
-
ENV['SANFORD_PROTOCOL_DEBUG'] = @env_sanford_protocol_debug
|
18
|
-
end
|
19
|
-
|
20
|
-
class FakeConnectionTests < RequestHandlingTests
|
21
|
-
setup do
|
22
|
-
@host_data = Sanford::HostData.new(TestHost)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
class EchoTests < FakeConnectionTests
|
27
|
-
desc "running a request for the echo server"
|
28
|
-
setup do
|
29
|
-
@connection = FakeConnection.with_request('echo', { :message => 'test' })
|
30
|
-
@worker = Sanford::Worker.new(@host_data, @connection)
|
31
|
-
end
|
32
|
-
|
33
|
-
should "return a successful response and echo the params sent to it" do
|
34
|
-
assert_nothing_raised{ @worker.run }
|
35
|
-
response = @connection.response
|
36
|
-
|
37
|
-
assert_equal 200, response.code
|
38
|
-
assert_equal nil, response.status.message
|
39
|
-
assert_equal 'test', response.data
|
40
|
-
end
|
41
|
-
|
42
|
-
should "have cloased the connection's write stream" do
|
43
|
-
assert_nothing_raised{ @worker.run }
|
44
|
-
|
45
|
-
assert_equal true, @connection.write_stream_closed
|
46
|
-
end
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
|
-
class MissingServiceNameTests < FakeConnectionTests
|
51
|
-
desc "running a request with no service name"
|
52
|
-
setup do
|
53
|
-
request_hash = Sanford::Protocol::Request.new('what', {}).to_hash
|
54
|
-
request_hash.delete('name')
|
55
|
-
@connection = FakeConnection.new(request_hash)
|
56
|
-
@worker = Sanford::Worker.new(@host_data, @connection)
|
57
|
-
end
|
58
|
-
|
59
|
-
should "return a bad request response" do
|
60
|
-
assert_raises(Sanford::Protocol::BadRequestError) do
|
61
|
-
@worker.run
|
62
|
-
end
|
63
|
-
response = @connection.response
|
64
|
-
|
65
|
-
assert_equal 400, response.code
|
66
|
-
assert_match "request", response.status.message
|
67
|
-
assert_match "name", response.status.message
|
68
|
-
assert_equal nil, response.data
|
69
|
-
end
|
70
|
-
|
71
|
-
end
|
72
|
-
|
73
|
-
class NotFoundServiceTests < FakeConnectionTests
|
74
|
-
desc "running a request with no matching service name"
|
75
|
-
setup do
|
76
|
-
@connection = FakeConnection.with_request('what', {})
|
77
|
-
@worker = Sanford::Worker.new(@host_data, @connection)
|
78
|
-
end
|
79
|
-
|
80
|
-
should "return a bad request response" do
|
81
|
-
assert_raises(Sanford::NotFoundError) do
|
82
|
-
@worker.run
|
83
|
-
end
|
84
|
-
response = @connection.response
|
85
|
-
|
86
|
-
assert_equal 404, response.code
|
87
|
-
assert_equal nil, response.status.message
|
88
|
-
assert_equal nil, response.data
|
89
|
-
end
|
90
|
-
|
91
|
-
end
|
92
|
-
|
93
|
-
class ErrorServiceTests < FakeConnectionTests
|
94
|
-
desc "running a request that errors on the server"
|
95
|
-
setup do
|
96
|
-
@connection = FakeConnection.with_request('bad', {})
|
97
|
-
@worker = Sanford::Worker.new(@host_data, @connection)
|
98
|
-
end
|
99
|
-
|
100
|
-
should "return a bad request response" do
|
101
|
-
assert_raises(RuntimeError) do
|
102
|
-
@worker.run
|
103
|
-
end
|
104
|
-
response = @connection.response
|
105
|
-
|
106
|
-
assert_equal 500, response.code
|
107
|
-
assert_match "error", response.status.message
|
108
|
-
assert_equal nil, response.data
|
109
|
-
end
|
110
|
-
|
111
|
-
end
|
112
|
-
|
113
|
-
class HaltTests < FakeConnectionTests
|
114
|
-
desc "running a request that halts"
|
115
|
-
setup do
|
116
|
-
@connection = FakeConnection.with_request('halt_it', {})
|
117
|
-
@worker = Sanford::Worker.new(@host_data, @connection)
|
118
|
-
end
|
119
|
-
|
120
|
-
should "return the response that was halted" do
|
121
|
-
assert_nothing_raised{ @worker.run }
|
122
|
-
response = @connection.response
|
123
|
-
|
124
|
-
assert_equal 728, response.code
|
125
|
-
assert_equal "I do what I want", response.status.message
|
126
|
-
assert_equal [ 1, true, 'yes' ], response.data
|
127
|
-
end
|
128
|
-
|
129
|
-
end
|
130
|
-
|
131
|
-
class AuthorizeRequestTests < FakeConnectionTests
|
132
|
-
desc "running a request that halts in a callback"
|
133
|
-
setup do
|
134
|
-
@connection = FakeConnection.with_request('authorized', {})
|
135
|
-
@worker = Sanford::Worker.new(@host_data, @connection)
|
136
|
-
end
|
137
|
-
|
138
|
-
should "return the response that was halted" do
|
139
|
-
assert_nothing_raised{ @worker.run }
|
140
|
-
response = @connection.response
|
141
|
-
|
142
|
-
assert_equal 401, response.code
|
143
|
-
assert_equal "Not authorized", response.status.message
|
144
|
-
assert_equal nil, response.data
|
145
|
-
end
|
146
|
-
|
147
|
-
end
|
148
|
-
|
149
|
-
class WithCustomErrorHandlerTests < FakeConnectionTests
|
150
|
-
desc "running a request that triggers our custom error handler"
|
151
|
-
setup do
|
152
|
-
@connection = FakeConnection.with_request('custom_error', {})
|
153
|
-
@worker = Sanford::Worker.new(@host_data, @connection)
|
154
|
-
end
|
155
|
-
|
156
|
-
should "return the response that was halted" do
|
157
|
-
assert_raises(::MyCustomError){ @worker.run }
|
158
|
-
response = @connection.response
|
159
|
-
|
160
|
-
assert_equal 987, response.code
|
161
|
-
assert_equal "custom error!", response.status.message
|
162
|
-
assert_equal nil, response.data
|
163
|
-
end
|
164
|
-
|
165
|
-
end
|
166
|
-
|
167
|
-
class WithBadResponseHashTests < FakeConnectionTests
|
168
|
-
desc "running a request that builds an object that can't be encoded"
|
169
|
-
setup do
|
170
|
-
@connection = FakeConnection.with_request('echo', { :message => 'cant encode' }, true)
|
171
|
-
@worker = Sanford::Worker.new(@host_data, @connection)
|
172
|
-
end
|
173
|
-
|
174
|
-
should "return the response that was halted" do
|
175
|
-
assert_raises(RuntimeError){ @worker.run }
|
176
|
-
response = @connection.response
|
177
|
-
|
178
|
-
assert_equal 500, response.code
|
179
|
-
assert_equal "An unexpected error occurred.", response.status.message
|
180
|
-
assert_equal nil, response.data
|
181
|
-
end
|
182
|
-
|
183
|
-
end
|
184
|
-
|
185
|
-
# essentially, don't call `IO.select`
|
186
|
-
class FakeProtocolConnection < Sanford::Protocol::Connection
|
187
|
-
def wait_for_data(*args)
|
188
|
-
true
|
189
|
-
end
|
190
|
-
end
|
191
|
-
|
192
|
-
class WithAKeepAliveTests < FakeConnectionTests
|
193
|
-
desc "receiving a keep-alive connection"
|
194
|
-
setup do
|
195
|
-
@server = Sanford::Server.new(TestHost, {
|
196
|
-
:ready_timeout => 0.1,
|
197
|
-
:keep_alive => true
|
198
|
-
})
|
199
|
-
@server.on_run
|
200
|
-
@socket = Sanford::Protocol::FakeSocket.new
|
201
|
-
@fake_connection = FakeProtocolConnection.new(@socket)
|
202
|
-
Sanford::Protocol::Connection.stubs(:new).with(@socket).returns(@fake_connection)
|
203
|
-
end
|
204
|
-
teardown do
|
205
|
-
Sanford::Protocol::Connection.unstub(:new)
|
206
|
-
end
|
207
|
-
|
208
|
-
should "not error and nothing should be written" do
|
209
|
-
assert_nothing_raised do
|
210
|
-
@server.serve(@socket)
|
211
|
-
assert_equal "", @socket.out
|
212
|
-
end
|
213
|
-
end
|
214
|
-
|
215
|
-
end
|
216
|
-
|
217
|
-
class ForkedServerTests < RequestHandlingTests
|
218
|
-
include Test::SpawnServerHelper
|
219
|
-
end
|
220
|
-
|
221
|
-
# Simple service test that echos back the params sent to it
|
222
|
-
class EchoServerTests < ForkedServerTests
|
223
|
-
desc "when hitting an echo service"
|
224
|
-
|
225
|
-
should "return a successful response and echo the params sent to it" do
|
226
|
-
self.start_server(TestHost) do
|
227
|
-
response = SimpleClient.call_with_request(TestHost, 'echo', {
|
228
|
-
:message => 'test'
|
229
|
-
})
|
230
|
-
|
231
|
-
assert_equal 200, response.code
|
232
|
-
assert_equal nil, response.status.message
|
233
|
-
assert_equal 'test', response.data
|
234
|
-
end
|
235
|
-
end
|
236
|
-
end
|
237
|
-
|
238
|
-
# Sending the server a completely wrong stream of bytes
|
239
|
-
class BadMessageTests < ForkedServerTests
|
240
|
-
desc "when sent a invalid request stream"
|
241
|
-
|
242
|
-
should "return a bad request response with an error message" do
|
243
|
-
self.start_server(TestHost) do
|
244
|
-
bytes = [ Sanford::Protocol.msg_version, "\000" ].join
|
245
|
-
response = SimpleClient.call_with(TestHost, bytes)
|
246
|
-
|
247
|
-
assert_equal 400, response.code
|
248
|
-
assert_match "size", response.status.message
|
249
|
-
assert_equal nil, response.data
|
250
|
-
end
|
251
|
-
end
|
252
|
-
end
|
253
|
-
|
254
|
-
# Sending the server a protocol version that doesn't match its version
|
255
|
-
class WrongProtocolVersionTests < ForkedServerTests
|
256
|
-
desc "when sent a request with a wrong protocol version"
|
257
|
-
|
258
|
-
should "return a bad request response with an error message" do
|
259
|
-
self.start_server(TestHost) do
|
260
|
-
bytes = [ Sanford::Protocol.msg_version, "\000" ].join
|
261
|
-
response = SimpleClient.call_with_msg_body(TestHost, {}, nil, "\000")
|
262
|
-
|
263
|
-
assert_equal 400, response.code
|
264
|
-
assert_match "Protocol version", response.status.message
|
265
|
-
assert_equal nil, response.data
|
266
|
-
end
|
267
|
-
end
|
268
|
-
end
|
269
|
-
|
270
|
-
# Sending the server a body that it can't parse
|
271
|
-
class BadBodyTests < ForkedServerTests
|
272
|
-
desc "when sent a request with an invalid body"
|
273
|
-
|
274
|
-
should "return a bad request response with an error message" do
|
275
|
-
self.start_server(TestHost) do
|
276
|
-
response = SimpleClient.call_with_encoded_msg_body(TestHost, "\000\001\010\011" * 2)
|
277
|
-
|
278
|
-
assert_equal 400, response.code
|
279
|
-
assert_match "body", response.status.message
|
280
|
-
assert_equal nil, response.data
|
281
|
-
end
|
282
|
-
end
|
283
|
-
end
|
284
|
-
|
285
|
-
class HangingRequestTests < ForkedServerTests
|
286
|
-
desc "when a client connects but doesn't send anything for to long"
|
287
|
-
setup do
|
288
|
-
ENV['SANFORD_TIMEOUT'] = '0.1'
|
289
|
-
end
|
290
|
-
teardown do
|
291
|
-
ENV.delete('SANFORD_TIMEOUT')
|
292
|
-
end
|
293
|
-
|
294
|
-
should "timeout" do
|
295
|
-
self.start_server(TestHost) do
|
296
|
-
client = SimpleClient.new(TestHost, :with_delay => 1)
|
297
|
-
response = client.call_with_request('echo', { :message => 'test' })
|
298
|
-
|
299
|
-
assert_equal 408, response.code
|
300
|
-
assert_equal nil, response.status.message
|
301
|
-
assert_equal nil, response.data
|
302
|
-
end
|
303
|
-
end
|
304
|
-
end
|
305
|
-
|
306
|
-
end
|
data/test/unit/config_tests.rb
DELETED
@@ -1,56 +0,0 @@
|
|
1
|
-
require 'assert'
|
2
|
-
require 'sanford/config'
|
3
|
-
|
4
|
-
require 'ns-options/assert_macros'
|
5
|
-
require 'ns-options/proxy'
|
6
|
-
require 'sanford/logger'
|
7
|
-
require 'sanford/runner'
|
8
|
-
require 'sanford/template_source'
|
9
|
-
require 'test/support/factory'
|
10
|
-
|
11
|
-
class Sanford::Config
|
12
|
-
|
13
|
-
class UnitTests < Assert::Context
|
14
|
-
include NsOptions::AssertMacros
|
15
|
-
|
16
|
-
desc "Sanford::Config"
|
17
|
-
setup do
|
18
|
-
@config = Sanford::Config.new
|
19
|
-
end
|
20
|
-
subject{ @config }
|
21
|
-
|
22
|
-
should have_options :services_file, :logger
|
23
|
-
should have_readers :template_source
|
24
|
-
should have_imeths :set_template_source
|
25
|
-
|
26
|
-
should "be an NsOptions::Proxy" do
|
27
|
-
assert_includes NsOptions::Proxy, subject.class
|
28
|
-
end
|
29
|
-
|
30
|
-
should "default its services file" do
|
31
|
-
exp = Pathname.new(ENV['SANFORD_SERVICES_FILE'])
|
32
|
-
assert_equal exp, subject.services_file
|
33
|
-
end
|
34
|
-
|
35
|
-
should "default its logger to a NullLogger" do
|
36
|
-
assert_kind_of Sanford::NullLogger, subject.logger
|
37
|
-
end
|
38
|
-
|
39
|
-
should "have a null template source by default" do
|
40
|
-
assert_kind_of Sanford::NullTemplateSource, subject.template_source
|
41
|
-
end
|
42
|
-
|
43
|
-
should "set a new template source" do
|
44
|
-
path = '/path/to/app/assets'
|
45
|
-
block_called = false
|
46
|
-
subject.set_template_source(path) { |s| block_called = true}
|
47
|
-
|
48
|
-
assert_not_nil subject.template_source
|
49
|
-
assert_kind_of Sanford::TemplateSource, subject.template_source
|
50
|
-
assert_equal path, subject.template_source.path
|
51
|
-
assert_true block_called
|
52
|
-
end
|
53
|
-
|
54
|
-
end
|
55
|
-
|
56
|
-
end
|
@@ -1,71 +0,0 @@
|
|
1
|
-
require 'assert'
|
2
|
-
require 'sanford/host_data'
|
3
|
-
|
4
|
-
class Sanford::HostData
|
5
|
-
|
6
|
-
class UnitTests < Assert::Context
|
7
|
-
desc "Sanford::HostData"
|
8
|
-
setup do
|
9
|
-
TestHost.init_has_been_called = false
|
10
|
-
@host_data = Sanford::HostData.new(TestHost)
|
11
|
-
end
|
12
|
-
teardown do
|
13
|
-
TestHost.init_has_been_called = false
|
14
|
-
end
|
15
|
-
subject{ @host_data }
|
16
|
-
|
17
|
-
should have_readers :name, :logger, :verbose, :keep_alive, :error_procs
|
18
|
-
should have_imeths :handler_class_for, :run
|
19
|
-
|
20
|
-
should "call the init procs" do
|
21
|
-
assert_equal true, TestHost.init_has_been_called
|
22
|
-
end
|
23
|
-
|
24
|
-
should "default its attrs from the host configuration" do
|
25
|
-
assert_equal TestHost.configuration.name, subject.name
|
26
|
-
assert_equal TestHost.configuration.logger.class, subject.logger.class
|
27
|
-
assert_equal TestHost.configuration.verbose_logging, subject.verbose
|
28
|
-
assert_equal TestHost.configuration.receives_keep_alive, subject.keep_alive
|
29
|
-
assert_equal TestHost.configuration.error_procs, subject.error_procs
|
30
|
-
end
|
31
|
-
|
32
|
-
should "allow overriding host configuration attrs" do
|
33
|
-
host_data = Sanford::HostData.new(TestHost, :verbose_logging => false)
|
34
|
-
|
35
|
-
assert_false host_data.verbose
|
36
|
-
assert_equal TestHost.receives_keep_alive, host_data.keep_alive
|
37
|
-
end
|
38
|
-
|
39
|
-
should "ignore nil values passed as overrides" do
|
40
|
-
host_data = Sanford::HostData.new(TestHost, :verbose_logging => nil)
|
41
|
-
assert_not_nil host_data.verbose
|
42
|
-
end
|
43
|
-
|
44
|
-
should "constantize a host's handlers" do
|
45
|
-
handlers = subject.instance_variable_get("@handlers")
|
46
|
-
assert_equal TestHost::Authorized, handlers['authorized']
|
47
|
-
assert_equal TestHost::Bad, handlers['bad']
|
48
|
-
assert_equal TestHost::Echo, handlers['echo']
|
49
|
-
assert_equal TestHost::HaltIt, handlers['halt_it']
|
50
|
-
assert_equal TestHost::Multiply, handlers['multiply']
|
51
|
-
end
|
52
|
-
|
53
|
-
should "look up handler classes with #handler_class_for" do
|
54
|
-
assert_equal TestHost::Echo, subject.handler_class_for('echo')
|
55
|
-
end
|
56
|
-
|
57
|
-
should "raise a custom error when handler_class_for is called with an unknown service" do
|
58
|
-
assert_raises(Sanford::NotFoundError) do
|
59
|
-
subject.handler_class_for('not_defined')
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
should "raise a custom error when a service is configured with an undefined class" do
|
64
|
-
assert_raises(Sanford::NoHandlerClassError) do
|
65
|
-
Sanford::HostData.new(UndefinedHandlersHost).setup
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
end
|
70
|
-
|
71
|
-
end
|
data/test/unit/host_tests.rb
DELETED
@@ -1,141 +0,0 @@
|
|
1
|
-
require 'assert'
|
2
|
-
require 'sanford/host'
|
3
|
-
|
4
|
-
require 'sanford/logger'
|
5
|
-
require 'sanford/runner'
|
6
|
-
|
7
|
-
module Sanford::Host
|
8
|
-
|
9
|
-
class UnitTests < Assert::Context
|
10
|
-
desc "Sanford::Host"
|
11
|
-
setup do
|
12
|
-
@host_class = Class.new{ include Sanford::Host }
|
13
|
-
end
|
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 :error, :init, :service_handler_ns, :service
|
19
|
-
|
20
|
-
should "know its configuration" do
|
21
|
-
assert_kind_of Configuration, subject.configuration
|
22
|
-
end
|
23
|
-
|
24
|
-
should "have no services by default" do
|
25
|
-
assert_empty subject.services
|
26
|
-
end
|
27
|
-
|
28
|
-
should "get/set its configuration options" do
|
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
|
-
end
|
58
|
-
|
59
|
-
should "add error procs to the configuration" do
|
60
|
-
assert_empty subject.configuration.error_procs
|
61
|
-
subject.error &proc{}
|
62
|
-
assert_not_empty subject.configuration.error_procs
|
63
|
-
end
|
64
|
-
|
65
|
-
should "add init procs to the configuration" do
|
66
|
-
assert_empty subject.configuration.init_procs
|
67
|
-
subject.init &proc{}
|
68
|
-
assert_not_empty subject.configuration.init_procs
|
69
|
-
end
|
70
|
-
|
71
|
-
should "get/set its service_handler_ns" do
|
72
|
-
assert_nil subject.service_handler_ns
|
73
|
-
subject.service_handler_ns 'a-ns'
|
74
|
-
assert_equal 'a-ns', subject.service_handler_ns
|
75
|
-
end
|
76
|
-
|
77
|
-
should "add services" do
|
78
|
-
subject.service_handler_ns 'MyNamespace'
|
79
|
-
subject.service('test', 'MyServiceHandler')
|
80
|
-
|
81
|
-
assert_equal 'MyNamespace::MyServiceHandler', subject.services['test']
|
82
|
-
end
|
83
|
-
|
84
|
-
should "force string names when adding services" do
|
85
|
-
subject.service(:another_service, 'MyServiceHandler')
|
86
|
-
assert_nil subject.services[:another_service]
|
87
|
-
assert_equal 'MyServiceHandler', subject.services['another_service']
|
88
|
-
end
|
89
|
-
|
90
|
-
should "ignore a namespace when a service class has leading colons" do
|
91
|
-
subject.service_handler_ns 'MyNamespace'
|
92
|
-
subject.service('test', '::MyServiceHandler')
|
93
|
-
|
94
|
-
assert_equal '::MyServiceHandler', subject.services['test']
|
95
|
-
end
|
96
|
-
|
97
|
-
end
|
98
|
-
|
99
|
-
class ClassMethodsTests < UnitTests
|
100
|
-
desc "class"
|
101
|
-
subject{ @host_class }
|
102
|
-
|
103
|
-
should "proxy its method to its instance" do
|
104
|
-
assert_equal subject.instance.name, subject.name
|
105
|
-
assert subject.respond_to?(:pid_file)
|
106
|
-
end
|
107
|
-
|
108
|
-
should "have registered the class with sanford's known hosts" do
|
109
|
-
assert_includes subject, Sanford.hosts
|
110
|
-
end
|
111
|
-
|
112
|
-
end
|
113
|
-
|
114
|
-
class ConfigurationTests < UnitTests
|
115
|
-
desc "Configuration"
|
116
|
-
setup do
|
117
|
-
@configuration = Configuration.new(@host_class.instance)
|
118
|
-
end
|
119
|
-
subject{ @configuration }
|
120
|
-
|
121
|
-
should have_imeths :name, :ip, :port, :pid_file, :logger, :verbose_logging
|
122
|
-
should have_imeths :receives_keep_alive, :error_procs, :init_procs
|
123
|
-
|
124
|
-
should "default name to the class name of the host" do
|
125
|
-
assert_equal @host_class.name, subject.name
|
126
|
-
end
|
127
|
-
|
128
|
-
should "default its other attrs" do
|
129
|
-
assert_equal '0.0.0.0', subject.ip
|
130
|
-
assert_nil subject.port
|
131
|
-
assert_nil subject.pid_file
|
132
|
-
assert_equal Sanford.config.logger.class, subject.logger.class
|
133
|
-
assert_true subject.verbose_logging
|
134
|
-
assert_false subject.receives_keep_alive
|
135
|
-
assert_empty subject.error_procs
|
136
|
-
assert_empty subject.init_procs
|
137
|
-
end
|
138
|
-
|
139
|
-
end
|
140
|
-
|
141
|
-
end
|
data/test/unit/hosts_tests.rb
DELETED
@@ -1,50 +0,0 @@
|
|
1
|
-
require 'assert'
|
2
|
-
require 'sanford/hosts'
|
3
|
-
|
4
|
-
require 'sanford/host'
|
5
|
-
|
6
|
-
class Sanford::Hosts
|
7
|
-
|
8
|
-
class UnitTests < Assert::Context
|
9
|
-
desc "Sandford::Hosts"
|
10
|
-
setup do
|
11
|
-
@hosts = Sanford::Hosts.new
|
12
|
-
end
|
13
|
-
subject{ @hosts }
|
14
|
-
|
15
|
-
should have_instance_methods :add, :first, :find
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
class FindTests < UnitTests
|
20
|
-
desc "find method"
|
21
|
-
setup do
|
22
|
-
@hosts.add ::NotNamedHost
|
23
|
-
@hosts.add ::NamedHost
|
24
|
-
@hosts.add ::BadlyNamedHost
|
25
|
-
end
|
26
|
-
|
27
|
-
should "allow finding hosts by their class name or configured name" do
|
28
|
-
assert_includes ::NotNamedHost, subject
|
29
|
-
assert_includes ::NamedHost, subject
|
30
|
-
|
31
|
-
assert_equal ::NotNamedHost, subject.find('NotNamedHost')
|
32
|
-
assert_equal ::NamedHost, subject.find('NamedHost')
|
33
|
-
assert_equal ::NamedHost, subject.find('named_host')
|
34
|
-
end
|
35
|
-
|
36
|
-
should "prefer hosts with a matching class name over configured name" do
|
37
|
-
assert_includes ::BadlyNamedHost, subject
|
38
|
-
assert_equal NotNamedHost, subject.find('NotNamedHost')
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
# Using this syntax because these classes need to be defined as top-level
|
44
|
-
# constants for ease in using their class names in the tests
|
45
|
-
|
46
|
-
::NotNamedHost = Class.new{ include Sanford::Host }
|
47
|
-
::NamedHost = Class.new{ include Sanford::Host; name 'named_host' }
|
48
|
-
::BadlyNamedHost = Class.new{ include Sanford::Host; name 'NotNamedHost' }
|
49
|
-
|
50
|
-
end
|