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") |
Jake Farrell | 96be007 | 2012-10-06 00:26:28 +0000 | [diff] [blame] | 95 | @server_trans = mock("ServerTransport") |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 96 | @trans = mock("BaseTransport") |
| 97 | @prot = mock("BaseProtocol") |
| 98 | @client = mock("Client") |
Jake Farrell | 96be007 | 2012-10-06 00:26:28 +0000 | [diff] [blame] | 99 | @server = described_class.new(@processor, @server_trans, @trans, @prot) |
James E. King, III | 1ce7a5b | 2017-11-30 08:39:17 -0500 | [diff] [blame^] | 100 | sleep(0.1) |
Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 101 | end |
| 102 | |
| 103 | it "should serve inside a thread" do |
Jake Farrell | 96be007 | 2012-10-06 00:26:28 +0000 | [diff] [blame] | 104 | exception_q = @server.instance_variable_get(:@exception_q) |
| 105 | described_class.any_instance.should_receive(:serve) do |
| 106 | exception_q.push(StandardError.new('ERROR')) |
| 107 | end |
| 108 | expect { @server.rescuable_serve }.to(raise_error('ERROR')) |
Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 109 | end |
| 110 | |
| 111 | it "should avoid running the server twice when retrying rescuable_serve" do |
Jake Farrell | 96be007 | 2012-10-06 00:26:28 +0000 | [diff] [blame] | 112 | exception_q = @server.instance_variable_get(:@exception_q) |
| 113 | described_class.any_instance.should_receive(:serve) do |
| 114 | exception_q.push(StandardError.new('ERROR1')) |
| 115 | exception_q.push(StandardError.new('ERROR2')) |
| 116 | end |
| 117 | expect { @server.rescuable_serve }.to(raise_error('ERROR1')) |
| 118 | expect { @server.rescuable_serve }.to(raise_error('ERROR2')) |
Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 119 | end |
| 120 | |
| 121 | it "should serve using a thread pool" do |
Jake Farrell | 96be007 | 2012-10-06 00:26:28 +0000 | [diff] [blame] | 122 | thread_q = mock("SizedQueue") |
| 123 | exception_q = mock("Queue") |
| 124 | @server.instance_variable_set(:@thread_q, thread_q) |
| 125 | @server.instance_variable_set(:@exception_q, exception_q) |
| 126 | @server_trans.should_receive(:listen).ordered |
| 127 | thread_q.should_receive(:push).with(:token) |
| 128 | thread_q.should_receive(:pop) |
Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 129 | Thread.should_receive(:new).and_yield |
Jake Farrell | 96be007 | 2012-10-06 00:26:28 +0000 | [diff] [blame] | 130 | @server_trans.should_receive(:accept).exactly(3).times.and_return(@client) |
Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 131 | @trans.should_receive(:get_transport).exactly(3).times.and_return(@trans) |
| 132 | @prot.should_receive(:get_protocol).exactly(3).times.and_return(@prot) |
| 133 | x = 0 |
| 134 | error = RuntimeError.new("Stopped") |
| 135 | @processor.should_receive(:process).exactly(3).times.with(@prot, @prot).and_return do |
| 136 | case (x += 1) |
Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 137 | when 1 then raise Thrift::TransportException |
| 138 | when 2 then raise Thrift::ProtocolException |
| 139 | when 3 then raise error |
Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 140 | end |
| 141 | end |
| 142 | @trans.should_receive(:close).exactly(3).times |
Jake Farrell | 96be007 | 2012-10-06 00:26:28 +0000 | [diff] [blame] | 143 | exception_q.should_receive(:push).with(error).and_throw(:stop) |
| 144 | @server_trans.should_receive(:close) |
| 145 | expect { @server.serve }.to(throw_symbol(:stop)) |
Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 146 | end |
| 147 | end |
| 148 | end |