utilrb 2.0.2.b2 → 2.1.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/.boring +40 -0
  3. data/.gitignore +13 -0
  4. data/.travis.yml +5 -0
  5. data/CMakeLists.txt +18 -0
  6. data/Gemfile +3 -0
  7. data/Makefile +8 -0
  8. data/Manifest.txt +0 -8
  9. data/{README.rd → README.md} +11 -7
  10. data/Rakefile +16 -63
  11. data/benchmarks/validate_options.rb +79 -0
  12. data/ext/utilrb/extconf.rb +1 -17
  13. data/ext/utilrb/utilrb.cc +0 -23
  14. data/lib/utilrb/column_formatter.rb +8 -5
  15. data/lib/utilrb/common.rb +1 -6
  16. data/lib/utilrb/enumerable/uniq.rb +2 -8
  17. data/lib/utilrb/event_loop.rb +5 -10
  18. data/lib/utilrb/kernel/load_dsl_file.rb +1 -2
  19. data/lib/utilrb/kernel/options.rb +25 -29
  20. data/lib/utilrb/logger/hierarchy.rb +0 -1
  21. data/lib/utilrb/logger/io.rb +3 -3
  22. data/lib/utilrb/logger/root.rb +12 -6
  23. data/lib/utilrb/module/ancestor_p.rb +0 -12
  24. data/lib/utilrb/module/is_singleton.rb +6 -0
  25. data/lib/utilrb/module/singleton_class_p.rb +14 -0
  26. data/lib/utilrb/object/attribute.rb +33 -65
  27. data/lib/utilrb/object/singleton_class.rb +1 -20
  28. data/lib/utilrb/pkgconfig.rb +21 -10
  29. data/lib/utilrb/socket/tcp_server.rb +2 -2
  30. data/lib/utilrb/spawn.rb +1 -1
  31. data/lib/utilrb/test.rb +65 -0
  32. data/lib/utilrb/thread_pool.rb +11 -13
  33. data/lib/utilrb/timepoints.rb +15 -0
  34. data/lib/utilrb/value_set.rb +10 -1
  35. data/lib/utilrb/version.rb +4 -0
  36. data/lib/utilrb/weakref.rb +11 -12
  37. data/lib/utilrb/yard.rb +0 -111
  38. data/lib/utilrb.rb +6 -1
  39. data/lib/yard-utilrb.rb +1 -0
  40. data/manifest.xml +19 -0
  41. data/package.xml +29 -0
  42. data/utilrb.gemspec +27 -0
  43. metadata +56 -107
  44. data/ext/utilrb/proc.c +0 -39
  45. data/ext/utilrb/readline.c +0 -52
  46. data/ext/utilrb/weakref.cc +0 -143
  47. data/lib/utilrb/models/inherited_enumerable.rb +0 -341
  48. data/lib/utilrb/models/registration.rb +0 -115
  49. data/lib/utilrb/module/inherited_enumerable.rb +0 -6
  50. data/lib/utilrb/objectstats.rb +0 -193
  51. data/lib/utilrb/ruby_object_graph.rb +0 -384
  52. data/test/data/test_pkgconfig.pc +0 -9
  53. data/test/data/test_pkgconfig_empty.pc +0 -10
  54. data/test/test_array.rb +0 -15
  55. data/test/test_config.rb +0 -4
  56. data/test/test_dir.rb +0 -22
  57. data/test/test_enumerable.rb +0 -119
  58. data/test/test_event_loop.rb +0 -407
  59. data/test/test_exception.rb +0 -38
  60. data/test/test_gc.rb +0 -34
  61. data/test/test_hash.rb +0 -102
  62. data/test/test_kernel.rb +0 -300
  63. data/test/test_logger.rb +0 -204
  64. data/test/test_misc.rb +0 -42
  65. data/test/test_models.rb +0 -212
  66. data/test/test_module.rb +0 -126
  67. data/test/test_object.rb +0 -77
  68. data/test/test_objectstats.rb +0 -26
  69. data/test/test_pkgconfig.rb +0 -84
  70. data/test/test_proc.rb +0 -31
  71. data/test/test_set.rb +0 -19
  72. data/test/test_thread_pool.rb +0 -409
  73. data/test/test_time.rb +0 -47
  74. data/test/test_unbound_method.rb +0 -23
  75. data/test/test_weakref.rb +0 -81
