sanford 0.10.1 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. data/Gemfile +1 -1
  2. data/README.md +41 -56
  3. data/Rakefile +0 -1
  4. data/bench/client.rb +8 -3
  5. data/bench/{services.rb → config.sanford} +11 -6
  6. data/bench/{runner.rb → report.rb} +2 -2
  7. data/bench/report.txt +32 -32
  8. data/lib/sanford/cli.rb +42 -28
  9. data/lib/sanford/config_file.rb +79 -0
  10. data/lib/sanford/{worker.rb → connection_handler.rb} +28 -20
  11. data/lib/sanford/error_handler.rb +7 -7
  12. data/lib/sanford/pid_file.rb +42 -0
  13. data/lib/sanford/process.rb +136 -0
  14. data/lib/sanford/process_signal.rb +20 -0
  15. data/lib/sanford/route.rb +48 -0
  16. data/lib/sanford/router.rb +36 -0
  17. data/lib/sanford/runner.rb +30 -58
  18. data/lib/sanford/sanford_runner.rb +19 -9
  19. data/lib/sanford/server.rb +211 -42
  20. data/lib/sanford/server_data.rb +47 -0
  21. data/lib/sanford/service_handler.rb +8 -46
  22. data/lib/sanford/template_source.rb +19 -2
  23. data/lib/sanford/test_runner.rb +27 -28
  24. data/lib/sanford/version.rb +1 -1
  25. data/lib/sanford.rb +1 -23
  26. data/sanford.gemspec +4 -5
  27. data/test/helper.rb +3 -20
  28. data/test/support/app_server.rb +142 -0
  29. data/test/support/config.sanford +7 -0
  30. data/test/support/config_invalid_run.sanford +3 -0
  31. data/test/support/config_no_run.sanford +0 -0
  32. data/test/support/fake_server_connection.rb +58 -0
  33. data/test/support/pid_file_spy.rb +19 -0
  34. data/test/support/template.erb +1 -0
  35. data/test/system/server_tests.rb +378 -0
  36. data/test/system/service_handler_tests.rb +224 -0
  37. data/test/unit/cli_tests.rb +187 -0
  38. data/test/unit/config_file_tests.rb +59 -0
  39. data/test/unit/connection_handler_tests.rb +254 -0
  40. data/test/unit/error_handler_tests.rb +30 -35
  41. data/test/unit/pid_file_tests.rb +70 -0
  42. data/test/unit/process_signal_tests.rb +61 -0
  43. data/test/unit/process_tests.rb +428 -0
  44. data/test/unit/route_tests.rb +92 -0
  45. data/test/unit/router_tests.rb +65 -0
  46. data/test/unit/runner_tests.rb +61 -15
  47. data/test/unit/sanford_runner_tests.rb +162 -28
  48. data/test/unit/sanford_tests.rb +0 -8
  49. data/test/unit/server_data_tests.rb +87 -0
  50. data/test/unit/server_tests.rb +502 -21
  51. data/test/unit/service_handler_tests.rb +114 -219
  52. data/test/unit/template_engine_tests.rb +1 -1
  53. data/test/unit/template_source_tests.rb +56 -16
  54. data/test/unit/test_runner_tests.rb +206 -0
  55. metadata +67 -67
  56. data/bench/tasks.rb +0 -41
  57. data/lib/sanford/config.rb +0 -28
  58. data/lib/sanford/host.rb +0 -129
  59. data/lib/sanford/host_data.rb +0 -65
  60. data/lib/sanford/hosts.rb +0 -38
  61. data/lib/sanford/manager.rb +0 -275
  62. data/test/support/fake_connection.rb +0 -36
  63. data/test/support/helpers.rb +0 -17
  64. data/test/support/service_handlers.rb +0 -154
  65. data/test/support/services.rb +0 -123
  66. data/test/support/simple_client.rb +0 -62
  67. data/test/system/request_handling_tests.rb +0 -306
  68. data/test/unit/config_tests.rb +0 -56
  69. data/test/unit/host_data_tests.rb +0 -71
  70. data/test/unit/host_tests.rb +0 -141
  71. data/test/unit/hosts_tests.rb +0 -50
  72. data/test/unit/manager_tests.rb +0 -195
  73. data/test/unit/worker_tests.rb +0 -24
