atdo 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. checksums.yaml +7 -0
  2. data/COPYING +22 -0
  3. data/README.md +4 -0
  4. data/Rakefile +14 -0
  5. data/lib/atdo.rb +42 -0
  6. data/test/test-atdo.rb +24 -0
  7. metadata +59 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e7c4e361094e51cfb7d19b065d7d4ebf10cd750c
4
+ data.tar.gz: 380b12dc661cb6fd8024a6651b100be4d5feaa02
5
+ SHA512:
6
+ metadata.gz: bdddad9056c539fd25bc8bf390b0a00ff0ebb673326403ed03c183b57e2f63a1b343cdec514174302c4d3413055f85d68acc581b3c392f89f9ac85c339738471
7
+ data.tar.gz: d731ec6c16e2de918a69df81d054ab6fab38e572cf585fc91abcaee82f28e74e38f00637cb71aeb02ab154bd975aef254886dc13a78f935f0da13916152c7b65
data/COPYING ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013, Joel VanderWerf, vjoel@users.sourceforge.net
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+
13
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ at-do
2
+ =====
3
+
4
+ At time, do code. That is all.
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ desc "Run tests"
5
+ Rake::TestTask.new :test do |t|
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/*.rb"]
8
+ end
9
+
10
+ desc "Run benchmarks"
11
+ Rake::TestTask.new :bench do |t|
12
+ t.libs << "lib"
13
+ t.test_files = FileList["bench/*.rb"]
14
+ end
data/lib/atdo.rb ADDED
@@ -0,0 +1,42 @@
1
+ require 'thread'
2
+
3
+ class AtDo
4
+ def initialize
5
+ @events = [] ## option to use rbtree
6
+ @mutex = Mutex.new
7
+ @cvar = ConditionVariable.new
8
+ @thread = nil
9
+ end
10
+
11
+ def stop
12
+ @thread.kill if @thread
13
+ end
14
+
15
+ def at time, &action
16
+ thread
17
+ @mutex.synchronize do
18
+ @events << [time, action]
19
+ t, a = @events.sort_by! {|t, a| t}.first
20
+ @cvar.signal if t == time
21
+ end
22
+ end
23
+
24
+ def thread
25
+ @thread ||= Thread.new do
26
+ @mutex.synchronize do
27
+ loop do
28
+ t_now = Time.now
29
+ duration =
30
+ loop do
31
+ t, a = @events.first
32
+ break nil if !t
33
+ break t-t_now if t > t_now
34
+ a.call
35
+ @events.shift
36
+ end
37
+ @cvar.wait @mutex, *duration
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
data/test/test-atdo.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'minitest/autorun'
2
+ require 'atdo'
3
+
4
+ class TestAtDo < Minitest::Test
5
+ def setup
6
+ @s = AtDo.new
7
+ end
8
+
9
+ def test_timing
10
+ t0 = Time.now
11
+ a = []
12
+ (0..20).to_a.shuffle.each do |i|
13
+ @s.at t0 + i/100.0 do
14
+ a[i] = Time.now
15
+ end
16
+ end
17
+ sleep 0.21
18
+ 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
22
+ end
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: atdo
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Joel VanderWerf
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: At time, do code.
14
+ email: vjoel@users.sourceforge.net
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files:
18
+ - README.md
19
+ - COPYING
20
+ files:
21
+ - README.md
22
+ - COPYING
23
+ - Rakefile
24
+ - lib/atdo.rb
25
+ - test/test-atdo.rb
26
+ homepage: https://github.com/vjoel/atdo
27
+ licenses:
28
+ - BSD
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options:
32
+ - --quiet
33
+ - --line-numbers
34
+ - --inline-source
35
+ - --title
36
+ - atdo
37
+ - --main
38
+ - README.md
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 2.0.4
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Schedule code to be executed at specified time.
57
+ test_files:
58
+ - test/test-atdo.rb
59
+ has_rdoc: