wait_until 0.1.2 → 0.2.0

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: fda8f4fcee46c7d2d20f75ac05ad27aa74092d55
4
- data.tar.gz: 9411470c6fefa44fabb608fdd856869813d3459e
3
+ metadata.gz: 7de9f2648acd119acf6be1818db422565693cf99
4
+ data.tar.gz: 9fbebbe283132d1b03df299a37cb2f1d097b856c
5
5
  SHA512:
6
- metadata.gz: 0016e0f6c78d14283024120000a1d4dfb3b9cebc9a78ddef2c70aa60397fbb8ac915b698ec639246843062548fdaddaf5a7c46522b6c618d31faec771903e307
7
- data.tar.gz: 024e70bc0f3e21b7f5c7ac2ac49d66919b7ce9f92a2b3b3093e4f2c6924f9ce0388782bb11f5f3f161d77b32cec89bc357c6f1e110795628df2c3034812bb1c1
6
+ metadata.gz: e4dbbb3806f5b26b61660227f1429471d43e0a42673f222647d62f79bdad6f2fb9115161a747d5473e8bdb115c1fc5b82ae5df3d4ab2e6995bb13f43fa7cdc59
7
+ data.tar.gz: 11244dcf7441bca457c2cb5eb5cb009b7b5be9cf229745bb49ff8ebb514bc532c97c653dc116f765839332b5e81391d9bd5602300edb0e15c7ecafe8c6ef82f0
data/lib/wait_until.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require_relative "wait_until/operation"
1
2
  require_relative "wait_until/wait"
2
3
 
3
- Wait = WaitUntil::Wait
4
+ ::Wait = WaitUntil::Wait
@@ -0,0 +1,45 @@
1
+ module WaitUntil
2
+
3
+ class Operation
4
+
5
+ def initialize(description, options)
6
+ @description = description
7
+ @timeout_in_seconds = options[:timeout_in_seconds] || ::WaitUntil::Wait.default_timeout_in_seconds
8
+ @on_failure = options[:on_failure]
9
+ end
10
+
11
+ def eventually_true?(&block)
12
+ @start_time = Time.now
13
+ loop do
14
+ return true if true?(&block)
15
+ return false if timed_out?
16
+ end
17
+ end
18
+
19
+ def failure_message
20
+ message = "Timed-out waiting until '#{@description}'"
21
+ message << ". Last observed exception: #{@last_error}" if @last_error
22
+ message
23
+ end
24
+
25
+ private
26
+
27
+ def true?(&block)
28
+ begin
29
+ !!block.call
30
+ rescue => error
31
+ @last_error = error
32
+ false
33
+ end
34
+ end
35
+
36
+ def timed_out?
37
+ elapsed_time = Time.now - @start_time
38
+ is_timed_out = elapsed_time >= @timeout_in_seconds
39
+ @on_failure.call if @on_failure if is_timed_out
40
+ is_timed_out
41
+ end
42
+
43
+ end
44
+
45
+ end
@@ -1,3 +1,3 @@
1
1
  module WaitUntil
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,4 +1,5 @@
1
1
  module WaitUntil
2
+
2
3
  class Wait
3
4
 
4
5
  class << self
@@ -6,22 +7,8 @@ module WaitUntil
6
7
  attr_accessor :default_timeout_in_seconds
7
8
 
8
9
  def until_true!(description, options={}, &block)
9
- timeout_in_seconds = options[:timeout_in_seconds] || self.default_timeout_in_seconds
10
- start_time = Time.now
11
- last_exc = nil
12
- loop do
13
- begin
14
- return if block.call
15
- rescue => exc
16
- last_exc = exc
17
- end
18
- elapsed_time = Time.now - start_time
19
- if elapsed_time >= timeout_in_seconds
20
- failure_message = "Timed-out waiting until '#{description}'"
21
- failure_message << ". Last observed exception: #{last_exc}" if last_exc
22
- raise failure_message
23
- end
24
- end
10
+ operation = WaitUntil::Operation.new(description, options)
11
+ raise operation.failure_message unless operation.eventually_true?(&block)
25
12
  end
26
13
 
