exceptional_synchrony 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,182 @@
1
+ require_relative '../test_helper'
2
+
3
+ describe ExceptionalSynchrony::ParallelSync do
4
+ before do
5
+ @em = ExceptionalSynchrony::EventMachineProxy.new(EventMachine, nil)
6
+ end
7
+
8
+ it "should run 3 tasks in parallel and return in order" do
9
+ stub_request(:get, "http://www.google.com/").
10
+ to_return(:status => 200, :body => "1", :headers => {})
11
+
12
+ stub_request(:get, "http://www.cnn.com/").
13
+ to_return(:status => 402, :body => "2", :headers => {})
14
+
15
+ stub_request(:get, "http://news.ycombinator.com/").
16
+ to_return(:status => 200, :body => "3", :headers => {})
17
+
18
+ ExceptionalSynchrony::EMP.run_and_stop do
19
+ responses = ExceptionalSynchrony::ParallelSync.parallel(@em) do |parallel|
20
+ parallel.add { @em.sleep(0.001); ExceptionalSynchrony::EMP.connection.new("http://www.google.com").get } # sleep will make this slower
21
+ parallel.add { ExceptionalSynchrony::EMP.connection.new("http://www.cnn.com").get }
22
+ parallel.add { ExceptionalSynchrony::EMP.connection.new("http://news.ycombinator.com").get }
23
+ end
24
+
25
+ assert_equal [0, 1, 2], responses.keys
26
+
27
+ assert_equal [200, "1"], [responses[0].response_header.status, responses[0].response]
28
+ assert_equal [402, "2"], [responses[1].response_header.status, responses[1].response]
29
+ assert_equal [200, "3"], [responses[2].response_header.status, responses[2].response]
30
+ end
31
+ end
32
+
33
+ it "should run 3 non-blocking tasks in parallel and return in order" do
34
+ ExceptionalSynchrony::EMP.run_and_stop do
35
+ responses = ExceptionalSynchrony::ParallelSync.parallel(@em) do |parallel|
36
+ parallel.add { 1 }
37
+ parallel.add { 2 }
38
+ parallel.add { 3 }
39
+ end
40
+
41
+ assert_equal [0, 1, 2], responses.keys
42
+
43
+ assert_equal 1, responses[0]
44
+ assert_equal 2, responses[1]
45
+ assert_equal 3, responses[2]
46
+ end
47
+ end
48
+
49
+ it "should pass an exception through and raise it on the other side" do
50
+ stub_request(:get, "http://www.google.com/").
51
+ to_return(:status => 200, :body => "1", :headers => {})
52
+
53
+ stub_request(:get, "http://www.cnn.com/").
54
+ to_return(:status => 200, :body => "2", :headers => {})
55
+
56
+ stub_request(:get, "http://news.ycombinator.com/").
57
+ to_return(:status => 200, :body => "3", :headers => {})
58
+
59
+ assert_raises(NotImplementedError) do
60
+ ExceptionalSynchrony::EMP.run_and_stop do
61
+ responses = ExceptionalSynchrony::ParallelSync.parallel(@em) do |parallel|
62
+ parallel.add { ExceptionalSynchrony::EMP.connection.new("http://www.google.com").get; raise NotImplementedError, "Not implemented!" }
63
+ parallel.add { ExceptionalSynchrony::EMP.connection.new("http://www.cnn.com").get }
64
+ parallel.add { ExceptionalSynchrony::EMP.connection.new("http://news.ycombinator.com").get }
65
+ end
66
+ end
67
+ end.to_s.must_match(/Not implemented!/)
68
+ end
69
+
70
+ it "should pass several exceptions through and raise them on the other side" do
71
+ stub_request(:get, "http://www.google.com/").
72
+ to_return(:status => 200, :body => "1", :headers => {})
73
+
74
+ stub_request(:get, "http://www.cnn.com/").
75
+ to_return(:status => 200, :body => "2", :headers => {})
76
+
77
+ stub_request(:get, "http://news.ycombinator.com/").
78
+ to_return(:status => 200, :body => "3", :headers => {})
79
+
80
+ assert_raises(NotImplementedError) do
81
+ ExceptionalSynchrony::EMP.run_and_stop do
82
+ responses = ExceptionalSynchrony::ParallelSync.parallel(@em) do |parallel|
83
+ parallel.add { ExceptionalSynchrony::EMP.connection.new("http://www.google.com").get; raise NotImplementedError, "Not implemented!" }
84
+ parallel.add { ExceptionalSynchrony::EMP.connection.new("http://www.cnn.com").get; raise LoadError, "A load error occurred" }
85
+ parallel.add { ExceptionalSynchrony::EMP.connection.new("http://news.ycombinator.com").get; raise IndexError, "An index error occurred" }
86
+ end
87
+ end
88
+ end.message.must_match(/Not implemented!.*LoadError: A load error occurred.*IndexError: An index error occurred/m)
89
+ end
90
+
91
+ class TestProc
92
+ def initialize(procs = {}, &block)
93
+ @cancel_procs = procs[:cancel]
94
+ @block = block
95
+ end
96
+
97
+ def encapsulate(procs = {}, &proc)
98
+ self.class.new(cancel: [procs[:cancel], @cancel_procs].flatten.compact, &proc)
99
+ end
100
+
101
+ def call
102
+ @block.call
103
+ end
104
+
105
+ def to_proc
106
+ @block
107
+ end
108
+
109
+ def cancel
110
+ Array(@cancel_procs).each_with_index { |proc, index| proc.call }
111
+ end
112
+ end
113
+
114
+ it "should allow objects to be added instead of Procs" do
115
+ ExceptionalSynchrony::EMP.run_and_stop do
116
+ queue = ExceptionalSynchrony::LimitedWorkQueue.new(@em, 2)
117
+
118
+ c = -1
119
+ started2 = nil; ended0 = nil; ended1 = nil
120
+ responses = ExceptionalSynchrony::ParallelSync.parallel(@em, queue) do |parallel|
121
+ parallel.add(TestProc.new { c+=1; @em.sleep(0.001); ended0 = c+=1 })
122
+ parallel.add(TestProc.new { c+=1; @em.sleep(0.005); ended1 = c+=1 })
123
+ parallel.add(TestProc.new { started2 = c+=1; c+=1 })
124
+ end
125
+
126
+ assert_equal 5, c
127
+ assert started2 > ended0 || started2 > ended1, [ended0, ended1, started2].inspect
128
+
129
+ assert_equal [0, 1, 2], responses.keys
130
+ end
131
+ end
132
+
133
+ it "should be composable with LimitedWorkQueue" do
134
+ ExceptionalSynchrony::EMP.run_and_stop do
135
+ queue = ExceptionalSynchrony::LimitedWorkQueue.new(@em, 2)
136
+
137
+ c = -1
138
+ started2 = nil; ended0 = nil; ended1 = nil
139
+ responses = ExceptionalSynchrony::ParallelSync.parallel(@em, queue) do |parallel|
140
+ parallel.add { c+=1; @em.sleep(0.001); ended0 = c+=1 }
141
+ parallel.add { c+=1; @em.sleep(0.005); ended1 = c+=1 }
142
+ parallel.add { started2 = c+=1; c+=1 }
143
+ end
144
+
145
+ assert_equal 5, c
146
+ assert started2 > ended0 || started2 > ended1, [ended0, ended1, started2].inspect
147
+
148
+ assert_equal [0, 1, 2], responses.keys
149
+ end
150
+ end
151
+
152
+ class TestProcWithMergeReplace < TestProc
153
+ def merge(queue)
154
+ if same = queue.find { |entry| entry.is_a?(self.class) }
155
+ same.cancel
156
+ queue - [same] + [self]
157
+ end
158
+ end
159
+ end
160
+
161
+ it "should be composable with LimitedWorkQueue with an object implementing merge/cancel" do
162
+ ExceptionalSynchrony::EMP.run_and_stop do
163
+ queue = ExceptionalSynchrony::LimitedWorkQueue.new(@em, 2)
164
+
165
+ c = 0
166
+ started2 = nil; ended0 = nil; ended1 = nil
167
+ responses = ExceptionalSynchrony::ParallelSync.parallel(@em, queue) do |parallel|
168
+ parallel.add(TestProc.new { c+=1; @em.sleep(0.001); ended0 = c+=2 })
169
+ parallel.add(TestProc.new { c+=4; @em.sleep(0.005); ended1 = c+=8 })
170
+ parallel.add(TestProcWithMergeReplace.new(cancel: -> { c+=16 }) { @em.sleep(0.005); ended1 = c+=32 })
171
+ parallel.add(TestProcWithMergeReplace.new(cancel: -> { c+=64 }) { started2 = c+=128 })
172
+ end
173
+
174
+ @em.sleep(0.050)
175
+
176
+ assert_equal 1+2+4+8+16+128, c
177
+ assert started2 > ended0 || started2 > ended1, [ended0, ended1, started2].inspect
178
+
179
+ assert_equal [0, 1, 2, 3], responses.keys
180
+ end
181
+ end
182
+ end
metadata ADDED
@@ -0,0 +1,188 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: exceptional_synchrony
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Colin Kelley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: exception_handling
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: eventmachine
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.3
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: em-synchrony
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.3
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.3
55
+ - !ruby/object:Gem::Dependency
56
+ name: em-http-request
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: hobo_support
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: thor
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: minitest
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: 1.17.1
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ~>
123
+ - !ruby/object:Gem::Version
124
+ version: 1.17.1
125
+ - !ruby/object:Gem::Dependency
126
+ name: rr
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ~>
130
+ - !ruby/object:Gem::Version
131
+ version: 1.1.2
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ~>
137
+ - !ruby/object:Gem::Version
138
+ version: 1.1.2
139
+ description: ''
140
+ email: colin@invoca.com
141
+ executables: []
142
+ extensions: []
143
+ extra_rdoc_files: []
144
+ files:
145
+ - .gitignore
146
+ - .ruby-gemset
147
+ - Gemfile
148
+ - Gemfile.lock
149
+ - README.md
150
+ - Rakefile
151
+ - Thorfile
152
+ - exceptional_synchrony.gemspec
153
+ - lib/exceptional_synchrony.rb
154
+ - lib/exceptional_synchrony/callback_exceptions.rb
155
+ - lib/exceptional_synchrony/event_machine_proxy.rb
156
+ - lib/exceptional_synchrony/limited_work_queue.rb
157
+ - lib/exceptional_synchrony/parallel_sync.rb
158
+ - lib/exceptional_synchrony/version.rb
159
+ - test/test_helper.rb
160
+ - test/unit/callback_exceptions_test.rb
161
+ - test/unit/event_machine_proxy_test.rb
162
+ - test/unit/limited_work_queue_test.rb
163
+ - test/unit/parallel_sync_test.rb
164
+ homepage: https://github.com/Invoca/exceptional_synchrony
165
+ licenses:
166
+ - MIT
167
+ metadata: {}
168
+ post_install_message:
169
+ rdoc_options: []
170
+ require_paths:
171
+ - lib
172
+ required_ruby_version: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - '>='
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
177
+ required_rubygems_version: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - '>='
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ requirements: []
183
+ rubyforge_project:
184
+ rubygems_version: 2.0.3
185
+ signing_key:
186
+ specification_version: 4
187
+ summary: Extensions to EventMachine/Synchrony to work well with exceptions
188
+ test_files: []