ruby-druid 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,200 @@
1
+ require "spec_helper"
2
+
3
+ module ZK
4
+ def self.new(uri, opts = {})
5
+ Mock.new uri, opts
6
+ end
7
+
8
+ class Mock
9
+ include RSpec::Matchers
10
+
11
+ def initialize(uri, opts)
12
+ uri.should == 'test-uri'
13
+ opts.should == { :chroot => :check }
14
+ @registrations = {}
15
+ @paths = {
16
+ '/disco' => ['a', 'b'],
17
+ '/disco/a' => ['b1', 'm1'],
18
+ '/disco/b' => ['b2', 'm2']
19
+ }
20
+ @unregisters = []
21
+ end
22
+
23
+ def register(path, opts, &block)
24
+ opts.should == { :only => :child }
25
+ @registrations[path].should == nil
26
+
27
+ zk = self
28
+ block.define_singleton_method :unregister do
29
+ zk.instance_variable_get('@unregisters').push path
30
+ zk.instance_variable_get('@registrations').delete path
31
+ end
32
+ @registrations[path] = block
33
+ end
34
+
35
+ def unregistrations
36
+ result = @unregisters
37
+ @unregisters = []
38
+ result
39
+ end
40
+
41
+ def children(path, opts)
42
+ @registrations[path].should be_a(Proc)
43
+
44
+ value = @paths[path]
45
+ throw "no mock code for #{path}" unless value
46
+ value
47
+ end
48
+
49
+ def get(path)
50
+ case path
51
+ when '/disco/a/b1'
52
+ [{
53
+ :address => 'b1_address',
54
+ :port => 80
55
+ }.to_json]
56
+ when '/disco/a/m1'
57
+ [{
58
+ :address => 'm1_address',
59
+ :port => 81
60
+ }.to_json]
61
+ when '/disco/b/b2'
62
+ [{
63
+ :address => 'b2_address',
64
+ :port => 90
65
+ }.to_json]
66
+ when '/disco/b/m2'
67
+ [{
68
+ :address => 'm2_address',
69
+ :port => 85
70
+ }.to_json]
71
+ when '/disco/a/b3'
72
+ [{
73
+ :address => 'b3_address',
74
+ :port => 83
75
+ }.to_json]
76
+ else
77
+ throw "no mock code for #{path}"
78
+ end
79
+ end
80
+
81
+ def change(uri, values)
82
+ @paths[uri] = values
83
+ @registrations[uri].call if @registrations[uri]
84
+ end
85
+
86
+ def on_expired_session
87
+ end
88
+ end
89
+ end
90
+
91
+ class RestClientResponseMock
92
+ def initialize(code, value)
93
+ @code = code
94
+ @value = value
95
+ end
96
+
97
+ def code
98
+ @code
99
+ end
100
+
101
+ def to_str
102
+ @value.to_str
103
+ end
104
+ end
105
+
106
+ describe Druid::ZooHandler do
107
+ it 'reports services and data sources correctly' do
108
+ calls = []
109
+ RestClient::Request.stub(:execute) do |opts|
110
+ uri_match = opts[:url].match /^http:\/\/(.+)_address:(.+)\/druid\/v2\/datasources\/$/
111
+
112
+ host = uri_match[1]
113
+ port = uri_match[2].to_i
114
+
115
+ calls.push [host, port]
116
+
117
+ case host
118
+ when 'b1'
119
+ RestClientResponseMock.new(200, ['s1','s2'].to_json)
120
+ when 'b2'
121
+ RestClientResponseMock.new(200, ['s3','s4'].to_json)
122
+ when 'b3'
123
+ RestClientResponseMock.new(200, ['s5','s6'].to_json)
124
+ else
125
+ RestClientResponseMock.new(404, nil)
126
+ end
127
+ end
128
+
129
+ zk = Druid::ZooHandler.new 'test-uri', :discovery_path => '/disco'
130
+
131
+ calls.should == [
132
+ ['b1', 80],
133
+ ['m1', 81],
134
+ ['b2', 90],
135
+ ['m2', 85]
136
+ ]
137
+ zk.services.should == ['a', 'b']
138
+ zk.data_sources.should == {
139
+ 'a/s1' => 'http://b1_address:80/druid/v2/',
140
+ 'a/s2' => 'http://b1_address:80/druid/v2/',
141
+ 'b/s3' => 'http://b2_address:90/druid/v2/',
142
+ 'b/s4' => 'http://b2_address:90/druid/v2/'
143
+ }
144
+
145
+ calls = []
146
+ mock = zk.instance_variable_get('@zk')
147
+ mock.unregistrations.should == []
148
+
149
+ # unregister a whole service
150
+ mock.change '/disco', ['a']
151
+ calls.should == []
152
+ zk.services.should == ['a']
153
+ zk.data_sources.should == {
154
+ 'a/s1' => 'http://b1_address:80/druid/v2/',
155
+ 'a/s2' => 'http://b1_address:80/druid/v2/'
156
+ }
157
+ mock.unregistrations.should == ['/disco/b']
158
+ # register it again
159
+ mock.change '/disco', ['a', 'b']
160
+ calls.should == [
161
+ ['b2', 90],
162
+ ['m2', 85]
163
+ ]
164
+ zk.services.should == ['a', 'b']
165
+ zk.data_sources.should == {
166
+ 'a/s1' => 'http://b1_address:80/druid/v2/',
167
+ 'a/s2' => 'http://b1_address:80/druid/v2/',
168
+ 'b/s3' => 'http://b2_address:90/druid/v2/',
169
+ 'b/s4' => 'http://b2_address:90/druid/v2/'
170
+ }
171
+ mock.unregistrations.should == []
172
+
173
+ #register a new broker
174
+ calls = []
175
+ mock.change '/disco/a', ['b1', 'b3']
176
+ calls.should == [['b3', 83]]
177
+ zk.services.should == ['a', 'b']
178
+ zk.data_sources.should == {
179
+ "a/s1" => "http://b1_address:80/druid/v2/",
180
+ "a/s2" => "http://b1_address:80/druid/v2/",
181
+ "b/s3" => "http://b2_address:90/druid/v2/",
182
+ "b/s4" => "http://b2_address:90/druid/v2/",
183
+ "a/s5" => "http://b3_address:83/druid/v2/",
184
+ "a/s6" => "http://b3_address:83/druid/v2/"
185
+ }
186
+ mock.unregistrations.should == ['/disco/a']
187
+ # unregister it
188
+ calls = []
189
+ mock.change '/disco/a', ['b1']
190
+ calls.should == []
191
+ zk.services.should == ['a', 'b']
192
+ zk.data_sources.should == {
193
+ 'a/s1' => 'http://b1_address:80/druid/v2/',
194
+ 'a/s2' => 'http://b1_address:80/druid/v2/',
195
+ 'b/s3' => 'http://b2_address:90/druid/v2/',
196
+ 'b/s4' => 'http://b2_address:90/druid/v2/'
197
+ }
198
+ mock.unregistrations.should == ['/disco/a']
199
+ end
200
+ end
@@ -0,0 +1,2 @@
1
+ require "druid"
2
+ require 'webmock/rspec'
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-druid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - The LiquidM Team
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: zk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rest-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Ruby client for metamx druid
42
+ email: tech@liquidm.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - .gitignore
48
+ - Gemfile
49
+ - Guardfile
50
+ - LICENSE
51
+ - README.md
52
+ - Rakefile
53
+ - bin/dripl
54
+ - dot_driplrc_example
55
+ - lib/druid.rb
56
+ - lib/druid/client.rb
57
+ - lib/druid/console.rb
58
+ - lib/druid/filter.rb
59
+ - lib/druid/having.rb
60
+ - lib/druid/post_aggregation.rb
61
+ - lib/druid/query.rb
62
+ - lib/druid/response_row.rb
63
+ - lib/druid/zoo_handler.rb
64
+ - ruby-druid.gemspec
65
+ - spec/lib/client_spec.rb
66
+ - spec/lib/query_spec.rb
67
+ - spec/lib/zoo_handler_spec.rb
68
+ - spec/spec_helper.rb
69
+ homepage: https://github.com/madvertise/ruby-druid
70
+ licenses: []
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.0.5
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Ruby client for druid
92
+ test_files:
93
+ - spec/lib/client_spec.rb
94
+ - spec/lib/query_spec.rb
95
+ - spec/lib/zoo_handler_spec.rb
96
+ - spec/spec_helper.rb