27
14
  def until_false!(description, options={}, &block)
@@ -40,4 +27,5 @@ module WaitUntil
40
27
  self.default_timeout_in_seconds = ENV["timeout"] ? ENV["timeout"].to_i : 20
41
28
 
42
29
  end
30
+
43
31
  end
@@ -0,0 +1,146 @@
1
+ describe WaitUntil::Operation do
2
+
3
+ class InvocationData
4
+
5
+ class << self
6
+ attr_accessor :counter
7
+ end
8
+
9
+ end
10
+
11
+ let(:description) { "some operations description" }
12
+ let(:options) { {} }
13
+
14
+ let(:operation) { described_class.new(description, options) }
15
+
16
+ before(:example) { InvocationData.counter = 0 }
17
+
18
+ describe "#eventually_true?" do
19
+
20
+ subject { operation.eventually_true?(&block) }
21
+
22
+ context "when the block returns true" do
23
+
24
+ let(:block) { lambda { true } }
25
+
26
+ it "returns true" do
27
+ expect(subject).to be(true)
28
+ end
29
+
30
+ end
31
+
32
+ context "when the blocks always returns false" do
33
+
34
+ let(:block) { lambda { false } }
35
+
36
+ it "returns false" do
37
+ expect(subject).to be(false)
38
+ end
39
+
40
+ context "and no timeout is provided" do
41
+
42
+ it "waits until at least the default period of time before returning false" do
43
+ start_time = Time.now
44
+
45
+ subject
46
+
47
+ period_of_time_waited = (Time.now - start_time)
48
+ expect(period_of_time_waited).to be >= WaitUntil::Wait.default_timeout_in_seconds
49
+ end
50
+
51
+ end
52
+
53
+ context "and a timeout is provided" do
54
+
55
+ let(:timeout_in_seconds) { 2 }
56
+ let(:options) { { timeout_in_seconds: timeout_in_seconds } }
57
+
58
+ it "waits until at least that period of time before returning false" do
59
+ start_time = Time.now
60
+
61
+ subject
62
+
63
+ period_of_time_waited = (Time.now - start_time)
64
+ expect(period_of_time_waited).to be >= timeout_in_seconds
65
+ end
66
+
67
+ end
68
+
69
+ context "and a failure callback is provided" do
70
+
71
+ let(:on_failure_callback) { lambda { InvocationData.counter += 1 } }
72
+ let(:options) { { on_failure: on_failure_callback } }
73
+
74
+ it "executes the callback once" do
75
+ subject
76
+
77
+ expect(InvocationData.counter).to eql(1)
78
+ end
79
+
80
+ end
81
+
82
+ end
83
+
84
+ context "when the block eventually returns true" do
85
+
86
+ let(:block) do
87
+ lambda do
88
+ InvocationData.counter += 1
89
+ InvocationData.counter == 3
90
+ end
91
+ end
92
+
93
+ it "returns true" do
94
+ expect(subject).to be(true)
95
+ end
96
+
97
+ end
98
+
99
+ end
100
+
101
+ describe "#failure_message" do
102
+
103
+ subject { operation.failure_message }
104
+
105
+ before(:example) { operation.eventually_true?(&block) }
106
+
107
+ shared_examples_for "a standard failure message" do
108
+
109
+ it "indicates a time-out occurred" do
110
+ expect(subject).to include("Timed-out")
111
+ end
112
+
113
+ it "contains the operations description" do
114
+ expect(subject).to include(description)
115
+ end
116
+
117
+ end
118
+
119
+ context "when no exception occurred evaluating the operation" do
120
+
121
+ let(:block) { lambda { false } }
122
+
123
+ it_behaves_like "a standard failure message"
124
+
125
+ end
126
+
127
+ context "when an exception occurred evaluating the operation" do
128
+
129
+ let(:block) do
130
+ lambda do
131
+ InvocationData.counter += 1
132
+ raise "Forced error #{InvocationData.counter}"
133
+ end
134
+ end
135
+
136
+ it_behaves_like "a standard failure message"
137
+
138
+ it "includes the last error" do
139
+ expect(subject).to include("Forced error #{InvocationData.counter}")
140
+ end
141
+
142
+ end
143
+
144
+ end
145
+
146
+ end
@@ -1,188 +1,146 @@
1
1
  describe WaitUntil::Wait do
