ruby_skynet 1.3.0.alpha3 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,120 +0,0 @@
1
- # Allow test to be run in-place without requiring a gem install
2
- $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
3
- $LOAD_PATH.unshift File.dirname(__FILE__)
4
-
5
- require 'rubygems'
6
- require 'test/unit'
7
- require 'shoulda'
8
- require 'ruby_skynet'
9
-
10
- # Register an appender if one is not already registered
11
- SemanticLogger.default_level = :trace
12
- SemanticLogger.add_appender('test.log') if SemanticLogger.appenders.size == 0
13
-
14
- class ClientTestService
15
- include RubySkynet::Service
16
-
17
- # Methods implemented by this service
18
- # Must take a Hash as input
19
- # Must Return a Hash response or nil for no response
20
- def test1(params)
21
- { 'result' => 'test1' }
22
- end
23
-
24
- def sleep(params)
25
- Kernel.sleep params['duration'] || 1
26
- { 'result' => 'sleep' }
27
- end
28
-
29
- def fail(params)
30
- if params['attempt'].to_i >= 2
31
- { 'result' => 'fail' }
32
- else
33
- nil
34
- end
35
- end
36
-
37
- end
38
-
39
- # Test Client Class
40
- class ClientTestServiceClient < RubySkynet::Client
41
- end
42
-
43
- # Unit Test for ResilientSocket::TCPClient
44
- class ClientTest < Test::Unit::TestCase
45
- context RubySkynet::Client do
46
-
47
- context "without server" do
48
- should "raise exception when not registered" do
49
- exception = assert_raise RubySkynet::ServiceUnavailable do
50
- client = RubySkynet::Client.new('SomeService','*','ClientTest')
51
- client.call(:test, :hello => 'there')
52
- end
53
- assert_match /No servers available for service: SomeService with version: -1 in region: ClientTest/, exception.message
54
- end
55
-
56
- end
57
-
58
- context "with server" do
59
- setup do
60
- @region = 'ClientTest'
61
- RubySkynet.region = @region
62
- RubySkynet::Server.start
63
- # Give Service Registry time to push out the presence of the service above
64
- sleep 0.1
65
-
66
- @service_name = 'ClientTestService'
67
- @version = 1
68
-
69
- @read_timeout = 3.0
70
- end
71
-
72
- teardown do
73
- RubySkynet::Server.stop
74
- end
75
-
76
- context "with client connection" do
77
- setup do
78
- @client = RubySkynet::Client.new(@service_name, @version, @region)
79
- end
80
-
81
- should "successfully send and receive data" do
82
- reply = @client.call(:test1, 'some' => 'parameters')
83
- assert_equal 'test1', reply['result']
84
- end
85
-
86
- should "timeout on receive" do
87
- request = { 'duration' => @read_timeout + 0.5}
88
-
89
- exception = assert_raise ResilientSocket::ReadTimeout do
90
- # Read 4 bytes from server
91
- @client.call('sleep', request, :read_timeout => @read_timeout)
92
- end
93
- assert_match /Timedout after #{@read_timeout} seconds trying to read/, exception.message
94
- end
95
- end
96
-
97
- context "using client class" do
98
- setup do
99
- @client = ClientTestServiceClient.new("ClientTestService")
100
- end
101
-
102
- should "successfully send and receive data" do
103
- reply = @client.test1('some' => 'parameters')
104
- assert_equal 'test1', reply['result']
105
- end
106
-
107
- should "timeout on receive" do
108
- request = { 'duration' => @read_timeout + 0.5}
109
-
110
- exception = assert_raise ResilientSocket::ReadTimeout do
111
- # Read 4 bytes from server
112
- @client.sleep(request, :read_timeout => @read_timeout)
113
- end
114
- assert_match /Timedout after #{@read_timeout} seconds trying to read/, exception.message
115
- end
116
- end
117
-
118
- end
119
- end
120
- end
@@ -1,122 +0,0 @@
1
- # Allow test to be run in-place without requiring a gem install
2
- $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
3
-
4
- require 'rubygems'
5
- require 'test/unit'
6
- require 'shoulda'
7
- require 'ruby_skynet'
8
-
9
- # Register an appender if one is not already registered
10
- SemanticLogger.default_level = :trace
11
- SemanticLogger.add_appender('test.log') if SemanticLogger.appenders.size == 0
12
-
13
- # Unit Test
14
- class ServiceRegistryTest < Test::Unit::TestCase
15
- context RubySkynet::ServiceRegistry do
16
-
17
- setup do
18
- @service_name = 'MyRegistryService'
19
- @version = 5
20
- @region = 'RegistryTest'
21
- @hostname = '127.0.0.1'
22
- @port = 2100
23
- @service_key = "/services/#{@service_name}/#{@version}/#{@region}/#{@hostname}/#{@port}"
24
- RubySkynet.local_ip_address = @hostname
25
- end
26
-
27
- teardown do
28
- RubySkynet.local_ip_address = nil
29
- end
30
-
31
- context "with a registered service" do
32
- setup do
33
- RubySkynet.service_registry.register_service(@service_name, @version, @region, @hostname, @port)
34
- RubySkynet.service_registry.register_service(@service_name, @version, @region+'BLAH', @hostname, @port)
35
- # Allow time for registry callback that service was registered
36
- sleep 0.1
37
- end
38
-
39
- teardown do
40
- RubySkynet.service_registry.deregister_service(@service_name, @version, @region, @hostname, @port)
41
- RubySkynet.service_registry.deregister_service(@service_name, @version, @region+'BLAH', @hostname, @port)
42
- # Allow time for registry callback that service was deregistered
43
- sleep 0.1
44
- # No servers should be in the local registry
45
- # assert_equal nil, RubySkynet.service_registry.servers_for(@service_name, @version, @region)
46
- end
47
-
48
- should "find server using exact match" do
49
- assert (servers = RubySkynet.service_registry.servers_for(@service_name, @version, @region)), RubySkynet.service_registry.to_h.inspect
50
- assert_equal 1, servers.size
51
- assert_equal "#{@hostname}:#{@port}", servers.first
52
- end
53
-
54
- should "find server using * version match" do
55
- assert (servers = RubySkynet.service_registry.servers_for(@service_name, '*', @region)), RubySkynet.service_registry.to_h.inspect
56
- assert_equal 1, servers.size
57
- assert_equal "#{@hostname}:#{@port}", servers.first
58
- end
59
-
60
- context "with multiple servers" do
61
- setup do
62
- @second_hostname = '127.0.10.1'
63
- RubySkynet.service_registry.register_service(@service_name, @version, @region, @hostname, @port+1)
64
- RubySkynet.service_registry.register_service(@service_name, @version, @region, @hostname, @port+3)
65
- RubySkynet.service_registry.register_service(@service_name, @version-1, @region, @hostname, @port+2)
66
- RubySkynet.service_registry.register_service(@service_name, @version, @region, @second_hostname, @port)
67
- end
68
-
69
- teardown do
70
- RubySkynet.service_registry.deregister_service(@service_name, @version, @region, @hostname, @port+1)
71
- RubySkynet.service_registry.deregister_service(@service_name, @version, @region, @hostname, @port+3)
72
- RubySkynet.service_registry.deregister_service(@service_name, @version-1, @region, @hostname, @port+2)
73
- RubySkynet.service_registry.deregister_service(@service_name, @version, @region, @second_hostname, @port)
74
- end
75
-
76
- should "using * version match" do
77
- assert (servers = RubySkynet.service_registry.servers_for(@service_name, '*', @region)), RubySkynet.service_registry.to_h.inspect
78
- expected = RubySkynet::ServiceRegistry.respond_to?(:score_for_server) ? 3 : 4
79
- assert_equal expected, servers.size, servers
80
- assert_equal true, servers.include?("#{@hostname}:#{@port}"), servers
81
- assert_equal true, servers.include?("#{@hostname}:#{@port+1}"), servers
82
- assert_equal true, servers.include?("#{@hostname}:#{@port+3}"), servers
83
- end
84
- end
85
-
86
- should "return nil when service not found" do
87
- assert_raise RubySkynet::ServiceUnavailable do
88
- RubySkynet.service_registry.servers_for('MissingService', @version, @region)
89
- end
90
- end
91
-
92
- should "return nil when version not found" do
93
- assert_raise RubySkynet::ServiceUnavailable do
94
- RubySkynet.service_registry.servers_for(@service_name, @version+1, @region)
95
- end
96
- end
97
-
98
- should "return nil when region not found" do
99
- assert_raise RubySkynet::ServiceUnavailable do
100
- RubySkynet.service_registry.servers_for(@service_name, @version, 'OtherRegion')
101
- end
102
- end
103
- end
104
-
105
- context "scoring" do
106
- [
107
- ['192.168.11.0', 4 ],
108
- ['192.168.11.10', 3 ],
109
- ['192.168.10.0', 2 ],
110
- ['192.5.10.0', 1 ],
111
- ['10.0.11.0', 0 ],
112
- ].each do |test|
113
- should "handle score #{test[1]}" do
114
- skip "Not applicable to Static Service Registry" unless RubySkynet::ServiceRegistry.respond_to?(:score_for_server)
115
- local_ip_address = "192.168.11.0"
116
- assert_equal test[1], RubySkynet::ServiceRegistry.score_for_server(test[0], local_ip_address), "Local: #{local_ip_address} Server: #{test[0]} Score: #{test[1]}"
117
- end
118
- end
119
- end
120
-
121
- end
122
- end
@@ -1,71 +0,0 @@
1
- # Allow test to be run in-place without requiring a gem install
2
- $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
3
-
4
- require 'rubygems'
5
- require 'test/unit'
6
- require 'shoulda'
7
- require 'ruby_skynet'
8
-
9
- # Register an appender if one is not already registered
10
- SemanticLogger.default_level = :trace
11
- SemanticLogger.add_appender('test.log') if SemanticLogger.appenders.size == 0
12
-
13
- class TestService
14
- include RubySkynet::Service
15
-
16
- # Methods implemented by this service
17
- # Must take a Hash as input
18
- # Must Return a Hash response or nil for no response
19
- def echo(params)
20
- params
21
- end
22
-
23
- def exception_test(params)
24
- raise "Exception message"
25
- end
26
- end
27
-
28
- # Unit Test for ResilientSocket::TCPClient
29
- class ServiceTest < Test::Unit::TestCase
30
- context 'RubySkynet::Service' do
31
- context "with server" do
32
- setup do
33
- @region = 'Test'
34
- @service_name = 'TestService'
35
- @version = 1
36
- RubySkynet.region = @region
37
- RubySkynet::Server.start
38
- sleep 0.2
39
- end
40
-
41
- teardown do
42
- RubySkynet::Server.stop
43
- SemanticLogger::Logger.flush
44
- end
45
-
46
- should "be running" do
47
- assert_equal true, RubySkynet::Server.running?
48
- end
49
-
50
- context "using a client" do
51
- setup do
52
- @client = RubySkynet::Client.new(@service_name, @version, @region)
53
- end
54
-
55
- should "successfully send and receive data" do
56
- reply = @client.call(:echo, 'some' => 'parameters')
57
- assert_equal 'some', reply.keys.first
58
- assert_equal 'parameters', reply.values.first
59
- end
60
-
61
- # Cellulloid 0.13.0.pre2 is doing something weird here and preventing the
62
- # Server from receiving the exception
63
- should "handle service exceptions" do
64
- reply = @client.call(:exception_test, 'some' => 'parameters')
65
- assert_equal "Exception message", reply['exception']['message']
66
- end
67
- end
68
-
69
- end
70
- end
71
- end
@@ -1,209 +0,0 @@
1
- # Allow test to be run in-place without requiring a gem install
2
- $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
3
-
4
- require 'rubygems'
5
- require 'test/unit'
6
- require 'shoulda'
7
- require 'ruby_skynet/zookeeper'
8
- require 'date'
9
-
10
- # NOTE:
11
- # This test assumes that ZooKeeper is running locally on the default port
12
-
13
- # Register an appender if one is not already registered
14
- SemanticLogger.default_level = :trace
15
- SemanticLogger.add_appender('test.log') if SemanticLogger.appenders.size == 0
16
-
17
- # Unit Test for RubySkynet::Zookeeper::Registry
18
- class ZookeeperRegistryTest < Test::Unit::TestCase
19
- context RubySkynet::Zookeeper::Registry do
20
- setup do
21
- @date = Date.parse('2013-04-04')
22
- @time = Time.at(1365102658)
23
- @test_data = {
24
- 'bar' => 'test',
25
- 'one' => 'one',
26
- 'string_with_underscores' => 'and_a_value',
27
- 'two' => :two,
28
- 'integer' => 10,
29
- 'float' => 10.5,
30
- 'date' => @date,
31
- 'time' => @time,
32
- 'false' => false,
33
- 'true' => true,
34
- 'child' => { :symbol_with_underscores => :and_a_value, :this => 'is', :an => ['array', :symbol, :smallest => {'a' => 'b', :c => :d}]}
35
- }
36
- @test_data['all_types'] = @test_data.dup
37
- end
38
-
39
- context "serialization" do
40
- setup do
41
- @json = {
42
- 'bar' => 'test',
43
- 'one' => 'one',
44
- 'string_with_underscores' => 'and_a_value',
45
- 'two' => ':two',
46
- 'integer' => '10',
47
- 'float' => '10.5',
48
- 'date' => @date.to_s,
49
- 'time' => @time.to_s,
50
- 'false' => 'false',
51
- 'true' => 'true',
52
- 'child' => "{\":symbol_with_underscores\":\":and_a_value\",\":this\":\"is\",\":an\":[\"array\",\":symbol\",{\":smallest\":{\"a\":\"b\",\":c\":\":d\"}}]}",
53
- 'all_types' => "{\"bar\":\"test\",\"one\":\"one\",\"string_with_underscores\":\"and_a_value\",\"two\":\":two\",\"integer\":\"10\",\"float\":\"10.5\",\"date\":\"2013-04-04\",\"time\":\"2013-04-04 15:10:58 -0400\",\"false\":\"false\",\"true\":\"true\",\"child\":{\":symbol_with_underscores\":\":and_a_value\",\":this\":\"is\",\":an\":[\"array\",\":symbol\",{\":smallest\":{\"a\":\"b\",\":c\":\":d\"}}]}}"
54
- }
55
- end
56
-
57
- should ".serialize" do
58
- @test_data.each_pair do |k,v|
59
- assert_equal @json[k], RubySkynet::Zookeeper::Json::Serializer.serialize(v), "Key: #{k}"
60
- end
61
- end
62
-
63
- should ".deserialize" do
64
- @json.each_pair do |k,v|
65
- assert_equal @test_data[k], RubySkynet::Zookeeper::Json::Deserializer.deserialize(v), "Key: #{k}"
66
- end
67
- end
68
- end
69
-
70
- context "with registry" do
71
- setup do
72
- params = {
73
- :root => "/registrytest",
74
- :registry => {
75
- :connect_timeout => 5,
76
- }
77
- }
78
- @registry = RubySkynet::Zookeeper::Registry.new(params)
79
- @registry.each_pair {|k,v, ver, num_children| @registry.delete(k) if num_children == 0}
80
- @test_data.each_pair {|k,v| @registry[k] = v}
81
- @path = 'a/b/c/d/e/f/g/h'
82
- @registry[@path] = 'hello'
83
- # Ensure hash is not modified
84
- assert_equal "/registrytest", params[:root]
85
- assert_equal 5, params[:registry][:connect_timeout]
86
- end
87
-
88
- def teardown
89
- if @registry
90
- @registry.each_pair {|k,v, ver, num_children| @registry.delete(k) if num_children == 0}
91
- # Allow callbacks to complete so that errors are not generated on close
92
- sleep 0.1
93
- @registry.close
94
- end
95
- end
96
-
97
- should "#[]" do
98
- @test_data.each_pair do |k,v|
99
- assert_equal v, @registry[k], "Expected #{k}=>#{v}, #{@registry.to_h.inspect}"
100
- end
101
- end
102
-
103
- should "#each_pair" do
104
- h = {}
105
- @registry.each_pair {|k,v| h[k] = v}
106
- assert_hash_equal @test_data, h
107
- end
108
-
109
- should "#to_h" do
110
- assert_hash_equal @test_data, @registry.to_h
111
- end
112
-
113
- should "#[]=" do
114
- @registry['three'] = 'value'
115
- # Give Zookeeper time to send back the change
116
- sleep 0.3
117
- result = @registry['three']
118
- assert_equal 'value', result
119
- end
120
-
121
- should "#delete parent paths when no other leaf nodes exist" do
122
- @registry.delete(@path)
123
- assert_equal nil, @registry['a']
124
- end
125
-
126
- [nil, '*'].each do |monitor_path|
127
- context "with monitor_path:#{monitor_path}" do
128
- should "callback on update" do
129
- updated_revision = nil
130
- updated_path = nil
131
- updated_value = nil
132
- @registry.on_update(monitor_path||'bar') do |path, value, revision|
133
- updated_revision = revision
134
- updated_path = path
135
- updated_value = value
136
- end
137
- # Allow monitoring thread to start
138
- sleep 0.1
139
- @registry['bar'] = 'updated'
140
- # Allow Zookeeper to send back the change
141
- sleep 0.3
142
- assert_equal 'bar', updated_path
143
- assert_equal 'updated', updated_value
144
- assert_equal true, updated_revision > 0
145
- end
146
-
147
- should "callback on delete" do
148
- deleted_path = nil
149
- @registry.on_delete(monitor_path||'bar') do |path, revision|
150
- deleted_path = path
151
- end
152
- # Allow monitoring thread to start
153
- sleep 0.1
154
- # Allow Zookeeper to send back the change
155
- @registry.delete('bar')
156
- sleep 2
157
- assert_equal 'bar', deleted_path
158
- end
159
- end
160
- end
161
-
162
- ['other', 'one'].each do |monitor_path|
163
- context "with monitor_path:#{monitor_path}" do
164
- should "not callback on update" do
165
- updated_path = nil
166
- updated_value = nil
167
- @registry.on_update(monitor_path) do |path, value|
168
- updated_path = path
169
- updated_value = value
170
- end
171
- # Allow monitoring thread to start
172
- sleep 0.1
173
- @registry['bar'] = 'updated'
174
- # Allow Zookeeper to send back the change
175
- sleep 0.3
176
- assert_equal nil, updated_path
177
- assert_equal nil, updated_value
178
- end
179
-
180
- should "not callback on delete" do
181
- deleted_path = nil
182
- @registry.on_delete(monitor_path) do |path|
183
- deleted_path = path
184
- end
185
- # Allow monitoring thread to start
186
- sleep 0.1
187
- # Allow Zookeeper to send back the change
188
- @registry.delete('bar')
189
- sleep 0.3
190
- assert_equal nil, deleted_path
191
- end
192
- end
193
- end
194
-
195
- end
196
- end
197
-
198
- # Verify that two hashes match
199
- def assert_hash_equal(expected, actual)
200
- expected.each_pair do |k,v|
201
- if v.is_a?(Hash)
202
- assert_hash_equal(v, actual[k])
203
- else
204
- assert_equal expected[k], actual[k], "Expected: #{expected.inspect}, Actual:#{actual.inspect}"
205
- end
206
- end
207
- end
208
-
209
- end