File: fiber.rb

package info (click to toggle)
mruby 3.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,584 kB
  • sloc: ansic: 51,933; ruby: 29,510; yacc: 7,077; cpp: 517; makefile: 51; sh: 42
file content (210 lines) | stat: -rw-r--r-- 4,337 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
begin
  $fiber_test_activity = __FILE__

  assert('Fiber.new') do
    f = Fiber.new{}
    assert_kind_of Fiber, f
  end

  assert('Fiber#resume') do
    f = Fiber.new{|x| x }
    assert_equal 2, f.resume(2)
  end

  assert('Fiber#transfer') do
    ary = []
    f2 = nil
    f1 = Fiber.new{
      ary << f2.transfer(:foo)
      :ok
    }
    f2 = Fiber.new{
      ary << f1.transfer(:baz)
      :ng
    }
    assert_equal(:ok, f1.transfer)
    assert_equal([:baz], ary)
    assert_false f1.alive?
  end

  assert('Fiber#alive?') do
    f = Fiber.new{ Fiber.yield }
    f.resume
    assert_true f.alive?
    f.resume
    assert_false f.alive?
  end

  assert('Fiber#==') do
    root = Fiber.current
    assert_equal root, root
    assert_equal root, Fiber.current
    assert_false root != Fiber.current
    f = Fiber.new {
      assert_false root == Fiber.current
    }
    f.resume
    assert_false f == root
    assert_true f != root
  end

  assert('Fiber.yield') do
    f = Fiber.new{|x| Fiber.yield x }
    assert_equal 3, f.resume(3)
    assert_true f.alive?
  end

  assert('FiberError') do
    assert_equal StandardError, FiberError.superclass
  end

  assert('Fiber iteration') do
    f1 = Fiber.new{
      [1,2,3].each{|x| Fiber.yield(x)}
    }
    f2 = Fiber.new{
      [9,8,7].each{|x| Fiber.yield(x)}
    }
    a = []
    3.times {
      a << f1.resume
      a << f2.resume
    }
    assert_equal [1,9,2,8,3,7], a
  end

  assert('Fiber with splat in the block argument list') {
    assert_equal([1], Fiber.new{|*x|x}.resume(1))
  }

  assert('Fiber raises on resume when dead') do
    assert_raise(FiberError) do
      f = Fiber.new{}
      f.resume
      assert_false f.alive?
      f.resume
    end
  end

  assert('Yield raises when called on root fiber') do
    assert_raise(FiberError) { Fiber.yield }
  end

  assert('Double resume of Fiber') do
    f1 = Fiber.new {}
    f2 = Fiber.new {
      f1.resume
      assert_raise(FiberError) { f2.resume }
      Fiber.yield 0
    }
    assert_equal 0, f2.resume
    f2.resume
    assert_false f1.alive?
    assert_false f2.alive?
  end

  assert('Recursive resume of Fiber') do
    f1, f2 = nil, nil
    f1 = Fiber.new { assert_raise(FiberError) { f2.resume } }
    f2 = Fiber.new {
      f1.resume
      Fiber.yield 0
    }
    f3 = Fiber.new {
      f2.resume
    }
    assert_equal 0, f3.resume
    f2.resume
    assert_false f1.alive?
    assert_false f2.alive?
    assert_false f3.alive?
  end

  assert('Root fiber resume') do
    root = Fiber.current
    assert_raise(FiberError) { root.resume }
    f = Fiber.new {
      assert_raise(FiberError) { root.resume }
    }
    f.resume
    assert_false f.alive?
  end

  assert('Fiber without block') do
    assert_raise(ArgumentError) { Fiber.new }
  end


  assert('Transfer to self.') do
    result = []
    f = Fiber.new { result << :start; f.transfer; result << :end }
    f.transfer
    assert_equal [:start, :end], result

    result = []
    f = Fiber.new { result << :start; f.transfer; result << :end }
    f.resume
    assert_equal [:start, :end], result
  end

  assert('Resume transferred fiber') do
    f = Fiber.new {
      assert_raise(FiberError) { f.resume }
    }
    f.transfer
  end

  assert('Root fiber transfer.') do
    result = nil
    root = Fiber.current
    f = Fiber.new {
      result = :ok
      root.transfer
    }
    f.transfer
    assert_true f.alive?
    assert_equal :ok, result
  end

  assert('Break nested fiber with root fiber transfer') do
    root = Fiber.current

    result = nil
    f2 = nil
    f1 = Fiber.new {
      root.transfer(f2.transfer)
      result = :f1
    }
    f2 = Fiber.new {
      result = :to_root
      root.transfer :from_f2
      result = :f2
    }
    assert_equal :from_f2, f1.transfer
    assert_equal :to_root, result
    assert_equal :f2, f2.transfer
    assert_equal :f2, result
    assert_false f2.alive?
    assert_equal nil, f1.transfer
    assert_equal :f1, f1.transfer
    assert_equal :f1, result
    assert_false f1.alive?
  end

  assert('CRuby Fiber#transfer test.') do
    ary = []
    f2 = nil
    f1 = Fiber.new{
      ary << f2.transfer(:foo)
      :ok
    }
    f2 = Fiber.new{
      ary << f1.transfer(:baz)
      :ng
    }
    assert_equal :ok, f1.transfer
    assert_equal [:baz], ary
  end
ensure
  $fiber_test_activity = nil
end