rspec-openhab-scripting 0.0.6-java → 0.0.9-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 36b9bb280e4fd6893f93a5ca0197dd4a7a08c286b4cc6835c44f81c487e497fe
4
- data.tar.gz: 6390495a9d8ff5a522f86f4e1d50155bf93d45a14a59b9ba3b9147c49cd27548
3
+ metadata.gz: ec85830015f39fcbcf027f869f5af4b215717b1c027deb47959428a8f156b652
4
+ data.tar.gz: 22050257da398fa958a657e69c773f27e215e2d478bacb9d9e1400f84b0c6191
5
5
  SHA512:
6
- metadata.gz: c571aa03ed3fc7521be9e65924b274204a7ab4382101467af88847350632a7cc159795cbb238765af6fd6b0abb41b2934b656037571de5a3936972905784255e
7
- data.tar.gz: 757a9bcfbbe1d5361500413b5e6cd74b6a1eda00df2483abb22e200f4d1ae3845fa6bfafcb4501b3b1060f91710a721e1727db3a695447a90aa7f91ef3b8643a
6
+ metadata.gz: d29f9f59280c73b33cb7caef1bbde2baf7b1931bbf9c8fdd8ccb51c3ec8ec13bf9efe51d6e13a410fadd9a53bf78928f22cdc25917ee37d64dc66906fc47bcfc
7
+ data.tar.gz: b0132dec180bf8e9bf6fca0e8223c67e6fda04529f376f74a9b2405c0defd01bf1661fed63534a15ccf4392440a4faefe8936f4a42428af8de1be5c6778755d8
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module DSL
5
+ module Actions
6
+ # redefine these to do nothing so that rules won't fail
7
+ def notify(msg, email: nil) # rubocop:disable Lint/UnusedMethodArgument:
8
+ logger.debug("notify: #{msg}")
9
+ end
10
+
11
+ def say(text, voice: nil, sink: nil, volume: nil) # rubocop:disable Lint/UnusedMethodArgument:
12
+ logger.debug("say: #{text}")
13
+ end
14
+
15
+ def play_sound(filename, sink: nil, volume: nil) # rubocop:disable Lint/UnusedMethodArgument:
16
+ logger.debug("play_sound: #{filename}")
17
+ end
18
+ end
19
+ end
20
+ end
@@ -213,10 +213,11 @@ module OpenHAB
213
213
  se = org.openhab.core.automation.module.script.rulesupport.internal
214
214
  .RuleSupportScriptExtension.new(rr, srp, scmhf, scmtp, spmhf)
215
215
  sew = ScriptExtensionManagerWrapper.new(se)
216
- $se = $scriptExtension = sew # rubocop:disable Style/GlobalVars
216
+ $se = $scriptExtension = sew
217
217
 
218
218
  # need to create some singletons referencing registries
219
- org.openhab.core.model.script.ScriptServiceUtil.new(ir, tr, ep, nil, nil)
219
+ scheduler = org.openhab.core.internal.scheduler.SchedulerImpl.new
220
+ org.openhab.core.model.script.ScriptServiceUtil.new(ir, tr, ep, nil, scheduler)
220
221
  org.openhab.core.model.script.internal.engine.action.SemanticsActionService.new(ir)
221
222
 
222
223
  # link up event bus infrastructure
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # rubocop:disable Style/GlobalVars
4
3
  module RSpec
5
4
  module OpenHAB
6
5
  module Items
@@ -76,4 +75,3 @@ module RSpec
76
75
  end
77
76
  end
78
77
  end
79
- # rubocop:enable Style/GlobalVars
@@ -1,11 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  RSpec.configure do |config|
4
- org.openhab.core.items.GenericItem.field_reader :eventPublisher
5
-
6
- # rubocop:disable Style/GlobalVars
7
4
  config.before(:each) do
8
- ep = $ir.eventPublisher
5
+ ep = $ir.event_publisher
9
6
 
10
7
  # stash event publishers to avoid triggering any rules
11
8
  $ir.for_each do |_provider, item|
@@ -20,5 +17,4 @@ RSpec.configure do |config|
20
17
  item.event_publisher = ep
21
18
  end
22
19
  end
23
- # rubocop:enable Style/GlobalVars
24
20
  end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.configure do |config|
4
+ config.before(:each) do
5
+ OpenHAB::DSL::Timers.timer_manager.cancel_all
6
+ wait_for_background_tasks
7
+ end
8
+ config.after(:each) do
9
+ OpenHAB::DSL::Timers.timer_manager.cancel_all
10
+ wait_for_background_tasks
11
+ end
12
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RSpec
4
4
  module OpenHAB
5
- VERSION = "0.0.6"
5
+ VERSION = "0.0.9"
6
6
  end
7
7
  end
@@ -4,14 +4,75 @@ module RSpec
4
4
  module OpenHAB
5
5
  module Wait
6
6
  def wait_for_rules
