em-test 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +46 -0
- data/Rakefile +8 -0
- data/em-test.gemspec +21 -0
- data/lib/em-test.rb +95 -0
- data/lib/em-test/version.rb +5 -0
- data/test/api_test.rb +88 -0
- data/test/test_helper.rb +6 -0
- metadata +66 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
About
|
2
|
+
=====
|
3
|
+
|
4
|
+
Simple TDD API for testing asynchronous EventMachine code. Extracted from em-spec.
|
5
|
+
|
6
|
+
(c) 2008 Aman Gupta (tmm1)
|
7
|
+
|
8
|
+
Usage
|
9
|
+
=====
|
10
|
+
|
11
|
+
There are two ways to use the `Test::Unit` extension. To use it as a helper, include `EM::TestHelper` in your test unit class. You then use the `em` method to wrap your evented test code. Inside the `em` block, you must call `#done` after your expectations. Everything works normally otherwise.
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
require 'em-test'
|
15
|
+
|
16
|
+
class EmTestHelperTest < Test::Unit::TestCase
|
17
|
+
include EventMachine::TestHelper
|
18
|
+
|
19
|
+
def test_trivial
|
20
|
+
em do
|
21
|
+
assert_equal 1, 1
|
22
|
+
done
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
```
|
27
|
+
|
28
|
+
The other option is to include `EM::Test` in your test class. This will patch `Test::Unit` so that all of your examples run inside an `em` block automatically:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
require 'em-test'
|
32
|
+
|
33
|
+
class EmTestTest < Test::Unit::TestCase
|
34
|
+
include EventMachine::Test
|
35
|
+
|
36
|
+
def test_timer
|
37
|
+
start = Time.now
|
38
|
+
|
39
|
+
EM.add_timer(0.5){
|
40
|
+
assert_in_delta 0.5, Time.now-start, 0.1
|
41
|
+
done
|
42
|
+
}
|
43
|
+
end
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
data/Rakefile
ADDED
data/em-test.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "em-test/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "em-test"
|
7
|
+
s.version = EventMachine::Test::VERSION
|
8
|
+
s.authors = ["Paweł Pacana"]
|
9
|
+
s.email = ["pawel.pacana@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = "TDD for EventMachine"
|
12
|
+
s.description = "Simple TDD API for testing asynchronous EventMachine code. Extracted from em-spec to remove dependencies."
|
13
|
+
s.rubyforge_project = "em-test"
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.required_ruby_version = '~> 1.9.2'
|
19
|
+
|
20
|
+
s.add_dependency 'eventmachine', '>= 1.0.0.beta.1'
|
21
|
+
end
|
data/lib/em-test.rb
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'em-test/version'
|
2
|
+
require 'eventmachine'
|
3
|
+
|
4
|
+
module EventMachine
|
5
|
+
TestTimeoutExceededError = Class.new(RuntimeError)
|
6
|
+
|
7
|
+
module TestHelper
|
8
|
+
|
9
|
+
def self.included(cls)
|
10
|
+
cls.class_eval(<<-HERE_DOC, __FILE__, __LINE__)
|
11
|
+
DefaultTimeout = nil unless const_defined?(:DefaultTimeout)
|
12
|
+
|
13
|
+
def self.default_timeout(timeout)
|
14
|
+
|
15
|
+
if self.const_defined? :DefaultTimeout
|
16
|
+
self.ancestors.each do |cls|
|
17
|
+
begin
|
18
|
+
cls.send(:remove_const, :DefaultTimeout)
|
19
|
+
cls.send(:const_set, :DefaultTimeout, timeout)
|
20
|
+
break
|
21
|
+
rescue
|
22
|
+
end
|
23
|
+
end
|
24
|
+
else
|
25
|
+
self.send(:const_set, :DefaultTimeout, timeout)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def current_default_timeout
|
30
|
+
DefaultTimeout
|
31
|
+
end
|
32
|
+
HERE_DOC
|
33
|
+
end
|
34
|
+
|
35
|
+
def timeout(time_to_run)
|
36
|
+
EM.cancel_timer(@_em_timer) if @_em_timer
|
37
|
+
@_em_timer = EM.add_timer(time_to_run) { done('timeout exceeded') }
|
38
|
+
end
|
39
|
+
|
40
|
+
def em(time_to_run = current_default_timeout, &block)
|
41
|
+
@flunk_test = nil
|
42
|
+
EM.run do
|
43
|
+
timeout(time_to_run) if time_to_run
|
44
|
+
em_spec_exception = nil
|
45
|
+
@_em_spec_fiber = Fiber.new do
|
46
|
+
@_output = begin
|
47
|
+
block.call
|
48
|
+
rescue Exception => em_spec_exception
|
49
|
+
done
|
50
|
+
end
|
51
|
+
Fiber.yield
|
52
|
+
end
|
53
|
+
@_em_spec_fiber.resume
|
54
|
+
|
55
|
+
raise em_spec_exception if em_spec_exception
|
56
|
+
end
|
57
|
+
raise(@flunk_test) if @flunk_test
|
58
|
+
@_output
|
59
|
+
end
|
60
|
+
|
61
|
+
def done(flunk_reason = nil)
|
62
|
+
EM.next_tick {
|
63
|
+
@flunk_test = flunk_reason
|
64
|
+
finish_em_spec_fiber
|
65
|
+
}
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
def finish_em_spec_fiber
|
70
|
+
EM.stop_event_loop if EM.reactor_running?
|
71
|
+
@_em_spec_fiber.resume if @_em_spec_fiber.alive?
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
module Test
|
77
|
+
def self.included(cls)
|
78
|
+
cls.class_eval(<<-HERE_DOC, __FILE__, __LINE__)
|
79
|
+
include TestHelper
|
80
|
+
|
81
|
+
alias_method :run_without_em, :run
|
82
|
+
def run(result, &block)
|
83
|
+
em(DefaultTimeout) { run_without_em(result, &block) }
|
84
|
+
rescue Exception => e
|
85
|
+
if RUBY_VERSION >= "1.9.1"
|
86
|
+
result.puke(self.class, @name, e)
|
87
|
+
else
|
88
|
+
add_error($!)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
HERE_DOC
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
data/test/api_test.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class NormalTest < Test::Unit::TestCase
|
4
|
+
def test_trivial
|
5
|
+
assert_equal 1, 1
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class EmTestHelperTest < Test::Unit::TestCase
|
10
|
+
include EventMachine::TestHelper
|
11
|
+
|
12
|
+
def test_trivial
|
13
|
+
em do
|
14
|
+
assert_equal 1, 1
|
15
|
+
done
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_timer
|
20
|
+
em do
|
21
|
+
start = Time.now
|
22
|
+
|
23
|
+
EM.add_timer(0.5){
|
24
|
+
assert_in_delta 0.5, Time.now-start, 0.1
|
25
|
+
done
|
26
|
+
}
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class EmTestTest < Test::Unit::TestCase
|
32
|
+
include EventMachine::Test
|
33
|
+
|
34
|
+
def test_working
|
35
|
+
done
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_timer
|
39
|
+
start = Time.now
|
40
|
+
|
41
|
+
EM.add_timer(0.5){
|
42
|
+
assert_in_delta 0.5, Time.now-start, 0.1
|
43
|
+
done
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_deferrable
|
48
|
+
defr = EM::DefaultDeferrable.new
|
49
|
+
defr.timeout(1)
|
50
|
+
defr.errback{
|
51
|
+
done
|
52
|
+
}
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class EmTestWithTimeoutTest < Test::Unit::TestCase
|
57
|
+
include EventMachine::Test
|
58
|
+
|
59
|
+
default_timeout 2
|
60
|
+
|
61
|
+
# if this test fails with RuntimeError: timeout exceeded
|
62
|
+
# this is good and expected
|
63
|
+
def test_should_fail_to_timeout
|
64
|
+
EM.add_timer(3) {
|
65
|
+
done
|
66
|
+
}
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
class EmTestWithTimeoutOverrideTest < Test::Unit::TestCase
|
71
|
+
include EventMachine::Test
|
72
|
+
|
73
|
+
default_timeout 2
|
74
|
+
|
75
|
+
def test_timeout_override
|
76
|
+
timeout(4)
|
77
|
+
EM.add_timer(3) {
|
78
|
+
done
|
79
|
+
}
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
class AnotherNormalTest < Test::Unit::TestCase
|
84
|
+
def test_normal
|
85
|
+
assert_equal 1, 1
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: em-test
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Paweł Pacana
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-17 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: eventmachine
|
16
|
+
requirement: &23665160 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.0.0.beta.1
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *23665160
|
25
|
+
description: Simple TDD API for testing asynchronous EventMachine code. Extracted
|
26
|
+
from em-spec to remove dependencies.
|
27
|
+
email:
|
28
|
+
- pawel.pacana@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- .gitignore
|
34
|
+
- Gemfile
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- em-test.gemspec
|
38
|
+
- lib/em-test.rb
|
39
|
+
- lib/em-test/version.rb
|
40
|
+
- test/api_test.rb
|
41
|
+
- test/test_helper.rb
|
42
|
+
homepage: ''
|
43
|
+
licenses: []
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.9.2
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubyforge_project: em-test
|
62
|
+
rubygems_version: 1.8.10
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: TDD for EventMachine
|
66
|
+
test_files: []
|