sanford 0.17.0 → 0.18.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,12 +8,21 @@ class Sanford::Router
8
8
  class UnitTests < Assert::Context
9
9
  desc "Sanford::Router"
10
10
  setup do
11
- @router = Sanford::Router.new
11
+ @router_class = Sanford::Router
12
+ end
13
+ subject{ @router_class }
14
+
15
+ end
16
+
17
+ class InitTests < UnitTests
18
+ desc "when init"
19
+ setup do
20
+ @router = @router_class.new
12
21
  end
13
22
  subject{ @router }
14
23
 
15
24
  should have_readers :routes
16
- should have_imeths :service_handler_ns, :service
25
+ should have_imeths :service_handler_ns, :service, :validate!
17
26
 
18
27
  should "build an empty array for its routes by default" do
19
28
  assert_equal [], subject.routes
@@ -49,17 +58,26 @@ class Sanford::Router
49
58
  subject.service service_name, handler_name
50
59
 
51
60
  route = subject.routes.last
52
- expected = "#{namespace}::#{handler_name}"
53
- assert_equal expected, route.handler_class_name
61
+ exp = "#{namespace}::#{handler_name}"
62
+ assert_equal exp, route.handler_class_name
63
+ end
64
+
65
+ should "validate each route when validating" do
66
+ subject.service(Factory.string, TestHandler.to_s)
67
+ subject.routes.each{ |route| assert_nil route.handler_class }
68
+ subject.validate!
69
+ subject.routes.each{ |route| assert_not_nil route.handler_class }
54
70
  end
55
71
 
56
72
  should "know its custom inspect" do
57
73
  reference = '0x0%x' % (subject.object_id << 1)
58
- expected = "#<#{subject.class}:#{reference} " \
59
- "@service_handler_ns=#{subject.service_handler_ns.inspect}>"
60
- assert_equal expected, subject.inspect
74
+ exp = "#<#{subject.class}:#{reference} " \
75
+ "@service_handler_ns=#{subject.service_handler_ns.inspect}>"
76
+ assert_equal exp, subject.inspect
61
77
  end
62
78
 
63
79
  end
64
80
 
81
+ TestHandler = Class.new
82
+
65
83
  end
@@ -8,72 +8,76 @@ class Sanford::ServerData
8
8
  class UnitTests < Assert::Context
9
9
  desc "Sanford::ServerData"
10
10
  setup do
11
- @orig_ip_env_var = ENV['SANFORD_IP']
12
- @orig_port_env_var = ENV['SANFORD_PORT']
11
+ @orig_ip_env_var = ENV['SANFORD_IP']
12
+ @orig_port_env_var = ENV['SANFORD_PORT']
13
+ @orig_label_env_var = ENV['SANFORD_PROCESS_LABEL']
13
14
  ENV.delete('SANFORD_IP')
14
15
  ENV.delete('SANFORD_PORT')
16
+ ENV.delete('SANFORD_PROCESS_LABEL')
17
+ ENV.delete('SANFORD_DEBUG')
15
18
 
16
19
  @route = Sanford::Route.new(Factory.string, TestHandler.to_s).tap(&:validate!)
20
+
17
21
  @config_hash = {
18
22
  :name => Factory.string,
19
23
  :ip => Factory.string,
20
24
  :port => Factory.integer,
21
25
  :pid_file => Factory.file_path,
22
- :receives_keep_alive => Factory.boolean,
26
+ :shutdown_timeout => Factory.integer,
23
27
  :worker_class => Class.new,
24
28
  :worker_params => { Factory.string => Factory.string },
25
29
  :num_workers => Factory.integer,
26
- :verbose_logging => Factory.boolean,
27
- :logger => Factory.string,
28
- :template_source => Factory.string,
29
- :shutdown_timeout => Factory.integer,
30
- :init_procs => Factory.integer(3).times.map{ proc{} },
31
30
  :error_procs => Factory.integer(3).times.map{ proc{} },
31
+ :template_source => Factory.string,
32
+ :logger => Factory.string,
32
33
  :router => Factory.string,
34
+ :receives_keep_alive => Factory.boolean,
35
+ :verbose_logging => Factory.boolean,
33
36
  :routes => [@route]
34
37
  }
35
38
  @server_data = Sanford::ServerData.new(@config_hash)
36
39
  end
37
40
  teardown do
