uringmachine 0.7 → 0.8.1

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/test/test_actor.rb CHANGED
@@ -7,7 +7,7 @@ require 'uringmachine/actor'
7
7
  class ActorTest < UMBaseTest
8
8
  module Counter
9
9
  def setup
10
- @count = 0
10
+ @count = 0
11
11
  end
12
12
 
13
13
  def incr
@@ -24,16 +24,17 @@ class ActorTest < UMBaseTest
24
24
  end
25
25
 
26
26
  def test_basic_actor_functionality
27
+ mailbox = UM::Queue.new
27
28
  actor = @machine.spin_actor(Counter)
28
-
29
+
29
30
  assert_kind_of Fiber, actor
30
31
 
31
- assert_equal 0, actor.call(:get)
32
- assert_equal 1, actor.call(:incr)
32
+ assert_equal 0, actor.call(mailbox, :get)
33
+ assert_equal 1, actor.call(mailbox, :incr)
33
34
  assert_equal actor, actor.cast(:incr)
34
- assert_equal 2, actor.call(:get)
35
+ assert_equal 2, actor.call(mailbox, :get)
35
36
  assert_equal actor, actor.cast(:reset)
36
- assert_equal 0, actor.call(:get)
37
+ assert_equal 0, actor.call(mailbox, :get)
37
38
  end
38
39
 
39
40
  module Counter2
@@ -57,7 +58,8 @@ class ActorTest < UMBaseTest
57
58
 
58
59
  def test_actor_with_args
59
60
  actor = @machine.spin_actor(Counter2, 43)
60
-
61
- assert_equal 43, actor.call(:get)
61
+ mailbox = UM::Queue.new
62
+
63
+ assert_equal 43, actor.call(mailbox, :get)
62
64
  end
63
65
  end
@@ -37,9 +37,9 @@ class AsyncOpTest < UMBaseTest
37
37
  assert_equal 1, machine.pending_count
38
38
  @op.cancel
39
39
  assert_equal false, @op.done?
40
-
40
+
41
41
  machine.sleep(0.01)
42
-
42
+
43
43
  assert_equal 0, machine.pending_count
44
44
  assert_equal true, @op.done?
45
45
  assert_equal (-ECANCELED), @op.result
@@ -52,7 +52,7 @@ class AsyncOpTest < UMBaseTest
52
52
  end
53
53
 
54
54
  res = @op.await
55
-
55
+
56
56
  assert_equal 0, machine.pending_count
57
57
  assert_equal true, @op.done?
58
58
  assert_equal (-ECANCELED), res
@@ -63,7 +63,7 @@ class AsyncOpTest < UMBaseTest
63
63
 
64
64
  def test_async_op_await_with_timeout
65
65
  e = nil
66
-
66
+
67
67
  begin
68
68
  machine.timeout(0.01, TOError) do
69
69
  @op.await
@@ -80,7 +80,7 @@ class AsyncOpTest < UMBaseTest
80
80
 
81
81
  def test_async_op_await_with_timeout2
82
82
  e = nil
83
-
83
+
84
84
  begin
85
85
  machine.timeout(0.1, TOError) do
86
86
  @op.await
