polyphony 0.29 → 0.30

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/CHANGELOG.md +14 -0
  4. data/Gemfile.lock +1 -1
  5. data/TODO.md +15 -10
  6. data/docs/getting-started/tutorial.md +3 -3
  7. data/docs/index.md +2 -3
  8. data/docs/{technical-overview → main-concepts}/concurrency.md +62 -15
  9. data/docs/{technical-overview → main-concepts}/design-principles.md +21 -8
  10. data/docs/{technical-overview → main-concepts}/exception-handling.md +80 -38
  11. data/docs/{technical-overview → main-concepts}/extending.md +4 -3
  12. data/docs/{technical-overview → main-concepts}/fiber-scheduling.md +3 -3
  13. data/docs/{technical-overview.md → main-concepts.md} +2 -2
  14. data/examples/core/xx-at_exit.rb +29 -0
  15. data/examples/core/xx-fork-terminate.rb +27 -0
  16. data/examples/core/xx-pingpong.rb +18 -0
  17. data/examples/core/xx-stop.rb +20 -0
  18. data/ext/gyro/async.c +1 -1
  19. data/ext/gyro/extconf.rb +0 -3
  20. data/ext/gyro/gyro.c +7 -8
  21. data/ext/gyro/gyro.h +2 -0
  22. data/ext/gyro/queue.c +6 -6
  23. data/ext/gyro/selector.c +32 -1
  24. data/ext/gyro/thread.c +55 -9
  25. data/ext/gyro/timer.c +1 -0
  26. data/lib/polyphony/core/exceptions.rb +4 -1
  27. data/lib/polyphony/core/global_api.rb +1 -6
  28. data/lib/polyphony/core/thread_pool.rb +3 -3
  29. data/lib/polyphony/extensions/core.rb +7 -1
  30. data/lib/polyphony/extensions/fiber.rb +159 -72
  31. data/lib/polyphony/extensions/io.rb +2 -4
  32. data/lib/polyphony/extensions/openssl.rb +0 -17
  33. data/lib/polyphony/extensions/thread.rb +46 -22
  34. data/lib/polyphony/version.rb +1 -1
  35. data/lib/polyphony.rb +20 -18
  36. data/test/coverage.rb +1 -1
  37. data/test/helper.rb +7 -3
  38. data/test/test_fiber.rb +285 -72
  39. data/test/test_global_api.rb +7 -52
  40. data/test/test_io.rb +8 -0
  41. data/test/test_signal.rb +1 -0
  42. data/test/test_thread.rb +76 -56
  43. data/test/test_thread_pool.rb +27 -5
  44. data/test/test_throttler.rb +1 -0
  45. metadata +12 -12
  46. data/lib/polyphony/core/supervisor.rb +0 -114
  47. data/lib/polyphony/line_reader.rb +0 -82
  48. data/test/test_gyro.rb +0 -25
  49. data/test/test_supervisor.rb +0 -180