38
- ENV['SANFORD_IP'] = @orig_ip_env_var
39
- ENV['SANFORD_PORT'] = @orig_port_env_var
41
+ ENV['SANFORD_IP'] = @orig_ip_env_var
42
+ ENV['SANFORD_PORT'] = @orig_port_env_var
43
+ ENV['SANFORD_PROCESS_LABEL'] = @orig_label_env_var
44
+ ENV['SANFORD_DEBUG'] = @orig_debug_env_var
40
45
  end
41
46
  subject{ @server_data }
42
47
 
43
- should have_readers :name
44
- should have_readers :pid_file
45
- should have_readers :receives_keep_alive
46
- should have_readers :worker_class, :worker_params, :num_workers
47
- should have_readers :debug, :logger, :dtcp_logger, :verbose_logging
48
- should have_readers :template_source, :shutdown_timeout
49
- should have_readers :init_procs, :error_procs
50
- should have_readers :router, :routes
51
48
  should have_accessors :ip, :port
49
+ should have_readers :name, :pid_file, :shutdown_timeout
50
+ should have_readers :worker_class, :worker_params, :num_workers
51
+ should have_readers :error_procs, :template_source, :logger, :router
52
+ should have_readers :receives_keep_alive, :verbose_logging
53
+ should have_readers :debug, :dtcp_logger, :routes, :process_label
54
+ should have_imeths :route_for
52
55
 
53
- should "know its attributes" do
56
+ should "know its attrs" do
54
57
  h = @config_hash
55
58
  assert_equal h[:name], subject.name
56
59
  assert_equal h[:ip], subject.ip
57
60
  assert_equal h[:port], subject.port
58
61
  assert_equal h[:pid_file], subject.pid_file
59
62
 
60
- assert_equal h[:receives_keep_alive], subject.receives_keep_alive
61
-
62
- assert_equal h[:worker_class], subject.worker_class
63
- assert_equal h[:worker_params], subject.worker_params
64
- assert_equal h[:num_workers], subject.num_workers
65
-
66
- assert_equal h[:verbose_logging], subject.verbose_logging
67
- assert_equal h[:logger], subject.logger
63
+ assert_equal h[:shutdown_timeout], subject.shutdown_timeout
68
64
 
65
+ assert_equal h[:worker_class], subject.worker_class
66
+ assert_equal h[:worker_params], subject.worker_params
67
+ assert_equal h[:num_workers], subject.num_workers
68
+ assert_equal h[:error_procs], subject.error_procs
69
69
  assert_equal h[:template_source], subject.template_source
70
+ assert_equal h[:logger], subject.logger
71
+ assert_equal h[:router], subject.router
70
72
 
71
- assert_equal h[:shutdown_timeout], subject.shutdown_timeout
73
+ assert_equal h[:receives_keep_alive], subject.receives_keep_alive
74
+ assert_equal h[:verbose_logging], subject.verbose_logging
72
75
 
73
- assert_equal h[:init_procs], subject.init_procs
74
- assert_equal h[:error_procs], subject.error_procs
76
+ assert_false subject.debug
77
+ assert_nil subject.dtcp_logger
75
78
 
76
- assert_equal h[:router], subject.router
79
+ exp = { @route.name => @route }
80
+ assert_equal exp, subject.routes
77
81
  end
78
82
 
79
83
  should "use ip and port env vars if they are set" do
@@ -90,14 +94,32 @@ class Sanford::ServerData
90
94
  assert_equal @config_hash[:port], server_data.port
91
95
  end
92
96
 
93
- should "build a routes lookup hash" do
94
- expected = { @route.name => @route }
95
- assert_equal expected, subject.routes
97
+ should "know its process label" do
98
+ ENV['SANFORD_PROCESS_LABEL'] = Factory.string
99
+ server_data = Sanford::ServerData.new(@config_hash)
100
+ exp = ENV['SANFORD_PROCESS_LABEL']
101
+ assert_equal exp, server_data.process_label
102
+
103
+ ENV['SANFORD_PROCESS_LABEL'] = ""
104
+ server_data = Sanford::ServerData.new(@config_hash)
105
+ exp = "#{@config_hash[:name]}-#{@config_hash[:ip]}-#{@config_hash[:port]}"
106
+ assert_equal exp, server_data.process_label
107
+
108
+ ENV.delete('SANFORD_PROCESS_LABEL')
109
+ server_data = Sanford::ServerData.new(@config_hash)
110
+ exp = "#{@config_hash[:name]}-#{@config_hash[:ip]}-#{@config_hash[:port]}"
111
+ assert_equal exp, server_data.process_label
112
+ end
113
+
114
+ should "use the debug env var if set" do
115
+ ENV['SANFORD_DEBUG'] = Factory.string
116
+ server_data = Sanford::ServerData.new(@config_hash)
117
+ assert_true server_data.debug
118
+ assert_equal server_data.logger, server_data.dtcp_logger
96
119
  end
