rspec-eventually 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -0
- data/lib/rspec/eventually.rb +14 -3
- data/lib/rspec/eventually/version.rb +1 -1
- data/spec/eventually_spec.rb +47 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac1a8c6c27ec5d59a5b7d129296693c2d21dbb89
|
4
|
+
data.tar.gz: dc5f9cf13500fe274b2d828047e73a930d825c3b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc83c071364e151e0d1c66b13afa6ad3c68bae7bc95c0f256d34dfe18cfaa2996299dfeb899edc1b17673f33492f0faeccbaaf659e5583b4e3aa40943cef9663
|
7
|
+
data.tar.gz: d3c7cc383897c3f8c5cb8b3d3eefe6061e2e29e2bd4920b918461bc1b3c307834ba21bd5eb4912dfdd1b976ca8407f326b31d4b8cb69fb0ef76ce33f9d0d41fb
|
data/README.md
CHANGED
data/lib/rspec/eventually.rb
CHANGED
@@ -4,9 +4,10 @@ require 'rspec/core'
|
|
4
4
|
module Rspec
|
5
5
|
module Eventually
|
6
6
|
class << self
|
7
|
-
attr_accessor :timeout
|
7
|
+
attr_accessor :timeout, :pause
|
8
8
|
end
|
9
9
|
self.timeout = 5
|
10
|
+
self.pause = 0.1
|
10
11
|
|
11
12
|
class FailedMatcherError < StandardError; end
|
12
13
|
|
@@ -20,12 +21,14 @@ module Rspec
|
|
20
21
|
@tries = 0
|
21
22
|
@negative = false
|
22
23
|
@custom_msg = custom_msg
|
24
|
+
@pause = pause
|
23
25
|
end
|
24
26
|
|
25
27
|
def matches?(expected_block)
|
26
28
|
Timeout.timeout(timeout) { eventually_matches? expected_block }
|
27
29
|
rescue Timeout::Error
|
28
|
-
@tries == 0 && raise('Timeout before first evaluation, use a longer `eventually` timeout
|
30
|
+
@tries == 0 && raise('Timeout before first evaluation, use a longer `eventually` timeout \
|
31
|
+
or shorter `eventually` pause')
|
29
32
|
end
|
30
33
|
|
31
34
|
def does_not_match?
|
@@ -53,13 +56,17 @@ module Rspec
|
|
53
56
|
tap { @timeout = timeout }
|
54
57
|
end
|
55
58
|
|
59
|
+
def pause_for(pause)
|
60
|
+
tap { @pause = pause }
|
61
|
+
end
|
62
|
+
|
56
63
|
private
|
57
64
|
|
58
65
|
def eventually_matches?(expected_block)
|
59
66
|
target_matches?(expected_block) || fail(FailedMatcherError)
|
60
67
|
rescue => e
|
61
68
|
if suppress_errors || e.is_a?(FailedMatcherError)
|
62
|
-
sleep
|
69
|
+
sleep pause
|
63
70
|
@tries += 1
|
64
71
|
retry
|
65
72
|
else
|
@@ -75,6 +82,10 @@ module Rspec
|
|
75
82
|
def timeout
|
76
83
|
@timeout || Rspec::Eventually.timeout
|
77
84
|
end
|
85
|
+
|
86
|
+
def pause
|
87
|
+
@pause || Rspec::Eventually.pause
|
88
|
+
end
|
78
89
|
end
|
79
90
|
|
80
91
|
def eventually(target, custom_msg = nil)
|
data/spec/eventually_spec.rb
CHANGED
@@ -51,6 +51,45 @@ module Rspec
|
|
51
51
|
expect(took).to be_within(0.1).of 0.5
|
52
52
|
end
|
53
53
|
|
54
|
+
it 'has a configurable default pause' do
|
55
|
+
begin
|
56
|
+
::Rspec::Eventually.pause = 0.5
|
57
|
+
expect(::Rspec::Eventually.pause).to eq 0.5
|
58
|
+
|
59
|
+
matcher = Eventually.new(eq 1)
|
60
|
+
matcher.matches? -> { 0 }
|
61
|
+
|
62
|
+
expect(matcher.instance_variable_get('@tries')).to eq 9
|
63
|
+
|
64
|
+
ensure
|
65
|
+
::Rspec::Eventually.pause = 0.1
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'can have a specific pause' do
|
70
|
+
matcher = Eventually.new(eq 1)
|
71
|
+
matcher.pause_for(1.5).matches? -> { 0 }
|
72
|
+
|
73
|
+
expect(matcher.instance_variable_get('@tries')).to eq 3
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'can have a specific pause and timeout' do
|
77
|
+
matcher = Eventually.new(eq 1)
|
78
|
+
matcher.pause_for(1).within(3).matches? -> { 0 }
|
79
|
+
|
80
|
+
expect(matcher.instance_variable_get('@tries')).to eq 2
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'it will not pause when evaluation is true' do
|
84
|
+
before = Time.now
|
85
|
+
matcher = Eventually.new(eq 1)
|
86
|
+
matcher.pause_for(1).matches? -> { 1 }
|
87
|
+
took = Time.now - before
|
88
|
+
|
89
|
+
expect(matcher.instance_variable_get('@tries')).to eq 0
|
90
|
+
expect(took).to be < 1
|
91
|
+
end
|
92
|
+
|
54
93
|
it 'raises errors by default' do
|
55
94
|
block = lambda do
|
56
95
|
Eventually.new(eq 1).matches? -> { fail 'I am throwing an error' }
|
@@ -98,6 +137,14 @@ module Rspec
|
|
98
137
|
expect { block.call }.to raise_error(/Timeout before first evaluation/)
|
99
138
|
end
|
100
139
|
|
140
|
+
it 'raises an error when timeout occurs before the first evaluation due to long pause' do
|
141
|
+
block = lambda do
|
142
|
+
Eventually.new(eq 1).pause_for(1).within(0.5).matches? -> { sleep 5 }
|
143
|
+
end
|
144
|
+
|
145
|
+
expect { block.call }.to raise_error(/Timeout before first evaluation/)
|
146
|
+
end
|
147
|
+
|
101
148
|
it 'supports custom error messages' do
|
102
149
|
(matcher = Eventually.new(eq(1), '0 is not within 0.5 of 1').within(0.5)).matches? -> { 0 }
|
103
150
|
message = matcher.failure_message
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-eventually
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hawk Newton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-08-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -107,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
107
107
|
version: '0'
|
108
108
|
requirements: []
|
109
109
|
rubyforge_project:
|
110
|
-
rubygems_version: 2.
|
110
|
+
rubygems_version: 2.4.5.1
|
111
111
|
signing_key:
|
112
112
|
specification_version: 4
|
113
113
|
summary: Make your matchers match eventually
|