@@ -1,34 +1,80 @@
1
1
  require 'assert'
2
2
  require 'sanford/runner'
3
3
 
4
- require 'test/support/service_handlers'
4
+ require 'sanford/server_data'
5
+ require 'sanford/service_handler'
5
6
 
6
- module Sanford::Runner
7
+ class Sanford::Runner
7
8
 
8
9
  class UnitTests < Assert::Context
9
10
  desc "Sanford::Runner"
10
11
  setup do
11
- request = Sanford::Protocol::Request.new('test', {})
12
- @runner_class = Class.new do
13
- include Sanford::Runner
14
- end
15
- @runner = @runner_class.new(BasicServiceHandler, request)
12
+ @handler_class = TestServiceHandler
13
+
14
+ @runner_class = Sanford::Runner
15
+ end
16
+ subject{ @runner_class }
17
+
18
+ end
19
+
20
+ class InitTests < UnitTests
21
+ desc "when init"
22
+ setup do
23
+ @runner = @runner_class.new(@handler_class)
16
24
  end
17
25
  subject{ @runner }
18
26
 
19
- should have_cmeths :run
20
- should have_readers :handler_class, :request, :logger, :handler
21
- should have_imeths :init, :init!, :run, :run!
22
- should have_imeths :halt, :catch_halt
27
+ should have_readers :handler_class, :handler
28
+ should have_readers :request, :params, :logger, :template_source
29
+ should have_imeths :run
30
+ should have_imeths :halt
23
31
 
24
- should "not implement the run behavior" do
25
- assert_raises NotImplementedError do
26
- subject.run
32
+ should "know its handler class and handler" do
33
+ assert_equal @handler_class, subject.handler_class
34
+ assert_instance_of @handler_class, subject.handler
35
+ end
36
+
37
+ should "not set its request, params, logger or template source" do
38
+ assert_nil subject.request
39
+ assert_nil subject.params
40
+ assert_nil subject.logger
41
+ assert_nil subject.template_source
42
+ end
43
+
44
+ should "throw halt with response args using `halt`" do
45
+ code = Factory.integer
46
+ message = Factory.string
47
+ data = Factory.string
48
+
49
+ result = catch(:halt) do
50
+ subject.halt(code, :message => message, :data => data)
27
51
  end
52
+ assert_instance_of ResponseArgs, result
53
+ assert_equal [ code, message ], result.status
54
+ assert_equal data, result.data
55
+ end
56
+
57
+ should "accept string keys using `halt`" do
58
+ code = Factory.integer
59
+ message = Factory.string
60
+ data = Factory.string
61
+
62
+ result = catch(:halt) do
63
+ subject.halt(code, 'message' => message, 'data' => data)
64
+ end
65
+ assert_instance_of ResponseArgs, result
66
+ assert_equal [ code, message ], result.status
67
+ assert_equal data, result.data
68
+ end
69
+
70
+ should "raise a not implemented error when run" do
71
+ assert_raises(NotImplementedError){ subject.run }
28
72
  end
29
73
 
30
74
  end
31
75
 
32
- # runner behavior tests are handled via system tests
76
+ class TestServiceHandler
77
+ include Sanford::ServiceHandler
78
+ end
33
79
 
34
80
  end
@@ -1,29 +1,29 @@
1
1
  require 'assert'
2
2
  require 'sanford/sanford_runner'
3
3
 
4
- require 'sanford/runner'
5
- require 'test/support/service_handlers'
4
+ require 'sanford/server_data'
5
+ require 'sanford/service_handler'
6
6
 
7
7
  class Sanford::SanfordRunner
8
8
 