97
120
 
98
- should "allow lookup a route using `route_for`" do
99
- route = subject.route_for(@route.name)
100
- assert_equal @route, route
121
+ should "look up a route using `route_for`" do
122
+ assert_equal @route, subject.route_for(@route.name)
101
123
  end
102
124
 
103
125
  should "raise a not found error using `route_for` with an invalid name" do
@@ -106,30 +128,30 @@ class Sanford::ServerData
106
128
  end
107
129
  end
108
130
 
109
- should "default its attributes when they aren't provided" do
131
+ should "default its attrs when they aren't provided" do
110
132
  server_data = Sanford::ServerData.new
111
133
  assert_nil server_data.name
112
134
  assert_nil server_data.ip
113
135
  assert_nil server_data.port
114
136
  assert_nil server_data.pid_file
115
-
116
- assert_false server_data.receives_keep_alive
137
+ assert_nil server_data.shutdown_timeout
117
138
 
118
139
  assert_nil server_data.worker_class
119
140
  assert_equal({}, server_data.worker_params)
120
141
  assert_nil server_data.num_workers
121
142
 
122
- assert_false server_data.verbose_logging
123
- assert_nil server_data.logger
143
+ assert_equal [], server_data.error_procs
124
144
 
125
145
  assert_nil server_data.template_source
146
+ assert_nil server_data.logger
147
+ assert_nil server_data.router
126
148
 
127
- assert_nil server_data.shutdown_timeout
149
+ assert_false server_data.receives_keep_alive
150
+ assert_false server_data.verbose_logging
128
151
 
129
- assert_equal [], server_data.init_procs
130
- assert_equal [], server_data.error_procs
152
+ assert_false server_data.debug
153
+ assert_nil server_data.dtcp_logger
131
154
 
132
- assert_nil server_data.router
133
155
  assert_equal({}, server_data.routes)
134
156
  end
135
157
 
@@ -3,158 +3,107 @@ require 'sanford/server'
3
3
 
4
4
  require 'dat-tcp/server_spy'
5
5
  require 'much-plugin'
6
- require 'ns-options/assert_macros'
7
- require 'sanford/route'
6
+ require 'sanford/logger'
7
+ require 'sanford/router'
8
+ require 'sanford/template_source'
8
9
 
9
10
  module Sanford::Server
10
11
 
11
12
  class UnitTests < Assert::Context
12
13
  desc "Sanford::Server"
13
14
  setup do
14
- @server_class = Class.new do
15
- include Sanford::Server
16
- end
15
+ @server_class = Class.new{ include Sanford::Server }
17
16
  end
18
17
  subject{ @server_class }
19
18
 
20
- should have_imeths :configuration
21
- should have_imeths :name, :ip, :port, :pid_file
22
- should have_imeths :receives_keep_alive
23
- should have_imeths :worker_class, :worker_params
24
- should have_imeths :num_workers, :workers
25
- should have_imeths :verbose_logging, :logger
26
- should have_imeths :shutdown_timeout
27
- should have_imeths :init, :error
28
- should have_imeths :router, :template_source
19
+ should have_imeths :config
20
+ should have_imeths :name, :ip, :port, :pid_file, :shutdown_timeout
21
+ should have_imeths :worker_class, :worker_params, :num_workers, :workers
22
+ should have_imeths :init, :error, :template_source, :logger, :router
23
+ should have_imeths :receives_keep_alive, :verbose_logging
29
24
 
30
25
  should "use much-plugin" do
31
26
  assert_includes MuchPlugin, Sanford::Server
32
27
  end
33
28
 
34
- should "know its configuration" do
35
- config = subject.configuration
36
- assert_instance_of Configuration, config
37
- assert_same config, subject.configuration
38
- end
29
+ should "allow setting its config values" do
30
+ config = subject.config
39
31
 
