rspec-openhab-scripting 0.0.5-java → 0.0.8-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 66ee3ecec1c902dcf931cb3e51a7cacdb4df1e3b0798fa973aabf0147787c72f
4
- data.tar.gz: da69d9608f4242545a4e01b0ae22e0133b6a1868b518ad1a95dd5d67bcd94785
3
+ metadata.gz: 5b4f4a385d3672410faee3ca6a90eb931f4499060de30d5dfc11869c73b78db7
4
+ data.tar.gz: 2d6c73ceabd6796134434250824ee255f80cc74b98b16f4172ac76d6be350c5b
5
5
  SHA512:
6
- metadata.gz: 5160b3281ea2f567fd040f49cb0acf5b6c1b56ae050711ae049868ec645dc8a38799434c8c51c3c2a2fb806da98b5261b9c1e54119d3fd2c0d2888680cc2b762
7
- data.tar.gz: c0ca81a870b864c45ba50d281fa9584421b36d64c48b86048765b32d6d0ab5aa64c92a49a4ddfcc6fa21d9b085b7e5a5baa63c95fcde295b090dd5967996d823
6
+ metadata.gz: e9153da220c8491f9a8f79bed277646df27b95ed4c17fb0181dcfd8519df0d9443c989ace2f2fe8da3fb39c14e177d7147e7148cffd5a47baa6a121a821ce883
7
+ data.tar.gz: 70b3fefd8b2df95db3fc553fcb785c48c8e63d3671c0922328bd0937819e7f3b3cc164edb58e3d4fc04da20101297c65868696ebf624b664892b16c86bed22cd
@@ -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.5"
5
+ VERSION = "0.0.8"
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}"
@@ -64,28 +64,33 @@ OpenHAB::DSL::Imports.import_presets
64
64
 
65
65
  require "openhab"
66
66
 
67
+ require "rspec/openhab/actions"
67
68
  require "rspec/openhab/core/cron_scheduler"
68
69
 
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
70
  # RSpec additions
71
+ require "rspec/core"
74
72
  require "rspec/openhab/dsl/rules/rspec"
73
+ require "rspec/openhab/items"
75
74
  require "rspec/openhab/state"
75
+ require "rspec/openhab/timer"
76
76
  require "rspec/openhab/trigger"
77
77
  require "rspec/openhab/wait"
78
- require "rspec/openhab/items"
79
78
 
80
79
  RSpec::OpenHAB::Items.populate_items_from_api(api)
81
80
 
81
+ # make bundler/inline _not_ destroy the already existing load path
82
+ module Bundler
83
+ module SharedHelpers
84
+ def clean_load_path; end
85
+ end
86
+ end
87
+
82
88
  # load rules files
83
89
  OPENHAB_AUTOMATION_PATH = "#{org.openhab.core.OpenHAB.config_folder}/automation/jsr223/ruby/personal"
84
90
 
85
- # set up some environment the rules files expect
86
91
  Dir["#{OPENHAB_AUTOMATION_PATH}/*.rb"].each do |f|
87
92
  load f
88
93
  rescue Exception => e # rubocop:disable Lint/RescueException
89
- warn "Failed loading #{f}: #{e}"
94
+ warn "Failed loading #{f}: #{e.inspect}"
90
95
  warn e.backtrace
91
96
  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.5
4
+ version: 0.0.8
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,7 +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/bundler/inline.rb
175
+ - lib/rspec/openhab/actions.rb
176
176
  - lib/rspec/openhab/api.rb
177
177
  - lib/rspec/openhab/core/cron_scheduler.rb
178
178
  - lib/rspec/openhab/core/load_path.rb
@@ -182,9 +182,9 @@ files:
182
182
  - lib/rspec/openhab/core/script_handling.rb
183
183
  - lib/rspec/openhab/dsl/imports.rb
184
184
  - lib/rspec/openhab/dsl/rules/rspec.rb
185
- - lib/rspec/openhab/dsl/timers/timer.rb
186
185
  - lib/rspec/openhab/items.rb
187
186
  - lib/rspec/openhab/state.rb
187
+ - lib/rspec/openhab/timer.rb
188
188
  - lib/rspec/openhab/trigger.rb
189
189
  - lib/rspec/openhab/version.rb
190
190
  - lib/rspec/openhab/wait.rb
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Bundler
4
- class DummyDsl
5
- def source(*); end
6
- end
7
- end
8
-
9
- def gemfile(*, &block)
10
- # needs to be a no-op, since we're already running in the context of bundler
11
- Bundler::DummyDsl.new.instance_eval(&block)
12
- end
@@ -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