Sha256: 55344d4d3265fb9c18068ad951c377955f85c0a7a2842172ce7b60cb8a68842f

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 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/udp'

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

    describe '#start' do
      it 'listens on an UDP port and passes received inputs to the handler' do
        server = Server::UDP.new(addr: addr, handler: handler = fake(:handler))
        server.start
        foo = UDPSocket.new
        bar = UDPSocket.new
        3.times do
          foo.send 'DATA', 0, *server.addr
          bar.send 'PING', 0, *server.addr
        end
        run_all_threads
        server.stop
        handler.must_have_received :handle, [Message.parse('DATA'), any(Addr)]
        handler.must_have_received :handle, [Message.parse('PING'), any(Addr)]
      end

      it 'doesn’t blow up on unknown inputs' do
        server = Server::UDP.new(addr: addr)
        server.start
        UDPSocket.new.send 'foo', 0, *server.addr
        run_all_threads
        server.stop
      end
    end

    describe '#stop' do
      it 'closes the socket (and thus allows rebinding to it)' do
        Server::UDP.new(addr: addr).start.stop
        UDPSocket.new.tap { |socket| socket.bind(*addr) }.close
      end
    end

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

Version data entries

1 entries across 1 versions & 1 rubygems

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