sanford 0.15.0 → 0.15.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -79,7 +79,7 @@ module Sanford
79
79
  end
80
80
 
81
81
  def listen(*args)
82
- args = [ @server_data.ip, @server_data.port ] if args.empty?
82
+ args = [@server_data.ip, @server_data.port] if args.empty?
83
83
  @dat_tcp_server.listen(*args) do |server_socket|
84
84
  configure_tcp_server(server_socket)
85
85
  end
@@ -274,7 +274,7 @@ module Sanford
274
274
  include NsOptions::Proxy
275
275
 
276
276
  option :name, String, :required => true
277
- option :ip, String, :required => true
277
+ option :ip, String, :required => true, :default => '0.0.0.0'
278
278
  option :port, Integer, :required => true
279
279
  option :pid_file, Pathname
280
280
 
@@ -291,8 +291,6 @@ module Sanford
291
291
 
292
292
  def initialize(values = nil)
293
293
  super(values)
294
- self.ip = !(v = ENV['SANFORD_IP'].to_s).empty? ? v : '0.0.0.0'
295
- self.port = !(v = ENV['SANFORD_PORT'].to_s).empty? ? v : nil
296
294
  @init_procs, @error_procs = [], []
297
295
  @worker_start_procs, @worker_shutdown_procs = [], []
298
296
  @worker_sleep_procs, @worker_wakeup_procs = [], []
@@ -20,8 +20,8 @@ module Sanford
20
20
  def initialize(args = nil)
21
21
  args ||= {}
22
22
  @name = args[:name]
23
- @ip = args[:ip]
24
- @port = args[:port]
23
+ @ip = !(v = ENV['SANFORD_IP'].to_s).empty? ? v : args[:ip]
24
+ @port = !(v = ENV['SANFORD_PORT'].to_s).empty? ? v.to_i : args[:port]
25
25
  @pid_file = args[:pid_file]
26
26
 
27
27
  @receives_keep_alive = !!args[:receives_keep_alive]
@@ -1,3 +1,3 @@
1
1
  module Sanford
2
- VERSION = "0.15.0"
2
+ VERSION = "0.15.1"
3
3
  end
@@ -7,7 +7,8 @@ module Factory
7
7
  klass ||= StandardError
8
8
  message ||= Factory.text
9
9
  exception = nil
10
- begin; raise(klass, message); rescue StandardError => exception; end
10
+ begin; raise(klass, message); rescue klass => exception; end
11
+ exception.set_backtrace(nil) if Factory.boolean
11
12
  exception
12
13
  end
13
14
 
@@ -23,21 +23,28 @@ module Sanford::Server
23
23
  # results
24
24
  @current_sanford_debug = ENV['SANFORD_DEBUG']
25
25
  ENV.delete('SANFORD_DEBUG')
26
+ end
27
+ teardown do
28
+ ENV['SANFORD_DEBUG'] = @current_sanford_debug
29
+ ENV['SANFORD_PROTOCOL_DEBUG'] = @current_sanford_protocol_debug
30
+ end
31
+
32
+ end
26
33
 
34
+ class RunningAppServerTests < SystemTests
35
+ setup do
27
36
  @server = AppServer.new
28
37
  @client = TestClient.new(AppServer.ip, AppServer.port)
29
38
  @server_runner = ServerRunner.new(@server).tap(&:start)
30
39
  end
31
40
  teardown do
32
41
  @server_runner.stop
33
- ENV['SANFORD_DEBUG'] = @current_sanford_debug
34
- ENV['SANFORD_PROTOCOL_DEBUG'] = @current_sanford_protocol_debug
35
42
  end
36
43
  subject{ @response }
37
44
 
38
45
  end
39
46
 
40
- class SuccessTests < SystemTests
47
+ class SuccessTests < RunningAppServerTests
41
48
  desc "calling a service"
42
49
  setup do
43
50
  @message = Factory.string
@@ -53,7 +60,7 @@ module Sanford::Server
53
60
 
54
61
  end
55
62
 
56
- class BadProtocolVersionTests < SystemTests
63
+ class BadProtocolVersionTests < RunningAppServerTests
57
64
  desc "calling a server with an invalid protocol version"
58
65
  setup do
59
66
  @client.bytes = "\000"
@@ -68,7 +75,7 @@ module Sanford::Server
68
75
 
69
76
  end
70
77
 
71
- class BadMessageTests < SystemTests
78
+ class BadMessageTests < RunningAppServerTests
72
79
  desc "calling a server with an invalid message size"
73
80
  setup do
74
81
  @client.bytes = [ Sanford::Protocol.msg_version, "\000" ].join
@@ -83,7 +90,7 @@ module Sanford::Server
83
90
 
