blob: eaecbda09c7409dca943bf09fb426dc4061e90f2 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001#
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 Farrella87810f2012-09-28 01:59:04 +000019require 'spec_helper'
Kevin Clarkccc86582008-06-18 01:09:00 +000020
Jake Farrella87810f2012-09-28 01:59:04 +000021describe 'Server' do
Kevin Clarkccc86582008-06-18 01:09:00 +000022
Jake Farrella87810f2012-09-28 01:59:04 +000023 describe Thrift::BaseServer do
Bryan Duxburyd1d15422009-04-04 00:58:03 +000024 it "should default to BaseTransportFactory and BinaryProtocolFactory when not specified" do
Jake Farrella87810f2012-09-28 01:59:04 +000025 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 Clarkccc86582008-06-18 01:09:00 +000028 end
29
30 # serve is a noop, so can't test that
31 end
32
Jake Farrella87810f2012-09-28 01:59:04 +000033 describe Thrift::SimpleServer do
Kevin Clarkccc86582008-06-18 01:09:00 +000034 before(:each) do
35 @processor = mock("Processor")
36 @serverTrans = mock("ServerTransport")
Bryan Duxburyd1d15422009-04-04 00:58:03 +000037 @trans = mock("BaseTransport")
38 @prot = mock("BaseProtocol")
Kevin Clarkccc86582008-06-18 01:09:00 +000039 @client = mock("Client")
Jake Farrella87810f2012-09-28 01:59:04 +000040 @server = described_class.new(@processor, @serverTrans, @trans, @prot)
Kevin Clarkccc86582008-06-18 01:09:00 +000041 end
Jake Farrella87810f2012-09-28 01:59:04 +000042
Kevin Clarkccc86582008-06-18 01:09:00 +000043 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 Duxburye3ab50d2009-03-25 21:06:53 +000051 when 1 then raise Thrift::TransportException
52 when 2 then raise Thrift::ProtocolException
53 when 3 then throw :stop
Kevin Clarkccc86582008-06-18 01:09:00 +000054 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 Farrella87810f2012-09-28 01:59:04 +000062 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 Clarkccc86582008-06-18 01:09:00 +000070 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 Duxburye3ab50d2009-03-25 21:06:53 +000081 when 1 then raise Thrift::TransportException
82 when 2 then raise Thrift::ProtocolException
83 when 3 then throw :stop
Kevin Clarkccc86582008-06-18 01:09:00 +000084 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 Farrella87810f2012-09-28 01:59:04 +000092 describe Thrift::ThreadPoolServer do
93 before(:each) do
94 @processor = mock("Processor")
Jake Farrell96be0072012-10-06 00:26:28 +000095 @server_trans = mock("ServerTransport")
Jake Farrella87810f2012-09-28 01:59:04 +000096 @trans = mock("BaseTransport")
97 @prot = mock("BaseProtocol")
98 @client = mock("Client")
Jake Farrell96be0072012-10-06 00:26:28 +000099 @server = described_class.new(@processor, @server_trans, @trans, @prot)
Kevin Clarkccc86582008-06-18 01:09:00 +0000100 end
101
102 it "should serve inside a thread" do
Jake Farrell96be0072012-10-06 00:26:28 +0000103 exception_q = @server.instance_variable_get(:@exception_q)
104 described_class.any_instance.should_receive(:serve) do
105 exception_q.push(StandardError.new('ERROR'))
106 end
107 expect { @server.rescuable_serve }.to(raise_error('ERROR'))
Kevin Clarkccc86582008-06-18 01:09:00 +0000108 end
109
110 it "should avoid running the server twice when retrying rescuable_serve" do
Jake Farrell96be0072012-10-06 00:26:28 +0000111 exception_q = @server.instance_variable_get(:@exception_q)
112 described_class.any_instance.should_receive(:serve) do
113 exception_q.push(StandardError.new('ERROR1'))
114 exception_q.push(StandardError.new('ERROR2'))
115 end
116 expect { @server.rescuable_serve }.to(raise_error('ERROR1'))
117 expect { @server.rescuable_serve }.to(raise_error('ERROR2'))
Kevin Clarkccc86582008-06-18 01:09:00 +0000118 end
119
120 it "should serve using a thread pool" do
Jake Farrell96be0072012-10-06 00:26:28 +0000121 thread_q = mock("SizedQueue")
122 exception_q = mock("Queue")
123 @server.instance_variable_set(:@thread_q, thread_q)
124 @server.instance_variable_set(:@exception_q, exception_q)
125 @server_trans.should_receive(:listen).ordered
126 thread_q.should_receive(:push).with(:token)
127 thread_q.should_receive(:pop)
Kevin Clarkccc86582008-06-18 01:09:00 +0000128 Thread.should_receive(:new).and_yield
Jake Farrell96be0072012-10-06 00:26:28 +0000129 @server_trans.should_receive(:accept).exactly(3).times.and_return(@client)
Kevin Clarkccc86582008-06-18 01:09:00 +0000130 @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 Duxburye3ab50d2009-03-25 21:06:53 +0000136 when 1 then raise Thrift::TransportException
137 when 2 then raise Thrift::ProtocolException
138 when 3 then raise error
Kevin Clarkccc86582008-06-18 01:09:00 +0000139 end
140 end
141 @trans.should_receive(:close).exactly(3).times
Jake Farrell96be0072012-10-06 00:26:28 +0000142 exception_q.should_receive(:push).with(error).and_throw(:stop)
143 @server_trans.should_receive(:close)
144 expect { @server.serve }.to(throw_symbol(:stop))
Kevin Clarkccc86582008-06-18 01:09:00 +0000145 end
146 end
147end