@@ -0,0 +1,174 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'helper'
4
+ require 'socket'
5
+
6
+ class FiberSpinTest < UMBaseTest
7
+ def test_spin
8
+ x = nil
9
+ f = machine.spin do
10
+ x = :foo
11
+ end
12
+
13
+ assert_kind_of Fiber, f
14
+ assert_nil x
15
+
16
+ machine.snooze
17
+
18
+ assert_equal :foo, x
19
+ end
20
+
21
+ def test_spin_with_initial_value
22
+ x = nil
23
+ f = machine.spin(42) do |v|
24
+ x = v
25
+ end
26
+
27
+ assert_kind_of Fiber, f
28
+ assert_nil x
29
+
30
+ machine.snooze
31
+ assert_equal 42, x
32
+ end
33
+
34
+ class MyFiber < Fiber
35
+ end
36
+
37
+ def test_spin_with_custom_class
38
+ f = machine.spin(nil, MyFiber) do
39
+ end
40
+
41
+ assert_kind_of MyFiber, f
42
+ end
43
+ end
44
+
45
+ class FiberTerminateTest < UMBaseTest
46
+ def test_terminate_fiber
47
+ x = nil
48
+ f = machine.spin do
49
+ x = 1
50
+ machine.sleep 0.01
51
+ ensure
52
+ x = 0
53
+ end
54
+
55
+ assert_nil x
56
+ machine.snooze
57
+ assert_equal 1, x
58
+
59
+ machine.schedule(f, UM::Terminate.new)
60
+ 2.times { machine.snooze }
61
+
62
+ assert_equal 0, x
63
+ end
64
+ end
65
+
66
+ class JoinTest < UMBaseTest
67
+ def test_join
68
+ q = UM::Queue.new
69
+ x = nil
70
+
71
+ f = machine.spin do
72
+ x = 1
73
+ machine.push q, machine.shift(q) + 1
74
+ 42
75
+ ensure
76
+ x = 0
77
+ end
78
+
79
+ assert_nil x
80
+ machine.snooze
81
+ assert_equal 1, x
82
+
83
+ machine.spin do
84
+ x = 2
85
+ machine.push q, 2
86
+ end
87
+
88
+ res = machine.join(f)
89
+ assert_equal 0, x
90
+ assert_equal 3, machine.shift(q)
91
+ assert_equal [42], res
92
+ end
93
+
94
+ def test_join_multiple
95
+ f1 = machine.spin do
96
+ :foo
97
+ end
98
+
99
+ f2 = machine.spin do
100
+ machine.sleep(0.001)
101
+ :bar
102
+ end
103
+
104
+ f3 = machine.spin do
105
+ :baz
106
+ end
107
+
108
+ res = machine.join(f1, f2, f3)
109
+ assert_equal [:foo, :bar, :baz], res
110
+ end
111
+
112
+ def test_join_cross_thread
113
+ q = UM::Queue.new
114
+
115
+ t2 = Thread.new do
116
+ m2 = UM.new
117
+ f = m2.spin do
118
+ m2.push(q, f)
119
+ m2.snooze
120
+ :foo
121
+ end
122
+ m2.join(f)
123
+ end
124
+
125
+ f = machine.shift(q)
126
+ assert_kind_of Fiber, f
127
+ res = machine.join(f)
128
+ assert_equal [:foo], res
129
+ ensure
130
+ t2.join
131
+ end
132
+
133
+ def test_join_with_exception
134
+ f = machine.spin do
135
+ raise "Foobar"
136
+ end
137
+
138
+ res = machine.join(f)
139
+ e = res.first
140
+ assert_kind_of RuntimeError, e
141
+ assert_equal 'Foobar', e.message
142
+ end
143
+ end
144
+
145
+ class ScopeTest < UMBaseTest
146
+ def test_scope
147
+ skip
148
+
149
+ x1 = nil
150
+ x2 = nil
151
+
152
+ t0 = monotonic_clock
153
+ machine.scope do
154
+ f1 = machine.spin do
155
+ x1 = 1
156
+ machine.sleep 0.01
157
+ ensure
158
+ x1 = 0
159
+ end
160
+
161
+ f2 = machine.spin do
162
+ x2 = 1
163
+ machine.sleep 0.03
164
+ ensure
165
+ x2 = 0
166
+ end
167
+ end
168
+ elapsed = monotonic_clock - t0
169
+ assert_in_range 0.03..0.05, elapsed
170
+
171
+ assert_equal 0, x1
172
+ assert_equal 0, x2
173
+ end
174
+ end
data/test/test_um.rb CHANGED
@@ -11,7 +11,7 @@ class UringMachineTest < Minitest::Test
11
11
  end
12
12
  end
13
13
 
14
- class FiberTest < UMBaseTest
14
+ class SpinTest < UMBaseTest
15
15
  def test_spin
16
16
  x = nil
17
17
  f = machine.spin do
@@ -27,6 +27,24 @@ class FiberTest < UMBaseTest
27
27
  end
28
28
  end
29
29
 
30
+ class SnoozeTest < UMBaseTest
31
+ def test_snooze_while_sleeping_fiber
32
+ f = machine.spin do
33
+ machine.sleep(0.1)
34
+ end
35
+
36
+ t0 = monotonic_clock
37
+ machine.snooze
38
+ t1 = monotonic_clock
39
+ assert_in_range 0..0.001, t1 - t0
40
+
41
+ t0 = monotonic_clock
42
+ machine.snooze
43
+ t1 = monotonic_clock
44
+ assert_in_range 0..0.001, t1 - t0
45
+ end
46
+ end
47
+
30
48
  class ScheduleTest < UMBaseTest
