Skip to content

Commit 30d9782

Browse files
committed
Tidy up fiber scheduler tests.
1 parent 2df72c0 commit 30d9782

File tree

2 files changed

+54
-41
lines changed

2 files changed

+54
-41
lines changed

test/fiber/scheduler.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -489,15 +489,19 @@ def blocking(&block)
489489
end
490490

491491
class IOScheduler < Scheduler
492-
def __io_ops__
493-
@__io_ops__ ||= []
492+
def operations
493+
@operations ||= []
494494
end
495495

496496
def io_write(io, buffer, length, offset)
497-
fd = io.fileno
498-
str = buffer.get_string
499-
__io_ops__ << [:io_write, fd, str]
500-
Fiber.blocking { buffer.write(io, 0, offset) }
497+
descriptor = io.fileno
498+
string = buffer.get_string
499+
500+
self.operations << [:io_write, descriptor, string]
501+
502+
Fiber.blocking do
503+
buffer.write(io, 0, offset)
504+
end
501505
end
502506
end
503507

test/fiber/test_scheduler.rb

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -288,90 +288,99 @@ def test_post_fork_fiber_blocking
288288

289289
def test_io_write_on_flush
290290
begin
291-
fn = File.join(Dir.tmpdir, "ruby_test_io_write_on_flush_#{SecureRandom.hex}")
292-
write_fd = nil
293-
io_ops = nil
291+
path = File.join(Dir.tmpdir, "ruby_test_io_write_on_flush_#{SecureRandom.hex}")
292+
descriptor = nil
293+
operations = nil
294+
294295
thread = Thread.new do
295296
scheduler = IOScheduler.new
296297
Fiber.set_scheduler scheduler
297298

298299
Fiber.schedule do
299-
File.open(fn, 'w+') do |f|
300-
write_fd = f.fileno
301-
f << 'foo'
302-
f.flush
303-
f << 'bar'
300+
File.open(path, 'w+') do |file|
301+
descriptor = file.fileno
302+
file << 'foo'
303+
file.flush
304+
file << 'bar'
304305
end
305306
end
306-
io_ops = scheduler.__io_ops__
307+
308+
operations = scheduler.operations
307309
end
310+
308311
thread.join
309312
assert_equal [
310-
[:io_write, write_fd, 'foo'],
311-
[:io_write, write_fd, 'bar']
312-
], io_ops
313+
[:io_write, descriptor, 'foo'],
314+
[:io_write, descriptor, 'bar']
315+
], operations
313316

314-
assert_equal 'foobar', IO.read(fn)
317+
assert_equal 'foobar', IO.read(path)
315318
ensure
316319
thread.kill rescue nil
317-
FileUtils.rm_f(fn)
320+
FileUtils.rm_f(path)
318321
end
319322
end
320323

321324
def test_io_read_error
322-
fn = File.join(Dir.tmpdir, "ruby_test_io_read_error_#{SecureRandom.hex}")
323-
exception = nil
325+
path = File.join(Dir.tmpdir, "ruby_test_io_read_error_#{SecureRandom.hex}")
326+
error = nil
327+
324328
thread = Thread.new do
325329
scheduler = IOErrorScheduler.new
326330
Fiber.set_scheduler scheduler
327331
Fiber.schedule do
328-
File.open(fn, 'w+') { it.read }
329-
rescue => e
330-
exception = e
332+
File.open(path, 'w+') { it.read }
333+
rescue => error
334+
# Ignore.
331335
end
332336
end
337+
333338
thread.join
334-
assert_kind_of Errno::EBADF, exception
339+
assert_kind_of Errno::EBADF, error
335340
ensure
336341
thread.kill rescue nil
337-
FileUtils.rm_f(fn)
342+
FileUtils.rm_f(path)
338343
end
339344

340345
def test_io_write_error
341-
fn = File.join(Dir.tmpdir, "ruby_test_io_write_error_#{SecureRandom.hex}")
342-
exception = nil
346+
path = File.join(Dir.tmpdir, "ruby_test_io_write_error_#{SecureRandom.hex}")
347+
error = nil
348+
343349
thread = Thread.new do
344350
scheduler = IOErrorScheduler.new
345351
Fiber.set_scheduler scheduler
346352
Fiber.schedule do
347-
File.open(fn, 'w+') { it.sync = true; it << 'foo' }
348-
rescue => e
349-
exception = e
353+
File.open(path, 'w+') { it.sync = true; it << 'foo' }
354+
rescue => error
355+
# Ignore.
350356
end
351357
end
358+
352359
thread.join
353-
assert_kind_of Errno::EINVAL, exception
360+
assert_kind_of Errno::EINVAL, error
354361
ensure
355362
thread.kill rescue nil
356-
FileUtils.rm_f(fn)
363+
FileUtils.rm_f(path)
357364
end
358365

359366
def test_io_write_flush_error
360-
fn = File.join(Dir.tmpdir, "ruby_test_io_write_flush_error_#{SecureRandom.hex}")
361-
exception = nil
367+
path = File.join(Dir.tmpdir, "ruby_test_io_write_flush_error_#{SecureRandom.hex}")
368+
error = nil
369+
362370
thread = Thread.new do
363371
scheduler = IOErrorScheduler.new
364372
Fiber.set_scheduler scheduler
365373
Fiber.schedule do
366-
File.open(fn, 'w+') { it << 'foo' }
367-
rescue => e
368-
exception = e
374+
File.open(path, 'w+') { it << 'foo' }
375+
rescue => error
376+
# Ignore.
369377
end
370378
end
379+
371380
thread.join
372-
assert_kind_of Errno::EINVAL, exception
381+
assert_kind_of Errno::EINVAL, error
373382
ensure
374383
thread.kill rescue nil
375-
FileUtils.rm_f(fn)
384+
FileUtils.rm_f(path)
376385
end
377386
end

0 commit comments

Comments
 (0)