40
- should "allow reading/writing its configuration name" do
41
- new_name = Factory.string
42
- subject.name(new_name)
43
- assert_equal new_name, subject.configuration.name
44
- assert_equal new_name, subject.name
45
- end
32
+ exp = Factory.string
33
+ subject.name exp
34
+ assert_equal exp, config.name
46
35
 
47
- should "allow reading/writing its configuration ip" do
48
- new_ip = Factory.string
49
- subject.ip(new_ip)
50
- assert_equal new_ip, subject.configuration.ip
51
- assert_equal new_ip, subject.ip
52
- end
36
+ exp = Factory.string
37
+ subject.ip exp
38
+ assert_equal exp, config.ip
53
39
 
54
- should "allow reading/writing its configuration port" do
55
- new_port = Factory.integer
56
- subject.port(new_port)
57
- assert_equal new_port, subject.configuration.port
58
- assert_equal new_port, subject.port
59
- end
40
+ exp = Factory.integer
41
+ subject.port exp
42
+ assert_equal exp, config.port
60
43
 
61
- should "allow reading/writing its configuration pid file" do
62
- new_pid_file = Factory.string
63
- subject.pid_file(new_pid_file)
64
- exp = Pathname.new(new_pid_file)
65
- assert_equal exp, subject.configuration.pid_file
66
- assert_equal exp, subject.pid_file
67
- end
44
+ exp = Factory.file_path
45
+ subject.pid_file exp
46
+ assert_equal exp, config.pid_file
68
47
 
69
- should "allow reading/writing its configuration receives keep alive" do
70
- new_keep_alive = Factory.boolean
71
- subject.receives_keep_alive(new_keep_alive)
72
- assert_equal new_keep_alive, subject.configuration.receives_keep_alive
73
- assert_equal new_keep_alive, subject.receives_keep_alive
74
- end
48
+ exp = Factory.integer
49
+ subject.shutdown_timeout exp
50
+ assert_equal exp, config.shutdown_timeout
75
51
 
76
- should "allow reading/writing its configuration worker class" do
77
- new_worker_class = Class.new
78
- subject.worker_class(new_worker_class)
79
- assert_equal new_worker_class, subject.configuration.worker_class
80
- assert_equal new_worker_class, subject.worker_class
81
- end
52
+ exp = Class.new
53
+ subject.worker_class exp
54
+ assert_equal exp, subject.config.worker_class
82
55
 
83
- should "allow reading/writing its configuration worker params" do
84
- new_worker_params = { Factory.string => Factory.string }
85
- subject.worker_params(new_worker_params)
86
- assert_equal new_worker_params, subject.configuration.worker_params
87
- assert_equal new_worker_params, subject.worker_params
88
- end
56
+ exp = { Factory.string => Factory.string }
57
+ subject.worker_params exp
58
+ assert_equal exp, subject.config.worker_params
89
59
 
90
- should "allow reading/writing its configuration num workers" do
91
- new_num_workers = Factory.integer
92
- subject.num_workers(new_num_workers)
93
- assert_equal new_num_workers, subject.configuration.num_workers
94
- assert_equal new_num_workers, subject.num_workers
95
- end
60
+ exp = Factory.integer
61
+ subject.num_workers(exp)
62
+ assert_equal exp, subject.config.num_workers
63
+ assert_equal exp, subject.workers
96
64
 
97
- should "alias workers as num workers" do
98
- new_workers = Factory.integer
99
- subject.workers(new_workers)
100
- assert_equal new_workers, subject.configuration.num_workers
101
- assert_equal new_workers, subject.workers
102
- end
65
+ exp = proc{ }
66
+ assert_equal 0, config.init_procs.size
67
+ subject.init(&exp)
68
+ assert_equal 1, config.init_procs.size
69
+ assert_equal exp, config.init_procs.first
103
70
 
104
- should "allow reading/writing its configuration verbose logging" do
105
- new_verbose = Factory.boolean
106
- subject.verbose_logging(new_verbose)
107
- assert_equal new_verbose, subject.configuration.verbose_logging
108
- assert_equal new_verbose, subject.verbose_logging
109
- end
71
+ exp = proc{ }
72
+ assert_equal 0, config.error_procs.size
73
+ subject.error(&exp)
74
+ assert_equal 1, config.error_procs.size
75
+ assert_equal exp, config.error_procs.first
110
76
 