31
49
  def test_schedule_and_yield
32
50
  buf = []
@@ -60,7 +78,7 @@ class ScheduleTest < UMBaseTest
60
78
  buf << e
61
79
  machine.yield
62
80
  end
63
-
81
+
64
82
  machine.schedule(f, nil)
65
83
  # start the f fiber
66
84
  machine.snooze
@@ -80,7 +98,7 @@ class ScheduleTest < UMBaseTest
80
98
  machine.schedule(main, e)
81
99
  machine.yield
82
100
  end
83
-
101
+
84
102
  machine.schedule(f, nil)
85
103
  t0 = monotonic_clock
86
104
 
@@ -234,7 +252,7 @@ class PeriodicallyTest < UMBaseTest
234
252
  t1 = monotonic_clock
235
253
  assert_in_range 0.05..0.09, t1 - t0
236
254
  assert_equal 5, count
237
- assert_equal 1, cancel
255
+ assert_equal 1, cancel
238
256
  end
239
257
 
240
258
  def test_periodically_with_timeout
@@ -258,7 +276,7 @@ class PeriodicallyTest < UMBaseTest
258
276
  t1 = monotonic_clock
259
277
  assert_in_range 0.05..0.08, t1 - t0
260
278
  assert_in_range 4..6, count
261
- assert_equal 1, cancel
279
+ assert_equal 1, cancel
262
280
 
263
281
  end
264
282
  end
@@ -331,7 +349,7 @@ class ReadTest < UMBaseTest
331
349
 
332
350
  buffer = +'foo'
333
351
  sio = StringIO.new(buffer)
334
-
352
+
335
353
  r, w = IO.pipe
336
354
  w << 'bar'
337
355
 
@@ -366,7 +384,7 @@ class ReadEachTest < UMBaseTest
366
384
  w.close
367
385
  machine.yield
368
386
  end
369
-
387
+
370
388
  machine.schedule(f, nil)
371
389
 
372
390
  machine.read_each(r.fileno, bgid) do |buf|
@@ -464,7 +482,7 @@ class ReadEachTest < UMBaseTest
464
482
  sleep 0.1
465
483
  w.close
466
484
  end
467
-
485
+
468
486
  bufs = []
469
487
  machine.read_each(r.fileno, bgid) do |b|
470
488
  bufs << b
@@ -591,7 +609,7 @@ class AcceptTest < UMBaseTest
591
609
 
592
610
  def test_accept
593
611
  conn = TCPSocket.new('127.0.0.1', @port)
594
-
612
+
595
613
  assert_equal 0, machine.pending_count
596
614
  fd = machine.accept(@server.fileno)
597
615
  assert_equal 0, machine.pending_count
@@ -635,6 +653,32 @@ class AcceptEachTest < UMBaseTest
635
653
  ensure
636
654
  t&.kill
637
655
  end
656
+
657
+ def test_accept_each_interrupted
658
+ conns = []
659
+ count = 0
660
+ terminated = nil
661
+ f = @machine.spin do
662
+ machine.accept_each(@server.fileno) do |fd|
663
+ count += 1
664
+ break if count == 3
665
+ end
666
+ rescue UM::Terminate
667
+ terminated = true
668
+ end
669
+
670
+ s = TCPSocket.new('127.0.0.1', @port)
671
+ @machine.sleep(0.01)
672
+
673
+ assert_equal 1, count
674
+ refute terminated
675
+
676
+ @machine.schedule(f, UM::Terminate.new)
677
+ @machine.sleep(0.01)
678
+
679
+ assert f.done?
680
+ assert terminated
681
+ end
638
682
  end
639
683
 
640
684
  class SocketTest < UMBaseTest
@@ -1041,10 +1085,9 @@ class QueueTest < UMBaseTest
1041
1085
  machine.schedule(f2, nil)
1042
1086
 
1043
1087
  machine.snooze
1044
-
1045
1088
  assert_equal [[1, :foo]], buf
1046
- machine.push(q, :bar)
1047
1089
 
1090
+ machine.push(q, :bar)
1048
1091
  machine.snooze