2
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
3
+ let(:description) { "some operations description" }
4
+ let(:options) { {} }
7
5
 
8
- after(:all) { WaitUntil::Wait.default_timeout_in_seconds = @initial_default_timeout }
6
+ shared_examples_for "a wait method that times-out" do
9
7
 
10
- describe ".until_true!" do
8
+ it "raises an error indicating the operation timed-out" do
9
+ expect(subject).to raise_error(/Timed-out waiting until '#{description}'/i)
10
+ end
11
11
 
12
- context "when the block returns true" do
12
+ context "and a timeout is provided" do
13
13
 
14
- let(:block) do
15
- lambda { WaitUntil::Wait.until_true!("some operation") { true } }
16
- end
14
+ let(:timeout_in_seconds) { 2 }
15
+ let(:options) { { timeout_in_seconds: timeout_in_seconds } }
17
16
 
18
- it "executes without error" do
19
- expect(block).to_not raise_error
17
+ it "waits until at least that period of time before raising an error" do
18
+ start_time = Time.now
19
+
20
+ subject.call rescue nil
21
+
22
+ period_of_time_waited = (Time.now - start_time)
23
+ expect(period_of_time_waited).to be >= timeout_in_seconds
20
24
  end
21
25
 
22
26
  end
23
27
 
24
- context "when the blocks always returns false" do
28
+ end
25
29
 
26
- let(:timeout_in_seconds) { nil }
27
- let(:block) do
28
- lambda do
29
- WaitUntil::Wait.until_true!("another operation finishes", timeout_in_seconds: timeout_in_seconds) { false }
30
- end
31
- end
30
+ describe "::until_true!" do
32
31
 
33
- it "raises an error indicating the operation timed-out" do
34
- expect(block).to raise_error(/Timed-out waiting until 'another operation finishes'/i)
35
- end
32
+ subject { lambda { described_class.until_true!(description, options, &block) } }
33
+
34
+ context "when the block returns true" do
36
35
 
37
- context "and a timeout is provided" do
36
+ let(:block) { lambda { true } }
38
37
 
39
- let(:timeout_in_seconds) { 2 }
38
+ it "executes without error" do
39
+ expect(subject).to_not raise_error
40
+ end
40
41
 
41
- it "waits until at least a period of time matching the timeout before raising an error" do
42
- start_time = Time.now
42
+ end
43
43
 
44
- expect(block).to raise_error
44
+ context "when the blocks always returns false" do
45
45
 
46
- period_of_time_waited = (Time.now - start_time)
47
- expect(period_of_time_waited).to be >= timeout_in_seconds
48
- end
46
+ let(:block) { lambda { false } }
49
47
 
50
- end
48
+ it_behaves_like "a wait method that times-out"
51
49
 
52
50
  end
53
51
 
54
52
  context "when the block eventually returns true" do
55
53
 
56
- it "executes without error" do
54
+ let(:block) do
57
55
  invocation_count = 0
58
- expect(lambda do
59
- WaitUntil::Wait.until_true!("some operation") do
60
- invocation_count += 1
61
- invocation_count == 3
62
- end
63
- end).to_not raise_error
56
+ lambda do
57
+ invocation_count += 1
58
+ invocation_count == 3
59
+ end
60
+ end
61
+
62
+ it "executes without error" do
63
+ expect(subject).to_not raise_error
64
64
  end
65
65
 
66
66
  end
67
67
 
68
68
  end
69
69
 
70
- describe ".until_false!" do
70
+ describe "::until_false!" do
71
+
72
+ subject { lambda { described_class.until_false!(description, options, &block) } }
71
73
 
72
74
  context "when the block returns false" do
73
75
 
74
- let(:block) do
75
- lambda { WaitUntil::Wait.until_false!("some operation") { false } }
76
- end
76
+ let(:block) { lambda { false } }
77
77
 
78
78
  it "executes without error" do
79
- expect(block).to_not raise_error
79
+ expect(subject).to_not raise_error
80
80
  end