9
9
  class UnitTests < Assert::Context
10
10
  desc "Sanford::SanfordRunner"
11
11
  setup do
12
+ @handler_class = TestServiceHandler
13
+ @request = Sanford::Protocol::Request.new(Factory.string, {
14
+ :something => Factory.string
15
+ })
16
+ @server_data = Sanford::ServerData.new({
17
+ :logger => Factory.string,
18
+ :template_source => Factory.string
19
+ })
20
+
12
21
  @runner_class = Sanford::SanfordRunner
13
22
  end
14
23
  subject{ @runner_class }
15
24
 
16
- should "be a Runner" do
17
- assert_includes Sanford::Runner, subject
18
- end
19
-
20
- should "be able to build a runner with a handler class and params and run it" do
21
- response = nil
22
- assert_nothing_raised do
23
- response = subject.run(BasicServiceHandler, {})
24
- end
25
-
26
- assert_equal 200, response.code
25
+ should "be a runner" do
26
+ assert_true subject < Sanford::Runner
27
27
  end
28
28
 
29
29
  end
@@ -31,36 +31,170 @@ class Sanford::SanfordRunner
31
31
  class InitTests < UnitTests
32
32
  desc "when init"
33
33
  setup do
34
- @request = Sanford::Protocol::Request.new('test', {})
35
- @runner = @runner_class.new(BasicServiceHandler, @request)
34
+ @runner = @runner_class.new(@handler_class, @request, @server_data)
36
35
  end
37
36
  subject{ @runner }
38
37
 
39
- should "run the handler and return the response it generates when run" do
40
- response = subject.run
38
+ should "know its request, params, logger and template source" do
39
+ assert_equal @request, subject.request
40
+ assert_equal @request.params, subject.params
41
+ assert_equal @server_data.logger, subject.logger
42
+ assert_equal @server_data.template_source, subject.template_source
43
+ end
44
+
45
+ end
46
+
47
+ class RunTests < InitTests
48
+ desc "and run"
49
+ setup do
50
+ @handler = @runner.handler
51
+ @response = @runner.run
52
+ end
53
+ subject{ @response }
54
+
55
+ should "run the handlers before callbacks" do
56
+ assert_equal 1, @handler.first_before_call_order
57
+ assert_equal 2, @handler.second_before_call_order
58
+ end
59
+
60
+ should "run the handlers init" do
61
+ assert_equal 3, @handler.init_call_order
62
+ end
63
+
64
+ should "run the handlers run and use its result to build a response" do
65
+ assert_equal 4, @handler.run_call_order
66
+ assert_instance_of Sanford::Protocol::Response, subject
67
+ assert_equal @handler.response_data, subject.data
68
+ end
41
69
 
42
- assert_instance_of Sanford::Protocol::Response, response
43
- assert_equal 200, response.code
44
- assert_equal 'Joe Test', response.data['name']
45
- assert_equal 'joe.test@example.com', response.data['email']
70
+ should "run the handlers after callbacks" do
71
+ assert_equal 5, @handler.first_after_call_order
72
+ assert_equal 6, @handler.second_after_call_order
46
73
  end
47
74
 
48
75
  end
49
76
 
50
- class CallbackTests < InitTests
77
+ class RunHaltInBeforeTests < UnitTests
78
+ desc "running a handler that halts in a before callback"
51
79
  setup do
52
- @runner = @runner_class.new(FlagServiceHandler, @request)
80
+ req = Sanford::Protocol::Request.new(Factory.string, 'halt' => 'before')
81
+ runner = @runner_class.new(@handler_class, req, @server_data).tap(&:run)
82
+ @handler = runner.handler
83
+ end
84
+ subject{ @handler }
85
+
86
+ should "stop processing when the halt is called" do
87
+ assert_not_nil subject.first_before_call_order
88
+ assert_nil subject.second_before_call_order
89
+ assert_nil subject.init_call_order
90
+ assert_nil subject.run_call_order
91
+ assert_nil subject.first_after_call_order
92
+ assert_nil subject.second_after_call_order
53
93
  end