1049
1092
  assert_equal [[1, :foo], [2, :bar]], buf
1050
1093
  end
@@ -1076,6 +1119,30 @@ class QueueTest < UMBaseTest
1076
1119
  assert_equal :bar, machine.shift(q)
1077
1120
  assert_equal :foo, machine.shift(q)
1078
1121
  end
1122
+
1123
+ def test_cross_thread_push_shift
1124
+ q = UM::Queue.new
1125
+
1126
+ t1 = Thread.new {
1127
+ m = UM.new
1128
+ 3.times { m.push(q, it) }
1129
+ }
1130
+
1131
+ items = []
1132
+
1133
+ t2 = Thread.new {
1134
+ m = UM.new
1135
+ 3.times {
1136
+ i = m.shift(q)
1137
+ items << i
1138
+ m.sleep(0.01)
1139
+ }
1140
+ }
1141
+
1142
+ [t1, t2].each(&:join)
1143
+
1144
+ assert_equal [0, 1, 2], items
1145
+ end
1079
1146
  end
1080
1147
 
1081
1148
  class OpenTest < UMBaseTest
@@ -1119,7 +1186,7 @@ class PipeTest < UMBaseTest
1119
1186
  rfd, wfd = UM.pipe
1120
1187
  ret = machine.write(wfd, 'foo')
1121
1188
  assert_equal 3, ret
1122
-
1189
+
1123
1190
  ret = machine.close(wfd)
1124
1191
  assert_equal wfd, ret
1125
1192
 
@@ -1161,6 +1228,6 @@ class WaitTest < UMBaseTest
1161
1228
  def test_waitpid_bad_pid
1162
1229
  skip if UM.kernel_version < 607
1163
1230
 
1164
- assert_raises(Errno::ECHILD) { machine.waitpid(1, UM::WEXITED) }
1231
+ assert_raises(Errno::ECHILD) { machine.waitpid(1, UM::WEXITED) }
1165
1232
  end
1166
1233
  end
data/uringmachine.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.extra_rdoc_files = ["README.md"]
19
19
  s.extensions = ["ext/um/extconf.rb"]
20
20
  s.require_paths = ["lib"]
21
- s.required_ruby_version = '>= 3.3'
21
+ s.required_ruby_version = '>= 3.4'
22
22
 
23
23
  s.add_development_dependency 'rake-compiler', '1.2.9'
24
24
  s.add_development_dependency 'minitest', '5.25.4'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uringmachine
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.7'
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-04-28 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: rake-compiler
@@ -123,23 +123,18 @@ files:
123
123
  - ext/um/um_mutex_class.c
124
124
  - ext/um/um_op.c
125
125
  - ext/um/um_queue_class.c
126
- - ext/um/um_ssl.c
127
- - ext/um/um_ssl.h
128
- - ext/um/um_ssl_class.c
129
126
  - ext/um/um_sync.c
130
127
  - ext/um/um_utils.c
131
128
  - lib/uringmachine.rb
132
129
  - lib/uringmachine/actor.rb
133
130
  - lib/uringmachine/dns_resolver.rb
134
- - lib/uringmachine/ssl.rb
135
- - lib/uringmachine/ssl/context_builder.rb
136
131
  - lib/uringmachine/version.rb
137
132
  - supressions/ruby.supp
138
133
  - test/helper.rb
139
134
  - test/run.rb
140
135
  - test/test_actor.rb
141
136
  - test/test_async_op.rb
142
- - test/test_ssl.rb
137
+ - test/test_fiber.rb
143
138
  - test/test_um.rb
144
139
  - uringmachine.gemspec
145
140
  - vendor/liburing/.github/actions/codespell/stopwords
@@ -453,14 +448,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
453
448
  requirements:
454
449
  - - ">="
455
450
  - !ruby/object:Gem::Version
456
- version: '3.3'
451
+ version: '3.4'
457
452
  required_rubygems_version: !ruby/object:Gem::Requirement
458
453
  requirements:
459
454
  - - ">="
460
455
  - !ruby/object:Gem::Version
461
456
  version: '0'
462
457
  requirements: []
463
- rubygems_version: 3.6.2
458
+ rubygems_version: 3.6.8
464
459
  specification_version: 4
465
460
  summary: A lean, mean io_uring machine
466
461
  test_files: []