Class: DnsMock::Server

Inherits:
Object
  • Object
show all
Includes:
Error::Helper
Defined in:
lib/dns_mock/server.rb,
lib/dns_mock/server/records_dictionary_builder.rb

Defined Under Namespace

Classes: RecordsDictionaryBuilder

Constant Summary collapse

CURRENT_HOST_NAME =
'localhost'
CURRENT_HOST_ADDRESS =
'127.0.0.1'
WARMUP_DELAY =
0.1
PACKET_MAX_BYTES_SIZE =
65_535

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Error::Helper

#raise_record_context_type_error, #raise_record_type_error

Constructor Details

#initialize(socket = ::UDPSocket.new, records_dictionary_builder = DnsMock::Server::RecordsDictionaryBuilder, thread_class = ::Thread, records: nil, port: nil, exception_if_not_found: false) ⇒ Server

rubocop:disable Metrics/ParameterLists



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dns_mock/server.rb', line 14

def initialize( # rubocop:disable Metrics/ParameterLists
  socket = ::UDPSocket.new,
  records_dictionary_builder = DnsMock::Server::RecordsDictionaryBuilder,
  thread_class = ::Thread,
  records: nil,
  port: nil,
  exception_if_not_found: false
)
  @socket = socket
  @records_dictionary_builder = records_dictionary_builder
  @thread_class = thread_class
  @records = records_dictionary_builder.call(records || {})
  @port = port || 0
  @exception_if_not_found = exception_if_not_found
  @messages = []
  prepare_server_thread
end

Instance Attribute Details

#messagesObject (readonly)

Returns the value of attribute messages.



12
13
14
# File 'lib/dns_mock/server.rb', line 12

def messages
  @messages
end

#portObject

Returns the value of attribute port.



12
13
14
# File 'lib/dns_mock/server.rb', line 12

def port
  @port
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/dns_mock/server.rb', line 67

def alive?
  thread.alive?
end

#assign_mocks(new_records) ⇒ Object



51
52
53
# File 'lib/dns_mock/server.rb', line 51

def assign_mocks(new_records)
  !!self.records = records_dictionary_builder.call(new_records) if records.empty?
end

#clear_messages!Object



59
60
61
# File 'lib/dns_mock/server.rb', line 59

def clear_messages!
  !!messages.clear
end

#reset_mocks!Object



55
56
57
# File 'lib/dns_mock/server.rb', line 55

def reset_mocks!
  records.clear.empty?
end

#runObject

rubocop:disable Metrics/AbcSize



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dns_mock/server.rb', line 32

def run # rubocop:disable Metrics/AbcSize
  prepare_socket_for_session
  update_server_port

  begin
    loop do
      packet, addr = socket.recvfrom(DnsMock::Server::PACKET_MAX_BYTES_SIZE)
      break if packet.empty?

      address, port = addr.values_at(3, 1)
      message = DnsMock::Response::Message.new(packet, records, exception_if_not_found)
      socket.send(message.as_binary_string, 0, address, port)
      messages.push(message.dns_message)
    end
  ensure
    socket.close
  end
end

#stop!Object



71
72
73
# File 'lib/dns_mock/server.rb', line 71

def stop!
  thread.kill.stop?
end

#without_mocks?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/dns_mock/server.rb', line 63

def without_mocks?
  records.empty?
end