wait_until 0.2.0 → 0.3.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: 7de9f2648acd119acf6be1818db422565693cf99
4
- data.tar.gz: 9fbebbe283132d1b03df299a37cb2f1d097b856c
3
+ metadata.gz: a5c68a76d714894ee9485a1b5ac4912796483a0f
4
+ data.tar.gz: 0e2f8e3181b35e07be9db356b98fff21bc48cc1a
5
5
  SHA512:
6
- metadata.gz: e4dbbb3806f5b26b61660227f1429471d43e0a42673f222647d62f79bdad6f2fb9115161a747d5473e8bdb115c1fc5b82ae5df3d4ab2e6995bb13f43fa7cdc59
7
- data.tar.gz: 11244dcf7441bca457c2cb5eb5cb009b7b5be9cf229745bb49ff8ebb514bc532c97c653dc116f765839332b5e81391d9bd5602300edb0e15c7ecafe8c6ef82f0
6
+ metadata.gz: 206d00e29b59bda409e945970a93d7ba81b84337297c40f0e4adbd956830cf79615e1ff3dd00fdfeb4590b84e1120c3e3bdb3ea237e7263e5043045e843d68ac
7
+ data.tar.gz: d49ed27c88ae9885dbfa8d0689d9696ce37a2647980dfe5ef6633422e64848bd6479764b06f2a81562711a18fe13f74dc8c2d1c1824e5e965c18ac0deaaf2082
@@ -2,10 +2,11 @@ module WaitUntil
2
2
 
3
3
  class Operation
4
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]
5
+ def initialize(args)
6
+ @timeout_in_seconds = args[:timeout_in_seconds] || ::WaitUntil::Wait.default_timeout_in_seconds
7
+ @description = args[:description]
8
+ @failure_message = args[:failure_message]
9
+ @on_failure = args[:on_failure]
9
10
  end
10
11
 
11
12
  def eventually_true?(&block)
@@ -17,8 +18,10 @@ module WaitUntil
17
18
  end
18
19
 
19
20
  def failure_message
20
- message = "Timed-out waiting until '#{@description}'"
21
- message << ". Last observed exception: #{@last_error}" if @last_error
21
+ message = @failure_message
22
+ message ||= "Timed-out waiting until '#{@description}'" if @description
23
+ message ||= "Timed-out waiting until operation completed"
24
+ message << "\nLast observed exception: #{@last_error}" if @last_error
22
25
  message
23
26
  end
24
27
 
@@ -1,3 +1,3 @@
1
1
  module WaitUntil
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -6,17 +6,17 @@ module WaitUntil
6
6
 
7
7
  attr_accessor :default_timeout_in_seconds
8
8
 
9
- def until_true!(description, options={}, &block)
10
- operation = WaitUntil::Operation.new(description, options)
9
+ def until_true!(args={}, &block)
10
+ operation = WaitUntil::Operation.new(args)
11
11
  raise operation.failure_message unless operation.eventually_true?(&block)
12
12
  end
13
13
 
14
- def until_false!(description, options={}, &block)
15
- until_true!(description, options) { !block.call }
14
+ def until_false!(args={}, &block)
15
+ until_true!(args) { !block.call }
16
16
  end
17
17
 
18
- def until!(description, options={}, &block)
19
- until_true!(description, options) do
18
+ def until!(args={}, &block)
19
+ until_true!(args) do
20
20
  block.call
21
21
  true
22
22
  end
@@ -8,10 +8,9 @@ describe WaitUntil::Operation do
8
8
 
9
9
  end
10
10
 
11
- let(:description) { "some operations description" }
12
- let(:options) { {} }
11
+ let(:args) { {} }
13
12
 
14
- let(:operation) { described_class.new(description, options) }
13
+ let(:operation) { described_class.new(args) }
15
14
 
16
15
  before(:example) { InvocationData.counter = 0 }
17
16
 
@@ -53,7 +52,7 @@ describe WaitUntil::Operation do
53
52
  context "and a timeout is provided" do
54
53
 
55
54
  let(:timeout_in_seconds) { 2 }
56
- let(:options) { { timeout_in_seconds: timeout_in_seconds } }
55
+ let(:args) { { timeout_in_seconds: timeout_in_seconds } }
57
56
 
58
57
  it "waits until at least that period of time before returning false" do
59
58
  start_time = Time.now
