Sha256: 41b42f93ffb94060bb8f3a40dcb268c9cd756bf9bdbd47f1e9633f2631be398f

Contents?: true

Size: 1.28 KB

Versions: 65

Compression:

Stored size: 1.28 KB

Contents

require 'eventmachine'
require 'test/unit'
require 'rbconfig'
require 'socket'

class Test::Unit::TestCase
  class EMTestTimeout < StandardError ; end

  def setup_timeout(timeout = TIMEOUT_INTERVAL)
    EM.schedule {
      EM.add_timer(timeout) {
        raise EMTestTimeout, "Test was cancelled after #{timeout} seconds."
      }
    }
  end

  def port_in_use?(port, host="127.0.0.1")
    s = TCPSocket.new(host, port)
    s.close
    s
  rescue Errno::ECONNREFUSED
    false
  end

  def next_port
    @@port ||= 9000
    begin
      @@port += 1
    end while port_in_use?(@@port)

    @@port
  end

  def exception_class
    jruby? ? NativeException : RuntimeError
  end

  module PlatformHelper
    # http://blog.emptyway.com/2009/11/03/proper-way-to-detect-windows-platform-in-ruby/
    def windows?
      RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
    end

    # http://stackoverflow.com/questions/1342535/how-can-i-tell-if-im-running-from-jruby-vs-ruby/1685970#1685970
    def jruby?
      defined? JRUBY_VERSION
    end
  end

  include PlatformHelper
  extend PlatformHelper

  # Tests run significantly slower on windows. YMMV
  TIMEOUT_INTERVAL = windows? ? 1 : 0.25

  def silent
    backup, $VERBOSE = $VERBOSE, nil
    begin
      yield
    ensure
      $VERBOSE = backup
    end
  end
end

Version data entries

65 entries across 65 versions & 5 rubygems

Version Path
eventmachine-1.0.0.beta.4.1-x86-mingw32 tests/em_test_helper.rb
eventmachine-1.0.0.beta.4-x86-mswin32-60 tests/em_test_helper.rb
eventmachine-1.0.0.beta.4-x86-mingw32 tests/em_test_helper.rb
eventmachine-1.0.0.beta.4-java tests/em_test_helper.rb
eventmachine-1.0.0.beta.4 tests/em_test_helper.rb