timers 4.0.1 → 4.0.4

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
  SHA1:
3
- metadata.gz: ff04e8dedf4d39bd0ff36650200e6c3537669cd8
4
- data.tar.gz: fd2f6c05f04a626f73401ea9c06e8be89956024e
3
+ metadata.gz: fc5b681c3922f53f0a1c332aad2955d61ee278a4
4
+ data.tar.gz: 4e90a800064db9f6cf1cdb4410cbb1ca8badd2ac
5
5
  SHA512:
6
- metadata.gz: 5b829c8ffc7da2676e9b32e0f16fc54135dce451825db5c669f40566d6bd8a4334dd8d6b28d06dcd191cc92cd55a27dcd3fe9476b16fcb56a80c23b335fcb2f4
7
- data.tar.gz: 08314ad60709460ff1d4f55cf4c61ccb0333bf7d67186702d5e3268cfe9ac2ba9e511c936b63be0ed4f9d6532a5f3ca0235bd65ceba319b812ffa4564115f014
6
+ metadata.gz: fd8330e2dfd5b23813c55d242ad44d6c8d7c00e251a047f4206c72511132eeeb8fdf640fc2802a9e83e87dabab2134532dea10f25a993e9fd98fb9b759c3faca
7
+ data.tar.gz: 5e8d5e38d58ee3c7167dae55a293c5bf2a5cc6f84ea65edbba5435eafc8b85b31d2865f81cec97cdc8b612af4f87654fadc8a4da565822e61dc09e6756a65b05
@@ -8,6 +8,7 @@ rvm:
8
8
  - rbx-2
9
9
 
10
10
  matrix:
11
+ fast_finish: true
11
12
  allow_failures:
12
13
  - rvm: ruby-head
13
14
  - rvm: jruby-head
data/CHANGES.md CHANGED
@@ -1,3 +1,16 @@
1
+ 4.0.4 (2015-09-01)
2
+ ------------------
3
+ * Re-release of the `4.1.1` version under the `4.0.4` designation to work-around issues caused by yanking `4.0.2` a couple weeks ago.
4
+
5
+ 4.1.1 (2015-08-21)
6
+ ------------------
7
+ * Remove `RubyProf` from Gemfile and a test, due to it providing no substantial benefit while increasing problems building bundles under Rubinius.
8
+
9
+ 4.1.0 (2015-08-16)
10
+ ------------------
11
+ * Addition of `now_and_every` method; fires block immediately, then sets recurring timer.
12
+ * Includes `now_and_after` method; does the same as above for one-shot timers: essentially a "two-shot" timer.
13
+
1
14
  4.0.1 (2014-09-10)
2
15
  ------------------
3
16
  * Memory leak fixes
data/Gemfile CHANGED
@@ -4,7 +4,3 @@ gem 'coveralls', :require => false
4
4
 
5
5
  # Specify your gem's dependencies in timers.gemspec
6
6
  gemspec
7
-
8
- unless RUBY_PLATFORM =~ /java/
9
- gem "ruby-prof", :group => :development
10
- end
data/README.md CHANGED
@@ -50,6 +50,13 @@ every_five_seconds = timers.every(5) { puts "Another 5 seconds" }
50
50
  loop { timers.wait }
51
51
  ```
52
52
 
53
+ You can also schedule a block to run immediately and periodically with `Timers::Group#now_and_every`:
54
+ ```ruby
55
+ now_and_every_five_seconds = timers.now_and_every(5) { puts "Now and in another 5 seconds" }
56
+
57
+ loop { timers.wait }
58
+ ```
59
+
53
60
  If you'd like another method to do the waiting for you, e.g. `Kernel.select`,
54
61
  you can use `Timers::Group#wait_interval` to obtain the amount of time to wait. When
55
62
  a timeout is encountered, you can fire all pending timers with `Timers::Group#fire`:
@@ -72,6 +72,7 @@ module Timers
72
72
  return handle
73
73
  end
74
74
  end
75
+ # @sequence.reverse.find { |handle| !handle.cancelled? }
75
76
  end
76
77
 
77
78
  # Returns the number of pending (possibly cancelled) events.
@@ -93,7 +94,7 @@ module Timers
93
94
  def pop(time)
94
95
  index = bisect_left(@sequence, time)
95
96
 
96
- return @sequence.pop(@sequence.size - index)
97
+ @sequence.pop(@sequence.size - index)
97
98
  end
98
99
 
99
100
  # Return the left-most index where to insert item e, in a list a, assuming
@@ -38,12 +38,26 @@ module Timers
38
38
  Timer.new(self, interval, false, &block)
39
39
  end
40
40
 
41
+ # Call the given block immediately, and then after the given interval. The first
42
+ # argument will be the time at which the group was asked to fire timers for.
43
+ def now_and_after(interval, &block)
44
+ block.call
45
+ after(interval, &block)
46
+ end
47
+
41
48
  # Call the given block periodically at the given interval. The first
42
49
  # argument will be the time at which the group was asked to fire timers for.
43
50
  def every(interval, recur = true, &block)
44
51
  Timer.new(self, interval, recur, &block)
45
52
  end
46
53
 
54
+ # Call the given block immediately, and then periodically at the given interval. The first
55
+ # argument will be the time at which the group was asked to fire timers for.
56
+ def now_and_every(interval, recur = true, &block)
57
+ block.call
58
+ every(interval, recur, &block)
59
+ end
60
+
47
61
  # Wait for the next timer and fire it. Can take a block, which should behave