111
- should "allow reading/writing its configuration logger" do
112
- new_logger = Factory.string
113
- subject.logger(new_logger)
114
- assert_equal new_logger, subject.configuration.logger
115
- assert_equal new_logger, subject.logger
116
- end
77
+ exp = Sanford::TemplateSource.new(Factory.path)
78
+ subject.template_source exp
79
+ assert_equal exp, config.template_source
117
80
 
118
- should "allow reading/writing its configuration shutdown timeout" do
119
- new_shutdown_timeout = Factory.integer
120
- subject.shutdown_timeout(new_shutdown_timeout)
121
- assert_equal new_shutdown_timeout, subject.configuration.shutdown_timeout
122
- assert_equal new_shutdown_timeout, subject.shutdown_timeout
123
- end
81
+ exp = Logger.new(STDOUT)
82
+ subject.logger exp
83
+ assert_equal exp, config.logger
124
84
 
125
- should "allow adding init procs to its configuration" do
126
- new_init_proc = proc{ Factory.string }
127
- subject.init(&new_init_proc)
128
- assert_includes new_init_proc, subject.configuration.init_procs
129
- end
85
+ exp = Factory.boolean
86
+ subject.receives_keep_alive exp
87
+ assert_equal exp, config.receives_keep_alive
130
88
 
131
- should "allow adding error procs to its configuration" do
132
- new_error_proc = proc{ Factory.string }
133
- subject.error(&new_error_proc)
134
- assert_includes new_error_proc, subject.configuration.error_procs
89
+ exp = Factory.boolean
90
+ subject.verbose_logging exp
91
+ assert_equal exp, config.verbose_logging
135
92
  end
136
93
 
137
- should "allow reading/writing its configuration router" do
138
- new_router = Factory.string
139
- subject.router(new_router)
140
- assert_equal new_router, subject.configuration.router
141
- assert_equal new_router, subject.router
94
+ should "have a router by default and allow overriding it" do
95
+ assert_kind_of Sanford::Router, subject.router
96
+
97
+ new_router = Sanford::Router.new
98
+ subject.router new_router
99
+ assert_same new_router, subject.config.router
100
+ assert_same new_router, subject.router
142
101
  end
143
102
 
144
103
  should "allow configuring the router by passing a block to `router`" do
145
- new_router = Factory.string
146
-
147
104
  block_scope = nil
148
- subject.router(new_router){ block_scope = self }
149
- assert_equal new_router, subject.router
150
- assert_equal new_router, block_scope
151
- end
152
-
153
- should "allow setting the configuration template source" do
154
- new_template_source = Factory.string
155
- subject.template_source(new_template_source)
156
- assert_equal new_template_source, subject.configuration.template_source
157
- assert_equal new_template_source, subject.template_source
105
+ subject.router{ block_scope = self }
106
+ assert_equal subject.router, block_scope
158
107
  end
159
108
 
160
109
  end
@@ -165,17 +114,13 @@ module Sanford::Server
165
114
  @server_class.name Factory.string
166
115
  @server_class.ip Factory.string
167
116
  @server_class.port Factory.integer
168
- @server_class.num_workers Factory.integer
169
- @server_class.worker_params(Factory.string => Factory.string)
170
117
  @server_class.shutdown_timeout Factory.integer
118
+ @server_class.worker_params(Factory.string => Factory.string)
119
+ @server_class.num_workers Factory.integer
171
120
 
172
121
  @error_procs = Factory.integer(3).times.map{ proc{} }
173
122
  @error_procs.each{ |p| @server_class.error(&p) }
174
123
 
175
- @server_class.router do
176
- service Factory.string, TestHandler.to_s
177
- end
178
-
179
124
  @dtcp_spy = nil
180
125
  Assert.stub(DatTCP::Server, :new) do |*args|
181
126
  @dtcp_spy = DatTCP::ServerSpy.new(*args)
@@ -185,37 +130,44 @@ module Sanford::Server
185
130
  end
186
131
  subject{ @server }
187
132
 
188
- should have_readers :server_data, :dat_tcp_server
189
- should have_imeths :name, :ip, :port
190
- should have_imeths :file_descriptor, :client_file_descriptors
191
- should have_imeths :configured_ip, :configured_port
133
+ should have_readers :server_data
134
+ should have_imeths :ip, :port, :file_descriptor, :client_file_descriptors
135
+ should have_imeths :name, :configured_ip, :configured_port, :process_label
192
136
  should have_imeths :pid_file, :logger, :router, :template_source
193
137
  should have_imeths :listen, :start, :pause, :stop, :halt
194
- should have_imeths :paused?
138
+ should have_imeths :listening?, :running?, :paused?
195
139
 
196
- should "have validated its configuration" do
197
- assert_true subject.class.configuration.valid?
140
+ should "have validated its config" do
141
+ assert_true @server_class.config.valid?
198
142
  end
199
143
 
200
144
  should "know its server data" do
201
- configuration = subject.class.configuration
202
- data = subject.server_data
145
+ config = @server_class.config
146
+ data = subject.server_data
203
147
 
204
148
  assert_instance_of Sanford::ServerData, data
205
- assert_equal configuration.name, data.name
206
- assert_equal configuration.ip, data.ip
207
- assert_equal configuration.port, data.port
208
- assert_equal configuration.worker_class, data.worker_class
209
- assert_equal configuration.worker_params, data.worker_params
210
- assert_equal configuration.verbose_logging, data.verbose_logging
211
- assert_equal configuration.receives_keep_alive, data.receives_keep_alive
212
- assert_equal configuration.error_procs, data.error_procs
213
- assert_equal configuration.routes, data.routes.values
214
149
 
215
- assert_instance_of configuration.logger.class, data.logger
150
+ assert_equal config.name, data.name
151
+ assert_equal config.ip, data.ip
152
+ assert_equal config.port, data.port
153
+ assert_equal config.pid_file, data.pid_file
154
+ assert_equal config.shutdown_timeout, data.shutdown_timeout
155
+ assert_equal config.worker_class, data.worker_class
156
+ assert_equal config.worker_params, data.worker_params
157
+ assert_equal config.num_workers, data.num_workers
158
+ assert_equal config.error_procs, data.error_procs
159
+
160
+ assert_instance_of config.logger.class, data.logger
161
+ assert_instance_of config.router.class, data.router
162
+
163
+ assert_equal config.template_source, data.template_source
164
+ assert_equal config.verbose_logging, data.verbose_logging
165
+ assert_equal config.receives_keep_alive, data.receives_keep_alive
166
+
167
+ assert_equal config.routes, data.routes.values
216
168
  end
217
169
 
218
- should "know its dat tcp server" do
170
+ should "build a dat-tcp server" do
219
171
  data = subject.server_data
220
172
 
221
173
  assert_not_nil @dtcp_spy
@@ -227,21 +179,19 @@ module Sanford::Server
227
179
  :sanford_server_data => data
228
180
  })
229
181
  assert_equal exp, @dtcp_spy.worker_params
230
-
231
- assert_equal @dtcp_spy, subject.dat_tcp_server
232
182
  end
233
183
 
234
184
  should "demeter its server data" do
235
- assert_equal subject.server_data.name, subject.name
236
- assert_equal subject.server_data.ip, subject.configured_ip
237
- assert_equal subject.server_data.port, subject.configured_port
238
- assert_equal subject.server_data.pid_file, subject.pid_file
239
- end
185
+ data = subject.server_data
240
186
 
241
- should "know its logger, router and template source" do
242
- assert_equal subject.server_data.logger, subject.logger
243
- assert_equal subject.server_data.router, subject.router
244
- assert_equal subject.server_data.template_source, subject.template_source
187
+ assert_equal data.name, subject.name
188
+ assert_equal data.ip, subject.configured_ip
189
+ assert_equal data.port, subject.configured_port
190
+ assert_equal data.process_label, subject.process_label
191
+ assert_equal data.pid_file, subject.pid_file
192
+ assert_equal data.logger, subject.logger
193
+ assert_equal data.router, subject.router
194
+ assert_equal data.template_source, subject.template_source
245
195
  end
246
196
 
247
197
  should "call listen on its dat tcp server using `listen`" do
@@ -324,13 +274,21 @@ module Sanford::Server
324
274
  assert_equal wait, @dtcp_spy.waiting_for_halt
325
275
  end
326
276
 
327
- should "know if its been paused" do
277
+ should "know if its listening, running or been paused" do
278
+ assert_false subject.listening?
279
+ assert_false subject.running?
328
280
  assert_false subject.paused?
329
281
  subject.listen
282
+ assert_true subject.listening?
283
+ assert_false subject.running?
330
284
  assert_true subject.paused?
331
285
  subject.start
286
+ assert_true subject.listening?
287
+ assert_true subject.running?
332
288
  assert_false subject.paused?
333
289
  subject.pause
