io_unblock 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ bundler_args: --without human_testing
data/Gemfile CHANGED
@@ -4,12 +4,12 @@ source "http://rubygems.org"
4
4
  gemspec
5
5
 
6
6
  group :test do
7
- gem 'autotest'
8
7
  gem 'minitest'
9
- gem 'minitest-emoji'
10
8
  end
11
9
 
12
10
  group :human_testing do
11
+ gem 'autotest'
12
+ gem 'minitest-emoji'
13
13
  gem 'autotest-growl'
14
14
  gem 'simplecov'
15
15
  end
@@ -24,6 +24,7 @@ module IoUnblock
24
24
  # - started: called when the IO processing has started
25
25
  # - stopped: called when the IO processing has stopped
26
26
  # - looped: called when each time the IO processing loops
27
+ # - callback_failed: called when an exception is raised within a callback
27
28
  def initialize io, callbacks=nil
28
29
  @io = io
29
30
  @io_selector = [@io]
@@ -73,6 +74,13 @@ private
73
74
  def trigger_callbacks named, *args, &other
74
75
  other && other.call(*args)
75
76
  @callbacks.key?(named) && @callbacks[named].call(*args)
77
+ rescue Exception => ex
78
+ if named == :callback_failed
79
+ warn "Exception raised in callback_failed handler: #{ex}"
80
+ ex.backtrace.each { |b| warn b }
81
+ else
82
+ trigger_callbacks :callback_failed, ex, named
83
+ end
76
84
  end
77
85
 
78
86
  def flush_and_close
@@ -1,3 +1,3 @@
1
1
  module IoUnblock
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -1,8 +1,4 @@
1
1
  require File.expand_path("../../spec_helper.rb", __FILE__)
2
- require 'stringio'
3
- require 'logger'
4
-
5
- $log = Logger.new $stdout
6
2
 
7
3
  describe IoUnblock::Stream do
8
4
  def dummy_io; @dummy_io ||= DummyIO.new; end
@@ -101,21 +97,21 @@ describe IoUnblock::Stream do
101
97
  end
102
98
 
103
99
  it "triggers started when starting" do
104
- cb_stream = callback_stream(started: callback)
100
+ cb_stream = callback_stream(:started => callback)
105
101
  cb_stream.start
106
102
  cb_stream.stop
107
103
  called_with.must_equal [ [:start] ]
108
104
  end
109
105
 
110
106
  it "triggers stopped when stopping" do
111
- cb_stream = callback_stream(stopped: callback)
107
+ cb_stream = callback_stream(:stopped => callback)
112
108
  cb_stream.start
113
109
  cb_stream.stop
114
110
  called_with.must_equal [ [:stop] ]
115
111
  end
116
112
 
117
113
  it "triggers looped after each read/write cycle" do
118
- cb_stream = callback_stream(looped: callback)
114
+ cb_stream = callback_stream(:looped => callback)
119
115
  cb_stream.start
120
116
  Thread.pass while called_with.empty?
121
117
  cb_stream.stop
@@ -124,7 +120,7 @@ describe IoUnblock::Stream do
124
120
 
125
121
  it "triggers wrote when writing" do
126
122
  dummy_io.max_write = 3
127
- cb_stream = callback_stream(wrote: callback)
123
+ cb_stream = callback_stream(:wrote => callback)
128
124
  cb_stream.start
129
125
  cb_stream.write "hello"
130
126
  cb_stream.stop
@@ -134,7 +130,7 @@ describe IoUnblock::Stream do
134
130
  it "triggers read when reading" do
135
131
  dummy_io.max_read = 3
136
132
  dummy_io.r_stream.string = 'hello'
137
- cb_stream = callback_stream(read: callback)
133
+ cb_stream = callback_stream(:read => callback)
138
134
  cb_stream.start
139
135
  Thread.pass until dummy_io.r_stream.eof?
140
136
  cb_stream.stop
@@ -142,7 +138,7 @@ describe IoUnblock::Stream do
142
138
  end
