Class: AntiSmoker::AbstractSmokeTest

Inherits:
Object
  • Object
show all
Defined in:
lib/antismoker/tests.rb

Direct Known Subclasses

HttpTest

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, options = {}) ⇒ AbstractSmokeTest

Returns a new instance of AbstractSmokeTest.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/antismoker/tests.rb', line 8

def initialize(host, port, options={})
  @host = host
  @port = port
  @options = options

  @delay = options[:delay]
  @count = options[:count]
  @period = options[:period]
  @timeout = options[:timeout]

  @logger = Logger.new(STDERR)
  @logger.level = Logger.const_get(options.fetch(:log, :info).to_s.upcase)
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



26
27
28
# File 'lib/antismoker/tests.rb', line 26

def count
  @count
end

#delayObject (readonly)

Returns the value of attribute delay.



25
26
27
# File 'lib/antismoker/tests.rb', line 25

def delay
  @delay
end

#hostObject (readonly)

Returns the value of attribute host.



21
22
23
# File 'lib/antismoker/tests.rb', line 21

def host
  @host
end

#loggerObject (readonly)

Returns the value of attribute logger.



30
31
32
# File 'lib/antismoker/tests.rb', line 30

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



23
24
25
# File 'lib/antismoker/tests.rb', line 23

def options
  @options
end

#periodObject (readonly)

Returns the value of attribute period.



27
28
29
# File 'lib/antismoker/tests.rb', line 27

def period
  @period
end

#portObject (readonly)

Returns the value of attribute port.



22
23
24
# File 'lib/antismoker/tests.rb', line 22

def port
  @port
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



28
29
30
# File 'lib/antismoker/tests.rb', line 28

def timeout
  @timeout
end

Instance Method Details

#run(options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/antismoker/tests.rb', line 41

def run(options={})
  STDOUT.write("smoke testing: #{self}: ")
  sleep_with_progress(delay)
  n = 0
  while n < count
    if run_once_with_timeout(options)
      STDOUT.puts(" [\e[1;32m OK \e[0m]")
      return true
    else
      STDOUT.putc(?!)
      sleep_with_progress(period)
      n += 1
    end
  end
  STDOUT.puts(" [\e[31m NG \e[0m]")
  return false
end

#run_once(options = {}) ⇒ Object



72
73
74
# File 'lib/antismoker/tests.rb', line 72

def run_once(options={})
  raise("must be overridden")
end

#run_once_with_timeout(options = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/antismoker/tests.rb', line 59

def run_once_with_timeout(options={})
  begin
    Timeout.timeout(timeout) {
      return run_once(options)
    }
  rescue Timeout::Error => error
    logger.warn("timed out: #{self}: #{error}")
  rescue => error
    logger.warn("unknown error: #{self}: #{error}")
  end
  false
end

#sleep_with_progress(n) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/antismoker/tests.rb', line 32

def sleep_with_progress(n)
  deadline = Time.now + n
  while Time.now < deadline
    STDOUT.putc(?.)
    STDOUT.flush
    sleep(n.to_f / 3)
  end
end