Class: OpenHAB::RSpec::Mocks::Timer

Inherits:
Core::Timer show all
Defined in:
lib/openhab/rspec/mocks/timer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Core::Timer

#inspect, #reschedule

Constructor Details

#initialize(time, id:, thread_locals:, block:) ⇒ Timer

rubocop:disable Lint/MissingSuper



113
114
115
116
117
118
119
# File 'lib/openhab/rspec/mocks/timer.rb', line 113

def initialize(time, id:, thread_locals:, block:) # rubocop:disable Lint/MissingSuper
  @time = time
  @id = id
  @block = block
  @thread_locals = thread_locals
  reschedule!(time)
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



111
112
113
# File 'lib/openhab/rspec/mocks/timer.rb', line 111

def block
  @block
end

#execution_timeObject (readonly)

Returns the value of attribute execution_time.



111
112
113
# File 'lib/openhab/rspec/mocks/timer.rb', line 111

def execution_time
  @execution_time
end

#idObject (readonly)

Returns the value of attribute id.



111
112
113
# File 'lib/openhab/rspec/mocks/timer.rb', line 111

def id
  @id
end

Class Method Details

.mock_timers(mock_timers) { ... } ⇒ Object

Temporarily mock or unmock timers

Parameters:

  • mock_timers (true, false)

    if timers should be mocked

Yields:

Returns:

  • (Object)

    the block’s return value



88
89
90
91
92
93
94
# File 'lib/openhab/rspec/mocks/timer.rb', line 88

def mock_timers(mock_timers)
  old_mock_timers = @mock_timers
  @mock_timers = mock_timers
  yield
ensure
  @mock_timers = old_mock_timers
end

.mock_timers?true, false

If timers are currently mocked

Returns:

  • (true, false)


78
79
80
# File 'lib/openhab/rspec/mocks/timer.rb', line 78

def mock_timers?
  @mock_timers
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/openhab/rspec/mocks/timer.rb', line 164

def active?
  !terminated?
end

#cancelObject



139
140
141
142
143
144
145
# File 'lib/openhab/rspec/mocks/timer.rb', line 139

def cancel
  return false if terminated? || cancelled?

  DSL::TimerManager.instance.delete(self)
  cancel!
  true
end

#cancel!Object



147
148
149
150
# File 'lib/openhab/rspec/mocks/timer.rb', line 147

def cancel!
  @execution_time = nil
  true
end

#cancelled?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/openhab/rspec/mocks/timer.rb', line 152

def cancelled?
  @execution_time.nil?
end

#executeObject



131
132
133
134
135
136
137
# File 'lib/openhab/rspec/mocks/timer.rb', line 131

def execute
  raise "Timer already cancelled" if cancelled?
  raise "Timer already executed" if terminated?

  @executed = true
  super
end

#reschedule!(time = nil) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/openhab/rspec/mocks/timer.rb', line 121

def reschedule!(time = nil)
  Thread.current[:openhab_rescheduled_timer] = true if Thread.current[:openhab_rescheduled_timer] == self
  @execution_time = new_execution_time(time || @time)
  @executed = false

  DSL::TimerManager.instance.add(self)

  self
end

#running?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/openhab/rspec/mocks/timer.rb', line 160

def running?
  false
end

#terminated?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/openhab/rspec/mocks/timer.rb', line 156

def terminated?
  @executed || cancelled?
end