54
94
 
55
- should "call handler `before` and `after` callbacks when run" do
56
- subject.run
95
+ end
57
96
 
58
- assert_true subject.handler.before_called
59
- assert_true subject.handler.after_called
97
+ class RunHandlerHaltInitTests < UnitTests
98
+ desc "running a handler that halts in init"
99
+ setup do
100
+ req = Sanford::Protocol::Request.new(Factory.string, 'halt' => 'init')
101
+ runner = @runner_class.new(@handler_class, req, @server_data).tap(&:run)
102
+ @handler = runner.handler
103
+ end
104
+ subject{ @handler }
105
+
106
+ should "stop processing when the halt is called" do
107
+ assert_not_nil subject.first_before_call_order
108
+ assert_not_nil subject.second_before_call_order
109
+ assert_not_nil subject.init_call_order
110
+ assert_nil subject.run_call_order
111
+ assert_nil subject.first_after_call_order
112
+ assert_nil subject.second_after_call_order
60
113
  end
61
114
 
62
115
  end
63
116
 
64
- # live runner behavior tests are handled via system tests
117
+ class RunHandlerHaltRunTests < UnitTests
118
+ desc "running a handler that halts in run"
119
+ setup do
120
+ req = Sanford::Protocol::Request.new(Factory.string, 'halt' => 'run')
121
+ runner = @runner_class.new(@handler_class, req, @server_data).tap(&:run)
122
+ @handler = runner.handler
123
+ end
124
+ subject{ @handler }
125
+
126
+ should "stop processing when the halt is called" do
127
+ assert_not_nil subject.first_before_call_order
128
+ assert_not_nil subject.second_before_call_order
129
+ assert_not_nil subject.init_call_order
130
+ assert_not_nil subject.run_call_order
131
+ assert_nil subject.first_after_call_order
132
+ assert_nil subject.second_after_call_order
133
+ end
134
+
135
+ end
136
+
137
+ class RunHandlerHaltAfterTests < UnitTests
138
+ desc "running a handler that halts in a after callback"
139
+ setup do
140
+ req = Sanford::Protocol::Request.new(Factory.string, 'halt' => 'after')
141
+ runner = @runner_class.new(@handler_class, req, @server_data).tap(&:run)
142
+ @handler = runner.handler
143
+ end
144
+ subject{ @handler }
145
+
146
+ should "stop processing when the halt is called" do
147
+ assert_not_nil subject.first_before_call_order
148
+ assert_not_nil subject.second_before_call_order
149
+ assert_not_nil subject.init_call_order
150
+ assert_not_nil subject.run_call_order
151
+ assert_not_nil subject.first_after_call_order
152
+ assert_nil subject.second_after_call_order
153
+ end
154
+
155
+ end
156
+
157
+ class TestServiceHandler
158
+ include Sanford::ServiceHandler
159
+
160
+ attr_reader :first_before_call_order, :second_before_call_order
161
+ attr_reader :first_after_call_order, :second_after_call_order
162
+ attr_reader :init_call_order, :run_call_order
163
+ attr_reader :response_data
164
+
165
+ before do
166
+ @first_before_call_order = next_call_order
167
+ halt_if('before')
168
+ end
169
+ before{ @second_before_call_order = next_call_order }
170
+
171
+ after do
172
+ @first_after_call_order = next_call_order
173
+ halt_if('after')
174
+ end
175
+ after{ @second_after_call_order = next_call_order }
176
+
177
+ def init!
178
+ @init_call_order = next_call_order
179
+ halt_if('init')
180
+ end
181
+
182
+ def run!
183
+ @run_call_order = next_call_order
184
+ halt_if('run')
185
+ @response_data ||= Factory.string
186
+ end
187
+
188
+ private
189
+
190
+ def next_call_order
191
+ @order ||= 0
192
+ @order += 1
193
+ end
194
+
195
+ def halt_if(value)
196
+ halt Factory.integer if params['halt'] == value
197
+ end
198
+ end
65
199
 
