From: Kevin Clark Date: Wed, 18 Jun 2008 01:19:59 +0000 (+0000) Subject: rb: Make a few of the NonblockingServer specs pass under jruby X-Git-Tag: 0.2.0~515 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=980e4453472bb4c552707e6627e76ee8e3ba020a;p=common%2Fthrift.git rb: Make a few of the NonblockingServer specs pass under jruby git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@669036 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/rb/spec/ThriftSpec.thrift b/lib/rb/spec/ThriftSpec.thrift index 1012e513..4309829f 100644 --- a/lib/rb/spec/ThriftSpec.thrift +++ b/lib/rb/spec/ThriftSpec.thrift @@ -20,7 +20,7 @@ struct BoolStruct { service NonblockingService { Hello greeting(1:bool english) bool block() - async void unblock() + async void unblock(1:i32 n) async void shutdown() void sleep(1:double seconds) } diff --git a/lib/rb/spec/gen-rb/NonblockingService.rb b/lib/rb/spec/gen-rb/NonblockingService.rb index 411a6f07..ba6b3e48 100644 --- a/lib/rb/spec/gen-rb/NonblockingService.rb +++ b/lib/rb/spec/gen-rb/NonblockingService.rb @@ -43,12 +43,12 @@ require 'ThriftSpec_types' raise Thrift::ApplicationException.new(Thrift::ApplicationException::MISSING_RESULT, 'block failed: unknown result') end - def unblock() - send_unblock() + def unblock(n) + send_unblock(n) end - def send_unblock() - send_message('unblock', Unblock_args) + def send_unblock(n) + send_message('unblock', Unblock_args, :n => n) end def shutdown() send_shutdown() @@ -92,7 +92,7 @@ require 'ThriftSpec_types' def process_unblock(seqid, iprot, oprot) args = read_args(iprot, Unblock_args) - @handler.unblock() + @handler.unblock(args.n) return end @@ -146,8 +146,9 @@ require 'ThriftSpec_types' class Unblock_args include Thrift::Struct + Thrift::Struct.field_accessor self, :n FIELDS = { - + 1 => {:type => Thrift::Types::I32, :name => 'n'} } end diff --git a/lib/rb/spec/nonblockingserver_spec.rb b/lib/rb/spec/nonblockingserver_spec.rb index 04c569a7..fd0fb64d 100644 --- a/lib/rb/spec/nonblockingserver_spec.rb +++ b/lib/rb/spec/nonblockingserver_spec.rb @@ -25,8 +25,8 @@ class ThriftNonblockingServerSpec < Spec::ExampleGroup @queue.pop end - def unblock - @queue.num_waiting.times { @queue.push true } + def unblock(n) + n.times { @queue.push true } end def sleep(time) @@ -72,12 +72,25 @@ class ThriftNonblockingServerSpec < Spec::ExampleGroup end end + class SpecServerSocket < ServerSocket + def initialize(host, port, queue) + super(host, port) + @queue = queue + end + + def listen + super + @queue.push :listen + end + end + describe Thrift::NonblockingServer do before(:each) do @port = 43251 handler = Handler.new processor = NonblockingService::Processor.new(handler) - @transport = ServerSocket.new('localhost', @port) + queue = Queue.new + @transport = SpecServerSocket.new('localhost', @port, queue) transportFactory = FramedTransportFactory.new logger = Logger.new(STDERR) logger.level = Logger::WARN @@ -92,7 +105,7 @@ class ThriftNonblockingServerSpec < Spec::ExampleGroup master_thread.raise e end end - Thread.pass + queue.pop @clients = [] @catch_exceptions = false @@ -119,12 +132,13 @@ class ThriftNonblockingServerSpec < Spec::ExampleGroup Thread.new do begin client = setup_client - while (msg = queue.pop) + while (cmd = queue.pop) + msg, *args = cmd case msg when :block result << client.block when :unblock - client.unblock + client.unblock(args.first) when :hello result << client.greeting(true) # ignore result when :sleep @@ -154,9 +168,17 @@ class ThriftNonblockingServerSpec < Spec::ExampleGroup it "should handle concurrent clients" do queue = Queue.new trans_queue = Queue.new - 4.times { Thread.new { queue.push setup_client(trans_queue).block } } + 4.times do + Thread.new(Thread.current) do |main_thread| + begin + queue.push setup_client(trans_queue).block + rescue => e + main_thread.raise e + end + end + end 4.times { trans_queue.pop } - setup_client.unblock + setup_client.unblock(4) 4.times { queue.pop.should be_true } end @@ -175,7 +197,7 @@ class ThriftNonblockingServerSpec < Spec::ExampleGroup queues[6] << :hello 3.times { result.pop.should == Hello.new } client.greeting(true).should == Hello.new - queues[5] << :unblock + queues[5] << [:unblock, 4] 4.times { result.pop.should be_true } queues[2] << :hello result.pop.should == Hello.new