<commit_before>require 'spec_helper'

describe LIFX::Transport::UDP, integration: true do
  subject(:udp) { LIFX::Transport::UDP.new(host, port) }

  let(:host) { 'localhost' }
  let(:message) { double }
  let(:port) { 45_828 }

  describe '#write' do
    let(:payload) { double }

    it 'writes a Message to specified host' do
      expect(message).to receive(:pack).and_return(payload)
      expect_any_instance_of(UDPSocket).to receive(:send)
                                           .with(payload, 0, host, port)
      udp.write(message)
    end
  end

  describe '#listen' do
    let(:raw_message) { 'some binary data' }
    let(:socket) { UDPSocket.new }

    it 'listens to the specified socket data, unpacks it and notifies observers' do
      messages = []
      udp.add_observer(self) do |message: nil, ip: nil, transport: nil|
        messages << message
      end
      udp.listen

      expect(LIFX::Message).to receive(:unpack)
                               .with(raw_message)
                               .and_return(message)
      socket.send(raw_message, 0, host, port)
      wait { expect(messages).to include(message) }
    end<commit_msg>Use namespaces in UDP spec<commit_after>require 'spec_helper'

module LIFX
  describe Transport::UDP, integration: true do
    subject(:udp) { Transport::UDP.new(host, port) }

    let(:host) { 'localhost' }
    let(:message) { double }
    let(:port) { 45_828 }

    describe '#write' do
      let(:payload) { double }

      it 'writes a Message to specified host' do
        expect(message).to receive(:pack).and_return(payload)
        expect_any_instance_of(UDPSocket).to receive(:send)
                                             .with(payload, 0, host, port)
        udp.write(message)
      end
    end

    describe '#listen' do
      let(:raw_message) { 'some binary data' }
      let(:socket) { UDPSocket.new }
      let(:messages) { [] }
      before do
        udp.add_observer(self) do |message: nil, ip: nil, transport: nil|
          messages << message
        end
        udp.listen
      end

      it 'listens to the specified socket data, unpacks it and notifies observers' do
        expect(Message).to receive(:unpack)
                                 .with(raw_message)
                                 .and_return(message)
        socket.send(raw_message, 0, host, port)
        wait { expect(messages).to include(message) }
      end
    end
{'fraction_non_alphanumeric': 0.1118421052631579, 'fraction_numerical': 0.005756578947368421, 'mean_word_length': 2.180392156862745, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3278312', 'n_tokens_mistral': 739, 'n_tokens_neox': 709, 'n_words': 183}