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,409 +0,0 @@
1
- require './test/test_config'
2
- require 'utilrb/thread_pool'
3
- require 'minitest/spec'
4
-
5
- MiniTest::Unit.autorun
6
-
7
- describe Utilrb::ThreadPool do
8
- describe "when created" do
9
- it "must create min number of threads." do
10
- pool = Utilrb::ThreadPool.new(5)
11
- sleep 0.1
12
- assert_equal 5,pool.waiting
13
- assert_equal 5,pool.spawned
14
- assert_equal 5,(pool.instance_variable_get(:@workers)).size
15
- pool.shutdown
16
- pool.join
17
- end
18
-
19
- it "must create min number of threads." do
20
- pool = Utilrb::ThreadPool.new(5)
21
- sleep 0.1
22
- assert_equal 5,pool.waiting
23
- assert_equal 5,pool.spawned
24
- assert_equal 5,(pool.instance_variable_get(:@workers)).size
25
- pool.shutdown
26
- pool.join
27
- end
28
- end
29
-
30
- describe "when under heavy load" do
31
- it "must spawn upto max threads." do
32
- pool = Utilrb::ThreadPool.new(5,20)
33
- 0.upto 19 do
34
- pool.process do
35
- sleep 0.12
36
- end
37
- end
38
- assert_equal 20,pool.backlog
39
- assert_equal 20,pool.tasks.size
40
- sleep 0.1
41
- assert_equal 0,pool.backlog
42
- assert_equal 0,pool.waiting
43
- assert_equal 20,pool.spawned
44
- sleep 0.1
45
- assert_equal 20,pool.waiting
46
- assert_equal 20,pool.spawned
47
- pool.shutdown
48
- pool.join
49
- end
50
-
51
- it "must be possible to resize its limit" do
52
- pool = Utilrb::ThreadPool.new(5,5)
53
- 0.upto 19 do
54
- pool.process do
55
- sleep 0.32
56
- end
57
- end
58
- sleep 0.1
59
- assert_equal 0,pool.waiting
60
- assert_equal 5,pool.spawned
61
- pool.resize(5,20)
62
- sleep 0.1
63
- assert_equal 0,pool.waiting
64
- assert_equal 20,pool.spawned
65
- sleep 0.4
66
- assert_equal 20,pool.spawned
67
- assert_equal 20,pool.waiting
68
- pool.shutdown
69
- pool.join
70
- end
71
-
72
- it "must reduce its number of threads after the work is done if auto_trim == true." do
73
- pool = Utilrb::ThreadPool.new(5,20)
74
- pool.auto_trim = true
75
- 0.upto 19 do
76
- pool.process do
77
- sleep 0.15
78
- end
79
- end
80
- sleep 0.13
81
- assert_equal 0,pool.waiting
82
- assert_equal 20,pool.spawned
83
- sleep 0.4
84
- assert_equal 5,pool.waiting
85
- assert_equal 5,pool.spawned
86
- pool.shutdown
87
- pool.join
88
- end
89
-
90
- it "must not execute tasks with the same sync key in parallel" do
91
- pool = Utilrb::ThreadPool.new(5,10)
92
- pool.auto_trim = true
93
- time = Time.now
94
- 0.upto 10 do
95
- t = pool.process_with_options :sync_key => time do
96
- sleep 0.1
97
- end
98
- assert_equal time, t.sync_key
99
- end
100
- while pool.backlog > 0
101
- sleep 0.1
102
- end
103
- pool.shutdown
104
- pool.join
105
- assert Time.now - time >= 1.0
106
- end
107
-
108
- it "must not execute a task and a sync call in parallel if they have the same sync key" do
109
- pool = Utilrb::ThreadPool.new(5,5)
110
- time = Time.now
111
- t = pool.process_with_options :sync_key => 1 do
112
- sleep 0.2
113
- end
114
- pool.sync 1 do
115
- sleep 0.2
116
- end
117
- while pool.backlog > 0
118
- sleep 0.1
119
- end
120
- pool.shutdown
121
- pool.join
122
- assert Time.now - time >= 0.4
123
- end
124
-
125
- it "must execute a task and a sync call in parallel if they have different sync keys" do
126
- pool = Utilrb::ThreadPool.new(5,5)
127
- time = Time.now
128
- t = pool.process_with_options :sync_key => 1 do
129
- sleep 0.2
130
- end
131
- pool.sync 2 do
132
- sleep 0.2
133
- end
134
- while pool.backlog > 0
135
- sleep 0.1
136
- end
137
- pool.shutdown
138
- pool.join
139
- assert Time.now - time < 0.4
140
- end
141
- end
142
-
143
- describe "when running" do
144
- it "must call on_task_finished for each finised task." do
145
- pool = Utilrb::ThreadPool.new(5)
146
- count = 0
147
- pool.on_task_finished do |task|
148
- count += 1
149
- end
150
- task = pool.process do
151
- sleep 0.05
152
- end
153
- task = pool.process do
154
- raise
155
- end
156
- sleep 0.1
157
- assert_equal 2,count
158
- pool.shutdown
159
- pool.join
160
- end
161
-
162
- it "must be able to reque a task" do
163
- pool = Utilrb::ThreadPool.new(5)
164
- count = 0
165
- task = pool.process do
166
- count += 1
167
- end
168
- while !task.finished?
169
- sleep 0.001
170
- end
171
- pool << task
172
- while !task.finished?
173
- sleep 0.001
174
- end
175
- assert_equal 2, count
176
- end
177
-
178
- it "must process the next task if thread gets available" do
179
- pool = Utilrb::ThreadPool.new(1)
180
- count = 0
181
- pool.process do
182
- sleep 0.1
183
- count +=1
184
- end
185
- pool.process do
186
- sleep 0.1
187
- count +=1
188
- end
189
- sleep 0.25
190
- assert_equal 2, count
191
-
192
- task3 = Utilrb::ThreadPool::Task.new do
193
- count +=1
194
- sleep 0.1
195
- end
196
- task4 = Utilrb::ThreadPool::Task.new do
197
- count +=1
198
- sleep 0.1
199
- end
200
- pool << task3
201
- pool << task4
202
- sleep 0.15
203
- pool.shutdown
204
- pool.join
205
- assert_equal 4, count
206
- end
207
- end
208
-
209
- describe "when shutting down" do
210
- it "must terminate all threads" do
211
- pool = Utilrb::ThreadPool.new(5)
212
- task = pool.process do
213
- sleep 0.2
214
- end
215
- pool.shutdown()
216
- pool.join
217
- end
218
- end
219
- end
220
-
221
-
222
- describe Utilrb::ThreadPool::Task do
223
- describe "when created" do
224
- it "must raise if no block is given." do
225
- assert_raises(ArgumentError) do
226
- Utilrb::ThreadPool::Task.new
227
- end
228
- end
229
- it "must be in waiting state." do
230
- task = Utilrb::ThreadPool::Task.new do
231
- end
232
- assert !task.running?
233
- assert !task.finished?
234
- assert !task.exception?
235
- assert !task.terminated?
236
- assert !task.successfull?
237
- assert !task.started?
238
- assert_equal :waiting, task.state
239
- end
240
- it "must raise if wrong option is given." do
241
- assert_raises ArgumentError do
242
- task = Utilrb::ThreadPool::Task.new :bla => 123 do
243
- end
244
- end
245
- end
246
- it "must set its options." do
247
- task = Utilrb::ThreadPool::Task.new :sync_key => 2 do
248
- 123
249
- end
250
- assert_equal 2,task.sync_key
251
- end
252
- end
253
-
254
- describe "when executed" do
255
- it "must be in finished state if task successfully executed." do
256
- task = Utilrb::ThreadPool::Task.new do
257
- 123
258
- end
259
- task.pre_execute
260
- task.execute
261
- task.finalize
262
- assert !task.running?
263
- assert task.finished?
264
- assert !task.exception?
265
- assert !task.terminated?
266
- assert task.successfull?
267
- assert task.started?
268
- end
269
-
270
- it "must call the callback after it is finished." do
271
- task = Utilrb::ThreadPool::Task.new do
272
- 123
273
- end
274
- result = nil
275
- task.callback do |val,e|
276
- result = val
277
- end
278
- task.pre_execute
279
- task.execute
280
- task.finalize
281
-
282
- assert_equal 123,result
283
- assert !task.running?
284
- assert task.finished?
285
- assert !task.exception?
286
- assert !task.terminated?
287
- assert task.successfull?
288
- assert task.started?
289
- end
290
-
291
- it "must be in exception state if exception was raised." do
292
- task = Utilrb::ThreadPool::Task.new do
293
- raise
294
- end
295
- task.pre_execute
296
- task.execute
297
- task.finalize
298
- assert !task.running?
299
- assert task.finished?
300
- assert task.exception?
301
- assert !task.terminated?
302
- assert !task.successfull?
303
- assert task.started?
304
-
305
- task = Utilrb::ThreadPool::Task.new do
306
- raise
307
- end
308
- result = nil
309
- task.callback do |val,e|
310
- result = val ? val : e
311
- end
312
- task.pre_execute
313
- task.execute
314
- task.finalize
315
- assert !task.running?
316
- assert task.finished?
317
- assert task.exception?
318
- assert !task.terminated?
319
- assert !task.successfull?
320
- assert task.started?
321
- assert_equal RuntimeError, result.class
322
- end
323
-
324
- it "must return the default value if an error was raised." do
325
- task = Utilrb::ThreadPool::Task.new :default => 123 do
326
- raise
327
- end
328
- result = nil
329
- task.callback do |val,e|
330
- result = val
331
- end
332
- task.pre_execute
333
- task.execute
334
- task.finalize
335
- assert !task.running?
336
- assert task.finished?
337
- assert task.exception?
338
- assert !task.terminated?
339
- assert !task.successfull?
340
- assert task.started?
341
- assert_equal 123,result
342
- end
343
-
344
- it "must calculate its elapsed time." do
345
- task = Utilrb::ThreadPool::Task.new do
346
- sleep 0.2
347
- end
348
- assert_in_delta 0.0,task.time_elapsed,0.0001
349
- thread = Thread.new do
350
- task.pre_execute
351
- task.execute
352
- task.finalize
353
- end
354
- sleep 0.1
355
- assert_in_delta 0.1,task.time_elapsed,0.01
356
- thread.join
357
- assert_in_delta 0.2,task.time_elapsed,0.001
358
- sleep 0.1
359
- assert_in_delta 0.2,task.time_elapsed,0.001
360
- end
361
- end
362
-
363
- describe "when terminated" do
364
- it "it must be in terminated state." do
365
- task = Utilrb::ThreadPool::Task.new do
366
- sleep 10
367
- end
368
- thread = Thread.new do
369
- task.pre_execute
370
- task.execute
371
- task.finalize
372
- end
373
- sleep 0.1
374
- task.terminate!
375
- thread.join
376
-
377
- assert !task.running?
378
- assert task.finished?
379
- assert !task.exception?
380
- assert task.terminated?
381
- assert !task.successfull?
382
- assert task.started?
383
- end
384
- end
385
-
386
- describe "when terminated" do
387
- it "must be in state terminated." do
388
- task = Utilrb::ThreadPool::Task.new do
389
- sleep 10
390
- end
391
- thread = Thread.new do
392
- task.pre_execute
393
- task.execute
394
- task.finalize
395
- end
396
- sleep 0.1
397
- task.terminate!()
398
- thread.join
399
-
400
- assert !task.running?
401
- assert task.finished?
402
- assert !task.exception?
403
- assert task.terminated?
404
- assert !task.successfull?
405
- assert task.started?
406
- end
407
- end
408
- end
409
-
data/test/test_time.rb DELETED
@@ -1,47 +0,0 @@
1
- require './test/test_config'
2
- require 'utilrb/time'
3
-
4
- class TC_Time < Test::Unit::TestCase
5
- def test_to_hms
6
- assert_equal("0:00:00.000", Time.at(0).to_hms)
7
- assert_equal("0:00:00.100", Time.at(0.1).to_hms)
8
- assert_equal("0:00:34.100", Time.at(34.1).to_hms)
9
- assert("0:21:34.100" == Time.at(21 * 60 + 34, 100000).to_hms)
10
- assert("236:21:34.100", Time.at(236 * 3600 + 21 * 60 + 34, 100000).to_hms)
11
- end
12
- def test_from_hms
13
- assert_equal([0, 0, 0, 1], Time.hms_decomposition('0:00:00.001'))
14
- assert_equal([0, 0, 0, 10], Time.hms_decomposition('0:00:00.01'))
15
- assert_equal([0, 0, 0, 10], Time.hms_decomposition('0:00:00.010'))
16
- assert_equal([0, 0, 0, 100], Time.hms_decomposition('0:00:00.1'))
17
- assert_equal([0, 0, 0, 100], Time.hms_decomposition('0:00:00.10'))
18
- assert_equal([0, 0, 0, 100], Time.hms_decomposition('0:00:00.100'))
19
-
20
- assert_equal([0, 0, 0, 0], Time.hms_decomposition("0:00:00.000"))
21
- assert_equal(Time.at(34, 100000), Time.from_hms("0:00:34.100"))
22
- assert_equal(Time.at(21 * 60 + 34, 100000), Time.from_hms("0:21:34.100"))
23
- assert_equal(Time.at(236 * 3600 + 21 * 60 + 34, 100000), Time.from_hms("236:21:34.100"))
24
-
25
- assert_equal([329991, 0, 10, 0], Time.hms_decomposition("329991:00:10"))
26
- assert_equal([329991, 8, 8, 0], Time.hms_decomposition("329991:08:08"))
27
-
28
- assert_equal(Time.at(0), Time.from_hms("0"))
29
- assert_equal(Time.at(0), Time.from_hms(":0"))
30
- assert_equal(Time.at(62), Time.from_hms("1:2"))
31
- assert_equal(Time.at(3723), Time.from_hms("1:2:3"))
32
-
33
- assert_equal(Time.at(0), Time.from_hms("0"))
34
- assert_equal(Time.at(0), Time.from_hms("0."))
35
- assert_in_delta(0, Time.at(1.2) - Time.from_hms("1.2"), 0.001, Time.from_hms("1.2").to_hms)
36
- assert_in_delta(0, Time.at(121.3) - Time.from_hms("2:1.3"), 0.001)
37
- assert_in_delta(0, Time.at(3723.4) - Time.from_hms("1:2:3.4"), 0.001)
38
- assert_in_delta(0, Time.at(1.02) - Time.from_hms("1.02"), 0.001)
39
- assert_in_delta(0, Time.at(121.03) - Time.from_hms("2:1.03"), 0.001)
40
- assert_in_delta(0, Time.at(3723.04) - Time.from_hms("1:2:3.04"), 0.001)
41
- assert_in_delta(0, Time.at(1.75) - Time.from_hms("1.75"), 0.001)
42
- assert_in_delta(0, Time.at(121.75) - Time.from_hms("2:1.75"), 0.001)
43
- assert_in_delta(0, Time.at(3723.75) - Time.from_hms("1:2:3.75"), 0.001)
44
- assert_in_delta(0, Time.at(3723.243) - Time.from_hms("1:2:3.243"), 0.001)
45
- end
46
- end
47
-
@@ -1,23 +0,0 @@
1
- require './test/test_config'
2
- require 'utilrb/unbound_method'
3
- require 'flexmock'
4
-
5
- class TC_UnboundMethod < Test::Unit::TestCase
6
- def test_call
7
- FlexMock.use do |mock|
8
- klass = Class.new do
9
- define_method(:mock) { mock }
10
- def tag(value, &block)
11
- mock.method_called(value)
12
- block.call(value)
13
- end
14
- end
15
- obj = klass.new
16
-
17
- mock.should_receive(:method_called).with(42).once
18
- mock.should_receive(:block_called).with(42).once
19
- klass.instance_method(:tag).call(obj, 42) { |value| mock.block_called(value) }
20
- end
21
- end
22
- end
23
-
data/test/test_weakref.rb DELETED
@@ -1,81 +0,0 @@
1
- require 'test/unit/testcase'
2
- require 'utilrb/weakref'
3
-
4
- Utilrb.require_ext('TC_WeakRef') do
5
- class TC_WeakRef < Test::Unit::TestCase
6
- WeakRef = Utilrb::WeakRef
7
- def test_normal
8
- obj = Object.new
9
- ref = Utilrb::WeakRef.new(obj)
10
-
11
- assert_equal(obj, ref.get)
12
- end
13
-
14
- def test_initialize_validation
15
- assert_raises(ArgumentError) { Utilrb::WeakRef.new(nil) }
16
- assert_raises(ArgumentError) { Utilrb::WeakRef.new(5) }
17
-
18
- ref = WeakRef.new(Object.new)
19
- assert_raises(ArgumentError) { Utilrb::WeakRef.new(ref) }
20
- end
21
-
22
- def create_deep_pair(lvl)
23
- if lvl == 0
24
- 100.times do
25
- obj = Object.new
26
- WeakRef.new(obj)
27
- obj = nil
28
- end
29
- else
30
- create_deep_pair(lvl - 1)
31
- end
32
- end
33
-
34
- def create_deep_ref(lvl)
35
- if lvl == 0
36
- obj = Object.new
37
- refs = (1..100).map { WeakRef.new(obj) }
38
- return refs, obj.object_id
39
- else
40
- create_deep_ref(lvl - 1)
41
- end
42
- end
43
-
44
- def create_deep_obj(lvl)
45
- if lvl == 0
46
- ref = WeakRef.new(obj = Object.new)
47
- return obj, ref.object_id
48
- else
49
- create_deep_obj(lvl - 1)
50
- end
51
- end
52
-
53
- def test_finalized_objects
54
- refs, obj_id = create_deep_ref(100)
55
- create_deep_ref(200) # erase the stack ...
56
- GC.start
57
- for ref in refs
58
- assert_raises(WeakRef::RefError) { ref.get }
59
- end
60
- assert_equal(nil, WeakRef.refcount(obj_id))
61
- end
62
-
63
- def test_finalized_refs
64
- obj, ref_id = create_deep_obj(100)
65
- create_deep_ref(100) # erase the stack ...
66
- GC.start
67
- assert_raises(RangeError) do
68
- ref = ObjectSpace._id2ref(ref_id)
69
- if !ref.kind_of?(WeakRef)
70
- raise RangeError
71
- end
72
- end
73
- end
74
-
75
- def test_finalization_ordering_crash
76
- create_deep_pair(100)
77
- GC.start
78
- end
79
- end
80
- end
81
-