7
+ wait_for_background_tasks(timers: false)
8
+ end
9
+
10
+ def wait_for_timers
11
+ wait_for_background_tasks(rules: false)
12
+ end
13
+
14
+ def wait_for_background_tasks(rules: true, timers: true)
7
15
  loop do
8
- sleep(0.1)
9
- break if java.lang.Thread.all_stack_traces.keys.all? do |t|
10
- !t.name.match?(/^OH-rule-/) ||
11
- [java.lang.Thread::State::WAITING, java.lang.Thread::State::TIMED_WAITING].include?(t.state)
12
- end
16
+ sleep 0.1
17
+ next if java.lang.Thread.all_stack_traces.any? do |(t, stack)|
18
+ # this is just an estimate. I see 9 when it's parked waiting
19
+ # for an event, but once it hits ruby it gets real big real quick
20
+ min_frames = 15
21
+
22
+ case t.name
23
+ when /^OH-scheduler-/
24
+ # timer thread; born and die for each timer
25
+ if thread_running?(t) || stack.length > min_frames
26
+ logger.debug "thread #{t.name} is running (#{stack.length})"
27
+ stack.each do |frame|
28
+ logger.trace " #{frame}"
29
+ end
30
+ next timers
31
+ end
32
+ when /^OH-rule-/
33
+
34
+ if thread_running?(t) || stack.length > min_frames
35
+ logger.debug "thread #{t.name} is running (#{stack.length})"
36
+ stack.each do |frame|
37
+ logger.trace " #{frame}"
38
+ end
39
+
40
+ next rules
41
+ end
42
+ when /^OH-(?:eventwatcher|eventexecutor)-/
43
+ # an event is making its way through the system
44
+ if thread_running?(t)
45
+ logger.debug "thread #{t.name} is running"
46
+ next rules
47
+ end
48
+ end
49
+ end
50
+
51
+ # no need to retry if there were no timers
52
+ break unless timers && wait_for_next_timer
13
53
  end
14
54
  end
55
+
56
+ private
57
+
58
+ def thread_running?(thread)
59
+ ![java.lang.Thread::State::WAITING,
60
+ java.lang.Thread::State::TIMED_WAITING].include?(thread.state)
61
+ end
62
+
63
+ def wait_for_next_timer
64
+ latest = ::OpenHAB::DSL::Timers.timer_manager.instance_variable_get(:@timers).min_by(&:execution_time)
65
+ return false unless latest
66
+
67
+ delta = (latest.execution_time.to_instant.to_epoch_milli - java.time.Instant.now.to_epoch_milli) / 1000.0
68
+ # in the past? it's probably executing
69
+ return true if delta.negative?
70
+
71
+ logger.info("Waiting #{delta}s for next timer") if delta > 5
72
+
73
+ sleep(delta)
74
+ true
75
+ end
15
76
  end
16
77
  end
17
78
  end
@@ -34,10 +34,10 @@ maven_require do
34
34
  require "jar tech.units, indriya, 2.1.3"
35
35
 
36
36
  require "jar org.openhab.core.bundles, org.openhab.core, #{openhab_version}"
37
- require "jar org.openhab.core.bundles, org.openhab.core.config.core, #{openhab_version}"
38
37
  require "jar org.openhab.core.bundles, org.openhab.core.automation, #{openhab_version}"
39
38
  require "jar org.openhab.core.bundles, org.openhab.core.automation.module.script, #{openhab_version}"
40
39
  require "jar org.openhab.core.bundles, org.openhab.core.automation.module.script.rulesupport, #{openhab_version}"
40
+ require "jar org.openhab.core.bundles, org.openhab.core.config.core, #{openhab_version}"
41
41
  require "jar org.openhab.core.bundles, org.openhab.core.io.monitor, #{openhab_version}"
42
42
  require "jar org.openhab.core.bundles, org.openhab.core.model.core, #{openhab_version}"
43
43
  require "jar org.openhab.core.bundles, org.openhab.core.model.script, #{openhab_version}"
@@ -58,24 +58,28 @@ end
58
58
  # global variables need to be set up before openhab-scripting loads
59
59
  require "openhab/log/logger"
60
60
  require "rspec/openhab/core/logger"
61
+ # during testing, we don't want "regular" output from rules
62
+ OpenHAB::Log.logger("org.openhab.automation.jruby.runtime").level = :warn
61
63
  require "openhab/dsl/imports"
62
64
  OpenHAB::DSL::Imports.api = api
63
65
  OpenHAB::DSL::Imports.import_presets
64
66
 
65
67
  require "openhab"
66
68
 
69
+ require "rspec/openhab/actions"
67
70
  require "rspec/openhab/core/cron_scheduler"
68
71
 
69
- # openhab-scripting uses a require_relative, so our override doesn't get used
70
- OpenHAB::DSL.send(:remove_const, :Timer)
71
- require_relative "rspec/openhab/dsl/timers/timer"
72
-
73
72
  # RSpec additions
73
+ require "rspec/core"
74
74
  require "rspec/openhab/dsl/rules/rspec"