@@ -1,180 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'helper'
4
-
5
- class SupervisorTest < MiniTest::Test
6
- def test_await
7
- result = Polyphony::Supervisor.new.await { |s|
8
- s.spin {
9
- snooze
10
- :foo
11
- }
12
- }
13
- assert_equal [:foo], result
14
- end
15
-
16
- def test_await_multiple_fibers
17
- result = Polyphony::Supervisor.new.await { |s|
18
- (1..3).each { |i|
19
- s.spin {
20
- snooze
21
- i * 10
22
- }
23
- }
24
- }
25
- assert_equal [10, 20, 30], result
26
- end
27
-
28
- def test_new_with_block
29
- supervisor = Polyphony::Supervisor.new { |s|
30
- (1..3).each { |i|
31
- s.spin {
32
- snooze
33
- i * 10
34
- }
35
- }
36
- }
37
- assert_equal [10, 20, 30], supervisor.await
38
- end
39
-
40
- def test_join_multiple_fibers
41
- result = Polyphony::Supervisor.new.join { |s|
42
- (1..3).each { |i|
43
- s.spin {
44
- snooze
45
- i * 10
46
- }
47
- }
48
- }
49
- assert_equal [10, 20, 30], result
50
- end
51
-
52
- def test_spin_method
53
- buffer = []
54
- Polyphony::Supervisor.new.join { |s|
55
- (1..3).each { |i|
56
- buffer << s.spin {
57
- snooze
58
- i * 10
59
- }
60
- }
61
- }
62
-
63
- assert_equal [Fiber], buffer.map { |v| v.class }.uniq
64
- end
65
-
66
- def test_supervisor_select
67
- buffer = []
68
- foo_f = bar_f = baz_f = nil
69
- result, f = Polyphony::Supervisor.new.select { |s|
70
- foo_f = s.spin { sleep 0.1; buffer << :foo; :foo }
71
- bar_f = s.spin { sleep 0.3; buffer << :bar; :bar }
72
- baz_f = s.spin { sleep 0.5; buffer << :baz; :baz }
73
- }
74
-
75
- assert_equal :foo, result
76
- assert_equal foo_f, f
77
-
78
- sleep 0.05
79
- assert !bar_f.running?
80
- assert !baz_f.running?
81
- assert_equal [:foo], buffer
82
- end
83
-
84
- def test_await_with_exception
85
- buffer = []
86
- result = capture_exception do
87
- Polyphony::Supervisor.new.await { |s|
88
- (1..3).each { |i|
89
- s.spin {
90
- raise 'foo' if i == 1
91
- snooze
92
- buffer << i * 10
93
- i * 10
94
- }
95
- }
96
- }
97
- end
98
-
99
- assert_kind_of RuntimeError, result
100
- assert_equal 'foo', result.message
101
- snooze
102
- assert_equal [], buffer
103
- end
104
-
105
- def test_await_interruption
106
- buffer = []
107
- supervisor = nil
108
- supervisor = Polyphony::Supervisor.new
109
- spin { supervisor.interrupt(42) }
110
- buffer << supervisor.await { |s|
111
- (1..3).each { |i|
112
- s.spin {
113
- buffer << i
114
- sleep i
115
- buffer << i * 10
116
- }
117
- }
118
- }
119
-
120
- snooze
121
- assert_equal [1, 2, 3, 42], buffer
122
- end
123
-
124
- def test_select_interruption
125
- buffer = []
126
- supervisor = nil
127
- supervisor = Polyphony::Supervisor.new
128
- spin { supervisor.interrupt(42) }
129
- buffer << supervisor.select { |s|
130
- (1..3).each { |i|
131
- s.spin {
132
- buffer << i
133
- sleep i
134
- buffer << i * 10
135
- }
136
- }
137
- }
138
-
139
- snooze
140
- assert_equal [1, 2, 3, 42], buffer
141
- end
142
-
143
- def test_add
144
- supervisor = Polyphony::Supervisor.new
145
- supervisor << spin { :foo }
146
- supervisor << spin { :bar }
147
-
148
- assert_equal [:foo, :bar], supervisor.await
149
- end
150
- end
151
-
152
- class FiberExtensionsTest < MiniTest::Test
153
- def test_join
154
- f1 = spin { :foo }
155
- f2 = spin { :bar }
156
- assert_equal [:foo, :bar], Fiber.join(f1, f2)
157
-
158
- f1 = spin { :foo }
159
- f2 = spin { raise 'bar' }
160
- result = capture_exception { Fiber.join(f1, f2) }
161
- assert_kind_of RuntimeError, result
162
- assert_equal 'bar', result.message
163
- end
164
-
165
- def test_select
166
- f1 = spin { sleep 1; :foo }
167
- f2 = spin { :bar }
168
- assert_equal [:bar, f2], Fiber.select(f1, f2)
169
-
170
- f1 = spin { :foo }
171
- f2 = spin { sleep 0.01; raise 'bar' }
172
- assert_equal [:foo, f1], Fiber.select(f1, f2)
173
-
174
- f1 = spin { sleep 1; :foo }
175
- f2 = spin { raise 'bar' }
176
- result = capture_exception { Fiber.select(f1, f2) }
177
- assert_kind_of RuntimeError, result
178
- assert_equal 'bar', result.message
179
- end
180
- end