81
81
 
82
82
  end
83
83
 
84
84
  context "when the blocks always returns true" do
85
85
 
86
- let(:timeout_in_seconds) { nil }
87
- let(:block) do
88
- lambda do
89
- WaitUntil::Wait.until_false!("another operation finishes", timeout_in_seconds: timeout_in_seconds) { true }
90
- end
91
- end
92
-
93
- it "raises an error indicating the operation timed-out" do
94
- expect(block).to raise_error(/Timed-out waiting until 'another operation finishes'/i)
95
- end
86
+ let(:block) { lambda { true } }
96
87
 
97
- context "and a timeout is provided" do
88
+ it_behaves_like "a wait method that times-out"
98
89
 
99
- let(:timeout_in_seconds) { 2 }
100
-
101
- it "waits until at least a period of time matching the timeout before raising an error" do
102
- start_time = Time.now
90
+ end
103
91
 
104
- expect(block).to raise_error
92
+ context "when the block eventually returns false" do
105
93
 
106
- period_of_time_waited = (Time.now - start_time)
107
- expect(period_of_time_waited).to be >= timeout_in_seconds
94
+ let(:block) do
95
+ invocation_count = 0
96
+ lambda do
97
+ invocation_count += 1
98
+ invocation_count < 3
108
99
  end
109
-
110
100
  end
111
101
 
112
- end
113
-
114
- context "when the block eventually returns false" do
115
-
116
102
  it "executes without error" do
117
- invocation_count = 0
118
- expect(lambda do
119
- WaitUntil::Wait.until_false!("some operation") do
120
- invocation_count += 1
121
- invocation_count < 3
122
- end
123
- end).to_not raise_error
103
+ expect(subject).to_not raise_error
124
104
  end
125
105
 
126
106
  end
127
107
 
128
108
  end
129
109
 
130
- describe ".until!" do
110
+ describe "::until!" do
111
+
112
+ subject { lambda { described_class.until!(description, options, &block) } }
131
113
 
132
114
  context "when the block executes without error" do
133
115
 
134
- let(:block) do
135
- lambda { WaitUntil::Wait.until!("some operation") { } }
136
- end
116
+ let(:block) { lambda { nil } }
137
117
 
138
118
  it "executes without error" do
139
- expect(block).to_not raise_error
119
+ expect(subject).to_not raise_error
140
120
  end
141
121
 
142
122
  end
143
123
 
144
124
  context "when the block raises an error indefinitely" do
145
125
 
146
- let(:timeout_in_seconds) { nil }
147
- let(:block) do
148
- lambda do
149
- WaitUntil::Wait.until!("some operation finishes", timeout_in_seconds: timeout_in_seconds) do
150
- raise "forced error"
151
- end
152
- end
153
- end
126
+ let(:block) { lambda { raise "Forced Error" } }
154
127
 
155
- it "raises an error indicating the operation timed-out" do
156
- expect(block).to raise_error(/Timed-out waiting until 'some operation finishes'/i)
157
- end
128
+ it_behaves_like "a wait method that times-out"
158
129
 
159
- context "and a timeout is provided" do
160
-
161
- let(:timeout_in_seconds) { 2 }
162
-
163
- it "waits until at least a period of time matching the timeout before raising an error" do
164
- start_time = Time.now
130
+ end
165
131
 
166
- expect(block).to raise_error
132
+ context "when the block eventually executes without error" do
167
133
 
168
- period_of_time_waited = (Time.now - start_time)
169
- expect(period_of_time_waited).to be >= timeout_in_seconds
134
+ let(:block) do
135
+ invocation_count = 0
136
+ lambda do
137
+ invocation_count += 1
138
+ raise "Forced Error" if invocation_count < 3
170
139
  end
171
-
172
140
  end
173
141
 
174
- end
175
-
176
- context "when the block eventually executes without error" do
177
-
178
142
  it "executes without error" do
179
- invocation_count = 0
180
- expect(lambda do
181
- WaitUntil::Wait.until!("some operation") do
182
- invocation_count += 1
183
- raise "forced error" if invocation_count < 3
184
- end
185
- end).to_not raise_error
143
+ expect(subject).to_not raise_error
186
144
  end