290
+ assert_true subject.listening?
291
+ assert_false subject.running?
334
292
  assert_true subject.paused?
335
293
  end
336
294
 
@@ -355,111 +313,85 @@ module Sanford::Server
355
313
 
356
314
  end
357
315
 
358
- class ConfigurationTests < UnitTests
359
- include NsOptions::AssertMacros
360
-
361
- desc "Configuration"
316
+ class ConfigTests < UnitTests
317
+ desc "Config"
362
318
  setup do
363
- @configuration = Configuration.new.tap do |c|
364
- c.name Factory.string
365
- c.ip Factory.string
366
- c.port Factory.integer
367
- end
319
+ @config_class = Config
320
+ @config = Config.new
368
321
  end
369
- subject{ @configuration }
322
+ subject{ @config }
370
323
 
371
- should have_options :name, :ip, :port, :pid_file
372
- should have_options :receives_keep_alive
373
- should have_options :verbose_logging, :logger
374
- should have_options :template_source
375
- should have_accessors :init_procs, :error_procs
324
+ should have_accessors :name, :ip, :port, :pid_file, :shutdown_timeout
376
325
  should have_accessors :worker_class, :worker_params, :num_workers
377
- should have_accessors :shutdown_timeout
378
- should have_accessors :router
379
- should have_imeths :routes
380
- should have_imeths :to_hash
381
- should have_imeths :valid?, :validate!
326
+ should have_accessors :init_procs, :error_procs, :template_source, :logger, :router
327
+ should have_accessors :receives_keep_alive, :verbose_logging
328
+ should have_imeths :routes, :valid?, :validate!
382
329
 
383
- should "be an ns-options proxy" do
384
- assert_includes NsOptions::Proxy, subject.class
330
+ should "know its default attr values" do
331
+ assert_equal 4, @config_class::DEFAULT_NUM_WORKERS
332
+ assert_equal '0.0.0.0', @config_class::DEFAULT_IP_ADDRESS
385
333
  end
386
334
 
387
- should "know its default num workers" do
388
- assert_equal 4, Configuration::DEFAULT_NUM_WORKERS
389
- end
335
+ should "default its attrs" do
336
+ assert_nil subject.name
390
337
 
391
- should "default its options" do
392
- config = Configuration.new
393
- assert_nil config.name
394
- assert_equal '0.0.0.0', config.ip
395
- assert_nil config.port
396
- assert_nil config.pid_file
338
+ exp = @config_class::DEFAULT_IP_ADDRESS
339
+ assert_equal exp, subject.ip
397
340
 
398
- assert_false config.receives_keep_alive
341
+ assert_nil subject.port
342
+ assert_nil subject.pid_file
343
+ assert_nil subject.shutdown_timeout
399
344
 
400
- assert_true config.verbose_logging
401
- assert_instance_of Sanford::NullLogger, config.logger
402
- assert_instance_of Sanford::NullTemplateSource, config.template_source
345
+ assert_equal DefaultWorker, subject.worker_class
403
346
 
404
- assert_equal DefaultWorker, config.worker_class
405
- assert_nil config.worker_params
406
- assert_equal Configuration::DEFAULT_NUM_WORKERS, config.num_workers
347
+ assert_nil subject.worker_params
407
348
 
408
- assert_nil config.shutdown_timeout
349
+ exp = @config_class::DEFAULT_NUM_WORKERS
350
+ assert_equal exp, subject.num_workers
409
351
 
410
- assert_equal [], config.init_procs
411
- assert_equal [], config.error_procs
352
+ assert_equal [], subject.init_procs
353
+ assert_equal [], subject.error_procs
412
354
 
413
- assert_instance_of Sanford::Router, config.router
414
- assert_empty config.router.routes
415
- end
355
+ assert_instance_of Sanford::NullTemplateSource, subject.template_source
356
+ assert_equal ENV['PWD'], subject.template_source.path
416
357
 
417
- should "not be valid by default" do
418
- assert_false subject.valid?
358
+ assert_instance_of Sanford::NullLogger, subject.logger
359
+ assert_instance_of Sanford::Router, subject.router
360
+
361
+ assert_equal false, subject.receives_keep_alive
362
+ assert_equal true, subject.verbose_logging
419
363
  end
420
364
 
421
- should "know its routes" do
422
- assert_equal subject.router.routes, subject.routes
423
- subject.router.service(Factory.string, TestHandler.to_s)
365
+ should "demeter its router" do
424
366
  assert_equal subject.router.routes, subject.routes
