| David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | # | 
|  | 2 | # Licensed to the Apache Software Foundation (ASF) under one | 
|  | 3 | # or more contributor license agreements. See the NOTICE file | 
|  | 4 | # distributed with this work for additional information | 
|  | 5 | # regarding copyright ownership. The ASF licenses this file | 
|  | 6 | # to you under the Apache License, Version 2.0 (the | 
|  | 7 | # "License"); you may not use this file except in compliance | 
|  | 8 | # with the License. You may obtain a copy of the License at | 
|  | 9 | # | 
|  | 10 | #   http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 11 | # | 
|  | 12 | # Unless required by applicable law or agreed to in writing, | 
|  | 13 | # software distributed under the License is distributed on an | 
|  | 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | 
|  | 15 | # KIND, either express or implied. See the License for the | 
|  | 16 | # specific language governing permissions and limitations | 
|  | 17 | # under the License. | 
|  | 18 | # | 
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 19 | require 'spec_helper' | 
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 20 |  | 
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 21 | describe 'Server' do | 
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 22 |  | 
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 23 | describe Thrift::BaseServer do | 
| Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 24 | it "should default to BaseTransportFactory and BinaryProtocolFactory when not specified" do | 
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 25 | server = Thrift::BaseServer.new(mock("Processor"), mock("BaseServerTransport")) | 
|  | 26 | server.instance_variable_get(:'@transport_factory').should be_an_instance_of(Thrift::BaseTransportFactory) | 
|  | 27 | server.instance_variable_get(:'@protocol_factory').should be_an_instance_of(Thrift::BinaryProtocolFactory) | 
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 28 | end | 
|  | 29 |  | 
|  | 30 | # serve is a noop, so can't test that | 
|  | 31 | end | 
|  | 32 |  | 
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 33 | describe Thrift::SimpleServer do | 
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 34 | before(:each) do | 
|  | 35 | @processor = mock("Processor") | 
|  | 36 | @serverTrans = mock("ServerTransport") | 
| Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 37 | @trans = mock("BaseTransport") | 
|  | 38 | @prot = mock("BaseProtocol") | 
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 39 | @client = mock("Client") | 
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 40 | @server = described_class.new(@processor, @serverTrans, @trans, @prot) | 
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 41 | end | 
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 42 |  | 
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 43 | it "should serve in the main thread" do | 
|  | 44 | @serverTrans.should_receive(:listen).ordered | 
|  | 45 | @serverTrans.should_receive(:accept).exactly(3).times.and_return(@client) | 
|  | 46 | @trans.should_receive(:get_transport).exactly(3).times.with(@client).and_return(@trans) | 
|  | 47 | @prot.should_receive(:get_protocol).exactly(3).times.with(@trans).and_return(@prot) | 
|  | 48 | x = 0 | 
|  | 49 | @processor.should_receive(:process).exactly(3).times.with(@prot, @prot).and_return do | 
|  | 50 | case (x += 1) | 
| Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 51 | when 1 then raise Thrift::TransportException | 
|  | 52 | when 2 then raise Thrift::ProtocolException | 
|  | 53 | when 3 then throw :stop | 
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 54 | end | 
|  | 55 | end | 
|  | 56 | @trans.should_receive(:close).exactly(3).times | 
|  | 57 | @serverTrans.should_receive(:close).ordered | 
|  | 58 | lambda { @server.serve }.should throw_symbol(:stop) | 
|  | 59 | end | 
|  | 60 | end | 
|  | 61 |  | 
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 62 | describe Thrift::ThreadedServer do | 
|  | 63 | before(:each) do | 
|  | 64 | @processor = mock("Processor") | 
|  | 65 | @serverTrans = mock("ServerTransport") | 
|  | 66 | @trans = mock("BaseTransport") | 
|  | 67 | @prot = mock("BaseProtocol") | 
|  | 68 | @client = mock("Client") | 
|  | 69 | @server = described_class.new(@processor, @serverTrans, @trans, @prot) | 
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 70 | end | 
|  | 71 |  | 
|  | 72 | it "should serve using threads" do | 
|  | 73 | @serverTrans.should_receive(:listen).ordered | 
|  | 74 | @serverTrans.should_receive(:accept).exactly(3).times.and_return(@client) | 
|  | 75 | @trans.should_receive(:get_transport).exactly(3).times.with(@client).and_return(@trans) | 
|  | 76 | @prot.should_receive(:get_protocol).exactly(3).times.with(@trans).and_return(@prot) | 
|  | 77 | Thread.should_receive(:new).with(@prot, @trans).exactly(3).times.and_yield(@prot, @trans) | 
|  | 78 | x = 0 | 
|  | 79 | @processor.should_receive(:process).exactly(3).times.with(@prot, @prot).and_return do | 
|  | 80 | case (x += 1) | 
| Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 81 | when 1 then raise Thrift::TransportException | 
|  | 82 | when 2 then raise Thrift::ProtocolException | 
|  | 83 | when 3 then throw :stop | 
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 84 | end | 
|  | 85 | end | 
|  | 86 | @trans.should_receive(:close).exactly(3).times | 
|  | 87 | @serverTrans.should_receive(:close).ordered | 
|  | 88 | lambda { @server.serve }.should throw_symbol(:stop) | 
|  | 89 | end | 
|  | 90 | end | 
|  | 91 |  | 
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 92 | describe Thrift::ThreadPoolServer do | 
|  | 93 | before(:each) do | 
|  | 94 | @processor = mock("Processor") | 
|  | 95 | @serverTrans = mock("ServerTransport") | 
|  | 96 | @trans = mock("BaseTransport") | 
|  | 97 | @prot = mock("BaseProtocol") | 
|  | 98 | @client = mock("Client") | 
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 99 | @threadQ = mock("SizedQueue") | 
|  | 100 | SizedQueue.should_receive(:new).with(20).and_return(@threadQ) | 
|  | 101 | @excQ = mock("Queue") | 
|  | 102 | Queue.should_receive(:new).and_return(@excQ) | 
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 103 | @server = described_class.new(@processor, @serverTrans, @trans, @prot) | 
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 104 | end | 
|  | 105 |  | 
|  | 106 | it "should set up the queues" do | 
|  | 107 | @server.instance_variable_get(:'@thread_q').should be(@threadQ) | 
|  | 108 | @server.instance_variable_get(:'@exception_q').should be(@excQ) | 
|  | 109 | end | 
|  | 110 |  | 
|  | 111 | it "should serve inside a thread" do | 
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 112 | @server.should_receive(:serve) | 
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 113 | @excQ.should_receive(:pop).and_throw(:popped) | 
|  | 114 | lambda { @server.rescuable_serve }.should throw_symbol(:popped) | 
|  | 115 | end | 
|  | 116 |  | 
|  | 117 | it "should avoid running the server twice when retrying rescuable_serve" do | 
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 118 | @server.should_receive(:serve) | 
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 119 | @excQ.should_receive(:pop).twice.and_throw(:popped) | 
|  | 120 | lambda { @server.rescuable_serve }.should throw_symbol(:popped) | 
|  | 121 | lambda { @server.rescuable_serve }.should throw_symbol(:popped) | 
|  | 122 | end | 
|  | 123 |  | 
|  | 124 | it "should serve using a thread pool" do | 
|  | 125 | @serverTrans.should_receive(:listen).ordered | 
|  | 126 | @threadQ.should_receive(:push).with(:token) | 
|  | 127 | @threadQ.should_receive(:pop) | 
|  | 128 | Thread.should_receive(:new).and_yield | 
|  | 129 | @serverTrans.should_receive(:accept).exactly(3).times.and_return(@client) | 
|  | 130 | @trans.should_receive(:get_transport).exactly(3).times.and_return(@trans) | 
|  | 131 | @prot.should_receive(:get_protocol).exactly(3).times.and_return(@prot) | 
|  | 132 | x = 0 | 
|  | 133 | error = RuntimeError.new("Stopped") | 
|  | 134 | @processor.should_receive(:process).exactly(3).times.with(@prot, @prot).and_return do | 
|  | 135 | case (x += 1) | 
| Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 136 | when 1 then raise Thrift::TransportException | 
|  | 137 | when 2 then raise Thrift::ProtocolException | 
|  | 138 | when 3 then raise error | 
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 139 | end | 
|  | 140 | end | 
|  | 141 | @trans.should_receive(:close).exactly(3).times | 
|  | 142 | @excQ.should_receive(:push).with(error).and_throw(:stop) | 
|  | 143 | @serverTrans.should_receive(:close) | 
|  | 144 | lambda { @server.serve }.should throw_symbol(:stop) | 
|  | 145 | end | 
|  | 146 | end | 
|  | 147 | end |