Class: Timeoutable::TestableKlass

Inherits:
Object
  • Object
show all
Defined in:
lib/timeoutable/testable_klass.rb

Constant Summary collapse

BIT_NAME =
'testable_bit_change'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger: Logger.new($stdout)) ⇒ TestableKlass

Returns a new instance of TestableKlass.



24
25
26
# File 'lib/timeoutable/testable_klass.rb', line 24

def initialize(logger: Logger.new($stdout))
  @logger = logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



22
23
24
# File 'lib/timeoutable/testable_klass.rb', line 22

def logger
  @logger
end

Class Method Details

.perform_test(warn: 2, timeout: 3, sleep_after: 0.1, sleep_for: 0.1, logger: Logger.new($stdout)) ⇒ Object

:nocov:



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/timeoutable/testable_klass.rb', line 10

def self.perform_test(warn: 2, timeout: 3, sleep_after: 0.1 , sleep_for: 0.1, logger: Logger.new($stdout))
  params = {
    warn: warn,
    timeout: timeout,
    proc: ->(thread, sec) { thread[BIT_NAME] = 1; logger.warn('at: proc -- Set Bit') },
    message: "Yoooo. Your code violently blew up. Handle it accordingly",
  }
  Timeoutable.timeout(**params) do
    new.execute(sleep_after: sleep_after, sleep_for: sleep_for)
  end
end

Instance Method Details

#execute(sleep_after:, sleep_for:, max_iterations: 10_000, raise_warn: false, raise_timeout: false) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/timeoutable/testable_klass.rb', line 28

def execute(sleep_after:, sleep_for:, max_iterations: 10_000, raise_warn: false, raise_timeout: false)
  logger.info("at: execute -- Start")
  count = 0
  while count <= max_iterations && Thread.current[BIT_NAME].nil?
    raise StandardError, 'Throwing error before warn bit' if raise_warn
    logger.info("at: execute -- sleeping for #{sleep_for}'s [#{count} iterations]")
    sleep(sleep_for)
    count += 1
  end
  raise StandardError, 'Throwing error after warn bit' if raise_timeout

  logger.info("at: execute -- Worker noticed sleep bit changed -- Cycle broken")

  logger.warn("at: execute -- sleeping 1 last time for #{sleep_after}'s")
  sleep(sleep_after)
  logger.warn("at: execute -- returning after sleep")
  logger.info("at: execute -- End")
ensure
  logger.info("at: execute (ensure) -- ensure bit resetting")
end