@@ -69,7 +68,7 @@ describe WaitUntil::Operation do
69
68
  context "and a failure callback is provided" do
70
69
 
71
70
  let(:on_failure_callback) { lambda { InvocationData.counter += 1 } }
72
- let(:options) { { on_failure: on_failure_callback } }
71
+ let(:args) { { on_failure: on_failure_callback } }
73
72
 
74
73
  it "executes the callback once" do
75
74
  subject
@@ -106,12 +105,53 @@ describe WaitUntil::Operation do
106
105
 
107
106
  shared_examples_for "a standard failure message" do
108
107
 
109
- it "indicates a time-out occurred" do
110
- expect(subject).to include("Timed-out")
108
+ context "and a custom failure message was provided" do
109
+
110
+ let(:failure_message) { "some failure message" }
111
+ let(:args) { { failure_message: failure_message } }
112
+
113
+ it "contains the failure message" do
114
+ expect(subject).to include(failure_message)
115
+ end
116
+
117
+ context "and an operation description was provided" do
118
+
119
+ let(:description) { "some operation description" }
120
+ let(:args) { { failure_message: failure_message, description: description } }
121
+
122
+ it "contains the failure message" do
123
+ expect(subject).to include(failure_message)
124
+ end
125
+
126
+ it "excludes the description" do
127
+ expect(subject).to_not include(description)
128
+ end
129
+
130
+ end
131
+
111
132
  end
112
133
 
113
- it "contains the operations description" do
114
- expect(subject).to include(description)
134
+ context "and an operation description was provided" do
135
+
136
+ let(:description) { "some operation description" }
137
+ let(:args) { { description: description } }
138
+
139
+ it "indicates a time-out occurred" do
140
+ expect(subject).to include("Timed-out")
141
+ end
142
+
143
+ it "contains the description" do
144
+ expect(subject).to include(description)
145
+ end
146
+
147
+ end
148
+
149
+ context "and no failure message or description was provided" do
150
+
151
+ it "indicates a time-out occurred" do
152
+ expect(subject).to include("Timed-out")
153
+ end
154
+
115
155
  end
116
156
 
117
157
  end
@@ -1,26 +1,46 @@
1
1
  describe WaitUntil::Wait do
2
2
 
3
- let(:description) { "some operations description" }
4
- let(:options) { {} }
3
+ let(:block) { lambda { "some block" } }
4
+ let(:args) { {} }
5
5
 
6
- shared_examples_for "a wait method that times-out" do
6
+ shared_examples_for "a wait method that uses an operation to determine if it eventually succeeds" do
7
7
 
8
- it "raises an error indicating the operation timed-out" do
9
- expect(subject).to raise_error(/Timed-out waiting until '#{description}'/i)
8
+ let(:eventually_true_flag) { true }
9
+ let(:failure_message) { "some failure message" }
10
+ let(:operation) do
11
+ instance_double(WaitUntil::Operation, eventually_true?: eventually_true_flag, failure_message: failure_message)
10
12
  end
11
13
 
12
- context "and a timeout is provided" do
14
+ before(:example) { allow(WaitUntil::Operation).to receive(:new).and_return(operation) }
13
15
 
14
- let(:timeout_in_seconds) { 2 }
15
- let(:options) { { timeout_in_seconds: timeout_in_seconds } }
16
+ it "creates an operation representing the call" do
17
+ expect(WaitUntil::Operation).to receive(:new).with(args)
16
18
 
17
- it "waits until at least that period of time before raising an error" do
18
- start_time = Time.now
19
+ subject
20
+ end
21
+
22
+ it "determines if the operation eventually returns true" do
23
+ expect(operation).to receive(:eventually_true?)
24
+
25
+ subject
26
+ end
27
+
28
+ context "when the operation is eventually true" do
19
29
 
20
- subject.call rescue nil
30
+ let(:eventually_true_flag) { true }
21
31
 
22
- period_of_time_waited = (Time.now - start_time)
23
- expect(period_of_time_waited).to be >= timeout_in_seconds
32
+ it "executes without error" do
33
+ expect(lambda { subject }).to_not raise_error
34
+ end
35
+
36
+ end
37
+
38
+ context "when the operation is never eventually true" do
39
+
40
+ let(:eventually_true_flag) { false }
41
+
42
+ it "raises an error with the operations failure message" do
43
+ expect(lambda { subject }).to raise_error(failure_message)
24
44
  end
