sanford 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,142 @@
1
+ require 'assert'
2
+
3
+ module Sanford::Host
4
+
5
+ class BaseTest < Assert::Context
6
+ desc "Sanford::Host"
7
+ setup do
8
+ Test::Environment.store_and_clear_hosts
9
+ @host_class = Class.new do
10
+ include Sanford::Host
11
+ name 'anonymous_host'
12
+ ip 'anonymous.local'
13
+ end
14
+ @host = @host_class.new({ :port => 12345 })
15
+ end
16
+ teardown do
17
+ Test::Environment.restore_hosts
18
+ end
19
+ subject{ @host }
20
+
21
+ should have_instance_methods :name, :config, :run
22
+
23
+ should "set name to it's class #name" do
24
+ assert_equal subject.class.name, subject.name
25
+ end
26
+
27
+ should "proxy missing methods to it's config" do
28
+ assert_equal subject.config.port, subject.port
29
+ assert subject.respond_to?(:pid_dir)
30
+ end
31
+
32
+ should "default it's configuration from the class and overwrite with values passed to new" do
33
+ assert_equal 'anonymous.local', subject.config.ip
34
+ assert_equal 12345, subject.config.port
35
+ end
36
+ end
37
+
38
+ class ConfigTest < BaseTest
39
+ desc "config"
40
+ setup do
41
+ @config = @host.config
42
+ end
43
+ subject{ @config }
44
+
45
+ should have_instance_methods :name, :ip, :port, :pid_dir, :logger
46
+ end
47
+
48
+ class ClassMethodsTest < BaseTest
49
+ desc "class methods"
50
+ subject{ @host_class }
51
+
52
+ should have_instance_methods :config, :version
53
+ should have_instance_methods :name, :ip, :port, :pid_dir, :logger
54
+
55
+ should "have registered the class with sanford's known hosts" do
56
+ assert_includes subject, Sanford.config.hosts
57
+ end
58
+ end
59
+
60
+ class InvalidTest < BaseTest
61
+ desc "invalid configuration"
62
+ setup do
63
+ @host_class = Class.new do
64
+ include Sanford::Host
65
+ name 'invalid_host'
66
+ end
67
+ end
68
+
69
+ should "raise a custom exception" do
70
+ assert_raises(Sanford::InvalidHostError) do
71
+ @host_class.new
72
+ end
73
+ end
74
+ end
75
+
76
+ class VersionTest < BaseTest
77
+ desc "version class method"
78
+ setup do
79
+ @host_class.version('v1'){ }
80
+ end
81
+
82
+ should "have added a version hash to the versioned_services config" do
83
+ assert_equal({}, @host_class.config.versioned_services["v1"])
84
+ end
85
+ end
86
+
87
+ class RunTest < BaseTest
88
+ desc "run method"
89
+ setup do
90
+ @host_class.version('v1') do
91
+ service 'test', 'DummyHost::Multiply'
92
+ end
93
+ @host = @host_class.new({ :port => 12000 })
94
+ @request = Sanford::Protocol::Request.new('v1', 'test', { 'number' => 2 })
95
+ @returned = @host.run(@request)
96
+ end
97
+ subject{ @returned }
98
+
99
+ should "have returned the data of calling the `init` and `run` method of the handler" do
100
+ expected_status_code = 200
101
+ expected_data = 2 * @request.params['number']
102
+
103
+ assert_equal expected_status_code, subject.first
104
+ assert_equal expected_data, subject.last
105
+ end
106
+ end
107
+
108
+ class RunNotFoundTest < BaseTest
109
+ desc "run method with a service and no matching service handler"
110
+ setup do
111
+ @host_class.version('v1') do
112
+ service 'test', 'DummyHost::Echo'
113
+ end
114
+ @host = @host_class.new({ :port => 12000 })
115
+ @request = Sanford::Protocol::Request.new('v4', 'what', {})
116
+ end
117
+
118
+ should "raise a Sanford::NotFound exception" do
119
+ assert_raises(Sanford::NotFoundError) do
120
+ @host.run(@request)
121
+ end
122
+ end
123
+ end
124
+
125
+ class RunNoHandlerClassTest < BaseTest
126
+ desc "run method with a service handler that doesn't exist"
127
+ setup do
128
+ @host_class.version('v1') do
129
+ service 'test', 'DoesntExist::AtAll'
130
+ end
131
+ @host = @host_class.new({ :port => 12000 })
132
+ @request = Sanford::Protocol::Request.new('v1', 'test', {})
133
+ end
134
+
135
+ should "raise a Sanford::NoHandlerClass exception" do
136
+ assert_raises(Sanford::NoHandlerClassError) do
137
+ @host.run(@request)
138
+ end
139
+ end
140
+ end
141
+
142
+ end
@@ -0,0 +1,21 @@
1
+ require 'assert'
2
+ require 'ostruct'
3
+
4
+ class Sanford::Manager
5
+
6
+ class BaseTest < Assert::Context
7
+ desc "Sanford::Manager"
8
+ setup do
9
+ @dummy_host = DummyHost
10
+ @manager = Sanford::Manager.new(@dummy_host)
11
+ end
12
+ subject{ @manager }
13
+
14
+ should have_instance_methods :host, :process_name, :call
15
+ should have_class_methods :call
16
+ end
17
+
18
+ # Sanford::Manager#call methods are tested in the test/system/managing_test.rb
19
+ # they require mocking the Daemons gem
20
+
21
+ end
@@ -0,0 +1,22 @@
1
+ require 'assert'
2
+
3
+ class Sanford::Server
4
+
5
+ class BaseTest < Assert::Context
6
+ desc "Sanford::Server"
7
+ setup do
8
+ @service_host = DummyHost.new
9
+ @server = Sanford::Server.new(@service_host)
10
+ end
11
+ subject{ @server }
12
+
13
+ should "include DatTCP::Server" do
14
+ assert_includes DatTCP::Server, subject.class.included_modules
15
+ end
16
+ end
17
+
18
+ # Sanford::Server#serve is tested in test/system/request_handling_test.rb,
19
+ # it requires multiple parts of Sanford and basically tests a large portion of
20
+ # the entire system
21
+
22
+ end
@@ -0,0 +1,170 @@
1
+ require 'assert'
2
+
3
+ module Sanford::ServiceHandler
4
+
5
+ class BaseTest < Assert::Context
6
+ desc "Sanford::ServiceHandler"
7
+ setup do
8
+ @handler = StaticServiceHandler.new
9
+ end
10
+ subject{ @handler }
11
+
12
+ should have_instance_methods :logger, :request, :init, :init!, :run, :run!, :halt, :params
13
+
14
+ should "raise a NotImplementedError if run! is not overwritten" do
15
+ assert_raises(NotImplementedError){ subject.run! }
16
+ end
17
+ should "return the request's params with #params" do
18
+ assert_equal subject.request.params, subject.params
19
+ end
20
+ end
21
+
22
+ class WithMethodFlagsTest < BaseTest
23
+ setup do
24
+ @handler = FlaggedServiceHandler.new
25
+ end
26
+
27
+ should "should call the `init!` method when `init` is called" do
28
+ subject.init
29
+
30
+ assert_equal true, subject.init_bang_called
31
+ end
32
+ should "run the `init` and `run!` method when `run` is called" do
33
+ subject.run
34
+
35
+ assert_equal true, subject.init_called
36
+ assert_equal true, subject.run_bang_called
37
+ end
38
+ should "run it's callbacks when `run` is called" do
39
+ subject.run
40
+
41
+ assert_equal true, subject.before_run_called
42
+ assert_equal true, subject.after_run_called
43
+ end
44
+ end
45
+
46
+ class ManualWithThrowTest < BaseTest
47
+ desc "run that manuallly throws `:halt`"
48
+ setup do
49
+ handler = ManualThrowServiceHandler.new
50
+ @returned = handler.run
51
+ end
52
+
53
+ should "catch `:halt` and return what was thrown" do
54
+ assert_equal 'halted!', @returned
55
+ end
56
+ end
57
+
58
+ class HaltWithAStatusNameAndMessageTest < BaseTest
59
+ desc "halt with a status name and a message"
60
+ setup do
61
+ @halt_with = { :code => :success, :message => "Just a test" }
62
+ handler = HaltWithServiceHandler.new(@halt_with)
63
+ @response_status, @data = handler.run
64
+ end
65
+
66
+ should "return a response with the status passed to halt and a nil data" do
67
+ assert_equal @halt_with[:code], @response_status.first
68
+ assert_equal @halt_with[:message], @response_status.last
69
+ assert_equal @halt_with[:data], @data
70
+ end
71
+ end
72
+
73
+ class HaltWithAStatusCodeAndDataTest < BaseTest
74
+ desc "halt with a status code and data"
75
+ setup do
76
+ @halt_with = { :code => 648, :data => true }
77
+ handler = HaltWithServiceHandler.new(@halt_with)
78
+ @response_status, @data = handler.run
79
+ end
80
+
81
+ should "return a response status and data when passed a number and a data option" do
82
+ assert_equal @halt_with[:code], @response_status.first
83
+ assert_equal @halt_with[:message], @response_status.last
84
+ assert_equal @halt_with[:data], @data
85
+ end
86
+ end
87
+
88
+ class BeforeRunHaltsTest < BaseTest
89
+ desc "if 'before_run' halts"
90
+ setup do
91
+ @handler = ConfigurableServiceHandler.new({
92
+ :before_run => proc{ halt 601, :message => "before_run halted" }
93
+ })
94
+ @response_status, @data = @handler.run
95
+ end
96
+
97
+ should "only call 'before_run' and 'after_run'" do
98
+ assert_equal true, subject.before_run_called
99
+ assert_equal false, subject.init_called
100
+ assert_equal false, subject.init_bang_called
101
+ assert_equal false, subject.run_bang_called
102
+ assert_equal true, subject.after_run_called
103
+ end
104
+
105
+ should "return the 'before_run' response" do
106
+ assert_equal 601, @response_status.first
107
+ assert_equal "before_run halted", @response_status.last
108
+ assert_equal nil, @data
109
+ end
110
+ end
111
+
112
+ class AfterRunHaltsTest < BaseTest
113
+ desc "if 'after_run' halts"
114
+ setup do
115
+ @after_run = proc{ halt 801, :message => "after_run halted" }
116
+ end
117
+
118
+ end
119
+
120
+ class AndBeforeRunHaltsTest < AfterRunHaltsTest
121
+ desc "and 'before_run' halts"
122
+ setup do
123
+ @handler = ConfigurableServiceHandler.new({
124
+ :before_run => proc{ halt 601, :message => "before_run halted" },
125
+ :after_run => @after_run
126
+ })
127
+ @response_status, @data = @handler.run
128
+ end
129
+
130
+ should "only call 'before_run' and 'after_run'" do
131
+ assert_equal true, subject.before_run_called
132
+ assert_equal false, subject.init_called
133
+ assert_equal false, subject.init_bang_called
134
+ assert_equal false, subject.run_bang_called
135
+ assert_equal true, subject.after_run_called
136
+ end
137
+
138
+ should "return the 'after_run' response" do
139
+ assert_equal 801, @response_status.first
140
+ assert_equal "after_run halted", @response_status.last
141
+ assert_equal nil, @data
142
+ end
143
+ end
144
+
145
+ class AndRunBangHaltsTest < AfterRunHaltsTest
146
+ desc "and 'run!' halts"
147
+ setup do
148
+ @handler = ConfigurableServiceHandler.new({
149
+ :run! => proc{ halt 601, :message => "run! halted" },
150
+ :after_run => @after_run
151
+ })
152
+ @response_status, @data = @handler.run
153
+ end
154
+
155
+ should "call 'init!', 'run!' and the callbacks" do
156
+ assert_equal true, subject.before_run_called
157
+ assert_equal true, subject.init_called
158
+ assert_equal true, subject.init_bang_called
159
+ assert_equal true, subject.run_bang_called
160
+ assert_equal true, subject.after_run_called
161
+ end
162
+
163
+ should "return the 'after_run' response" do
164
+ assert_equal 801, @response_status.first
165
+ assert_equal "after_run halted", @response_status.last
166
+ assert_equal nil, @data
167
+ end
168
+ end
169
+
170
+ end
metadata ADDED
@@ -0,0 +1,209 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sanford
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Collin Redding
14
+ - Kelly Redding
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2012-12-04 00:00:00 Z
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ prerelease: false
23
+ version_requirements: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 13
29
+ segments:
30
+ - 1
31
+ - 1
32
+ version: "1.1"
33
+ requirement: *id001
34
+ name: daemons
35
+ type: :runtime
36
+ - !ruby/object:Gem::Dependency
37
+ prerelease: false
38
+ version_requirements: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 9
44
+ segments:
45
+ - 0
46
+ - 1
47
+ version: "0.1"
48
+ requirement: *id002
49
+ name: dat-tcp
50
+ type: :runtime
51
+ - !ruby/object:Gem::Dependency
52
+ prerelease: false
53
+ version_requirements: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ hash: 23
59
+ segments:
60
+ - 1
61
+ - 0
62
+ - 0
63
+ version: 1.0.0
64
+ requirement: *id003
65
+ name: ns-options
66
+ type: :runtime
67
+ - !ruby/object:Gem::Dependency
68
+ prerelease: false
69
+ version_requirements: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ~>
73
+ - !ruby/object:Gem::Version
74
+ hash: 1
75
+ segments:
76
+ - 0
77
+ - 5
78
+ version: "0.5"
79
+ requirement: *id004
80
+ name: sanford-protocol
81
+ type: :runtime
82
+ - !ruby/object:Gem::Dependency
83
+ prerelease: false
84
+ version_requirements: &id005 !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ hash: 15
90
+ segments:
91
+ - 1
92
+ - 0
93
+ version: "1.0"
94
+ requirement: *id005
95
+ name: assert
96
+ type: :development
97
+ - !ruby/object:Gem::Dependency
98
+ prerelease: false
99
+ version_requirements: &id006 !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ~>
103
+ - !ruby/object:Gem::Version
104
+ hash: 15
105
+ segments:
106
+ - 1
107
+ - 0
108
+ version: "1.0"
109
+ requirement: *id006
110
+ name: assert-mocha
111
+ type: :development
112
+ description: Simple hosts for Sanford services.
113
+ email:
114
+ - collin.redding@me.com
115
+ - kelly@kellyredding.com
116
+ executables: []
117
+
118
+ extensions: []
119
+
120
+ extra_rdoc_files: []
121
+
122
+ files:
123
+ - .gitignore
124
+ - Gemfile
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - bench/client.rb
129
+ - bench/report.txt
130
+ - bench/runner.rb
131
+ - bench/services.rb
132
+ - bench/tasks.rb
133
+ - lib/sanford.rb
134
+ - lib/sanford/config.rb
135
+ - lib/sanford/connection.rb
136
+ - lib/sanford/exception_handler.rb
137
+ - lib/sanford/exceptions.rb
138
+ - lib/sanford/host.rb
139
+ - lib/sanford/manager.rb
140
+ - lib/sanford/rake.rb
141
+ - lib/sanford/server.rb
142
+ - lib/sanford/service_handler.rb
143
+ - lib/sanford/version.rb
144
+ - log/.gitkeep
145
+ - sanford.gemspec
146
+ - test/helper.rb
147
+ - test/support/helpers.rb
148
+ - test/support/service_handlers.rb
149
+ - test/support/services.rb
150
+ - test/support/simple_client.rb
151
+ - test/system/managing_test.rb
152
+ - test/system/request_handling_test.rb
153
+ - test/unit/config_test.rb
154
+ - test/unit/connection_test.rb
155
+ - test/unit/exception_handler_test.rb
156
+ - test/unit/host/version_group_test.rb
157
+ - test/unit/host_test.rb
158
+ - test/unit/manager_test.rb
159
+ - test/unit/server_test.rb
160
+ - test/unit/service_handler_test.rb
161
+ homepage: https://github.com/redding/sanford
162
+ licenses: []
163
+
164
+ post_install_message:
165
+ rdoc_options: []
166
+
167
+ require_paths:
168
+ - lib
169
+ required_ruby_version: !ruby/object:Gem::Requirement
170
+ none: false
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ hash: 3
175
+ segments:
176
+ - 0
177
+ version: "0"
178
+ required_rubygems_version: !ruby/object:Gem::Requirement
179
+ none: false
180
+ requirements:
181
+ - - ">="
182
+ - !ruby/object:Gem::Version
183
+ hash: 3
184
+ segments:
185
+ - 0
186
+ version: "0"
187
+ requirements: []
188
+
189
+ rubyforge_project:
190
+ rubygems_version: 1.8.15
191
+ signing_key:
192
+ specification_version: 3
193
+ summary: Simple hosts for Sanford services.
194
+ test_files:
195
+ - test/helper.rb
196
+ - test/support/helpers.rb
197
+ - test/support/service_handlers.rb
198
+ - test/support/services.rb
199
+ - test/support/simple_client.rb
200
+ - test/system/managing_test.rb
201
+ - test/system/request_handling_test.rb
202
+ - test/unit/config_test.rb
203
+ - test/unit/connection_test.rb
204
+ - test/unit/exception_handler_test.rb
205
+ - test/unit/host/version_group_test.rb
206
+ - test/unit/host_test.rb
207
+ - test/unit/manager_test.rb
208
+ - test/unit/server_test.rb
209
+ - test/unit/service_handler_test.rb