| 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 |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 22 | describe Thrift::BaseServer do |
| James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 23 | before(:each) do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 24 | @processor = double("Processor") |
| 25 | @serverTrans = double("ServerTransport") |
| 26 | @trans = double("BaseTransport") |
| 27 | @prot = double("BaseProtocol") |
| James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 28 | @server = described_class.new(@processor, @serverTrans, @trans, @prot) |
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 29 | end |
| 30 | |
| James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 31 | it "should default to BaseTransportFactory and BinaryProtocolFactory when not specified" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 32 | @server = Thrift::BaseServer.new(double("Processor"), double("BaseServerTransport")) |
| 33 | expect(@server.instance_variable_get(:'@transport_factory')).to be_an_instance_of(Thrift::BaseTransportFactory) |
| 34 | expect(@server.instance_variable_get(:'@protocol_factory')).to be_an_instance_of(Thrift::BinaryProtocolFactory) |
| James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 35 | end |
| 36 | |
| 37 | it "should not serve" do |
| Dmytro Shteflyuk | 3b0ab4d | 2026-03-11 17:46:48 -0400 | [diff] [blame^] | 38 | expect { @server.serve() }.to raise_error(NotImplementedError) |
| James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 39 | end |
| Dmytro Shteflyuk | f5c80a4 | 2026-03-08 19:09:43 -0400 | [diff] [blame] | 40 | |
| James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 41 | it "should provide a reasonable to_s" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 42 | expect(@serverTrans).to receive(:to_s).once.and_return("serverTrans") |
| 43 | expect(@trans).to receive(:to_s).once.and_return("trans") |
| 44 | expect(@prot).to receive(:to_s).once.and_return("prot") |
| 45 | expect(@server.to_s).to eq("server(prot(trans(serverTrans)))") |
| James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 46 | end |
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 47 | end |
| 48 | |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 49 | describe Thrift::SimpleServer do |
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 50 | before(:each) do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 51 | @processor = double("Processor") |
| 52 | @serverTrans = double("ServerTransport") |
| 53 | @trans = double("BaseTransport") |
| 54 | @prot = double("BaseProtocol") |
| 55 | @client = double("Client") |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 56 | @server = described_class.new(@processor, @serverTrans, @trans, @prot) |
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 57 | end |
| Dmytro Shteflyuk | f5c80a4 | 2026-03-08 19:09:43 -0400 | [diff] [blame] | 58 | |
| James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 59 | it "should provide a reasonable to_s" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 60 | expect(@serverTrans).to receive(:to_s).once.and_return("serverTrans") |
| 61 | expect(@trans).to receive(:to_s).once.and_return("trans") |
| 62 | expect(@prot).to receive(:to_s).once.and_return("prot") |
| 63 | expect(@server.to_s).to eq("simple(server(prot(trans(serverTrans))))") |
| James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 64 | end |
| Dmytro Shteflyuk | f5c80a4 | 2026-03-08 19:09:43 -0400 | [diff] [blame] | 65 | |
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 66 | it "should serve in the main thread" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 67 | expect(@serverTrans).to receive(:listen).ordered |
| 68 | expect(@serverTrans).to receive(:accept).exactly(3).times.and_return(@client) |
| 69 | expect(@trans).to receive(:get_transport).exactly(3).times.with(@client).and_return(@trans) |
| 70 | expect(@prot).to receive(:get_protocol).exactly(3).times.with(@trans).and_return(@prot) |
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 71 | x = 0 |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 72 | expect(@processor).to receive(:process).exactly(3).times.with(@prot, @prot) do |
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 73 | case (x += 1) |
| Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 74 | when 1 then raise Thrift::TransportException |
| 75 | when 2 then raise Thrift::ProtocolException |
| 76 | when 3 then throw :stop |
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 77 | end |
| 78 | end |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 79 | expect(@trans).to receive(:close).exactly(3).times |
| 80 | expect(@serverTrans).to receive(:close).ordered |
| 81 | expect { @server.serve }.to throw_symbol(:stop) |
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 82 | end |
| 83 | end |
| 84 | |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 85 | describe Thrift::ThreadedServer do |
| 86 | before(:each) do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 87 | @processor = double("Processor") |
| 88 | @serverTrans = double("ServerTransport") |
| 89 | @trans = double("BaseTransport") |
| 90 | @prot = double("BaseProtocol") |
| 91 | @client = double("Client") |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 92 | @server = described_class.new(@processor, @serverTrans, @trans, @prot) |
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 93 | end |
| 94 | |
| James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 95 | it "should provide a reasonable to_s" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 96 | expect(@serverTrans).to receive(:to_s).once.and_return("serverTrans") |
| 97 | expect(@trans).to receive(:to_s).once.and_return("trans") |
| 98 | expect(@prot).to receive(:to_s).once.and_return("prot") |
| 99 | expect(@server.to_s).to eq("threaded(server(prot(trans(serverTrans))))") |
| James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 100 | end |
| Dmytro Shteflyuk | f5c80a4 | 2026-03-08 19:09:43 -0400 | [diff] [blame] | 101 | |
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 102 | it "should serve using threads" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 103 | expect(@serverTrans).to receive(:listen).ordered |
| 104 | expect(@serverTrans).to receive(:accept).exactly(3).times.and_return(@client) |
| 105 | expect(@trans).to receive(:get_transport).exactly(3).times.with(@client).and_return(@trans) |
| 106 | expect(@prot).to receive(:get_protocol).exactly(3).times.with(@trans).and_return(@prot) |
| 107 | expect(Thread).to receive(:new).with(@prot, @trans).exactly(3).times.and_yield(@prot, @trans) |
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 108 | x = 0 |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 109 | expect(@processor).to receive(:process).exactly(3).times.with(@prot, @prot) do |
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 110 | case (x += 1) |
| Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 111 | when 1 then raise Thrift::TransportException |
| 112 | when 2 then raise Thrift::ProtocolException |
| 113 | when 3 then throw :stop |
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 114 | end |
| 115 | end |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 116 | expect(@trans).to receive(:close).exactly(3).times |
| 117 | expect(@serverTrans).to receive(:close).ordered |
| 118 | expect { @server.serve }.to throw_symbol(:stop) |
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 119 | end |
| 120 | end |
| 121 | |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 122 | describe Thrift::ThreadPoolServer do |
| 123 | before(:each) do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 124 | @processor = double("Processor") |
| 125 | @server_trans = double("ServerTransport") |
| 126 | @trans = double("BaseTransport") |
| 127 | @prot = double("BaseProtocol") |
| 128 | @client = double("Client") |
| Jake Farrell | 96be007 | 2012-10-06 00:26:28 +0000 | [diff] [blame] | 129 | @server = described_class.new(@processor, @server_trans, @trans, @prot) |
| James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 130 | sleep(0.15) |
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 131 | end |
| 132 | |
| James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 133 | it "should provide a reasonable to_s" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 134 | expect(@server_trans).to receive(:to_s).once.and_return("server_trans") |
| 135 | expect(@trans).to receive(:to_s).once.and_return("trans") |
| 136 | expect(@prot).to receive(:to_s).once.and_return("prot") |
| 137 | expect(@server.to_s).to eq("threadpool(server(prot(trans(server_trans))))") |
| James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 138 | end |
| Dmytro Shteflyuk | f5c80a4 | 2026-03-08 19:09:43 -0400 | [diff] [blame] | 139 | |
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 140 | it "should serve inside a thread" do |
| Jake Farrell | 96be007 | 2012-10-06 00:26:28 +0000 | [diff] [blame] | 141 | exception_q = @server.instance_variable_get(:@exception_q) |
| Dmytro Shteflyuk | f5c80a4 | 2026-03-08 19:09:43 -0400 | [diff] [blame] | 142 | expect_any_instance_of(described_class).to receive(:serve) do |
| Jake Farrell | 96be007 | 2012-10-06 00:26:28 +0000 | [diff] [blame] | 143 | exception_q.push(StandardError.new('ERROR')) |
| 144 | end |
| 145 | expect { @server.rescuable_serve }.to(raise_error('ERROR')) |
| James E. King III | b92ca5f | 2018-06-09 23:02:12 -0400 | [diff] [blame] | 146 | sleep(0.15) |
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 147 | end |
| 148 | |
| 149 | it "should avoid running the server twice when retrying rescuable_serve" do |
| Jake Farrell | 96be007 | 2012-10-06 00:26:28 +0000 | [diff] [blame] | 150 | exception_q = @server.instance_variable_get(:@exception_q) |
| Dmytro Shteflyuk | f5c80a4 | 2026-03-08 19:09:43 -0400 | [diff] [blame] | 151 | expect_any_instance_of(described_class).to receive(:serve) do |
| Jake Farrell | 96be007 | 2012-10-06 00:26:28 +0000 | [diff] [blame] | 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 Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 157 | end |
| 158 | |
| 159 | it "should serve using a thread pool" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 160 | thread_q = double("SizedQueue") |
| 161 | exception_q = double("Queue") |
| Jake Farrell | 96be007 | 2012-10-06 00:26:28 +0000 | [diff] [blame] | 162 | @server.instance_variable_set(:@thread_q, thread_q) |
| 163 | @server.instance_variable_set(:@exception_q, exception_q) |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 164 | expect(@server_trans).to receive(:listen).ordered |
| 165 | expect(thread_q).to receive(:push).with(:token) |
| 166 | expect(thread_q).to receive(:pop) |
| 167 | expect(Thread).to receive(:new).and_yield |
| 168 | expect(@server_trans).to receive(:accept).exactly(3).times.and_return(@client) |
| 169 | expect(@trans).to receive(:get_transport).exactly(3).times.and_return(@trans) |
| 170 | expect(@prot).to receive(:get_protocol).exactly(3).times.and_return(@prot) |
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 171 | x = 0 |
| 172 | error = RuntimeError.new("Stopped") |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 173 | expect(@processor).to receive(:process).exactly(3).times.with(@prot, @prot) do |
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 174 | case (x += 1) |
| Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 175 | when 1 then raise Thrift::TransportException |
| 176 | when 2 then raise Thrift::ProtocolException |
| 177 | when 3 then raise error |
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 178 | end |
| 179 | end |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 180 | expect(@trans).to receive(:close).exactly(3).times |
| 181 | expect(exception_q).to receive(:push).with(error).and_throw(:stop) |
| 182 | expect(@server_trans).to receive(:close) |
| Jake Farrell | 96be007 | 2012-10-06 00:26:28 +0000 | [diff] [blame] | 183 | expect { @server.serve }.to(throw_symbol(:stop)) |
| Kevin Clark | ccc8658 | 2008-06-18 01:09:00 +0000 | [diff] [blame] | 184 | end |
| 185 | end |
| 186 | end |