atdo 0.2 → 0.3

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/Rakefile +28 -11
  3. data/lib/atdo.rb +18 -8
  4. data/test/test-atdo.rb +58 -3
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 555a85a3b237ef5da7cb7f3946790eafe07d36b1
4
- data.tar.gz: 11234da5c86fa981bb0034d64ca65e146e1af8a2
3
+ metadata.gz: deedb56fa531c6a41a0c0d7536e9eedcc32b0155
4
+ data.tar.gz: d1a079aa08f9d0a23a0585fc0299a4a70892d7f2
5
5
  SHA512:
6
- metadata.gz: cb993d986361038735039ef1cd7f2547569e8f8dbe6b29171aa6f5403a2bb361e16922ff331314b45d24710b41f8d4c496ce080da1e9a22d03a4381f08ae510d
7
- data.tar.gz: 1e8e86339816bb5bf998895a8917ce43fa39719f52402fc1d03df34a8642cb09ea609bce0e32806f974df7b1f3f10363036d935784c1921fa6e0a2f2b3009372
6
+ metadata.gz: 13f6bead5151b902888637b2374542dfc3a50c32b56be504e231e5fad94e2cb51fd7942dea830e8af1bd6f92a77d7cdee5f3ba8a611287b5c6bfef16d5cf6c0c
7
+ data.tar.gz: dfa6afd17bd3dcce51c6e993be0a61232d3576ad7838cb79cc4643243986c5e70f863508464acaeb454c9cffd6ae959a7b901afc5b9187381ba37015f9633da5
data/Rakefile CHANGED
@@ -1,12 +1,19 @@
1
1
  require 'rake'
2
2
  require 'rake/testtask'
3
3
 
4
+ PRJ = "atdo"
5
+
4
6
  def version
5
- require 'atdo'
6
- @version ||= AtDo::VERSION
7
+ @version ||= begin
8
+ require 'atdo'
9
+ warn "AtDo::VERSION not a string" unless AtDo::VERSION.kind_of? String
10
+ AtDo::VERSION
11
+ end
7
12
  end
8
13
 
9
- prj = "atdo"
14
+ def tag
15
+ @tag ||= "#{PRJ}-#{version}"
16
+ end
10
17
 
11
18
  desc "Run tests"
12
19
  Rake::TestTask.new :test do |t|
@@ -20,27 +27,37 @@ Rake::TestTask.new :bench do |t|
20
27
  t.test_files = FileList["bench/*.rb"]
21
28
  end
22
29
 
23
- desc "commit, tag, and push repo; build and push gem"
24
- task :release do
30
+ desc "Commit, tag, and push repo; build and push gem"
31
+ task :release => "release:is_new_version" do
25
32
  require 'tempfile'
26
33
 
27
- tag = "#{prj}-#{version}"
28
-
29
- sh "gem build #{prj}.gemspec"
34
+ sh "gem build #{PRJ}.gemspec"
30
35
 
31
36
  file = Tempfile.new "template"
32
37
  begin
33
38
  file.puts "release #{version}"
34
39
  file.close
35
- sh "git commit -a -v -t #{file.path}"
40
+ sh "git commit --allow-empty -a -v -t #{file.path}"
36
41
  ensure
37
42
  file.close unless file.closed?
38
43
  file.unlink
39
44
  end
40
45
 
41
- sh "git tag #{prj}-#{version}"
46
+ sh "git tag #{tag}"
42
47
  sh "git push"
43
48
  sh "git push --tags"
44
49
 
45
- sh "gem push #{prj}-#{version}.gem"
50
+ sh "gem push #{tag}.gem"
51
+ end
52
+
53
+ namespace :release do
54
+ desc "Diff to latest release"
55
+ task :diff do
56
+ latest = `git describe --abbrev=0 --tags`
57
+ sh "git diff #{latest}"
58
+ end
59
+
60
+ task :is_new_version do
61
+ abort "#{tag} exists; update version!" unless `git tag -l #{tag}`.empty?
62
+ end
46
63
  end
data/lib/atdo.rb CHANGED
@@ -1,22 +1,28 @@
1
1
  require 'thread'
2
2
 
3
3
  class AtDo