84
91
  end
85
92
 
86
- class BadBodyTests < SystemTests
93
+ class BadBodyTests < RunningAppServerTests
87
94
  desc "calling a server with an invalid message body"
88
95
  setup do
89
96
  # these are a special set of bytes that cause BSON to throw an exception
@@ -99,7 +106,7 @@ module Sanford::Server
99
106
 
100
107
  end
101
108
 
102
- class NotFoundTests < SystemTests
109
+ class NotFoundTests < RunningAppServerTests
103
110
  desc "with a request for a service the server doesn't provide"
104
111
  setup do
105
112
  @client.set_request('doesnt_exist')
@@ -114,7 +121,7 @@ module Sanford::Server
114
121
 
115
122
  end
116
123
 
117
- class ServerErrorTests < SystemTests
124
+ class ServerErrorTests < RunningAppServerTests
118
125
  desc "with a request that causes a server error"
119
126
  setup do
120
127
  @client.set_request('raise')
@@ -129,7 +136,7 @@ module Sanford::Server
129
136
 
130
137
  end
131
138
 
132
- class BadRequestTests < SystemTests
139
+ class BadRequestTests < RunningAppServerTests
133
140
  desc "calling a server with an invalid request"
134
141
  setup do
135
142
  @client.set_msg_body({})
@@ -144,7 +151,7 @@ module Sanford::Server
144
151
 
145
152
  end
146
153
 
147
- class BadResponseTests < SystemTests
154
+ class BadResponseTests < RunningAppServerTests
148
155
  desc "calling a service that builds an invalid response"
149
156
  setup do
150
157
  @client.set_request('bad_response')
@@ -159,7 +166,7 @@ module Sanford::Server
159
166
 
160
167
  end
161
168
 
162
- class TemplateTests < SystemTests
169
+ class TemplateTests < RunningAppServerTests
163
170
  desc "calling a service that renders a template"
164
171
  setup do
165
172
  @message = Factory.text
@@ -175,7 +182,7 @@ module Sanford::Server
175
182
 
176
183
  end
177
184
 
178
- class KeepAliveTests < SystemTests
185
+ class KeepAliveTests < RunningAppServerTests
179
186
  desc "receiving a keep-alive connection"
180
187
  setup do
181
188
  # no bytes means our client won't write any, this mimics a keep-alive
@@ -190,7 +197,7 @@ module Sanford::Server
190
197
 
191
198
  end
192
199
 
193
- class TimeoutErrorTests < SystemTests
200
+ class TimeoutErrorTests < RunningAppServerTests
194
201
  desc "with a client that connects but doesn't send anything"
195
202
  setup do
196
203
  @current_sanford_timeout = ENV['SANFORD_TIMEOUT']
@@ -215,7 +222,7 @@ module Sanford::Server
215
222
 
216
223
  end
217
224
 
218
- class HaltTests < SystemTests
225
+ class HaltTests < RunningAppServerTests
219
226
 
220
227
  should "allow halting in a before callback" do
221
228
  @client.set_request('halt', :when => 'before')
@@ -291,7 +298,7 @@ module Sanford::Server
291
298
 
292
299
  end
293
300
 
294
- class ErrorHandlerResponseTests < SystemTests
301
+ class ErrorHandlerResponseTests < RunningAppServerTests
295
302
  desc "calling a service that triggers an error handler"
296
303
  setup do
297
304
  @client.set_request('custom_error')
@@ -308,6 +315,33 @@ module Sanford::Server
308
315
 
309
316
  end
310
317
 
318
+ class WithEnvIpAndPortTests < SystemTests
319
+ desc "with an env var ip and port"
320
+ setup do
321
+ # get our current ip address, need something different than 0.0.0.0 and
322
+ # 127.0.0.1 to bind to
323
+ ENV['SANFORD_IP'] = IPSocket.getaddress(Socket.gethostname)
324
+ ENV['SANFORD_PORT'] = (AppServer.port + 1).to_s
325
+
326
+ @server = AppServer.new
327
+ @client = TestClient.new(ENV['SANFORD_IP'], ENV['SANFORD_PORT'])
328
+ @server_runner = ServerRunner.new(@server).tap(&:start)
329
+ end
330
+ teardown do
331
+ @server_runner.stop
332
+ ENV.delete('SANFORD_IP')
333
+ ENV.delete('SANFORD_PORT')
334
+ end
335
+
336
+ should "run the server on the env var ip and port" do
337
+ @client.set_request('echo', :message => Factory.string)
338
+ response = nil
339
+ assert_nothing_raised{ response = @client.call }
340
+ assert_equal 200, response.code
341
+ end
342
+
343
+ end
344
+
311
345
  class TestClient
