atdo 0.4 → 0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +42 -2
  3. data/lib/atdo.rb +8 -1
  4. data/test/test-atdo.rb +11 -2
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 766d057a668bcb0efce00c55758f212ef8a2cced
4
- data.tar.gz: 99b9259e58f6a311d695545f7fe5bb58e8844849
3
+ metadata.gz: ce5b3c549e91ce0834c69acc4becba2b11e5bb98
4
+ data.tar.gz: 78eba666d55525e0f22f7932f618f4cf1dbad4c6
5
5
  SHA512:
6
- metadata.gz: 16b0f91c852c9e84072d3e5ab9b644f646f8e3c7af792e21d6a836ae0292763d7103bda5442e3acbdc5e5448efdbc66d392c98beca9854a35ca52227fd459dfa
7
- data.tar.gz: 4e10c64a187a7e0f361e17ec5f13fdd2441c2ca4259fab0c076c48f76059e001fa4ab1b2dcbfb7782aca198297775b64f017bb7860df42b8eea7b7f9cbe52894
6
+ metadata.gz: 8ec3c9c9c67d364f9f1b054357a21c33bd8171ef862b33ef6dcfe9f0759e719ec77646d63cba853508da2cacfb18e0a85b0ead6aa05c227339148792a4a2c4be
7
+ data.tar.gz: 5e8b2d638380995afee556a3aa4459c89791bf2f66bebdd648c86d9524f1371de01f7c2aa32f9c666fe084f414f9663243c7a6ee01ca65e99ff4533c25f96166
data/README.md CHANGED
@@ -1,4 +1,44 @@
1
- at-do
2
- =====
1
+ atdo
2
+ ====
3
3
 
4
4
  At time, do code. That is all.
5
+
6
+ Oh, ok, if you insist, here's a little example:
7
+
8
+ ```ruby
9
+ require 'atdo'
10
+
11
+ scheduler = AtDo.new
12
+ scheduler.at Time.now + 2 do
13
+ puts "hello"
14
+ end
15
+ scheduler.at Time.now + 2 do
16
+ puts "world"
17
+ end
18
+ sleep 3
19
+ ```
20
+
21
+ And with rbtree storage instead of array:
22
+
23
+ ```ruby
24
+ require 'atdo'
25
+ require 'rbtree'
26
+
27
+ scheduler = AtDo.new storage: MultiRBTree
28
+ scheduler.at Time.now + 2 do
29
+ puts "hello"
30
+ end
31
+ scheduler.at Time.now + 2 do
32
+ puts "world"
33
+ end
34
+ sleep 3
35
+ ```
36
+
37
+ Both of these output
38
+
39
+ hello
40
+ world
41
+
42
+ See the unit tests for more examples.
43
+
44
+ The rbtree option is better for larger lists of tasks, especially with random inserts.
@@ -1,7 +1,7 @@
1
1
  require 'thread'
2
2
 
3
3
  class AtDo
4
- VERSION = "0.4"
4
+ VERSION = "0.5"
5
5
 
6
6
  # Storage classes known to work: Array (default), MultiRBTree.
7
7
  def initialize storage: Array
@@ -11,6 +11,7 @@ class AtDo
11
11
  @mon = Monitor.new
12
12
  @cvar = @mon.new_cond
13
13
  @thread = nil
14
+ @t0 = Time.now
14
15
  end
15
16
 
16
17
  def stop
@@ -23,8 +24,13 @@ class AtDo
23
24
  @thread.kill if @thread
24
25
  end
25
26
 
27
+ def wait
28
+ @thread.join
29
+ end
30
+
26
31
  def at time, &action
27
32
  thread
33
+ time < @t0
28
34
  @mon.synchronize do
29
35
  if @sorted
30
36
  @events[time] = action
@@ -40,6 +46,7 @@ class AtDo
40
46
 
41
47
  def thread
42
48
  @thread ||= Thread.new do
49
+ Thread.current.abort_on_exception = true
43
50
  @mon.synchronize do
44
51
  loop do
45
52
  duration =
@@ -45,7 +45,7 @@ module AtDoTests
45
45
  q.pop
46
46
  end
47
47
 
48
- def test_wait_indefinitely
48
+ def test_at_with_no_events
49
49
  q = Queue.new
50
50
  @s.at Time.now + 0.1 do
51
51
  q << true
@@ -65,7 +65,7 @@ module AtDoTests
65
65
  assert_equal "sleep", @s.thread.status
66
66
  end
67
67
 
68
- def test_wait_negative
68
+ def test_at_negative
69
69
  q = Queue.new
70
70
  @s.at Time.now - 1 do
71
71
  q << true
@@ -76,6 +76,15 @@ module AtDoTests
76
76
  assert_empty events
77
77
  assert_equal "sleep", @s.thread.status
78
78
  end
79
+
80
+ def test_incomparable
81
+ assert_raises ArgumentError do
82
+ @s.at(1) {}
83
+ end
84
+ assert_raises ArgumentError do
85
+ @s.at("foo") {}
86
+ end
87
+ end
79
88
  end
80
89
 
81
90
  class TestAtDo < Minitest::Test
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: atdo
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.4'
4
+ version: '0.5'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel VanderWerf
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-05 00:00:00.000000000 Z
11
+ date: 2013-09-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: At time, do code.
14
14
  email: vjoel@users.sourceforge.net