425
367
  end
426
368
 
427
- should "include its procs and router/routes in its `to_hash`" do
428
- config_hash = subject.to_hash
429
- assert_equal subject.worker_class, config_hash[:worker_class]
430
- assert_equal subject.worker_params, config_hash[:worker_params]
431
- assert_equal subject.num_workers, config_hash[:num_workers]
432
- assert_equal subject.shutdown_timeout, config_hash[:shutdown_timeout]
433
- assert_equal subject.init_procs, config_hash[:init_procs]
434
- assert_equal subject.error_procs, config_hash[:error_procs]
435
- assert_equal subject.router, config_hash[:router]
436
- assert_equal subject.routes, config_hash[:routes]
437
- end
369
+ should "not be valid until validate! has been run" do
370
+ assert_false subject.valid?
371
+
372
+ subject.name = Factory.string
373
+ subject.ip = Factory.string
374
+ subject.port = Factory.integer
438
375
 
439
- should "call its init procs when validated" do
440
- called = false
441
- subject.init_procs << proc{ called = true }
442
376
  subject.validate!
443
- assert_true called
377
+ assert_true subject.valid?
444
378
  end
445
379
 
446
- should "ensure its required options have been set when validated" do
447
- subject.name = nil
448
- assert_raises(InvalidError){ subject.validate! }
380
+ should "complain if validating and its name/ip/port is nil" do
449
381
  subject.name = Factory.string
382
+ subject.ip = Factory.string
383
+ subject.port = Factory.integer
450
384
 
451
- subject.ip = nil
385
+ a = [:name, :ip, :port].sample
386
+ subject.send("#{a}=", nil)
452
387
  assert_raises(InvalidError){ subject.validate! }
453
- subject.ip = Factory.string
388
+ end
454
389
 
455
- subject.port = nil
456
- assert_raises(InvalidError){ subject.validate! }
390
+ should "complain if validating and its worker class isn't a Worker" do
391
+ subject.name = Factory.string
392
+ subject.ip = Factory.string
457
393
  subject.port = Factory.integer
458
394
 
459
- assert_nothing_raised{ subject.validate! }
460
- end
461
-
462
- should "validate its worker class when validated" do
463
395
  subject.worker_class = Module.new
464
396
  assert_raises(InvalidError){ subject.validate! }
465
397
 
@@ -467,17 +399,44 @@ module Sanford::Server
467
399
  assert_raises(InvalidError){ subject.validate! }
468
400
  end
469
401
 
470
- should "validate its routes when validated" do
471
- subject.router.service(Factory.string, TestHandler.to_s)
472
- subject.routes.each{ |route| assert_nil route.handler_class }
402
+ end
403
+
404
+ class ValidationTests < ConfigTests
405
+ desc "when successfully validated"
406
+ setup do
407
+ @router = Sanford::Router.new
408
+ @router_validate_called = false
409
+ Assert.stub(@router, :validate!){ @router_validate_called = true }
410
+
411
+ @config = Config.new.tap do |c|
412
+ c.name = Factory.string
413
+ c.ip = Factory.string
414
+ c.port = Factory.integer
415
+ c.router = @router
416
+ end
417
+
418
+ @initialized = false
419
+ @config.init_procs << proc{ @initialized = true }
420
+
421
+ @other_initialized = false
422
+ @config.init_procs << proc{ @other_initialized = true }
423
+ end
424
+
425
+ should "call its init procs" do
426
+ assert_equal false, @initialized
427
+ assert_equal false, @other_initialized
428
+
473
429
  subject.validate!
474
- subject.routes.each{ |route| assert_not_nil route.handler_class }
430
+
431
+ assert_equal true, @initialized
432
+ assert_equal true, @other_initialized
475
433
  end
476
434
 
477
- should "be valid after being validated" do
478
- assert_false subject.valid?
435
+ should "call validate! on the router" do
436
+ assert_false @router_validate_called
437
+
479
438
  subject.validate!
480
- assert_true subject.valid?
439
+ assert_true @router_validate_called
481
440
  end
482
441
 
483
442
  should "only be able to be validated once" do
@@ -491,8 +450,6 @@ module Sanford::Server
491
450
 
492
451
  end
493
452
 
494
- TestHandler = Class.new
495
-
496
453
  class TCPServerSpy
497
454
  attr_reader :set_socket_option_calls
498
455