4
- VERSION = 0.2
4
+ VERSION = "0.3"
5
5
 
6
6
  def initialize
7
7
  @events = [] ## option to use rbtree
8
- @mutex = Mutex.new
9
- @cvar = ConditionVariable.new
8
+ @mon = Monitor.new
9
+ @cvar = @mon.new_cond
10
10
  @thread = nil
11
11
  end
12
12
 
13
13
  def stop
14
+ @mon.synchronize do
15
+ @thread.kill if @thread
16
+ end
17
+ end
18
+
19
+ def stop!
14
20
  @thread.kill if @thread
15
21
  end
16
22
 
17
23
  def at time, &action
18
24
  thread
19
- @mutex.synchronize do
25
+ @mon.synchronize do
20
26
  @events << [time, action]
21
27
  t, a = @events.sort_by! {|t, a| t}.first
22
28
  @cvar.signal if t == time
@@ -25,18 +31,22 @@ class AtDo
25
31
 
26
32
  def thread
27
33
  @thread ||= Thread.new do
28
- @mutex.synchronize do
34
+ @mon.synchronize do
29
35
  loop do
30
- t_now = Time.now
31
36
  duration =
32
37
  loop do
33
38
  t, a = @events.first
34
39
  break nil if !t
40
+ t_now = Time.now
35
41
  break t-t_now if t > t_now
36
- a.call
42
+ begin
43
+ a.call
44
+ rescue => ex
45
+ ## ?
46
+ end
37
47
  @events.shift
38
48
  end
39
- @cvar.wait @mutex, *duration
49
+ @cvar.wait *duration
40
50
  end
41
51
  end
42
52
  end
data/test/test-atdo.rb CHANGED
@@ -6,6 +6,10 @@ class TestAtDo < Minitest::Test
6
6
  @s = AtDo.new
7
7
  end
8
8
 
9
+ def teardown
10
+ @s.stop
11
+ end
12
+
9
13
  def test_timing
10
14
  t0 = Time.now
11
15
  a = []
@@ -16,9 +20,60 @@ class TestAtDo < Minitest::Test
16
20
  end
17
21
  sleep 0.21
18
22
  assert a.all?
19
- assert_in_delta t0, a[0], 0.02
20
- (1..20).each do |i|
21
- assert_in_delta t0 + i/100.0, a[i], 0.01
23
+ (0..20).each do |i|
24
+ assert_in_delta t0 + i/100.0, a[i], 0.02
25
+ end
26
+ end
27
+
28
+ def test_reentrant
29
+ t0 = Time.now
30
+ n = 20
31
+ dt = 0.01
32
+ i = 0
33
+ q = Queue.new
34
+ pr = proc do
35
+ i += 1
36
+ if i <= n
37
+ t1 = t0 + i*dt
38
+ @s.at t1, &pr
39
+ else
40
+ assert_in_delta n*dt, Time.now - t0, 0.01
41
+ q << 1
42
+ end
43
+ end
44
+ pr.call
45
+ q.pop
46
+ end
47
+
48
+ def test_wait_indefinitely
49
+ q = Queue.new
50
+ @s.at Time.now + 0.1 do
51
+ q << true
22
52
  end
53
+ q.pop; Thread.pass
54
+
55
+ events = @s.instance_variable_get(:@events)
56
+ assert_empty events
57
+ assert_equal "sleep", @s.thread.status
58
+
59
+ @s.at Time.now + 0.1 do
60
+ q << true
61
+ end
62
+ q.pop; Thread.pass
63
+
64
+ assert_empty events
65
+ assert_equal "sleep", @s.thread.status
66
+ end
67
+
68
+ def test_wait_negative
69
+ q = Queue.new
70
+ @s.at Time.now - 1 do
71
+ q << true
72
+ end
73
+ q.pop; Thread.pass
74
+
75
+ events = @s.instance_variable_get(:@events)
76
+ assert_empty events
77
+ assert_equal "sleep", @s.thread.status
23
78
  end
24
79
  end
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.2'
4
+ version: '0.3'
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-07-17 00:00:00.000000000 Z
11
+ date: 2013-07-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: At time, do code.
14
14
  email: vjoel@users.sourceforge.net