312
346
  attr_accessor :delay, :bytes
313
347
 
@@ -223,23 +223,6 @@ class Sanford::Process
223
223
 
224
224
  end
225
225
 
226
- class RunWithIPAndPortTests < RunSetupTests
227
- desc "with a custom IP and port is run"
228
- setup do
229
- ENV['SANFORD_IP'] = Factory.string
230
- ENV['SANFORD_PORT'] = Factory.integer.to_s
231
- @process = @process_class.new(@server_spy)
232
- @process.run
233
- end
234
-
235
- should "have used the custom IP and port when listening" do
236
- assert_true @server_spy.listen_called
237
- expected = [ @process.server_ip, @process.server_port ]
238
- assert_equal expected, @server_spy.listen_args
239
- end
240
-
241
- end
242
-
243
226
  class RunWithServerFDTests < RunSetupTests
244
227
  desc "with a server file descriptor is run"
245
228
  setup do
@@ -8,46 +8,35 @@ class Sanford::ServerData
8
8
  class UnitTests < Assert::Context
9
9
  desc "Sanford::ServerData"
10
10
  setup do
11
- @name = Factory.string
12
- @ip = Factory.string
13
- @port = Factory.integer
14
- @pid_file = Factory.file_path
15
-
16
- @receives_keep_alive = Factory.boolean
17
-
18
- @verbose_logging = Factory.boolean
19
- @logger = Factory.string
20
- @template_source = Factory.string
21
-
22
- @init_procs = Factory.integer(3).times.map{ proc{} }
23
- @error_procs = Factory.integer(3).times.map{ proc{} }
24
-
25
- @start_procs = Factory.integer(3).times.map{ proc{} }
26
- @shutdown_procs = Factory.integer(3).times.map{ proc{} }
27
- @sleep_procs = Factory.integer(3).times.map{ proc{} }
28
- @wakeup_procs = Factory.integer(3).times.map{ proc{} }
29
-
30
- @router = Factory.string
31
- @route = Sanford::Route.new(Factory.string, TestHandler.to_s).tap(&:validate!)
32
-
33
- @server_data = Sanford::ServerData.new({
34
- :name => @name,
35
- :ip => @ip,
36
- :port => @port,
37
- :pid_file => @pid_file,
38
- :receives_keep_alive => @receives_keep_alive,
39
- :verbose_logging => @verbose_logging,
40
- :logger => @logger,
41
- :template_source => @template_source,
42
- :init_procs => @init_procs,
43
- :error_procs => @error_procs,
44
- :worker_start_procs => @start_procs,
45
- :worker_shutdown_procs => @shutdown_procs,
46
- :worker_sleep_procs => @sleep_procs,
47
- :worker_wakeup_procs => @wakeup_procs,
48
- :router => @router,
11
+ @orig_ip_env_var = ENV['SANFORD_IP']
12
+ @orig_port_env_var = ENV['SANFORD_PORT']
13
+ ENV.delete('SANFORD_IP')
14
+ ENV.delete('SANFORD_PORT')
15
+
16
+ @route = Sanford::Route.new(Factory.string, TestHandler.to_s).tap(&:validate!)
17
+ @config_hash = {
18
+ :name => Factory.string,
19
+ :ip => Factory.string,
20
+ :port => Factory.integer,
21
+ :pid_file => Factory.file_path,
22
+ :receives_keep_alive => Factory.boolean,
23
+ :verbose_logging => Factory.boolean,
24
+ :logger => Factory.string,
25
+ :template_source => Factory.string,
26
+ :init_procs => Factory.integer(3).times.map{ proc{} },
27
+ :error_procs => Factory.integer(3).times.map{ proc{} },
28
+ :worker_start_procs => Factory.integer(3).times.map{ proc{} },
29
+ :worker_shutdown_procs => Factory.integer(3).times.map{ proc{} },
30
+ :worker_sleep_procs => Factory.integer(3).times.map{ proc{} },
31
+ :worker_wakeup_procs => Factory.integer(3).times.map{ proc{} },
32
+ :router => Factory.string,
49
33
  :routes => [@route]
50
- })
34
+ }
35
+ @server_data = Sanford::ServerData.new(@config_hash)
36
+ end
37
+ teardown do
38
+ ENV['SANFORD_IP'] = @orig_ip_env_var
39
+ ENV['SANFORD_PORT'] = @orig_port_env_var
51
40
  end
52
41
  subject{ @server_data }
53
42
 
@@ -62,26 +51,41 @@ class Sanford::ServerData
62
51
  should have_accessors :ip, :port
63
52
 
64
53
  should "know its attributes" do
