Ruby Thrift::TransportException End of File Reached

989 views Asked by At

I made a Ruby web server based on Apache Thrift, but the client (also in ruby, for unit tests) refuses to work and keeps telling me either Thrift::TransportException: Could not connect to 127.0.0.1:8001: Connection refused - connect(2) for 127.0.0.1:8001, or Thrift::TransportException: end of file reached. Tried a bunch of different server implementations and transports, and that doesn't seem to work.

When the server is running, lsof -i :8001 shows

COMMAND   PID      USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
ruby    19073 lafickens    9u  IPv4 0x878e6fd36b981a71      0t0  TCP *:vcom-tunnel (LISTEN)

So I think the server is functioning.

Here's the server code:

class Server

    attr_reader :name

    def initialize(name)
        @LOGGER = Logger.new $stdout
        @name = name
        @started = false
        @processor = ::Thrift::MultiplexedProcessor.new
        @processor.register_processor 'User Service', Thrift::UserService::Processor.new(Handlers::UserServiceHandler.new)
        @processor.register_processor 'Sync Service', Thrift::SyncService::Processor.new(Handlers::SyncServiceHandler.new)
    end

    def start
        @transport = ::Thrift::ServerSocket.new(Options.get('port'))
        @transport_factory = ::Thrift::BufferedTransportFactory.new
        @protocol_factory = ::Thrift::CompactProtocolFactory.new
        @server = ::Thrift::ThreadPoolServer.new @processor, @transport, @transport_factory, @protocol_factory
        @server_thread = Thread.new {
            @server.serve
        }
        @started = true
        @LOGGER.info('Server started successfully')
    end

    def stop
        return if @server_thread.nil?
        @server_thread.exit
        @transport.close
        @started = false
        @LOGGER.info('Server stopped successfully')
    end

    def restart
        stop
        start
    end

    def started?
        @started
    end
end

Client code (actually unit tests)

class TestUserServiceHandler < Test::Unit::TestCase
    def setup
        @server = Billboard::Server.new 'test handler'
        @server.start
        @port = Billboard::Options.get 'port'
        @transport = ::Thrift::BufferedTransport.new(::Thrift::Socket.new('127.0.0.1', @port))
        @binary_protocol = ::Thrift::BinaryProtocol.new @transport
        @multiplexed_protocol = ::Thrift::MultiplexedProtocol.new @binary_protocol, 'mprotocol'
        @client = Billboard::Thrift::UserService::Client.new @multiplexed_protocol
        @transport.open
    end

    def teardown
        @server.stop
        @transport.close
    end

    def test_authenticate
        @client.authenticate('test', 'test')
    end

    # And other tests...
end

Thanks in advance.

0

There are 0 answers