em-ventually 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # EM::Ventually
2
2
 
3
+ ## Background
4
+
3
5
  Your tests will eventually pass. You're not sure when, but you know it'll be quick. This is where EM::Ventually can help you!
4
6
 
5
7
  Take this trivial example. (It's in Minitest, but you can use whatever test suite you'd like*)
@@ -13,7 +15,9 @@ Take this trivial example. (It's in Minitest, but you can use whatever test suit
13
15
 
14
16
  So, you want to test that val is 'test', and as well, you want EM to start on the beginning of your test and shutdown when it's done. The other thing you want is the ability to not let this test run forever, but to shutdown EM and consider it a failure if it doesn't complete.
15
17
 
16
- **Eventually is here to help!**
18
+ **EM::Ventually is here to help!**
19
+
20
+ `EM::Ventually` will take care to start and stop EventMachine, and make sure your tests run serially (unless you don't want them to). It will also prevent your tests from running forever.
17
21
 
18
22
  Taking the example above you can enqueue a test.
19
23
 
@@ -56,10 +60,21 @@ As well, simple returning doesn't always cover your test. You can pass values ba
56
60
  def test_em_simple
57
61
  val = nil
58
62
  EM.add_timer(0.5) { val = 'test1' }
59
- eventually('test1') { |value| value.call(val1) } # The secret sauce is if you called
60
- # `eventually` with a parameter or not.
63
+ eventually('test1') { |test| test[val1] } # The secret sauce is if you called
64
+ # `eventually` with a block that takes a parameter or not.
61
65
  end
62
66
  ~~~~~
63
67
 
64
- Right now, `Test::Unit`, `RSpec` and `MiniTest` are supported. Just do `include EM::Ventually` in your test and you'll get all this for free.
68
+ ## Usage
69
+
70
+ Right now, `Test::Unit`, `RSpec` and `MiniTest` are supported. Just do `include EM::Ventually` in your test and you'll get all this for free. If you want to use this in all your tests, you can `require 'em-ventually/{minitest,unittest,rspec}'` your appropriate test suite.
71
+
72
+ There are a couple of global options you can mess with too. `EM::Ventually.every_default=` and `EM::Ventually.total_default=` can both set global `:every` and `:total` times for your tests.
65
73
 
74
+ If you don't pass a value to eventually, it will test that your value is true (in the ruby sense). Optionally, you can call `.test` to pass a custom tester.
75
+
76
+ ~~~~~ {ruby}
77
+ def test_em_with_test
78
+ eventually(3.5).test{ |v| v < 4 }
79
+ end
80
+ ~~~~~
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ task :default => :test
5
5
  task :test do
6
6
  matchers = {
7
7
  'minitest-2.0' => [/6 tests, 9 assertions, 1 failures, 0 errors, 0 skips/m],
8
- 'rspec-2.6' => [/\n\.\.\.\.F\.\n/m],
8
+ 'rspec-2.6' => [/\n\.\.\.\.F\.\.\.\n/m],
9
9
  'test-unit' => [/\n\.\.\.F\.\.\n/m, /test_out_of_order\(TestEMVentually\)/m]
10
10
  }
11
11
 
data/em-ventually.gemspec CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |s|
20
20
 
21
21
  # dependencies
22
22
  s.add_runtime_dependency 'eventmachine'
23
- s.add_runtime_dependency 'callsite'
23
+ s.add_runtime_dependency 'callsite', '~> 0.0.5'
24
24
  s.add_development_dependency 'code_stats'
25
25
  s.add_development_dependency 'rake', '~> 0.8.7'
26
26
  s.add_development_dependency 'phocus'
data/lib/em-ventually.rb CHANGED
@@ -1,17 +1,34 @@
1
1
  require 'set'
2
2
  require 'eventmachine'
3
3
  require 'callsite'
4
- require 'em-ventually/version'
5
4
  require 'em-ventually/eventually'
5
+ require 'em-ventually/emify'
6
6
  require 'em-ventually/pool'
7
+ require 'em-ventually/version'
7
8
 
8
9
  module EventMachine
9
10
  module Ventually
11
+ def self.every_default=(value)
12
+ @every_default = value
13
+ end
14
+
15
+ def self.every_default
16
+ instance_variable_defined?(:@every_default) ? @every_default : 0.1
17
+ end
18
+
19
+ def self.total_default=(value)
20
+ @total_default = value
21
+ end
22
+
23
+ def self.total_default
24
+ instance_variable_defined?(:@total_default) ? @total_default : 1.0
25
+ end
26
+
10
27
  def _pool
11
28
  @_pool ||= Pool.new
12
29
  end
13
30
 
14
- def eventually(expectation = true, opts = nil, &block)
31
+ def eventually(expectation = nil, opts = nil, &block)
15
32
  ancestors = self.class.ancestors.map{|s| s.to_s}
16
33
  cls = if ancestors.include?('MiniTest::Unit::TestCase')
17
34
  Eventually::MiniTest
@@ -37,7 +54,7 @@ module EventMachine
37
54
  Eventually::MiniTest
38
55
  elsif ancestors.include?('Test::Unit::TestCase')
39
56
  Eventually::TestUnit
40
- elsif o.respond_to?(:superclass) && o.superclass.to_s == 'RSpec::Core::ExampleGroup'
57
+ elsif (o.respond_to?(:name) && o.name.to_s == 'RSpec::Core::ExampleGroup') || (o.respond_to?(:superclass) && o.superclass.to_s == 'RSpec::Core::ExampleGroup')
41
58
  Eventually::RSpec
42
59
  else
43
60
  raise("I know what testsuite i'm in!")
@@ -0,0 +1,21 @@
1
+ module EventMachine
2
+ module Ventually
3
+ module Emify
4
+ def _em
5
+ result = nil
6
+ if EM.reactor_running?
7
+ result = yield
8
+ else
9
+ EM.run do
10
+ begin
11
+ result = yield
12
+ ensure
13
+ EM.stop if (!instance_variable_defined?(:@_pool) || @_pool.nil? || @_pool.empty?) && EM.reactor_running?
14
+ end
15
+ end
16
+ end
17
+ result
18
+ end
19
+ end
20
+ end
21
+ end
@@ -12,13 +12,18 @@ module EventMachine
12
12
  @pool, @runner, @caller, @expectation, @opts, @block = pool, runner, caller, expectation, opts, block
13
13
  @count = 0
14
14
  @pool.push self
15
- @total_time = opts && opts[:total] || 1
16
- @every_time = opts && opts[:every] || 0.1
17
- run
15
+ @total_time = opts && opts[:total] || EventMachine::Ventually.total_default
16
+ @every_time = opts && opts[:every] || EventMachine::Ventually.every_default
17
+ @test_blk = expectation.nil? ? proc{|r| r } : proc{|r| expectation == r}
18
+ EM.add_timer(0.05) { run }
18
19
  end
19
20
 
20
- def assert_equal(got)
21
- got == expectation
21
+ def test(&blk)
22
+ @test_blk = blk
23
+ end
24
+
25
+ def assert_test(got)
26
+ @test_blk[got]
22
27
  end
23
28
 
24
29
  def formatted_message(msg)
@@ -26,18 +31,18 @@ module EventMachine
26
31
  end
27
32
 
28
33
  def kill_timer
29
- @kill_timer ||= EM.add_timer(@total_time) { stop(formatted_message("Exceeded time, expected #{expectation.inspect}, last value was #{@last_val.inspect}")) }
34
+ @kill_timer ||= EM.add_timer(@total_time) { stop(formatted_message("Exceeded time#{", expected #{expectation.inspect}" unless expectation.nil?}, last value was #{@last_val.inspect}")) }
30
35
  end
31
36
 
32
37
  def run
33
38
  if @pool.should_run?(self)
34
39
  kill_timer
35
40
  if @block.arity != 1
36
- process_equality(@last_val = @block.call)
41
+ process_test(@last_val = @block.call)
37
42
  else
38
43
  @block[proc { |val|
39
44
  @last_val = val
40
- process_equality(val)
45
+ process_test(val)
41
46
  }]
42
47
  end
43
48
  else
@@ -45,8 +50,8 @@ module EventMachine
45
50
  end
46
51
  end
47
52
 
48
- def process_equality(val)
49
- if assert_equal(val)
53
+ def process_test(val)
54
+ if assert_test(val)
50
55
  stop
51
56
  else
52
57
  @count += 1
@@ -3,35 +3,31 @@ module EventMachine
3
3
  class Eventually
4
4
  class MiniTest < Eventually
5
5
  def self.inject
6
- ::MiniTest::Unit::TestCase.class_eval <<-EOT, __FILE__, __LINE__ + 1
7
- alias_method :__original__send__, :__send__
8
- def __send__(*args, &blk)
9
- if Callsite.parse(caller.first).method == 'run'
10
- result = nil
11
- EM.run do
12
- begin
13
- result = __original__send__(*args, &blk)
14
- ensure
15
- EM.stop if (!instance_variable_defined?(:@_pool) || @_pool.nil? || @_pool.empty?) && EM.reactor_running?
16
- end
17
- result
6
+ unless ::MiniTest::Unit::TestCase.public_method_defined?(:_em)
7
+ ::MiniTest::Unit::TestCase.class_eval <<-EOT, __FILE__, __LINE__ + 1
8
+ include EM::Ventually::Emify
9
+ alias_method :__original__send__, :__send__
10
+ def __send__(*args, &blk)
11
+ if Callsite.parse(caller.first).method == 'run'
12
+ _em { __original__send__(*args, &blk) }
13
+ else
14
+ __original__send__(*args, &blk)
18
15
  end
19
- else
20
- __original__send__(*args, &blk)
21
16
  end
17
+ EOT
22
18
  end
23
- EOT
24
19
  end
25
20
 
26
21
  def report(msg)
27
22
  @runner.assert false, msg
28
23
  end
29
24
 
30
- def assert_equal(result)
25
+ def assert_test(result)
31
26
  e = expectation
32
- if result == expectation
27
+ if super
28
+ msg = formatted_message("#{result.inspect} passed")
33
29
  @runner.instance_eval do
34
- assert_equal result, e
30
+ assert true, msg
35
31
  end
36
32
  true
37
33
  else
@@ -3,41 +3,27 @@ module EventMachine
3
3
  class Eventually
4
4
  class RSpec < Eventually
5
5
  def self.inject
6
- ::RSpec::Core::ExampleGroup.class_eval <<-EOT, __FILE__, __LINE__ + 1
7
- alias_method :original_instance_eval, :instance_eval
8
- def instance_eval(&block)
9
- _em do
10
- original_instance_eval(&block)
6
+ unless ::RSpec::Core::ExampleGroup.public_method_defined?(:_em)
7
+ ::RSpec::Core::ExampleGroup.class_eval <<-EOT, __FILE__, __LINE__ + 1
8
+ include EM::Ventually::Emify
9
+ alias_method :original_instance_eval, :instance_eval
10
+ def instance_eval(&block)
11
+ _em { original_instance_eval(&block) }
11
12
  end
13
+ EOT
12
14
  end
13
-
14
- def _em
15
- result = nil
16
- if EM.reactor_running?
17
- result = yield
18
- else
19
- EM.run do
20
- begin
21
- result = yield
22
- ensure
23
- EM.stop if (!instance_variable_defined?(:@_pool) || @_pool.nil? || @_pool.empty?) && EM.reactor_running?
24
- end
25
- end
26
- end
27
- result
28
- end
29
- EOT
30
15
  end
31
16
 
32
17
  def report(msg)
33
18
  ::RSpec::Expectations.fail_with(msg)
34
19
  end
35
20
 
36
- def assert_equal(result)
21
+ def assert_test(result)
37
22
  e = expectation
38
- if result == expectation
23
+ if super
24
+ msg = formatted_message("#{result.inspect} passed")
39
25
  @runner.instance_eval do
40
- result.should == e
26
+ 1.should == 1
41
27
  end
42
28
  true
43
29
  else
@@ -3,35 +3,31 @@ module EventMachine
3
3
  class Eventually
4
4
  class TestUnit < Eventually
5
5
  def self.inject
6
- ::Test::Unit::TestCase.class_eval <<-EOT, __FILE__, __LINE__ + 1
7
- alias_method :__original__send__, :__send__
8
- def __send__(*args, &blk)
9
- if Callsite.parse(caller.first).method == 'run'
10
- result = nil
11
- EM.run do
12
- begin
13
- result = __original__send__(*args, &blk)
14
- ensure
15
- EM.stop if (!instance_variable_defined?(:@_pool) || @_pool.nil? || @_pool.empty?) && EM.reactor_running?
16
- end
17
- result
6
+ unless ::Test::Unit::TestCase.public_method_defined?(:_em)
7
+ ::Test::Unit::TestCase.class_eval <<-EOT, __FILE__, __LINE__ + 1
8
+ include EM::Ventually::Emify
9
+ alias_method :__original__send__, :__send__
10
+ def __send__(*args, &blk)
11
+ if Callsite.parse(caller.first).method == 'run'
12
+ _em { __original__send__(*args, &blk) }
13
+ else
14
+ __original__send__(*args, &blk)
18
15
  end
19
- else
20
- __original__send__(*args, &blk)
21
16
  end
17
+ EOT
22
18
  end
23
- EOT
24
19
  end
25
20
 
26
21
  def report(msg)
27
22
  @runner.assert false, msg
28
23
  end
29
24
 
30
- def assert_equal(result)
25
+ def assert_test(result)
31
26
  e = expectation
32
- if result == expectation
27
+ if super
28
+ msg = formatted_message("#{result.inspect} passed")
33
29
  @runner.instance_eval do
34
- assert_equal result, e
30
+ assert true, msg
35
31
  end
36
32
  true
37
33
  else
@@ -0,0 +1,6 @@
1
+ require 'em-ventually'
2
+
3
+ MiniTest::Unit::TestCase.class_eval do
4
+ include EM::Ventually
5
+ end
6
+
@@ -0,0 +1,5 @@
1
+ require 'em-ventually'
2
+
3
+ RSpec::Core::ExampleGroup.class_eval do
4
+ include EM::Ventually
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'em-ventually'
2
+
3
+ Test::Unit::TestCase.class_eval do
4
+ include EM::Ventually
5
+ end
@@ -1,5 +1,5 @@
1
1
  module EventMachine
2
2
  module Ventually
3
- VERSION = "0.0.1"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -1,8 +1,7 @@
1
1
  $LOAD_PATH << "#{File.dirname(__FILE__)}/../../lib"
2
- require 'em-ventually'
2
+ require 'em-ventually/rspec'
3
3
 
4
4
  describe EM::Ventually do
5
- include EM::Ventually
6
5
 
7
6
  it "should allow a normal test" do
8
7
  1.should == 1
@@ -45,4 +44,21 @@ describe EM::Ventually do
45
44
  eventually('test2') {|m| m[val] }
46
45
  }
47
46
  end
47
+
48
+ it "should allow longer tests with a specific number of passes" do
49
+ val = nil
50
+ count = 0
51
+ EM.add_timer(3.1) { val = 'done'}
52
+ eventually('done', :every => 0.5, :total => 3.5) { count +=1; val }
53
+ eventually(7) { count }
54
+ end
55
+
56
+ it "should allow custom tests" do
57
+ val = nil
58
+ count = 0
59
+ time = Time.new.to_f
60
+ EM.add_timer(0.5) { val = 'done'}
61
+ eventually('done') { val }
62
+ eventually { Time.new.to_f - time }.test{|t| t < 1}
63
+ end
48
64
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: em-ventually
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 0
9
8
  - 1
10
- version: 0.0.1
9
+ - 0
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Josh Hull
@@ -15,12 +15,13 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-08-07 00:00:00 -07:00
18
+ date: 2011-08-08 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
+ name: eventmachine
22
23
  prerelease: false
23
- version_requirements: &id001 !ruby/object:Gem::Requirement
24
+ requirement: &id001 !ruby/object:Gem::Requirement
24
25
  none: false
25
26
  requirements:
26
27
  - - ">="
@@ -29,26 +30,28 @@ dependencies:
29
30
  segments:
30
31
  - 0
31
32
  version: "0"
32
- requirement: *id001
33
- name: eventmachine
34
33
  type: :runtime
34
+ version_requirements: *id001
35
35
  - !ruby/object:Gem::Dependency
36
+ name: callsite
36
37
  prerelease: false
37
- version_requirements: &id002 !ruby/object:Gem::Requirement
38
+ requirement: &id002 !ruby/object:Gem::Requirement
38
39
  none: false
39
40
  requirements:
40
- - - ">="
41
+ - - ~>
41
42
  - !ruby/object:Gem::Version
42
- hash: 3
43
+ hash: 21
43
44
  segments:
44
45
  - 0
45
- version: "0"
46
- requirement: *id002
47
- name: callsite
46
+ - 0
47
+ - 5
48
+ version: 0.0.5
48
49
  type: :runtime
50
+ version_requirements: *id002
49
51
  - !ruby/object:Gem::Dependency
52
+ name: code_stats
50
53
  prerelease: false
51
- version_requirements: &id003 !ruby/object:Gem::Requirement
54
+ requirement: &id003 !ruby/object:Gem::Requirement
52
55
  none: false
53
56
  requirements:
54
57
  - - ">="
@@ -57,12 +60,12 @@ dependencies:
57
60
  segments:
58
61
  - 0
59
62
  version: "0"
60
- requirement: *id003
61
- name: code_stats
62
63
  type: :development
64
+ version_requirements: *id003
63
65
  - !ruby/object:Gem::Dependency
66
+ name: rake
64
67
  prerelease: false
65
- version_requirements: &id004 !ruby/object:Gem::Requirement
68
+ requirement: &id004 !ruby/object:Gem::Requirement
66
69
  none: false
67
70
  requirements:
68
71
  - - ~>
@@ -73,12 +76,12 @@ dependencies:
73
76
  - 8
74
77
  - 7
75
78
  version: 0.8.7
76
- requirement: *id004
77
- name: rake
78
79
  type: :development
80
+ version_requirements: *id004
79
81
  - !ruby/object:Gem::Dependency
82
+ name: phocus
80
83
  prerelease: false
81
- version_requirements: &id005 !ruby/object:Gem::Requirement
84
+ requirement: &id005 !ruby/object:Gem::Requirement
82
85
  none: false
83
86
  requirements:
84
87
  - - ">="
@@ -87,12 +90,12 @@ dependencies:
87
90
  segments:
88
91
  - 0
89
92
  version: "0"
90
- requirement: *id005
91
- name: phocus
92
93
  type: :development
94
+ version_requirements: *id005
93
95
  - !ruby/object:Gem::Dependency
96
+ name: bundler
94
97
  prerelease: false
95
- version_requirements: &id006 !ruby/object:Gem::Requirement
98
+ requirement: &id006 !ruby/object:Gem::Requirement
96
99
  none: false
97
100
  requirements:
98
101
  - - ~>
@@ -103,9 +106,8 @@ dependencies:
103
106
  - 0
104
107
  - 0
105
108
  version: 1.0.0
106
- requirement: *id006
107
- name: bundler
108
109
  type: :development
110
+ version_requirements: *id006
109
111
  description: Eventually, your tests should pass in EventMachine.
110
112
  email:
111
113
  - joshbuddy@gmail.com
@@ -122,11 +124,15 @@ files:
122
124
  - Rakefile
123
125
  - em-ventually.gemspec
124
126
  - lib/em-ventually.rb
127
+ - lib/em-ventually/emify.rb
125
128
  - lib/em-ventually/eventually.rb
126
129
  - lib/em-ventually/eventually/minitest.rb
127
130
  - lib/em-ventually/eventually/rspec.rb
128
131
  - lib/em-ventually/eventually/testunit.rb
132
+ - lib/em-ventually/minitest.rb
129
133
  - lib/em-ventually/pool.rb
134
+ - lib/em-ventually/rspec.rb
135
+ - lib/em-ventually/testunit.rb
130
136
  - lib/em-ventually/version.rb
131
137
  - test/minitest-2.0/Gemfile
132
138
  - test/minitest-2.0/Rakefile