187
145
 
188
146
  end
data/spec/spec_helper.rb CHANGED
@@ -11,3 +11,16 @@ end if ENV["coverage"]
11
11
  require_relative "../lib/wait_until"
12
12
 
13
13
  Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |file| require file }
14
+
15
+ RSpec.configure do |config|
16
+
17
+ config.before(:example) do
18
+ @initial_default_timeout = WaitUntil::Wait.default_timeout_in_seconds
19
+ WaitUntil::Wait.default_timeout_in_seconds = 1
20
+ end
21
+
22
+ config.after(:example) do
23
+ WaitUntil::Wait.default_timeout_in_seconds = @initial_default_timeout
24
+ end
25
+
26
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wait_until
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Ueckerman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-23 00:00:00.000000000 Z
11
+ date: 2016-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: travis-lint
@@ -24,62 +24,48 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.0'
27
- - !ruby/object:Gem::Dependency
28
- name: metric_fu
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '4.11'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '4.11'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: rspec
43
29
  requirement: !ruby/object:Gem::Requirement
44
30
  requirements:
45
31
  - - "~>"
46
32
  - !ruby/object:Gem::Version
47
- version: '3.2'
33
+ version: '3.4'
48
34
  type: :development
49
35
  prerelease: false
50
36
  version_requirements: !ruby/object:Gem::Requirement
51
37
  requirements:
52
38
  - - "~>"
53
39
  - !ruby/object:Gem::Version
54
- version: '3.2'
40
+ version: '3.4'
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: rake
57
43
  requirement: !ruby/object:Gem::Requirement
58
44
  requirements:
59
45
  - - "~>"
60
46
  - !ruby/object:Gem::Version
61
- version: '10.4'
47
+ version: '11.1'
62
48
  type: :development
63
49
  prerelease: false
64
50
  version_requirements: !ruby/object:Gem::Requirement
65
51
  requirements:
66
52
  - - "~>"
67
53
  - !ruby/object:Gem::Version
68
- version: '10.4'
54
+ version: '11.1'
69
55
  - !ruby/object:Gem::Dependency
70
56
  name: simplecov
71
57
  requirement: !ruby/object:Gem::Requirement
72
58
  requirements:
73
59
  - - "~>"
74
60
  - !ruby/object:Gem::Version
75
- version: '0.9'
61
+ version: '0.11'
76
62
  type: :development
77
63
  prerelease: false
78
64
  version_requirements: !ruby/object:Gem::Requirement
79
65
  requirements:
80
66
  - - "~>"
81
67
  - !ruby/object:Gem::Version
82
- version: '0.9'
68
+ version: '0.11'
83
69
  description: Suspends execution until state changes via ::Wait.until! methods, timing-out
84
70
  after a configured period of time
85
71
  email: matthew.ueckerman@myob.com
@@ -88,8 +74,10 @@ extensions: []
88
74
  extra_rdoc_files: []
89
75
  files:
90
76
  - "./lib/wait_until.rb"
77
+ - "./lib/wait_until/operation.rb"
91
78
  - "./lib/wait_until/version.rb"
92
79
  - "./lib/wait_until/wait.rb"
80
+ - "./spec/lib/wait_until/operation_spec.rb"
93
81
  - "./spec/lib/wait_until/wait_spec.rb"
94
82
  - "./spec/lib/wait_until/wait_until_spec.rb"
95
83
  - "./spec/spec_helper.rb"
@@ -113,11 +101,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
101
  version: '0'
114
102
  requirements: []
115
103
  rubyforge_project: wait_until
116
- rubygems_version: 2.2.2
104
+ rubygems_version: 2.4.6
117
105
  signing_key:
118
106
  specification_version: 4
119
107
  summary: Suspends execution until state changes via ::Wait.until! methods
120
108
  test_files:
109
+ - "./spec/lib/wait_until/operation_spec.rb"
121
110
  - "./spec/lib/wait_until/wait_spec.rb"
122
111
  - "./spec/lib/wait_until/wait_until_spec.rb"
123
112
  - "./spec/spec_helper.rb"