75
+ require "rspec/openhab/items"
75
76
  require "rspec/openhab/state"
77
+ require "rspec/openhab/timer"
76
78
  require "rspec/openhab/trigger"
77
79
  require "rspec/openhab/wait"
78
- require "rspec/openhab/items"
80
+ RSpec.configure do |config|
81
+ config.include OpenHAB::Core::EntityLookup
82
+ end
79
83
 
80
84
  RSpec::OpenHAB::Items.populate_items_from_api(api)
81
85
 
@@ -92,6 +96,6 @@ OPENHAB_AUTOMATION_PATH = "#{org.openhab.core.OpenHAB.config_folder}/automation/
92
96
  Dir["#{OPENHAB_AUTOMATION_PATH}/*.rb"].each do |f|
93
97
  load f
94
98
  rescue Exception => e # rubocop:disable Lint/RescueException
95
- warn "Failed loading #{f}: #{e}"
99
+ warn "Failed loading #{f}: #{e.inspect}"
96
100
  warn e.backtrace
97
101
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-openhab-scripting
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.9
5
5
  platform: java
6
6
  authors:
7
7
  - Cody Cutrer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-01 00:00:00.000000000 Z
11
+ date: 2022-07-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -172,6 +172,7 @@ extra_rdoc_files: []
172
172
  files:
173
173
  - lib/rspec-openhab-scripting.rb
174
174
  - lib/rspec-openhab-scripting_jars.rb
175
+ - lib/rspec/openhab/actions.rb
175
176
  - lib/rspec/openhab/api.rb
176
177
  - lib/rspec/openhab/core/cron_scheduler.rb
177
178
  - lib/rspec/openhab/core/load_path.rb
@@ -181,9 +182,9 @@ files:
181
182
  - lib/rspec/openhab/core/script_handling.rb
182
183
  - lib/rspec/openhab/dsl/imports.rb
183
184
  - lib/rspec/openhab/dsl/rules/rspec.rb
184
- - lib/rspec/openhab/dsl/timers/timer.rb
185
185
  - lib/rspec/openhab/items.rb
186
186
  - lib/rspec/openhab/state.rb
187
+ - lib/rspec/openhab/timer.rb
187
188
  - lib/rspec/openhab/trigger.rb
188
189
  - lib/rspec/openhab/version.rb
189
190
  - lib/rspec/openhab/wait.rb
@@ -1,87 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module OpenHAB
4
- module DSL
5
- class Timer
6
- #
7
- # Create a new Timer Object
8
- #
9
- # @param [Duration] duration Duration until timer should fire
10
- # @param [Block] block Block to execute when timer fires
11
- #
12
- def initialize(duration:, thread_locals: {}, &block) # rubocop:disable Lint/UnusedMethodArgument
13
- @duration = duration
14
-
15
- Timers.timer_manager.add(self)
16
- end
17
-
18
- def reschedule(_duration = nil)
19
- Timers.timer_manager.add(self)
20
- end
21
-
22
- #
23
- # Cancel timer
24
- #
25
- # @return [Boolean] True if cancel was successful, false otherwise
26
- #
27
- def cancel
28
- Timers.timer_manager.delete(self)
29
- end
30
-
31
- def terminated?; end
32
- alias_method :has_terminated, :terminated?
33
-
34
- def is_active # rubocop:disable Naming/PredicateName
35
- false
36
- end
37
-
38
- private
39
-
40
- def timer_block
41
- proc do
42
- Timers.timer_manager.delete(self)
43
- yield(self)
44
- end
45
- end
46
- end
47
-
48
- #
49
- # Convert TemporalAmount (Duration), seconds (float, integer), and Ruby Time to ZonedDateTime
50
- # Note: TemporalAmount is added to now
51
- #
52
- # @param [Object] timestamp to convert
53
- #
54
- # @return [ZonedDateTime]
55
- #
56
- def self.to_zdt(timestamp)
57
- logger.trace("Converting #{timestamp} (#{timestamp.class}) to ZonedDateTime")
58
- return unless timestamp
59
-
60
- case timestamp
61
- when Java::JavaTimeTemporal::TemporalAmount then ZonedDateTime.now.plus(timestamp)
62
- when ZonedDateTime then timestamp
63
- when Time then timestamp.to_java(ZonedDateTime)
64
- else
65
- to_zdt(seconds_to_duration(timestamp)) ||
66
- raise(ArgumentError, "Timestamp must be a ZonedDateTime, a Duration, a Numeric, or a Time object")
67
- end
68
- end
69
-
70
- #
71
- # Convert numeric seconds to a Duration object
72
- #
73
- # @param [Float, Integer] secs The number of seconds in integer or float
74
- #
75
- # @return [Duration]
76
- #
77
- def self.seconds_to_duration(secs)
78
- return unless secs
79
-
80
- if secs.respond_to?(:to_f)
81
- secs.to_f.seconds
82
- elsif secs.respond_to?(:to_i)
83
- secs.to_i.seconds
84
- end
85
- end
86
- end
87
- end