Sha256: 21602bc82d0749e66689f6770ca65cb9b12af02d4c6193b776f266c8d52a0b4c

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

require 'socket'
require_relative '../../spec_helper'
require_relative '../../../lib/kamerling/addr'
require_relative '../../../lib/kamerling/handler'
require_relative '../../../lib/kamerling/message'
require_relative '../../../lib/kamerling/server/tcp'

module Kamerling
  describe Server::TCP do
    let(:addr) { Addr['localhost', 1981, :TCP] }

    describe '#addr' do
      it 'returns the server’s host + port as a TCP addr' do
        Server::TCP.new(addr: addr).addr.must_equal addr
      end
    end

    describe '#start' do
      it 'listens on a TCP port and passes received inputs to the handler' do
        server = Server::TCP.new(addr: addr, handler: handler = fake(:handler))
        server.start
        s_addr_foo = TCPSocket.open(*server.addr) do |socket|
          socket << 'DATA'
          Addr[*socket.local_address.ip_unpack, :TCP]
        end
        s_addr_bar = TCPSocket.open(*server.addr) do |socket|
          socket << 'PING'
          Addr[*socket.local_address.ip_unpack, :TCP]
        end
        run_all_threads
        server.stop
        handler.must_have_received :handle, [Message.parse('DATA'), s_addr_foo]
        handler.must_have_received :handle, [Message.parse('PING'), s_addr_bar]
      end

      it 'doesn’t blow up on unknown inputs' do
        server = Server::TCP.new(addr: addr)
        server.start
        TCPSocket.open(*server.addr) { |socket| socket << 'foo' }
        server.stop
      end
    end

    describe '#stop' do
      it 'stops the server' do
        server = Server::TCP.new(addr: addr).start
        server.stop
        -> { TCPSocket.open(*addr).close }.must_raise Errno::ECONNREFUSED
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kamerling-0.0.3 spec/kamerling/server/tcp_spec.rb