wait_until 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/wait_until.rb ADDED
@@ -0,0 +1,3 @@
1
+ require_relative "wait_until/wait"
2
+
3
+ Wait = WaitUntil::Wait
@@ -0,0 +1,3 @@
1
+ module WaitUntil
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,42 @@
1
+ module WaitUntil
2
+ class Wait
3
+
4
+ class << self
5
+
6
+ attr_accessor :default_timeout_in_seconds
7
+
8
+ def until_true!(description, &block)
9
+ start_time = Time.now
10
+ last_exc = nil
11
+ while true
12
+ begin
13
+ return if block.call
14
+ rescue => exc
15
+ last_exc = exc
16
+ end
17
+ elapsed_time = Time.now - start_time
18
+ if elapsed_time >= self.default_timeout_in_seconds
19
+ failure_message = "Timed-out waiting until '#{description}'"
20
+ failure_message << ". Last observed exception: #{last_exc}" if last_exc
21
+ raise failure_message
22
+ end
23
+ end
24
+ end
25
+
26
+ def until_false!(description, &block)
27
+ until_true!(description) { !block.call }
28
+ end
29
+
30
+ def until!(description, &block)
31
+ until_true!(description) do
32
+ block.call
33
+ true
34
+ end
35
+ end
36
+
37
+ end
38
+
39
+ self.default_timeout_in_seconds = ENV["timeout"] ? ENV["timeout"].to_i : 20
40
+
41
+ end
42
+ end
@@ -0,0 +1,120 @@
1
+ describe WaitUntil::Wait do
2
+
3
+ before(:all) do
4
+ @initial_default_timeout = WaitUntil::Wait.default_timeout_in_seconds
5
+ WaitUntil::Wait.default_timeout_in_seconds = 1
6
+ end
7
+
8
+ after(:all) { WaitUntil::Wait.default_timeout_in_seconds = @initial_default_timeout }
9
+
10
+ describe ".until_true!" do
11
+
12
+ describe "when the block returns true" do
13
+
14
+ it "should execute without error" do
15
+ lambda { WaitUntil::Wait.until_true!("some operation") { true } }.should_not raise_error
16
+ end
17
+
18
+ end
19
+
20
+ describe "when the blocks always returns false" do
21
+
22
+ it "should raise an error indicating the operation timed-out" do
23
+ lambda do
24
+ WaitUntil::Wait.until_true!("another operation finishes") { false }
25
+ end.should raise_error(/Timed-out waiting until 'another operation finishes'/i)
26
+ end
27
+
28
+ end
29
+
30
+ describe "when the block eventually returns true" do
31
+
32
+ it "should execute without error" do
33
+ invocation_count = 0
34
+ lambda do
35
+ WaitUntil::Wait.until_true!("some operation") do
36
+ invocation_count += 1
37
+ invocation_count == 3
38
+ end
39
+ end.should_not raise_error
40
+ end
41
+
42
+ end
43
+
44
+ end
45
+
46
+ describe ".until_false!" do
47
+
48
+ describe "when the block returns false" do
49
+
50
+ it "should execute without error" do
51
+ lambda { WaitUntil::Wait.until_false!("some operation") { false } }.should_not raise_error
52
+ end
53
+
54
+ end
55
+
56
+ describe "when the blocks always returns true" do
57
+
58
+ it "should raise an error indicating the operation timed-out" do
59
+ lambda do
60
+ WaitUntil::Wait.until_false!("another operation finishes") { true }
61
+ end.should raise_error(/Timed-out waiting until 'another operation finishes'/i)
62
+ end
63
+
64
+ end
65
+
66
+ describe "when the block eventually returns false" do
67
+
68
+ it "should execute without error" do
69
+ invocation_count = 0
70
+ lambda do
71
+ WaitUntil::Wait.until_false!("some operation") do
72
+ invocation_count += 1
73
+ invocation_count < 3
74
+ end
75
+ end.should_not raise_error
76
+ end
77
+
78
+ end
79
+
80
+ end
81
+
82
+ describe ".until!" do
83
+
84
+ describe "when the block executes without error" do
85
+
86
+ it "should execute without error" do
87
+ lambda { WaitUntil::Wait.until!("some operation") { } }.should_not raise_error
88
+ end
89
+
90
+ end
91
+
92
+ describe "when the block raises an error indefinitely" do
93
+
94
+ it "should raise an error indicating the operation timed-out" do
95
+ lambda do
96
+ WaitUntil::Wait.until!("some operation finishes") do
97
+ raise "forced error"
98
+ end
99
+ end.should raise_error(/Timed-out waiting until 'some operation finishes'/i)
100
+ end
101
+
102
+ end
103
+
104
+ describe "when the block eventually executes without error" do
105
+
106
+ it "should execute without error" do
107
+ invocation_count = 0
108
+ lambda do
109
+ WaitUntil::Wait.until!("some operation") do
110
+ invocation_count += 1
111
+ raise "forced error" if invocation_count < 3
112
+ end
113
+ end.should_not raise_error
114
+ end
115
+
116
+ end
117
+
118
+ end
119
+
120
+ end
@@ -0,0 +1,11 @@
1
+ describe WaitUntil do
2
+
3
+ describe "when loaded" do
4
+
5
+ it "should define the global Wait class as the WaitUntil::Wait class" do
6
+ Wait.should eql(WaitUntil::Wait)
7
+ end
8
+
9
+ end
10
+
11
+ end
@@ -0,0 +1,14 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter "/spec/"
4
+ minimum_coverage 100
5
+ refuse_coverage_drop
6
+ end if ENV["coverage"]
7
+
8
+ require_relative "../lib/wait_until"
9
+
10
+ require 'rubygems'
11
+ require 'bundler'
12
+ Bundler.require(:test)
13
+
14
+ Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |file| require file }
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wait_until
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matthew Ueckerman
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.12'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '2.12'
30
+ - !ruby/object:Gem::Dependency
31
+ name: simplecov
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.7.1
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.7.1
46
+ - !ruby/object:Gem::Dependency
47
+ name: flog
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 3.2.2
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 3.2.2
62
+ - !ruby/object:Gem::Dependency
63
+ name: travis-lint
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.6.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.6.0
78
+ description: Suspends execution until state changes via ::Wait.until! methods, timing-out
79
+ after a configured period of time
80
+ email: matthew.ueckerman@myob.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - ./lib/wait_until/version.rb
86
+ - ./lib/wait_until/wait.rb
87
+ - ./lib/wait_until.rb
88
+ - ./spec/lib/wait_until/wait_spec.rb
89
+ - ./spec/lib/wait_until/wait_until_spec.rb
90
+ - ./spec/spec_helper.rb
91
+ homepage: http://github.com/MYOB-Technology/wait_until
92
+ licenses:
93
+ - MIT
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: 1.9.3
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project: wait_until
112
+ rubygems_version: 1.8.25
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: Suspends execution until state changes via ::Wait.until! methods
116
+ test_files:
117
+ - ./spec/lib/wait_until/wait_spec.rb
118
+ - ./spec/lib/wait_until/wait_until_spec.rb
119
+ - ./spec/spec_helper.rb