| 1 |
# frozen_string_literal: false |
| 2 |
require 'envutil' |
| 3 |
require_relative "helper" |
| 4 |
|
| 5 |
class TestThreadInstrumentation < Test::Unit::TestCase |
| 6 |
include ThreadInstrumentation::TestHelper |
| 7 |
|
| 8 |
def setup |
| 9 |
pend("No windows support") if /mswin|mingw|bccwin/ =~ RUBY_PLATFORM |
| 10 |
|
| 11 |
require '-test-/thread/instrumentation' |
| 12 |
|
| 13 |
cleanup_threads |
| 14 |
end |
| 15 |
|
| 16 |
def teardown |
| 17 |
return if /mswin|mingw|bccwin/ =~ RUBY_PLATFORM |
| 18 |
Bug::ThreadInstrumentation.unregister_callback |
| 19 |
cleanup_threads |
| 20 |
end |
| 21 |
|
| 22 |
THREADS_COUNT = 3 |
| 23 |
|
| 24 |
def test_single_thread_timeline |
| 25 |
thread = nil |
| 26 |
full_timeline = record do |
| 27 |
thread = Thread.new { 1 + 1 } |
| 28 |
thread.join |
| 29 |
end |
| 30 |
assert_equal %i(started ready resumed suspended exited), timeline_for(thread, full_timeline) |
| 31 |
ensure |
| 32 |
thread&.kill |
| 33 |
end |
| 34 |
|
| 35 |
def test_thread_pass_single_thread |
| 36 |
full_timeline = record do |
| 37 |
Thread.pass |
| 38 |
end |
| 39 |
assert_equal [], timeline_for(Thread.current, full_timeline) |
| 40 |
end |
| 41 |
|
| 42 |
def test_thread_pass_multi_thread |
| 43 |
thread = Thread.new do |
| 44 |
cpu_bound_work(0.5) |
| 45 |
end |
| 46 |
|
| 47 |
full_timeline = record do |
| 48 |
Thread.pass |
| 49 |
end |
| 50 |
|
| 51 |
assert_equal %i(suspended ready resumed), timeline_for(Thread.current, full_timeline) |
| 52 |
ensure |
| 53 |
thread&.kill |
| 54 |
thread&.join |
| 55 |
end |
| 56 |
|
| 57 |
def test_muti_thread_timeline |
| 58 |
threads = nil |
| 59 |
full_timeline = record do |
| 60 |
threads = threaded_cpu_bound_work(1.0) |
| 61 |
results = threads.map(&:value) |
| 62 |
results.each do |r| |
| 63 |
refute_equal false, r |
| 64 |
end |
| 65 |
assert_equal [false] * THREADS_COUNT, threads.map(&:status) |
| 66 |
end |
| 67 |
|
| 68 |
threads.each do |thread| |
| 69 |
timeline = timeline_for(thread, full_timeline) |
| 70 |
assert_consistent_timeline(timeline) |
| 71 |
assert timeline.count(:suspended) > 1, "Expected threads to yield suspended at least once: #{timeline.inspect}" |
| 72 |
end |
| 73 |
|
| 74 |
timeline = timeline_for(Thread.current, full_timeline) |
| 75 |
assert_consistent_timeline(timeline) |
| 76 |
ensure |
| 77 |
threads&.each(&:kill) |
| 78 |
end |
| 79 |
|
| 80 |
def test_join_suspends # Bug #18900 |
| 81 |
thread = other_thread = nil |
| 82 |
full_timeline = record do |
| 83 |
other_thread = Thread.new { sleep 0.3 } |
| 84 |
thread = Thread.new { other_thread.join } |
| 85 |
thread.join |
| 86 |
end |
| 87 |
|
| 88 |
timeline = timeline_for(thread, full_timeline) |
| 89 |
assert_consistent_timeline(timeline) |
| 90 |
assert_equal %i(started ready resumed suspended ready resumed suspended exited), timeline |
| 91 |
ensure |
| 92 |
other_thread&.kill |
| 93 |
thread&.kill |
| 94 |
end |
| 95 |
|
| 96 |
def test_io_release_gvl |
| 97 |
r, w = IO.pipe |
| 98 |
thread = nil |
| 99 |
full_timeline = record do |
| 100 |
thread = Thread.new do |
| 101 |
w.write("Hello\n") |
| 102 |
end |
| 103 |
thread.join |
| 104 |
end |
| 105 |
|
| 106 |
timeline = timeline_for(thread, full_timeline) |
| 107 |
assert_consistent_timeline(timeline) |
| 108 |
assert_equal %i(started ready resumed suspended ready resumed suspended exited), timeline |
| 109 |
ensure |
| 110 |
r&.close |
| 111 |
w&.close |
| 112 |
end |
| 113 |
|
| 114 |
def test_queue_releases_gvl |
| 115 |
queue1 = Queue.new |
| 116 |
queue2 = Queue.new |
| 117 |
|
| 118 |
thread = nil |
| 119 |
|
| 120 |
full_timeline = record do |
| 121 |
thread = Thread.new do |
| 122 |
queue1 << true |
| 123 |
queue2.pop |
| 124 |
end |
| 125 |
|
| 126 |
queue1.pop |
| 127 |
queue2 << true |
| 128 |
thread.join |
| 129 |
end |
| 130 |
|
| 131 |
timeline = timeline_for(thread, full_timeline) |
| 132 |
assert_consistent_timeline(timeline) |
| 133 |
assert_equal %i(started ready resumed suspended ready resumed suspended exited), timeline |
| 134 |
end |
| 135 |
|
| 136 |
def test_blocking_on_ractor |
| 137 |
assert_ractor(<<-"RUBY", require_relative: "helper", require: "-test-/thread/instrumentation") |
| 138 |
include ThreadInstrumentation::TestHelper |
| 139 |
|
| 140 |
ractor = Ractor.new { |
| 141 |
Ractor.receive # wait until woke |
| 142 |
Thread.current |
| 143 |
} |
| 144 |
|
| 145 |
# Wait for the main thread to block, then wake the ractor |
| 146 |
Thread.new do |
| 147 |
while Thread.main.status != "sleep" |
| 148 |
Thread.pass |
| 149 |
end |
| 150 |
ractor.send true |
| 151 |
end |
| 152 |
|
| 153 |
full_timeline = record do |
| 154 |
ractor.take |
| 155 |
end |
| 156 |
|
| 157 |
timeline = timeline_for(Thread.current, full_timeline) |
| 158 |
assert_consistent_timeline(timeline) |
| 159 |
assert_equal %i(suspended ready resumed), timeline |
| 160 |
RUBY |
| 161 |
end |
| 162 |
|
| 163 |
def test_sleeping_inside_ractor |
| 164 |
assert_ractor(<<-"RUBY", require_relative: "helper", require: "-test-/thread/instrumentation") |
| 165 |
include ThreadInstrumentation::TestHelper |
| 166 |
|
| 167 |
thread = nil |
| 168 |
|
| 169 |
full_timeline = record do |
| 170 |
thread = Ractor.new{ |
| 171 |
sleep 0.1 |
| 172 |
Thread.current |
| 173 |
}.take |
| 174 |
sleep 0.1 |
| 175 |
end |
| 176 |
|
| 177 |
timeline = timeline_for(thread, full_timeline) |
| 178 |
assert_consistent_timeline(timeline) |
| 179 |
assert_equal %i(started ready resumed suspended ready resumed suspended exited), timeline |
| 180 |
RUBY |
| 181 |
end |
| 182 |
|
| 183 |
def test_thread_blocked_forever_on_mutex |
| 184 |
mutex = Mutex.new |
| 185 |
mutex.lock |
| 186 |
thread = nil |
| 187 |
|
| 188 |
full_timeline = record do |
| 189 |
thread = Thread.new do |
| 190 |
mutex.lock |
| 191 |
end |
| 192 |
10.times { Thread.pass } |
| 193 |
sleep 0.1 |
| 194 |
end |
| 195 |
|
| 196 |
mutex.unlock |
| 197 |
thread.join |
| 198 |
|
| 199 |
timeline = timeline_for(thread, full_timeline) |
| 200 |
assert_consistent_timeline(timeline) |
| 201 |
assert_equal %i(started ready resumed suspended), timeline |
| 202 |
end |
| 203 |
|
| 204 |
def test_thread_blocked_temporarily_on_mutex |
| 205 |
mutex = Mutex.new |
| 206 |
mutex.lock |
| 207 |
thread = nil |
| 208 |
|
| 209 |
full_timeline = record do |
| 210 |
thread = Thread.new do |
| 211 |
mutex.lock |
| 212 |
end |
| 213 |
10.times { Thread.pass } |
| 214 |
sleep 0.1 |
| 215 |
mutex.unlock |
| 216 |
10.times { Thread.pass } |
| 217 |
sleep 0.1 |
| 218 |
end |
| 219 |
|
| 220 |
thread.join |
| 221 |
|
| 222 |
timeline = timeline_for(thread, full_timeline) |
| 223 |
assert_consistent_timeline(timeline) |
| 224 |
assert_equal %i(started ready resumed suspended ready resumed suspended exited), timeline |
| 225 |
end |
| 226 |
|
| 227 |
def test_thread_instrumentation_fork_safe |
| 228 |
skip "No fork()" unless Process.respond_to?(:fork) |
| 229 |
|
| 230 |
thread_statuses = full_timeline = nil |
| 231 |
IO.popen("-") do |read_pipe| |
| 232 |
if read_pipe |
| 233 |
thread_statuses = Marshal.load(read_pipe) |
| 234 |
full_timeline = Marshal.load(read_pipe) |
| 235 |
else |
| 236 |
threads = threaded_cpu_bound_work.each(&:join) |
| 237 |
Marshal.dump(threads.map(&:status), STDOUT) |
| 238 |
full_timeline = Bug::ThreadInstrumentation.unregister_callback.map { |t, e| [t.to_s, e ] } |
| 239 |
Marshal.dump(full_timeline, STDOUT) |
| 240 |
end |
| 241 |
end |
| 242 |
assert_predicate $?, :success? |
| 243 |
|
| 244 |
assert_equal [false] * THREADS_COUNT, thread_statuses |
| 245 |
thread_names = full_timeline.map(&:first).uniq |
| 246 |
thread_names.each do |thread_name| |
| 247 |
assert_consistent_timeline(timeline_for(thread_name, full_timeline)) |
| 248 |
end |
| 249 |
end |
| 250 |
|
| 251 |
def test_thread_instrumentation_unregister |
| 252 |
assert Bug::ThreadInstrumentation::register_and_unregister_callbacks |
| 253 |
end |
| 254 |
|
| 255 |
private |
| 256 |
|
| 257 |
def fib(n = 30) |
| 258 |
return n if n <= 1 |
| 259 |
fib(n-1) + fib(n-2) |
| 260 |
end |
| 261 |
|
| 262 |
def cpu_bound_work(duration) |
| 263 |
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + duration |
| 264 |
i = 0 |
| 265 |
while deadline > Process.clock_gettime(Process::CLOCK_MONOTONIC) |
| 266 |
fib(25) |
| 267 |
i += 1 |
| 268 |
end |
| 269 |
i > 0 ? i : false |
| 270 |
end |
| 271 |
|
| 272 |
def threaded_cpu_bound_work(duration = 0.5) |
| 273 |
THREADS_COUNT.times.map do |
| 274 |
Thread.new do |
| 275 |
cpu_bound_work(duration) |
| 276 |
end |
| 277 |
end |
| 278 |
end |
| 279 |
|
| 280 |
def cleanup_threads |
| 281 |
Thread.list.each do |thread| |
| 282 |
if thread != Thread.current |
| 283 |
thread.kill |
| 284 |
thread.join rescue nil |
| 285 |
end |
| 286 |
end |
| 287 |
assert_equal [Thread.current], Thread.list |
| 288 |
end |
| 289 |
end |