Skip to content

Commit 6508600

Browse files
authored
Merge pull request #15 from upfluence/tg/processor-inherited-methods
Fix ruby inherited methods
2 parents 5466c5e + 689e077 commit 6508600

2 files changed

Lines changed: 41 additions & 7 deletions

File tree

lib/rb/lib/thrift/processor.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,14 @@ def initialize(handler, middlewares = [], logger=nil)
9292
end
9393
@middleware = Middleware.wrap(middlewares)
9494

95-
@processors = if self.class.const_defined? :METHODS
96-
self.class::METHODS.reduce({}) do |acc, (name, info)|
97-
acc.merge(name => build_processor(name, info))
98-
end
99-
else
100-
{}
101-
end
95+
# Walk ancestors child first so inherited methods are exposed and overrides win.
96+
@processors = self.class.ancestors.each_with_object({}) do |klass, acc|
97+
next unless klass.const_defined?(:METHODS, false)
98+
99+
klass::METHODS.each do |name, info|
100+
acc[name] ||= build_processor(name, info)
101+
end
102+
end
102103
end
103104

104105
def read_args(iprot, args_class)

lib/rb/spec/processor_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,20 @@ class CurrentProcessor
7777
result_klass: EmptyResult,
7878
args: [],
7979
exceptions: {},
80+
void_result: true,
81+
oneway: false
82+
}
83+
}.freeze
84+
end
85+
86+
class ExtendedProcessor < CurrentProcessor
87+
METHODS = {
88+
'pong' => {
89+
args_klass: EmptyArgs,
90+
result_klass: EmptyResult,
91+
args: [],
92+
exceptions: {},
93+
void_result: true,
8094
oneway: false
8195
}
8296
}.freeze
@@ -179,6 +193,25 @@ def read_exception(protocol)
179193
)
180194
end
181195

196+
%w[ping pong].each do |name|
197+
it "processes #{name} with a processor extending another service" do
198+
handler = double('handler', ping: nil, pong: nil)
199+
processor = ExtendedProcessor.new(handler)
200+
oprot, trans = output_protocol
201+
202+
expect(processor.process(request_protocol(name), oprot)).to eq(true)
203+
expect(oprot.read_message_begin).to eq(
204+
[name, Thrift::MessageTypes::REPLY, 17]
205+
)
206+
207+
EmptyResult.new.read(oprot)
208+
oprot.read_message_end
209+
210+
expect(trans.available).to eq(0)
211+
expect(handler).to have_received(name)
212+
end
213+
end
214+
182215
it 'writes internal errors raised by a current processor' do
183216
handler = double('handler')
184217
allow(handler).to receive(:ping).and_raise(StandardError, 'boom')

0 commit comments

Comments
 (0)