Class: Corgibytes::Freshli::Commons::TestServices

Inherits:
Object
  • Object
show all
Includes:
RSpec::Matchers
Defined in:
lib/corgibytes/freshli/commons/test_services.rb

Overview

Controls running test services on specific ports. Used to force a specific port to be in use.

Instance Method Summary collapse

Constructor Details

#initializeTestServices

Returns a new instance of TestServices.



12
13
14
# File 'lib/corgibytes/freshli/commons/test_services.rb', line 12

def initialize
  @test_services = {}
end

Instance Method Details

#start_on(port) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/corgibytes/freshli/commons/test_services.rb', line 16

def start_on(port)
  expect(@test_services).not_to have_key(port)

  socket4 = Socket.new(Socket::Constants::AF_INET, Socket::Constants::SOCK_STREAM, 0)
  socket4.bind(Socket.pack_sockaddr_in(port, '0.0.0.0'))

  begin
    # bind to a socket using both ipv4 and ipv6
    socket6 = Socket.new(Socket::Constants::AF_INET6, Socket::Constants::SOCK_STREAM, 0)
    socket6.bind(Socket.pack_sockaddr_in(port, '::'))
  rescue Errno::EADDRINUSE
    # if ipv6 is not available, then just use ipv4
  end

  @test_services[port] = { v4: socket4, v6: socket6 }
end

#stop_on(port) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/corgibytes/freshli/commons/test_services.rb', line 33

def stop_on(port)
  expect(@test_services).to have_key(port)

  socket4 = @test_services[port][:v4]
  socket4.close
  socket6 = @test_services[port][:v6]
  socket6.close
end