48
62
  # like sleep(n), except that n may be nil (sleep forever) or a negative
49
63
  # number (fire immediately after return).
@@ -61,7 +75,7 @@ module Timers
61
75
  end
62
76
  end
63
77
 
64
- fire
78
+ return fire
65
79
  end
66
80
 
67
81
  # Interval to wait until when the next timer will fire.
@@ -1,3 +1,3 @@
1
1
  module Timers
2
- VERSION = "4.0.1"
2
+ VERSION = "4.0.4"
3
3
  end
@@ -16,4 +16,19 @@ RSpec.describe Timers::Group do
16
16
 
17
17
  expect(result).to be == [:a, :c, :a, :a, :b, :d]
18
18
  end
19
+
20
+ it "should fire immediately and then several times later" do
21
+ result = []
22
+
23
+ subject.every(0.7) { result << :a }
24
+ subject.every(2.3) { result << :b }
25
+ subject.now_and_every(1.3) { result << :c }
26
+ subject.now_and_every(2.4) { result << :d }
27
+
28
+ Timers::Wait.for(2.5) do |remaining|
29
+ subject.wait if subject.wait_interval < remaining
30
+ end
31
+
32
+ expect(result).to be == [:c, :d, :a, :c, :a, :a, :b, :d]
33
+ end
19
34
  end
@@ -1,6 +1,4 @@
1
-
2
1
  require 'spec_helper'
3
- require 'ruby-prof' unless RUBY_PLATFORM =~ /java/
4
2
 
5
3
  # Event based timers:
6
4
 
@@ -41,26 +39,8 @@ require 'ruby-prof' unless RUBY_PLATFORM =~ /java/
41
39
  # Serviced 1142649 events in 11.194903921 seconds, 102068.70405115146 e/s.
42
40
 
43
41
  RSpec.describe Timers::Group do
44
- if defined? RubyProf
45
- before(:each) do
46
- # Running RubyProf makes the code slightly slower.
47
- RubyProf.start
48
- puts "*** Running with RubyProf reduces performance ***"
49
- end
50
42
 
51
- after(:each) do |arg|
52
- if RubyProf.running?
53
- # file = arg.metadata[:description].gsub(/\s+/, '-')
54
-
55
- result = RubyProf.stop
56
-
57
- printer = RubyProf::FlatPrinter.new(result)
58
- printer.print($stderr, min_percent: 1.0)
59
- end
60
- end
61
- end
62
-
63
- it "run efficiently" do
43
+ it "runs efficiently" do
64
44
  result = []
65
45
  range = (1..500)
66
46
  duration = 2.0
@@ -80,4 +60,41 @@ RSpec.describe Timers::Group do
80
60
 
81
61
  expect(subject.current_offset).to be_within(TIMER_QUANTUM).of(duration)
82
62
  end
63
+
64
+ =begin
65
+ it "runs efficiently at high volume" do
66
+ results = []
67
+ range = (1..300)
68
+ groups = (1..20)
69
+ duration = 101
70
+
71
+ timers = []
72
+ @mutex = Mutex.new
73
+ start = Time.now
74
+ groups.each do |gi|
75
+ timers << Thread.new {
76
+ result = []
77
+ timer = Timers::Group.new
78
+ total = 0
79
+ range.each do |ri|
80
+ offset = ri.to_f / range.max
81
+ total += (duration / offset).floor
82
+ timer.every(ri.to_f / range.max, :strict) { result << ri }
83
+ end
84
+ timer.wait while result.size < total
85
+ @mutex.synchronize { results += result }
86
+
87
+ }
88
+ end
89
+ timers.each { |t| t.join }
90
+ finish = Time.now
91
+
92
+ rate = results.size.to_f / ( runtime = finish - start )
93
+
94
+ puts "Serviced #{results.size} events in #{runtime} seconds, #{rate} e/s; across #{groups.max} timers."
95
+
96
+ expect(runtime).to be_within(TIMER_QUANTUM).of(duration)
97
+ end
98
+ =end
99
+
83
100
  end
metadata CHANGED
@@ -1,24 +1,24 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timers
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.1
4
+ version: 4.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tony Arcieri
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-10 00:00:00.000000000 Z
11
+ date: 2015-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hitimes
15
+ type: :runtime
16
+ prerelease: false
15
17
  requirement: !ruby/object:Gem::Requirement
16
18
  requirements:
17
19
  - - ">="
18
20
  - !ruby/object:Gem::Version
19
21
  version: '0'
20
- type: :runtime
21
- prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
@@ -26,13 +26,13 @@ dependencies:
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
+ type: :development
30
+ prerelease: false
29
31
  requirement: !ruby/object:Gem::Requirement
30
32
  requirements:
31
33
  - - ">="
32
34
  - !ruby/object:Gem::Version
33
35
  version: '0'
34
- type: :development
35
- prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
@@ -40,13 +40,13 @@ dependencies:
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
+ type: :development
44
+ prerelease: false
43
45
  requirement: !ruby/object:Gem::Requirement
44
46
  requirements:
45
47
  - - "~>"
46
48
  - !ruby/object:Gem::Version
47
49
  version: 3.0.0
48
- type: :development
49
- prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
@@ -104,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
104
  version: '0'
105
105
  requirements: []
106
106
  rubyforge_project:
107
- rubygems_version: 2.2.2
107
+ rubygems_version: 2.4.8
108
108
  signing_key:
109
109
  specification_version: 4
110
110
  summary: Schedule procs to run after a certain time, or at periodic intervals, using