66
200
  end
@@ -1,20 +1,12 @@
1
1
  require 'assert'
2
2
  require 'sanford'
3
3
 
4
- require 'sanford/config'
5
-
6
4
  module Sanford
7
5
 
8
6
  class UnitTests < Assert::Context
9
7
  desc "Sanford"
10
8
  subject{ Sanford }
11
9
 
12
- should have_imeths :config, :configure, :init, :register, :hosts
13
-
14
- should "return a `Config` instance with the `config` method" do
15
- assert_kind_of Sanford::Config, subject.config
16
- end
17
-
18
10
  end
19
11
 
20
12
  end
@@ -0,0 +1,87 @@
1
+ require 'assert'
2
+ require 'sanford/server_data'
3
+
4
+ require 'sanford/route'
5
+
6
+ class Sanford::ServerData
7
+
8
+ class UnitTests < Assert::Context
9
+ desc "Sanford::ServerData"
10
+ setup do
11
+ @name = Factory.string
12
+ @ip = Factory.string
13
+ @port = Factory.integer
14
+ @pid_file = Factory.file_path
15
+ @logger = Factory.string
16
+ @verbose_logging = Factory.boolean
17
+ @receives_keep_alive = Factory.boolean
18
+ @error_procs = [ proc{ } ]
19
+ @route = Sanford::Route.new(Factory.string, TestHandler.to_s).tap(&:validate!)
20
+
21
+ @server_data = Sanford::ServerData.new({
22
+ :name => @name,
23
+ :ip => @ip,
24
+ :port => @port,
25
+ :pid_file => @pid_file,
26
+ :logger => @logger,
27
+ :verbose_logging => @verbose_logging,
28
+ :receives_keep_alive => @receives_keep_alive,
29
+ :error_procs => @error_procs,
30
+ :routes => [ @route ]
31
+ })
32
+ end
33
+ subject{ @server_data }
34
+
35
+ should have_readers :name
36
+ should have_readers :ip, :port
37
+ should have_readers :pid_file
38
+ should have_readers :logger, :verbose_logging
39
+ should have_readers :receives_keep_alive
40
+ should have_readers :error_procs
41
+ should have_readers :routes
42
+
43
+ should "know its attributes" do
44
+ assert_equal @name, subject.name
45
+ assert_equal @ip, subject.ip
46
+ assert_equal @port, subject.port
47
+ assert_equal @pid_file, subject.pid_file
48
+ assert_equal @logger, subject.logger
49
+ assert_equal @verbose_logging, subject.verbose_logging
50
+ assert_equal @receives_keep_alive, subject.receives_keep_alive
51
+ assert_equal @error_procs, subject.error_procs
52
+ end
53
+
54
+ should "build a routes lookup hash" do
55
+ expected = { @route.name => @route }
56
+ assert_equal expected, subject.routes
57
+ end
58
+
59
+ should "allow lookup a route using `route_for`" do
60
+ route = subject.route_for(@route.name)
61
+ assert_equal @route, route
62
+ end
63
+
64
+ should "raise a not found error using `route_for` with an invalid name" do
65
+ assert_raises(Sanford::NotFoundError) do
66
+ subject.route_for(Factory.string)
67
+ end
68
+ end
69
+
70
+ should "default its attributes when they aren't provided" do
71
+ server_data = Sanford::ServerData.new
72
+ assert_nil server_data.name
73
+ assert_nil server_data.ip
74
+ assert_nil server_data.port
75
+ assert_nil server_data.pid_file
76
+ assert_nil server_data.logger
77
+ assert_false server_data.verbose_logging
78
+ assert_false server_data.receives_keep_alive
79
+ assert_equal [], server_data.error_procs
80
+ assert_equal({}, server_data.routes)
81
+ end
82
+
83
+ end
84
+
85
+ TestHandler = Class.new
86
+
87
+ end