Sha256: 60e723107469b498fabbf900e8a073cacda71cec8724a9c54df07f1c88bfbd62

Contents?: true

Size: 1.64 KB

Versions: 48

Compression:

Stored size: 1.64 KB

Contents

require 'socket'
require 'erb'

module Jasmine
  # this seemingly-over-complex method is necessary to get an open port on at least some of our Macs
  def self.open_socket_on_unused_port
    infos = Socket::getaddrinfo("localhost", nil, Socket::AF_UNSPEC, Socket::SOCK_STREAM, 0, Socket::AI_PASSIVE)
    families = Hash[*infos.collect { |af, *_| af }.uniq.zip([]).flatten]

    return TCPServer.open('0.0.0.0', 0) if families.has_key?('AF_INET')
    return TCPServer.open('::', 0) if families.has_key?('AF_INET6')
    return TCPServer.open(0)
  end

  def self.find_unused_port
    socket = open_socket_on_unused_port
    port = socket.addr[1]
    socket.close
    port
  end

  def self.server_is_listening_on(hostname, port)
    require 'socket'
    begin
      socket = TCPSocket.open(hostname, port)
    rescue Errno::ECONNREFUSED, Errno::ENETUNREACH, Errno::EAFNOSUPPORT, Errno::EADDRNOTAVAIL, Errno::EHOSTUNREACH
      return false
    end
    socket.close
    true
  end

  def self.wait_for_listener(port, hostname = "localhost", seconds_to_wait = 20)
    time_out_at = Time.now + seconds_to_wait
    until server_is_listening_on hostname, port
      sleep 0.1
      puts "Waiting for server on #{hostname}:#{port}..."
      raise "jasmine server didn't show up on port #{hostname}:#{port} after #{seconds_to_wait} seconds." if Time.now > time_out_at
    end
  end

  def self.runner_filepath
    File.expand_path(File.join(File.dirname(__FILE__), "run_specs.rb"))
  end

  def self.runner_template
    File.read(File.join(File.dirname(__FILE__), "run.html.erb"))
  end

  def self.root(*paths)
    File.expand_path(File.join(File.dirname(__FILE__), *paths))
  end

end

Version data entries

48 entries across 36 versions & 2 rubygems

Version Path
tdiary-5.2.4 vendor/bundle/ruby/3.1.0/gems/jasmine-2.99.0/lib/jasmine/base.rb
tdiary-5.2.3 vendor/bundle/ruby/3.1.0/gems/jasmine-2.99.0/lib/jasmine/base.rb
tdiary-5.2.2 vendor/bundle/ruby/3.1.0/gems/jasmine-2.99.0/lib/jasmine/base.rb
tdiary-5.2.1 vendor/bundle/ruby/3.1.0/gems/jasmine-2.99.0/lib/jasmine/base.rb
jasmine-3.99.0 lib/jasmine/base.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/jasmine-2.99.0/lib/jasmine/base.rb
tdiary-5.2.0 vendor/bundle/ruby/3.0.0/gems/jasmine-2.99.0/lib/jasmine/base.rb
jasmine-3.10.0 lib/jasmine/base.rb
tdiary-5.1.7 vendor/bundle/ruby/2.7.0/gems/jasmine-2.99.0/lib/jasmine/base.rb
tdiary-5.1.7 vendor/bundle/ruby/3.0.0/gems/jasmine-2.99.0/lib/jasmine/base.rb
jasmine-3.9.2 lib/jasmine/base.rb
jasmine-3.9.1 lib/jasmine/base.rb
jasmine-3.9.0 lib/jasmine/base.rb
jasmine-3.8.1 lib/jasmine/base.rb
jasmine-3.8.0 lib/jasmine/base.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/tdiary-5.1.4/vendor/bundle/ruby/2.7.0/gems/jasmine-2.99.0/lib/jasmine/base.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/2.7.0/gems/jasmine-2.99.0/lib/jasmine/base.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/jasmine-2.99.0/lib/jasmine/base.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/jasmine-2.99.0/lib/jasmine/base.rb
tdiary-5.1.6 vendor/bundle/ruby/3.0.0/gems/jasmine-2.99.0/lib/jasmine/base.rb