143
139
 
144
140
  it "triggers closed when closing" do
145
- cb_stream = callback_stream(closed: callback)
141
+ cb_stream = callback_stream(:closed => callback)
146
142
  cb_stream.start
147
143
  cb_stream.stop
148
144
  called_with.must_equal [ [] ]
@@ -150,7 +146,7 @@ describe IoUnblock::Stream do
150
146
 
151
147
  it "triggers failed when reading raises an error" do
152
148
  err = RuntimeError.new "fail"
153
- cb_stream = callback_stream(failed: callback)
149
+ cb_stream = callback_stream(:failed => callback)
154
150
  dummy_io.raise_read = err
155
151
  cb_stream.start
156
152
  Thread.pass until cb_stream.running?
@@ -161,7 +157,7 @@ describe IoUnblock::Stream do
161
157
 
162
158
  it "triggers failed when writing raises an error" do
163
159
  err = RuntimeError.new "fail"
164
- cb_stream = callback_stream(failed: callback)
160
+ cb_stream = callback_stream(:failed => callback)
165
161
  dummy_io.raise_write = err
166
162
  cb_stream.start
167
163
  cb_stream.write "hello"
@@ -186,7 +182,7 @@ describe IoUnblock::Stream do
186
182
  it "is not connected when failed is triggered" do
187
183
  is_connected = true
188
184
  cb_stream = callback_stream(
189
- failed: lambda { |ex| is_connected = cb_stream.connected? }
185
+ :failed => lambda { |ex| is_connected = cb_stream.connected? }
190
186
  )
191
187
  err = RuntimeError.new "fail"
192
188
  dummy_io.raise_write = err
@@ -195,5 +191,24 @@ describe IoUnblock::Stream do
195
191
  cb_stream.stop
196
192
  is_connected.must_equal false
197
193
  end
194
+
195
+ it "triggers callback_failed when a callback raises an exception" do
196
+ cb_err = RuntimeError.new 'failback!'
197
+ cb_stream = callback_stream(:callback_failed => callback)
198
+ cb_stream.start
199
+ cb_stream.write('a test') { raise cb_err }
200
+ cb_stream.stop
201
+ called_with.must_equal [ [cb_err, :wrote] ]
202
+ end
203
+
204
+ it "warns when triggering callback_failed raises an exception" do
205
+ cb_err = RuntimeError.new 'failback!'
206
+ cb_stream = callback_stream(
207
+ :callback_failed => lambda { |*_| raise cb_err }
208
+ )
209
+ cb_stream.start
210
+ cb_stream.write('another test') { raise cb_err }
211
+ cb_stream.stop
212
+ end
198
213
  end
199
214
  end
data/spec/spec_helper.rb CHANGED
@@ -6,8 +6,11 @@ if ENV['SCOV']
6
6
  end
7
7
  end
8
8
 
9
- require 'minitest/autorun'
10
- require 'minitest/emoji'
9
+ begin
10
+ require 'minitest/autorun'
11
+ require 'minitest/emoji'
12
+ rescue LoadError
13
+ end
11
14
  require 'io_unblock'
12
15
  require 'stringio'
13
16
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: io_unblock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-12 00:00:00.000000000Z
12
+ date: 2012-03-13 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
16
- requirement: &2161332520 !ruby/object:Gem::Requirement
16
+ requirement: &2161186640 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2161332520
24
+ version_requirements: *2161186640
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &2161331860 !ruby/object:Gem::Requirement
27
+ requirement: &2161185800 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2161331860
35
+ version_requirements: *2161185800
36
36
  description: Non-blocking IO reads/writes wrapped in a thread
37
37
  email:
38
38
  - ian.eccles@gmail.com
@@ -42,6 +42,7 @@ extra_rdoc_files: []
42
42
  files:
43
43
  - .autotest
44
44
  - .gitignore
45
+ - .travis.yml
45
46
  - Gemfile
46
47
  - README.md
47
48
  - Rakefile