<commit_before># frozen_string_literal: true

require 'socket'
require_relative '../test_helper'
require_relative '../../lib/kamerling/addr'
require_relative '../../lib/kamerling/net_dispatcher'

module Kamerling
  describe NetDispatcher do
    describe '#dispatch' do
      it 'dispatches messages to TCP clients' do
        server = TCPServer.open(0)
        thread = Thread.new { server.accept.read }
        addr   = Addr[server.addr[3], server.addr[1], :TCP]
        NetDispatcher.dispatch 'PING', addr: addr
        _(thread.value).must_equal 'PING'
      end

      it 'dispatches messages to UDP clients' do
        server = UDPSocket.new.tap { |s| s.bind '127.0.0.1', 0 }
        thread = Thread.new { server.recvfrom(2**16).first }
        addr   = Addr[server.addr[3], server.addr[1], :UDP]
        NetDispatcher.dispatch 'PING', addr: addr
        _(thread.value).must_equal 'PING'
      end
    end
  end
end
<commit_msg>Switch NetDispatcher test to {TCP,UDP}Addr<commit_after># frozen_string_literal: true

require 'socket'
require_relative '../test_helper'
require_relative '../../lib/kamerling/net_dispatcher'
require_relative '../../lib/kamerling/tcp_addr'
require_relative '../../lib/kamerling/udp_addr'

module Kamerling
  describe NetDispatcher do
    describe '#dispatch' do
      it 'dispatches messages to TCP clients' do
        server = TCPServer.open(0)
        thread = Thread.new { server.accept.read }
        addr   = TCPAddr[server.addr[3], server.addr[1]]
        NetDispatcher.dispatch 'PING', addr: addr
        _(thread.value).must_equal 'PING'
      end

      it 'dispatches messages to UDP clients' do
        server = UDPSocket.new.tap { |s| s.bind '127.0.0.1', 0 }
        thread = Thread.new { server.recvfrom(2**16).first }
        addr   = UDPAddr[server.addr[3], server.addr[1]]
        NetDispatcher.dispatch 'PING', addr: addr
        _(thread.value).must_equal 'PING'
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.12712300566134843, 'fraction_numerical': 0.01544004117344313, 'mean_word_length': 2.8043052837573383, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3937558', 'n_tokens_mistral': 635, 'n_tokens_neox': 611, 'n_words': 148}