65
- assert_equal @name, subject.name
66
- assert_equal @ip, subject.ip
67
- assert_equal @port, subject.port
68
- assert_equal @pid_file, subject.pid_file
54
+ h = @config_hash
55
+ assert_equal h[:name], subject.name
56
+ assert_equal h[:ip], subject.ip
57
+ assert_equal h[:port], subject.port
58
+ assert_equal h[:pid_file], subject.pid_file
59
+
60
+ assert_equal h[:receives_keep_alive], subject.receives_keep_alive
69
61
 
70
- assert_equal @receives_keep_alive, subject.receives_keep_alive
62
+ assert_equal h[:verbose_logging], subject.verbose_logging
63
+ assert_equal h[:logger], subject.logger
64
+ assert_equal h[:template_source], subject.template_source
71
65
 
72
- assert_equal @verbose_logging, subject.verbose_logging
73
- assert_equal @logger, subject.logger
74
- assert_equal @template_source, subject.template_source
66
+ assert_equal h[:init_procs], subject.init_procs
67
+ assert_equal h[:error_procs], subject.error_procs
75
68
 
76
- assert_equal @init_procs, subject.init_procs
77
- assert_equal @error_procs, subject.error_procs
69
+ assert_equal h[:worker_start_procs], subject.worker_start_procs
70
+ assert_equal h[:worker_shutdown_procs], subject.worker_shutdown_procs
71
+ assert_equal h[:worker_sleep_procs], subject.worker_sleep_procs
72
+ assert_equal h[:worker_wakeup_procs], subject.worker_wakeup_procs
78
73
 
79
- assert_equal @start_procs, subject.worker_start_procs
80
- assert_equal @shutdown_procs, subject.worker_shutdown_procs
81
- assert_equal @sleep_procs, subject.worker_sleep_procs
82
- assert_equal @wakeup_procs, subject.worker_wakeup_procs
74
+ assert_equal h[:router], subject.router
75
+ end
83
76
 
84
- assert_equal @router, subject.router
77
+ should "use ip and port env vars if they are set" do
78
+ ENV['SANFORD_IP'] = Factory.string
79
+ ENV['SANFORD_PORT'] = Factory.integer.to_s
80
+ server_data = Sanford::ServerData.new(@config_hash)
81
+ assert_equal ENV['SANFORD_IP'], server_data.ip
82
+ assert_equal ENV['SANFORD_PORT'].to_i, server_data.port
83
+
84
+ ENV['SANFORD_IP'] = ""
85
+ ENV['SANFORD_PORT'] = ""
86
+ server_data = Sanford::ServerData.new(@config_hash)
87
+ assert_equal @config_hash[:ip], server_data.ip
88
+ assert_equal @config_hash[:port], server_data.port
85
89
  end
86
90
 
87
91
  should "build a routes lookup hash" do
@@ -439,21 +439,12 @@ module Sanford::Server
439
439
 
440
440
  desc "Configuration"
441
441
  setup do
442
- @orig_ip_env_var = ENV['SANFORD_IP']
443
- @orig_port_env_var = ENV['SANFORD_PORT']
444
- ENV.delete('SANFORD_IP')
445
- ENV.delete('SANFORD_PORT')
446
-
447
442
  @configuration = Configuration.new.tap do |c|
448
443
  c.name Factory.string
449
444
  c.ip Factory.string
450
445
  c.port Factory.integer
451
446
  end
452
447
  end
453
- teardown do
454
- ENV['SANFORD_IP'] = @orig_ip_env_var
455
- ENV['SANFORD_PORT'] = @orig_port_env_var
456
- end
457
448
  subject{ @configuration }
458
449
 
459
450
  should have_options :name, :ip, :port, :pid_file
@@ -497,15 +488,6 @@ module Sanford::Server
497
488
  assert_empty config.router.routes
498
489
  end
499
490
 
500
- should "use env vars for its options if they are set" do
501
- ENV['SANFORD_IP'] = Factory.string
502
- ENV['SANFORD_PORT'] = Factory.integer.to_s
503
-
504
- config = Configuration.new
505
- assert_equal ENV['SANFORD_IP'], config.ip
506
- assert_equal ENV['SANFORD_PORT'].to_i, config.port
507
- end
508
-
509
491
  should "not be valid by default" do
510
492
  assert_false subject.valid?
511
493
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sanford
3
3
  version: !ruby/object:Gem::Version
4
- hash: 35
4
+ hash: 33
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 15
9
- - 0
10
- version: 0.15.0
9
+ - 1
10
+ version: 0.15.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Collin Redding
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2015-09-10 00:00:00 Z
19
+ date: 2015-11-04 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  requirement: &id001 !ruby/object:Gem::Requirement