blob: bc4d598bbe80a4f4d9a8eb0b7d72d3e017e2bce0 [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
James E. King III9aaf2952018-03-20 15:06:08 -040024 before(:each) do
25 @processor = mock("Processor")
26 @serverTrans = mock("ServerTransport")
27 @trans = mock("BaseTransport")
28 @prot = mock("BaseProtocol")
29 @server = described_class.new(@processor, @serverTrans, @trans, @prot)
Kevin Clarkccc86582008-06-18 01:09:00 +000030 end
31
James E. King III9aaf2952018-03-20 15:06:08 -040032 it "should default to BaseTransportFactory and BinaryProtocolFactory when not specified" do
33 @server = Thrift::BaseServer.new(mock("Processor"), mock("BaseServerTransport"))
34 @server.instance_variable_get(:'@transport_factory').should be_an_instance_of(Thrift::BaseTransportFactory)
35 @server.instance_variable_get(:'@protocol_factory').should be_an_instance_of(Thrift::BinaryProtocolFactory)
36 end
37
38 it "should not serve" do
39 expect { @server.serve()}.to raise_error(NotImplementedError)
40 end
41
42 it "should provide a reasonable to_s" do
43 @serverTrans.should_receive(:to_s).once.and_return("serverTrans")
44 @trans.should_receive(:to_s).once.and_return("trans")
45 @prot.should_receive(:to_s).once.and_return("prot")
46 @server.to_s.should == "server(prot(trans(serverTrans)))"
47 end
Kevin Clarkccc86582008-06-18 01:09:00 +000048 end
49
Jake Farrella87810f2012-09-28 01:59:04 +000050 describe Thrift::SimpleServer do
Kevin Clarkccc86582008-06-18 01:09:00 +000051 before(:each) do
52 @processor = mock("Processor")
53 @serverTrans = mock("ServerTransport")
Bryan Duxburyd1d15422009-04-04 00:58:03 +000054 @trans = mock("BaseTransport")
55 @prot = mock("BaseProtocol")
Kevin Clarkccc86582008-06-18 01:09:00 +000056 @client = mock("Client")
Jake Farrella87810f2012-09-28 01:59:04 +000057 @server = described_class.new(@processor, @serverTrans, @trans, @prot)
Kevin Clarkccc86582008-06-18 01:09:00 +000058 end
Jake Farrella87810f2012-09-28 01:59:04 +000059
James E. King III9aaf2952018-03-20 15:06:08 -040060 it "should provide a reasonable to_s" do
61 @serverTrans.should_receive(:to_s).once.and_return("serverTrans")
62 @trans.should_receive(:to_s).once.and_return("trans")
63 @prot.should_receive(:to_s).once.and_return("prot")
64 @server.to_s.should == "simple(server(prot(trans(serverTrans))))"
65 end
66
Kevin Clarkccc86582008-06-18 01:09:00 +000067 it "should serve in the main thread" do
68 @serverTrans.should_receive(:listen).ordered
69 @serverTrans.should_receive(:accept).exactly(3).times.and_return(@client)
70 @trans.should_receive(:get_transport).exactly(3).times.with(@client).and_return(@trans)
71 @prot.should_receive(:get_protocol).exactly(3).times.with(@trans).and_return(@prot)
72 x = 0
73 @processor.should_receive(:process).exactly(3).times.with(@prot, @prot).and_return do
74 case (x += 1)
Bryan Duxburye3ab50d2009-03-25 21:06:53 +000075 when 1 then raise Thrift::TransportException
76 when 2 then raise Thrift::ProtocolException
77 when 3 then throw :stop
Kevin Clarkccc86582008-06-18 01:09:00 +000078 end
79 end
80 @trans.should_receive(:close).exactly(3).times
81 @serverTrans.should_receive(:close).ordered
82 lambda { @server.serve }.should throw_symbol(:stop)
83 end
84 end
85
Jake Farrella87810f2012-09-28 01:59:04 +000086 describe Thrift::ThreadedServer do
87 before(:each) do
88 @processor = mock("Processor")
89 @serverTrans = mock("ServerTransport")
90 @trans = mock("BaseTransport")
91 @prot = mock("BaseProtocol")
92 @client = mock("Client")
93 @server = described_class.new(@processor, @serverTrans, @trans, @prot)
Kevin Clarkccc86582008-06-18 01:09:00 +000094 end
95
James E. King III9aaf2952018-03-20 15:06:08 -040096 it "should provide a reasonable to_s" do
97 @serverTrans.should_receive(:to_s).once.and_return("serverTrans")
98 @trans.should_receive(:to_s).once.and_return("trans")
99 @prot.should_receive(:to_s).once.and_return("prot")
100 @server.to_s.should == "threaded(server(prot(trans(serverTrans))))"
101 end
102
Kevin Clarkccc86582008-06-18 01:09:00 +0000103 it "should serve using threads" do
104 @serverTrans.should_receive(:listen).ordered
105 @serverTrans.should_receive(:accept).exactly(3).times.and_return(@client)
106 @trans.should_receive(:get_transport).exactly(3).times.with(@client).and_return(@trans)
107 @prot.should_receive(:get_protocol).exactly(3).times.with(@trans).and_return(@prot)
108 Thread.should_receive(:new).with(@prot, @trans).exactly(3).times.and_yield(@prot, @trans)
109 x = 0
110 @processor.should_receive(:process).exactly(3).times.with(@prot, @prot).and_return do
111 case (x += 1)
Bryan Duxburye3ab50d2009-03-25 21:06:53 +0000112 when 1 then raise Thrift::TransportException
113 when 2 then raise Thrift::ProtocolException
114 when 3 then throw :stop
Kevin Clarkccc86582008-06-18 01:09:00 +0000115 end
116 end
117 @trans.should_receive(:close).exactly(3).times
118 @serverTrans.should_receive(:close).ordered
119 lambda { @server.serve }.should throw_symbol(:stop)
120 end
121 end
122
Jake Farrella87810f2012-09-28 01:59:04 +0000123 describe Thrift::ThreadPoolServer do
124 before(:each) do
125 @processor = mock("Processor")
Jake Farrell96be0072012-10-06 00:26:28 +0000126 @server_trans = mock("ServerTransport")
Jake Farrella87810f2012-09-28 01:59:04 +0000127 @trans = mock("BaseTransport")
128 @prot = mock("BaseProtocol")
129 @client = mock("Client")
Jake Farrell96be0072012-10-06 00:26:28 +0000130 @server = described_class.new(@processor, @server_trans, @trans, @prot)
James E. King III9aaf2952018-03-20 15:06:08 -0400131 sleep(0.15)
Kevin Clarkccc86582008-06-18 01:09:00 +0000132 end
133
James E. King III9aaf2952018-03-20 15:06:08 -0400134 it "should provide a reasonable to_s" do
135 @server_trans.should_receive(:to_s).once.and_return("server_trans")
136 @trans.should_receive(:to_s).once.and_return("trans")
137 @prot.should_receive(:to_s).once.and_return("prot")
138 @server.to_s.should == "threadpool(server(prot(trans(server_trans))))"
139 end
140
Kevin Clarkccc86582008-06-18 01:09:00 +0000141 it "should serve inside a thread" do
Jake Farrell96be0072012-10-06 00:26:28 +0000142 exception_q = @server.instance_variable_get(:@exception_q)
143 described_class.any_instance.should_receive(:serve) do
144 exception_q.push(StandardError.new('ERROR'))
145 end
146 expect { @server.rescuable_serve }.to(raise_error('ERROR'))
Kevin Clarkccc86582008-06-18 01:09:00 +0000147 end
148
149 it "should avoid running the server twice when retrying rescuable_serve" do
Jake Farrell96be0072012-10-06 00:26:28 +0000150 exception_q = @server.instance_variable_get(:@exception_q)
151 described_class.any_instance.should_receive(:serve) do
152 exception_q.push(StandardError.new('ERROR1'))
153 exception_q.push(StandardError.new('ERROR2'))
154 end
155 expect { @server.rescuable_serve }.to(raise_error('ERROR1'))
156 expect { @server.rescuable_serve }.to(raise_error('ERROR2'))
Kevin Clarkccc86582008-06-18 01:09:00 +0000157 end
158
159 it "should serve using a thread pool" do
Jake Farrell96be0072012-10-06 00:26:28 +0000160 thread_q = mock("SizedQueue")
161 exception_q = mock("Queue")
162 @server.instance_variable_set(:@thread_q, thread_q)
163 @server.instance_variable_set(:@exception_q, exception_q)
164 @server_trans.should_receive(:listen).ordered
165 thread_q.should_receive(:push).with(:token)
166 thread_q.should_receive(:pop)
Kevin Clarkccc86582008-06-18 01:09:00 +0000167 Thread.should_receive(:new).and_yield
Jake Farrell96be0072012-10-06 00:26:28 +0000168 @server_trans.should_receive(:accept).exactly(3).times.and_return(@client)
Kevin Clarkccc86582008-06-18 01:09:00 +0000169 @trans.should_receive(:get_transport).exactly(3).times.and_return(@trans)
170 @prot.should_receive(:get_protocol).exactly(3).times.and_return(@prot)
171 x = 0
172 error = RuntimeError.new("Stopped")
173 @processor.should_receive(:process).exactly(3).times.with(@prot, @prot).and_return do
174 case (x += 1)
Bryan Duxburye3ab50d2009-03-25 21:06:53 +0000175 when 1 then raise Thrift::TransportException
176 when 2 then raise Thrift::ProtocolException
177 when 3 then raise error
Kevin Clarkccc86582008-06-18 01:09:00 +0000178 end
179 end
180 @trans.should_receive(:close).exactly(3).times
Jake Farrell96be0072012-10-06 00:26:28 +0000181 exception_q.should_receive(:push).with(error).and_throw(:stop)
182 @server_trans.should_receive(:close)
183 expect { @server.serve }.to(throw_symbol(:stop))
Kevin Clarkccc86582008-06-18 01:09:00 +0000184 end
185 end
186end