uringmachine 1.0.5 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/uringmachine.rb CHANGED
@@ -50,7 +50,7 @@ class UringMachine
50
50
  end
51
51
 
52
52
  # Runs the given block in the given fiber. This method is used to run fibers
53
- # indirectly.
53
+ # indirectly. See FiberScheduler#fiber
54
54
  #
55
55
  # @param fiber [Fiber] fiber
56
56
  # @param block [Proc] block to run
@@ -202,9 +202,10 @@ class UringMachine
202
202
  # the type of target and how it is read from:
203
203
  #
204
204
  # - :fd - read from the given fd using the buffer pool (default mode)
205
+ # - :file - read from a regular file
205
206
  # - :socket - receive from the given socket fd using the buffer pool
206
207
  # - :ssl - read from the given SSL socket
207
- #
208
+ #
208
209
  # If a block is given, the block will be called with the IO instance as
209
210
  # argument and the method will return the block's return value.
210
211
  #
@@ -220,6 +221,17 @@ class UringMachine
220
221
  res
221
222
  end
222
223
 
224
+ # call-seq:
225
+ # machine.file_read(fn) -> data
226
+ #
227
+ # Reads and returns the content of the given filename, with a maximum length of 1GB.
228
+ #
229
+ # @param fn [String] filename
230
+ # @return [String] file content
231
+ def file_read(fn)
232
+ open(fn, O_RDONLY) { io(it, :file).read(-(1 << 30)) }
233
+ end
234
+
223
235
  # Creates, binds and sets up a TCP socket for listening on the given host and
224
236
  # port.
225
237
  #
data/test/test_fiber.rb CHANGED
@@ -371,3 +371,36 @@ class ThreadMachineTest < UMBaseTest
371
371
  assert_equal m, m2
372
372
  end
373
373
  end
374
+
375
+ class FiberRubyAPITest < UMBaseTest
376
+ def test_fiber_raise
377
+ buf = []
378
+ q = UM::Queue.new
379
+ f = machine.spin do
380
+ buf << machine.shift(q)
381
+ rescue => e
382
+ buf << e
383
+ end
384
+
385
+ e = RuntimeError.new
386
+ 3.times { machine.snooze }
387
+ machine.schedule(Fiber.current, nil)
388
+ f.raise(e)
389
+
390
+ assert_equal e, buf[0]
391
+ end
392
+
393
+ def test_fiber_transfer
394
+ buf = []
395
+ q = UM::Queue.new
396
+ f = machine.spin do
397
+ buf << machine.switch
398
+ end
399
+
400
+ 3.times { machine.snooze }
401
+ machine.schedule(Fiber.current, nil)
402
+ f.transfer(42)
403
+
404
+ assert_equal 42, buf[0]
405
+ end
406
+ end
@@ -94,6 +94,10 @@ class FiberSchedulerTest < UMBaseTest
94
94
  f3 = Fiber.schedule do
95
95
  str = i.read
96
96
  buffer << str
97
+ rescue => e
98
+ p e
99
+ p e.backtrace
100
+ exit!
97
101
  end
98
102
 
99
103
  @scheduler.join
@@ -271,14 +275,12 @@ class FiberSchedulerTest < UMBaseTest
271
275
 
272
276
  def test_fiber_scheduler_sleep
273
277
  t0 = monotonic_clock
274
- assert_equal 0, machine.metrics[:ops_pending]
275
278
  Fiber.schedule do
276
279
  sleep(0.01)
277
280
  end
278
281
  Fiber.schedule do
279
282
  sleep(0.02)
280
283
  end
281
- assert_equal 2, machine.metrics[:ops_pending]
282
284
  @scheduler.join
283
285
  t1 = monotonic_clock
284
286
  assert_in_range 0.02..0.025, t1 - t0
@@ -355,7 +357,6 @@ class FiberSchedulerTest < UMBaseTest
355
357
  buf = +''
356
358
  sent = nil
357
359
 
358
- assert_equal 0, machine.metrics[:total_ops]
359
360
  Fiber.schedule do
360
361
  buf = s1.recv(12)
361
362
  end
@@ -365,7 +366,6 @@ class FiberSchedulerTest < UMBaseTest
365
366
 
366
367
  # In Ruby, sockets are by default non-blocking. The recv will cause io_wait
367
368
  # to be invoked, the send should get through without needing to poll.