25
45
 
26
46
  end
@@ -29,38 +49,26 @@ describe WaitUntil::Wait do
29
49
 
30
50
  describe "::until_true!" do
31
51
 
32
- subject { lambda { described_class.until_true!(description, options, &block) } }
52
+ subject { described_class.until_true!(args, &block) }
53
+
54
+ it_behaves_like "a wait method that uses an operation to determine if it eventually succeeds"
33
55
 
34
56
  context "when the block returns true" do
35
57
 
36
58
  let(:block) { lambda { true } }
37
59
 
38
60
  it "executes without error" do
39
- expect(subject).to_not raise_error
61
+ expect(lambda { subject }).to_not raise_error
40
62
  end
41
63
 
42
64
  end
43
65
 
44
- context "when the blocks always returns false" do
66
+ context "when the block returns false" do
45
67
 
46
68
  let(:block) { lambda { false } }
47
69
 
48
- it_behaves_like "a wait method that times-out"
49
-
50
- end
51
-
52
- context "when the block eventually returns true" do
53
-
54
- let(:block) do
55
- invocation_count = 0
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
70
+ it "raises an error" do
71
+ expect(lambda { subject }).to raise_error(/timed-out/i)
64
72
  end
65
73
 
66
74
  end
@@ -69,38 +77,26 @@ describe WaitUntil::Wait do
69
77
 
70
78
  describe "::until_false!" do
71
79
 
72
- subject { lambda { described_class.until_false!(description, options, &block) } }
80
+ subject { described_class.until_false!(args, &block) }
81
+
82
+ it_behaves_like "a wait method that uses an operation to determine if it eventually succeeds"
73
83
 
74
84
  context "when the block returns false" do
75
85
 
76
86
  let(:block) { lambda { false } }
77
87
 
78
88
  it "executes without error" do
79
- expect(subject).to_not raise_error
89
+ expect(lambda { subject }).to_not raise_error
80
90
  end
81
91
 
82
92
  end
83
93
 
84
- context "when the blocks always returns true" do
94
+ context "when the block returns true" do
85
95
 
86
96
  let(:block) { lambda { true } }
87
97
 
88
- it_behaves_like "a wait method that times-out"
89
-
90
- end
91
-
92
- context "when the block eventually returns false" do
93
-
94
- let(:block) do
95
- invocation_count = 0
96
- lambda do
97
- invocation_count += 1
98
- invocation_count < 3
99
- end
100
- end
101
-
102
- it "executes without error" do
103
- expect(subject).to_not raise_error
98
+ it "raises an error" do
99
+ expect(lambda { subject }).to raise_error(/timed-out/i)
104
100
  end
105
101
 
106
102
  end
@@ -109,38 +105,26 @@ describe WaitUntil::Wait do
109
105
 
110
106
  describe "::until!" do
111
107
 
112
- subject { lambda { described_class.until!(description, options, &block) } }
108
+ subject { described_class.until!(args, &block) }
109
+
110
+ it_behaves_like "a wait method that uses an operation to determine if it eventually succeeds"
113
111
 
114
112
  context "when the block executes without error" do
115
113
 
116
- let(:block) { lambda { nil } }
114
+ let(:block) { lambda { "does not raise an error" } }
117
115
 
118
116
  it "executes without error" do
119
- expect(subject).to_not raise_error
117
+ expect(lambda { subject }).to_not raise_error
120
118
  end
121
119
 
122
120
  end
123
121
 
124
- context "when the block raises an error indefinitely" do
125
-
126
- let(:block) { lambda { raise "Forced Error" } }
122
+ context "when the block raises an error" do
127
123
 
128
- it_behaves_like "a wait method that times-out"
129
-
130
- end
124
+ let(:block) { lambda { raise "forced error" } }
131
125
 
132
- context "when the block eventually executes without error" do
133
-
134
- let(:block) do
135
- invocation_count = 0
136
- lambda do
137
- invocation_count += 1
138
- raise "Forced Error" if invocation_count < 3
139
- end
140
- end
141
-
142
- it "executes without error" do
143
- expect(subject).to_not raise_error
126
+ it "raises an error" do
127
+ expect(lambda { subject }).to raise_error(/timed-out/i)
144
128
  end
145
129
 
146
130
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wait_until
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Ueckerman