@@ -1,407 +0,0 @@
1
- require './test/test_config'
2
- require 'utilrb/event_loop'
3
- require 'minitest/spec'
4
-
5
- MiniTest::Unit.autorun
6
-
7
- describe Utilrb::EventLoop::Forwardable do
8
- class Dummy
9
- def test(wait)
10
- sleep wait
11
- Thread.current
12
- end
13
-
14
- def error
15
- raise
16
- end
17
- end
18
-
19
- class DummyAsync
20
- extend Utilrb::EventLoop::Forwardable
21
- def_event_loop_delegator :@obj,:@event_loop,:test,:alias => :atest
22
- def_event_loop_delegators :@obj,:@event_loop,[:bla1,:bla2]
23
- def_event_loop_delegator :@obj,:@event_loop,:error,:on_error => :on_error2,:known_errors => RuntimeError
24
-
25
- forward_to :@obj, :@event_loop do
26
- thread_safe do
27
- def_delegators :bla3,:bla4
28
- end
29
- def_delegators :bla5
30
- end
31
-
32
- attr_accessor :last_error
33
-
34
- def initialize(event_loop,obj = Dummy.new)
35
- @event_loop = event_loop
36
- @obj = obj
37
- end
38
-
39
- def on_error2(error)
40
- @last_error = error
41
- end
42
-
43
- end
44
-
45
- class DummyAsyncFilter
46
- extend Utilrb::EventLoop::Forwardable
47
-
48
- def do_not_overwrite
49
- 222
50
- end
51
-
52
- def_event_loop_delegator :@obj,:@event_loop,:test,:alias => :atest2
53
- def_event_loop_delegator :@obj,:@event_loop,:test,:alias => :atest,:filter => :test_filter
54
- def_event_loop_delegator :@obj,:@event_loop,:test,:alias => :do_not_overwrite
55
-
56
- def initialize(event_loop,obj = Dummy.new)
57
- @event_loop = event_loop
58
- @obj = obj
59
- end
60
-
61
- # the result is always returned as array to check
62
- # that the filer is working
63
- def test_filter(result)
64
- [result,result]
65
- end
66
- end
67
-
68
- describe "when a class is extend but the designated object is nil" do
69
- it "must raise if a method call is delegated." do
70
- event_loop = Utilrb::EventLoop.new
71
- obj = DummyAsync.new(event_loop,nil)
72
- assert_raises Utilrb::EventLoop::Forwardable::DesignatedObjectNotFound do
73
- obj.atest(0.1)
74
- end
75
- assert_raises Utilrb::EventLoop::Forwardable::DesignatedObjectNotFound do
76
- obj.atest(0.1) do |_|
77
- end
78
- sleep 0.1
79
- event_loop.step
80
- end
81
- end
82
-
83
- it "must raise if a method call is delegated but the underlying object is nil." do
84
- event_loop = Utilrb::EventLoop.new
85
- obj = DummyAsyncFilter.new(event_loop,nil)
86
- assert_raises Utilrb::EventLoop::Forwardable::DesignatedObjectNotFound do
87
- obj.atest(0.1)
88
- end
89
- end
90
- end
91
-
92
- describe "when a class is extend" do
93
- it "must delegate the method call directly to the real object." do
94
- event_loop = Utilrb::EventLoop.new
95
- obj = DummyAsyncFilter.new(event_loop)
96
- assert_equal Thread.current,obj.atest(0.1).first
97
- end
98
-
99
- it 'must call the error callback' do
100
- event_loop = Utilrb::EventLoop.new
101
- obj = DummyAsync.new(event_loop)
102
- assert_raises RuntimeError do
103
- obj.error
104
- end
105
- obj.last_error.must_be_instance_of RuntimeError
106
- obj.last_error = nil
107
- error = nil
108
- obj.error do |_,e|
109
- error = e
110
- end
111
- sleep 0.1
112
- event_loop.step
113
- error.must_be_instance_of RuntimeError
114
- obj.last_error.must_be_instance_of RuntimeError
115
- end
116
-
117
- it 'must not overwrite instance methods' do
118
- event_loop = Utilrb::EventLoop.new
119
- obj = DummyAsyncFilter.new(event_loop)
120
- assert_equal 222,obj.do_not_overwrite
121
- end
122
-
123
- it "must defer the method call to a thread pool if a block is given." do
124
- event_loop = Utilrb::EventLoop.new
125
- obj = DummyAsync.new(event_loop)
126
- thread = nil
127
- task = obj.atest(0.05) do |result|
128
- thread = result
129
- end
130
- assert_equal Utilrb::ThreadPool::Task,task.class
131
- sleep 0.1
132
- event_loop.step
133
- assert task.successfull?
134
- assert thread != Thread.current
135
- end
136
- end
137
- end
138
-
139
- describe Utilrb::EventLoop do
140
- describe "when executed" do
141
- it "must call the timers at the right point in time." do
142
- event_loop = Utilrb::EventLoop.new
143
- val1 = nil
144
- val2 = nil
145
- val3 = nil
146
- timer1 = event_loop.every 0.1 do
147
- val1 = 123
148
- end
149
- timer2 = event_loop.every 0.2 do
150
- val2 = 345
151
- end
152
- event_loop.once 0.3 do
153
- val3 = 444
154
- end
155
-
156
- time = Time.now
157
- while Time.now - time < 0.101
158
- event_loop.step
159
- end
160
- event_loop.steps
161
- assert_equal 123,val1
162
- assert_equal nil,val2
163
- assert_equal nil,val3
164
- val1 = nil
165
-
166
- time = Time.now
167
- while Time.now - time < 0.101
168
- event_loop.step
169
- end
170
- assert_equal 123,val1
171
- assert_equal 345,val2
172
- assert_equal nil,val3
173
-
174
- time = Time.now
175
- while Time.now - time < 0.101
176
- event_loop.step
177
- end
178
- assert_equal 444,val3
179
- event_loop.clear
180
- end
181
-
182
- it 'must call a given block for every step' do
183
- event_loop = Utilrb::EventLoop.new
184
- val = nil
185
- event_loop.every_step do
186
- val = 123
187
- end
188
- event_loop.step
189
- assert_equal 123,val
190
- val = nil
191
- event_loop.step
192
- assert_equal 123,val
193
- val = nil
194
- event_loop.step
195
- assert_equal 123,val
196
- end
197
-
198
- it "must be able to start and stop timers." do
199
- event_loop = Utilrb::EventLoop.new
200
- val1 = nil
201
- val2 = nil
202
- timer1 = event_loop.every 0.1 do
203
- val1 = 123
204
- end
205
- assert timer1.running?
206
- timer2 = event_loop.every 0.2 do
207
- val2 = 345
208
- end
209
- assert timer2.running?
210
-
211
- timer1.cancel
212
- assert !timer1.running?
213
- time = Time.now
214
- event_loop.wait_for do
215
- Time.now - time >= 0.22
216
- end
217
- assert_equal nil,val1
218
- assert_equal 345,val2
219
- val2 = nil
220
- timer1.start
221
- timer2.cancel
222
-
223
- time = Time.now
224
- while Time.now - time < 0.201
225
- event_loop.step
226
- end
227
- assert_equal 123,val1
228
- assert_equal nil,val2
229
- end
230
-
231
- it "must defer blocking calls to a thread pool" do
232
- event_loop = Utilrb::EventLoop.new
233
- main_thread = Thread.current
234
- val = nil
235
- val2 = nil
236
- callback = Proc.new do |result|
237
- assert_equal main_thread,Thread.current
238
- assert main_thread != result
239
- val = result
240
- end
241
- event_loop.defer({:callback => callback},123,333) do |a,b|
242
- assert_equal 123,a
243
- assert_equal 333,b
244
- sleep 0.2
245
- assert main_thread != Thread.current
246
- val2 = Thread.current
247
- end
248
- sleep 0.1
249
- event_loop.step
250
- assert !val
251
-
252
- sleep 0.11
253
- event_loop.step
254
- assert val
255
- assert_equal val,val2
256
- end
257
- it "must peridically defer blocking calls to a thread pool" do
258
- event_loop = Utilrb::EventLoop.new
259
- main_thread = Thread.current
260
- work = Proc.new do
261
- Thread.current
262
- end
263
- val = nil
264
- event_loop.async_every work,:period => 0.1 do |result,e|
265
- val = result
266
- end
267
- sleep 0.11
268
- event_loop.step
269
- assert !val
270
- sleep 0.01
271
- event_loop.step
272
- assert val
273
- assert val != main_thread
274
- val = nil
275
-
276
- sleep 0.11
277
- event_loop.step
278
- sleep 0.01
279
- event_loop.step
280
- assert val
281
- assert val != main_thread
282
- end
283
-
284
- it 'must be able to overwrite an error' do
285
- event_loop = Utilrb::EventLoop.new
286
- work = Proc.new do
287
- raise ArgumentError
288
- end
289
- event_loop.async work do |r,e|
290
- if e
291
- RuntimeError.new
292
- end
293
- end
294
- sleep 0.1
295
- assert_raises RuntimeError do
296
- event_loop.step
297
- end
298
- end
299
-
300
- it 'must be able to ignore an error' do
301
- event_loop = Utilrb::EventLoop.new
302
- work = Proc.new do
303
- raise ArgumentError
304
- end
305
- event_loop.async work do |r,e|
306
- if e
307
- :ignore_error
308
- end
309
- end
310
- sleep 0.1
311
- event_loop.step
312
- end
313
-
314
- it 'must inform on_error block in an event of an error' do
315
- event_loop = Utilrb::EventLoop.new
316
- error = nil
317
- event_loop.on_error ArgumentError do |e|
318
- error = e
319
- end
320
-
321
- #check super class
322
- error2 = nil
323
- event_loop.on_error Exception do |e|
324
- error2 = e
325
- end
326
- work = Proc.new do
327
- raise ArgumentError
328
- end
329
- event_loop.async_with_options work, :default => :default_value,:known_errors => ArgumentError do |r,e|
330
- end
331
- sleep 0.1
332
- event_loop.step
333
- assert error
334
- assert error2
335
- end
336
-
337
- it 'must re-raise an error' do
338
- event_loop = Utilrb::EventLoop.new
339
- event_loop.once do
340
- raise ArgumentError
341
- end
342
- event_loop.once do
343
- raise ArgumentError
344
- end
345
- event_loop.once do
346
- raise ArgumentError
347
- end
348
- assert_raises ArgumentError do
349
- event_loop.step
350
- end
351
- assert_raises ArgumentError do
352
- event_loop.step
353
- end
354
- event_loop.clear_errors
355
- event_loop.step
356
-
357
- event_loop.defer Hash.new, 123,22,333 do |a,b,c|
358
- assert_equal 123,a
359
- assert_equal 22,b
360
- assert_equal 333,c
361
- raise ArgumentError
362
- end
363
- sleep 0.1
364
- assert_raises ArgumentError do
365
- event_loop.step
366
- end
367
- end
368
-
369
- it 'must automatically step' do
370
- event_loop = Utilrb::EventLoop.new
371
- event_loop.once 0.2 do
372
- event_loop.stop
373
- end
374
- event_loop.exec
375
- end
376
-
377
- it 'must raise if step is called from a wrong thread' do
378
- event_loop = Utilrb::EventLoop.new
379
- event_loop.defer do
380
- event_loop.step
381
- end
382
- sleep 0.1
383
- assert_raises RuntimeError do
384
- event_loop.step
385
- end
386
- end
387
-
388
- it 'must call a given block from the event loop thread' do
389
- event_loop = Utilrb::EventLoop.new
390
- event_loop.thread = Thread.current
391
- result1,result2 = [nil,nil]
392
- event_loop.defer do
393
- result1 = Thread.current
394
- event_loop.call do
395
- result2 = Thread.current
396
- assert event_loop.thread?
397
- end
398
- end
399
- sleep 0.1
400
- event_loop.step
401
- assert result1
402
- assert result1 != result2
403
- assert_equal Thread.current,result2
404
- end
405
- end
406
- end
407
-
@@ -1,38 +0,0 @@
1
- require './test/test_config'
2
- require 'utilrb/exception'
3
- require 'flexmock'
4
-
5
- class TC_Exception < Test::Unit::TestCase
6
- def test_full_message
7
- FlexMock.use do |mock|
8
- error = Exception.new
9
- def error.message; "this is an error" end
10
- def error.backtrace
11
- (0..10).map { |i| i.to_s }
12
- end
13
-
14
- full = error.full_message.split("\n")
15
- assert_equal(11, full.size)
16
-
17
- filtered = error.full_message { |line| Integer(line) % 2 == 0 }.split("\n")
18
- assert_equal(6, filtered.size)
19
- assert_equal(full.values_at(0, 2, 4, 6, 8, 10), filtered)
20
-
21
- since_matches = error.full_message(:since => /4/).split("\n")
22
- assert_equal(7, since_matches.size)
23
- assert_equal(full.values_at(*(5..10)), since_matches[1..-1])
24
-
25
- until_matches = error.full_message(:until => /3/).split("\n")
26
- assert_equal(3, until_matches.size)
27
- assert_equal(full.values_at(1, 2), until_matches[1..-1])
28
-
29
- since_and_until = error.full_message(:since => /3/, :until => /6/).split("\n")
30
- assert_equal(3, since_and_until.size)
31
- assert_equal(full.values_at(4, 5), since_and_until[1..-1])
32
-
33
- all = error.full_message(:since => /3/, :until => /6/) { |line| Integer(line) % 2 == 0 }.split("\n")
34
- assert_equal(1, all.size)
35
- end
36
- end
37
- end
38
-
data/test/test_gc.rb DELETED
@@ -1,34 +0,0 @@
1
- require './test/test_config'
2
-
3
- require 'utilrb/gc'
4
- require 'enumerator'
5
-
6
- class TC_GC < Test::Unit::TestCase
7
- def allocate(&block)
8
- # Allocate twice since it seems the last object stays on stack
9
- # (and is not GC'ed)
10
- 2.times { ObjectSpace.define_finalizer(Object.new, &block) }
11
- nil
12
- end
13
-
14
- def test_force
15
- finalized = false
16
- allocate { finalized = true }
17
- GC.start
18
- assert( finalized )
19
-
20
- GC.disable
21
- finalized = false
22
- allocate { finalized = true }
23
- GC.start
24
- assert( !finalized )
25
- GC.force
26
- assert( finalized )
27
- assert( GC.disable )
28
-
29
- GC.enable
30
- GC.force
31
- assert( !GC.enable )
32
- end
33
- end
34
-
data/test/test_hash.rb DELETED
@@ -1,102 +0,0 @@
1
- require './test/test_config'
2
- require 'enumerator'
3
- require 'set'
4
-
5
- require 'utilrb/hash'
6
-
7
- class TC_Hash < Test::Unit::TestCase
8
- def test_slice
9
- test = { :a => 1, :b => 2, :c => 3 }
10
- assert_equal({:a => 1, :c => 3}, test.slice(:a, :c))
11
- assert_equal({:a => 1, :c => 3}, test.slice(:a, :c, :d))
12
- end
13
-
14
- def test_to_sym_keys
15
- assert_equal({ :a => 10, :b => 20, :c => 30 }, { 'a' => 10, 'b' => 20, :c => 30 }.to_sym_keys)
16
- end
17
-
18
- def test_to_s
19
- obj = { 1 => 2, 2 => 3 }
20
- assert(obj.to_s =~ /^\{(.*)\}$/)
21
- values = $1.split(", ")
22
- assert_equal(["1 => 2", "2 => 3"].to_set, values.to_set)
23
-
24
- obj[3] = obj
25
- assert(obj.to_s =~ /^\{(.*)\}$/)
26
- values = $1.split(", ")
27
- assert_equal(["1 => 2", "2 => 3", "3 => ..."].to_set, values.to_set)
28
- end
29
-
30
- def test_map_key
31
- base = { 1 => 'a', 2 => 'b' }
32
- result = base.map_key { |k, v| k += 1 }
33
-
34
- assert_equal({ 1 => 'a', 2 => 'b' }, base)
35
- assert_equal({ 2 => 'a', 3 => 'b' }, result)
36
- end
37
-
38
- def test_map_value
39
- base = { 'a' => 1, 'b' => 2 }
40
- result = base.map_value { |k, v| v += 1 }
41
-
42
- assert_equal({ 'a' => 1, 'b' => 2 }, base)
43
- assert_equal({ 'a' => 2, 'b' => 3 }, result)
44
- end
45
-
46
- def test_recursive_merge
47
- base = { 'a' => 1, 'b' => { 'c' => 10, 'd' => 20 }, 'c' => 2 }
48
- base_orig = base.dup
49
- to_merge = { 'a' => 10, 'b' => { 'c' => 100 }, 'd' => 3 }
50
- to_merge_orig = to_merge.dup
51
- result = base.recursive_merge(to_merge)
52
-
53
- assert_equal(base, base_orig)
54
- assert_equal(to_merge, to_merge_orig)
55
- assert_equal({'a' => 10, 'b' => { 'c' => 100, 'd' => 20 }, 'c' => 2, 'd' => 3}, result)
56
- end
57
-
58
- def test_recursive_merge_with_block
59
- base = { 'a' => 1, 'b' => { 'c' => 10, 'd' => 20 }, 'c' => 2 }
60
- base_orig = base.dup
61
- to_merge = { 'a' => 10, 'b' => { 'c' => 100 }, 'd' => 3 }
62
- to_merge_orig = to_merge.dup
63
- args = []
64
- result = base.recursive_merge(to_merge) do |v, k1, k2|
65
- args << [v, k1, k2]
66
- k1
67
- end
68
-
69
- assert_equal(base, base_orig)
70
- assert_equal(to_merge, to_merge_orig)
71
- assert_equal([['a', 1, 10], ['c', 10, 100]].to_set, args.to_set)
72
- assert_equal({'a' => 1, 'b' => { 'c' => 10, 'd' => 20 }, 'c' => 2, 'd' => 3}, result)
73
- end
74
-
75
- def test_recursive_merge_bang
76
- base = { 'a' => 1, 'b' => { 'c' => 10, 'd' => 20 }, 'c' => 2 }
77
- to_merge = { 'a' => 10, 'b' => { 'c' => 100 }, 'd' => 3 }
78
- to_merge_orig = to_merge.dup
79
- result = base.recursive_merge!(to_merge)
80
-
81
- assert_equal(result, base)
82
- assert_equal(to_merge, to_merge_orig)
83
- assert_equal({'a' => 10, 'b' => { 'c' => 100, 'd' => 20 }, 'c' => 2, 'd' => 3}, result)
84
- end
85
-
86
- def test_recursive_merge_bang_with_block
87
- base = { 'a' => 1, 'b' => { 'c' => 10, 'd' => 20 }, 'c' => 2 }
88
- to_merge = { 'a' => 10, 'b' => { 'c' => 100 }, 'd' => 3 }
89
- to_merge_orig = to_merge.dup
90
- args = []
91
- result = base.recursive_merge!(to_merge) do |v, k1, k2|
92
- args << [v, k1, k2]
93
- k1
94
- end
95
-
96
- assert_equal(result, base)
97
- assert_equal(to_merge, to_merge_orig)
98
- assert_equal([['a', 1, 10], ['c', 10, 100]].to_set, args.to_set)
99
- assert_equal({'a' => 1, 'b' => { 'c' => 10, 'd' => 20 }, 'c' => 2, 'd' => 3}, result)
100
- end
101
- end
102
-