368
- assert_equal 1, machine.metrics[:total_ops]
369
369
  @scheduler.join
370
370
 
371
371
  assert_equal 6, sent
@@ -385,7 +385,6 @@ class FiberSchedulerTest < UMBaseTest
385
385
  Fiber.schedule do
386
386
  IO.write(fn, 'foobar')
387
387
  end
388
- assert_equal 1, machine.metrics[:total_ops]
389
388
 
390
389
  buf = nil
391
390
  Fiber.schedule do
@@ -394,10 +393,7 @@ class FiberSchedulerTest < UMBaseTest
394
393
  end
395
394
 
396
395
  @scheduler.join
397
- metrics = machine.metrics
398
- assert_in_range 5..12, metrics[:total_ops]
399
- assert_equal 0, metrics[:ops_pending]
400
- assert_equal 256, metrics[:ops_free]
396
+
401
397
  assert_equal 'foobar', buf
402
398
  assert_equal({
403
399
  fiber: 2,
@@ -416,15 +412,15 @@ class FiberSchedulerTest < UMBaseTest
416
412
  Fiber.schedule do
417
413
  File.open(fn, 'w') { it.write 'foobar' }
418
414
  end
419
- assert_equal 1, machine.metrics[:total_ops]
420
415
 
421
416
  buf = nil
422
417
  Fiber.schedule do
423
418
  sleep 0.001
424
419
  File.open(fn, 'r') { buf = it.read }
425
420
  end
421
+
426
422
  @scheduler.join
427
- assert_equal 8, machine.metrics[:total_ops]
423
+
428
424
  assert_equal 'foobar', buf
429
425
  assert_equal({
430
426
  fiber: 2,
@@ -500,27 +496,25 @@ class FiberSchedulerTest < UMBaseTest
500
496
  Fiber.schedule do
501
497
  buf << 11
502
498
  mutex.synchronize {
503
- buf << [12, machine.metrics[:total_ops]]
499
+ buf << 12
504
500
  sleep 0.01
505
- buf << [13, machine.metrics[:total_ops]]
501
+ buf << 13
506
502
  }
507
503
  buf << 14
508
504
  end
509
- assert_equal 1, machine.metrics[:total_ops]
510
505
 
511
506
  Fiber.schedule do
512
507
  buf << 21
513
508
  mutex.synchronize {
514
- buf << [22, machine.metrics[:total_ops]]
509
+ buf << 22
515
510
  sleep 0.01
516
- buf << [23, machine.metrics[:total_ops]]
511
+ buf << 23
517
512
  }
518
513
  buf << 24
519
514
  end
520
- assert_equal 1, machine.metrics[:total_ops]
521
515
 
522
516
  @scheduler.join
523
- assert_equal [11, [12, 0], 21, [13, 2], 14, [22, 2], [23, 4], 24], buf
517
+ assert_equal [11, 12, 21, 13, 14, 22, 23, 24], buf
524
518
  assert_equal({
525
519
  fiber: 2,
526
520
  kernel_sleep: 2,
@@ -535,19 +529,18 @@ class FiberSchedulerTest < UMBaseTest
535
529
 
536
530
  buf = []
537
531
  Fiber.schedule do
538
- buf << [11, machine.metrics[:total_ops]]
532
+ buf << 11
539
533
  buf << queue.shift
540
- buf << [12, machine.metrics[:total_ops]]
534
+ buf << 12
541
535
  end
542
536
  Fiber.schedule do
543
- buf << [21, machine.metrics[:total_ops]]
537
+ buf << 21
544
538
  queue << :foo
545
- buf << [22, machine.metrics[:total_ops]]
539
+ buf << 22
546
540
  end
547
- assert_equal 0, machine.metrics[:total_ops]
548
541
  @scheduler.join
549
542
 
550
- assert_equal [[11, 0], [21, 0], [22, 0], :foo, [12, 1]], buf
543
+ assert_equal [11, 21, 22, :foo, 12], buf
551
544
  assert_equal({
552
545
  fiber: 2,
553
546
  block: 1,
@@ -561,17 +554,16 @@ class FiberSchedulerTest < UMBaseTest
561
554
 
562
555
  buf = []
563
556
  Fiber.schedule do
564
- buf << [11, machine.metrics[:total_ops]]
557
+ buf << 11
565
558
  buf << queue.shift(timeout: 0.01)
566
- buf << [12, machine.metrics[:total_ops]]
559
+ buf << 12
567
560
  end
568
561
  Fiber.schedule do
569
- buf << [21, machine.metrics[:total_ops]]
562
+ buf << 21
570
563
  end
571
- assert_equal 1, machine.metrics[:total_ops]
572
564
  @scheduler.join
573
565
 
574
- assert_equal [[11, 0], [21, 1], nil, [12, 2]], buf
566
+ assert_equal [11, 21, nil, 12], buf
575
567
  assert_equal({
576
568
  fiber: 2,
577
569
  block: 1,
@@ -587,11 +579,8 @@ class FiberSchedulerTest < UMBaseTest
587
579
  thread.join
588
580
  end
589
581
 
590
- # No ops are issued, except for a NOP SQE used to wakeup the waiting thread.
591
- assert_equal 0, machine.metrics[:total_ops]
592
-
593
582
  @scheduler.join
594
- assert_equal 1, machine.metrics[:total_ops]
583
+
595
584
  assert_equal({
596
585
  fiber: 1,
597
586
  block: 1,
@@ -627,8 +616,9 @@ class FiberSchedulerTest < UMBaseTest
627
616
  Fiber.schedule do
628
617
  buf << `echo 'foo'`
629
618
  end
630
- assert_equal 1, machine.metrics[:total_ops]
619
+
631
620
  @scheduler.join
621
+
632
622
  assert_equal ["foo\n"], buf
633
623
  assert_equal({
634
624
  fiber: 1,
@@ -647,16 +637,16 @@ class FiberSchedulerTest < UMBaseTest
647
637
  buf = []
648
638
  Fiber.schedule do
649
639
  IO.popen('ruby', 'r+') do |pipe|
650
- buf << [11, machine.metrics[:total_ops]]
640
+ buf << 11
651
641
  pipe.puts 'puts "bar"'
652
- buf << [12, machine.metrics[:total_ops]]
642
+ buf << 12
653
643
  pipe.close_write
654
- buf << [13, pipe.gets.chomp, machine.metrics[:total_ops]]
644
+ buf << 13
655
645
  end
656
646
  end
657
- assert_equal 1, machine.metrics[:total_ops]
658
647
  @scheduler.join
659
- assert_equal [[11, 0], [12, 3], [13, "bar", 5]], buf
648
+
649
+ assert_equal [11, 12, 13], buf
660
650
  assert_equal({
661
651
  fiber: 1,
662
652
  io_write: 2,
@@ -678,7 +668,7 @@ class FiberSchedulerTest < UMBaseTest
678
668
  rescue Exception => e
679
669
  exception = e
680
670
  end
681
- assert_equal 1, machine.metrics[:total_ops]
671
+ assert_equal({ fiber: 1 }, scheduler_calls_tally)
682
672
  machine.snooze
683
673
  Thread.new {
684
674
  r.close
@@ -701,8 +691,8 @@ class FiberSchedulerTest < UMBaseTest
701
691
  Fiber.schedule do
702
692
  addrs = Addrinfo.getaddrinfo("localhost", 80, Socket::AF_INET, :STREAM)
703
693
  end
704
- assert_equal 1, machine.metrics[:total_ops]
705
694
  @scheduler.join
695
+
706
696
  assert_kind_of Array, addrs
707
697
  addr = addrs.first
708
698
  assert_kind_of Addrinfo, addr
@@ -726,8 +716,8 @@ class FiberSchedulerTest < UMBaseTest
726
716
  rescue => res
727
717
  end
728
718
  @scheduler.join
719
+
729
720
  assert_kind_of Timeout::Error, res
730
- assert_equal 3, machine.metrics[:total_ops]
731
721
  assert_equal({
732
722
  fiber: 1,
733
723
  timeout_after: 1,
data/test/test_io.rb CHANGED
@@ -296,11 +296,11 @@ class IOTest < IOBaseTest
296
296
  assert_equal [:ready, 'foo'], bufs
297
297
 
298
298
  machine.write(@wfd, 'barb')
299
- machine.snooze
299
+ 3.times { machine.snooze }
300
300
  assert_equal [:ready, 'foo', 'barb'], bufs
301
301
 
302
302
  machine.close(@wfd)
303
- machine.snooze
303
+ 3.times { machine.snooze }
304
304
  assert_equal [:ready, 'foo', 'barb', :done], bufs
305
305
  ensure
306
306
  machine.terminate(f)
@@ -356,6 +356,15 @@ class IOWriteTest < UMBaseTest
356
356
  assert_equal 'foobar', buf
357
357
  end
358
358
 
359
+ def test_io_write_io_buf
360
+ io_buf = IO::Buffer.for('foobar')
361
+ assert_equal 11, conn.write("6\r\n", io_buf, "\r\n")
362
+
363
+ buf = +''
364
+ machine.read(@s2, buf, 100)
365
+ assert_equal "6\r\nfoobar\r\n", buf
366
+ end
367
+
359
368
  def test_io_write_socket_mode
360
369
  conn = machine.io(@s2, :socket)
361
370
 
@@ -717,7 +726,7 @@ class IOModeTest < UMBaseTest
717
726
  end
718
727
 
719
728
  def test_io_file_mode
720
- fn = Tempfile.new('uringmachine_test_io_from_file').path
729
+ fn = Tempfile.new('uringmachine_test_io_file_mode').path
721
730
  IO.write(fn, (0..9).to_a.join)
722
731
  fd = machine.open(fn, UM::O_RDONLY)
723
732
  io = machine.io(fd, :file)
@@ -733,6 +742,18 @@ class IOModeTest < UMBaseTest
733
742
  machine.close(fd) if fd
734
743
  end
735
744
 
745
+ def test_io_file_mode_read_each
746
+ fn = Tempfile.new('test_io_file_mode_read_each').path
747
+ IO.write(fn, (0..9).to_a.join)
748
+ fd = machine.open(fn, UM::O_RDONLY)
749
+ io = machine.io(fd, :file)
750
+ buf = []
751
+ io.read_each { buf << it.get_string }
752
+ assert_equal ['0123456789'], buf
753
+ ensure
754
+ machine.close(fd) if fd
755
+ end
756
+
736
757
  def test_io_socket_mode_non_socket
737
758
  r, w = UM.pipe
738
759
  machine.write(w, 'foobar')
data/test/test_um.rb CHANGED
@@ -2,12 +2,25 @@
2
2
 
3
3
  require_relative 'helper'
4
4
  require 'socket'
5
+ require 'tempfile'
5
6
 
6
7
  class UringMachineTest < Minitest::Test
7
8
  def test_kernel_version
8
9
  v = UringMachine.kernel_version
9
10
  assert_kind_of Integer, v
10
- assert_in_range 600..700, v
11
+ assert_in_range 600..712, v
12
+ end
13
+
14
+ def test_ruby_io_buffer_version
15
+ v = UringMachine::RUBY_IO_BUFFER_VERSION
16
+ assert_kind_of Integer, v
17
+ assert_in_range 1..4, v
18
+ end
19
+
20
+ def test_ruby_fiber_scheduler_version
21
+ v = UringMachine::RUBY_FIBER_SCHEDULER_VERSION
22
+ assert_kind_of Integer, v
23
+ assert_in_range 1..5, v
11
24
  end
12
25
  end
13
26
 
@@ -3070,6 +3083,7 @@ class ForkTest < UMBaseTest
3070
3083
  assert_equal 3, ret
3071
3084
  assert_equal 'foo', buf
3072
3085
  ensure
3086
+ Process.kill(:SIGKILL, child_pid)
3073
3087
  Process.wait(child_pid) rescue nil
3074
3088
  end
3075
3089
  end
@@ -3520,6 +3534,23 @@ class IOMethodTest < UMBaseTest
3520
3534
  end
3521
3535
  end
3522
3536
 
3537
+ class FileHelperMethodsTest < UMBaseTest
3538
+ def setup
3539
+ super
3540
+ @fn = Tempfile.new('uringmachine_test_io_from_file').path
3541
+ end
3542
+
3543
+ def test_file_read
3544
+ IO.write(@fn, 'foobar' * 321)
3545
+ buf = machine.file_read(@fn)
3546
+ assert_equal 'foobar' * 321, buf
3547
+ end
3548
+
3549
+ def test_file_read_bad_fn
3550
+ assert_raises(Errno::ENOENT) { machine.file_read(@fn + '-missing') }
3551
+ end
3552
+ end
3553
+
3523
3554
  class TCPHelperMethodsTest < UMBaseTest
3524
3555
  def setup
3525
3556
  super