-
Notifications
You must be signed in to change notification settings - Fork 416
/
Copy pathexample_group_extensions.rb
67 lines (58 loc) · 1.5 KB
/
example_group_extensions.rb
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
require 'rbconfig'
module Concurrent
module TestHelpers
extend self
def pool_termination_timeout
5
end
def delta(v1, v2)
if block_given?
v1 = yield(v1)
v2 = yield(v2)
end
return (v1 - v2).abs
end
def monotonic_interval
raise ArgumentError.new('no block given') unless block_given?
start_time = Concurrent.monotonic_time
yield
Concurrent.monotonic_time - start_time
end
def in_fiber(&block)
Fiber.new(&block)
end
def in_thread(*arguments, &block)
@created_threads ||= Queue.new
new_thread = Thread.new(*arguments) do |*args, &b|
Thread.abort_on_exception = true
block.call(*args, &b)
end
@created_threads << new_thread
new_thread
end
def is_sleeping(thread)
expect(in_thread { Thread.pass until thread.status == 'sleep' }.join(1)).not_to eq nil
end
def repeat_until_success(timeout = 5, &test)
start_time = Concurrent.monotonic_time
last_exception = nil
while Concurrent.monotonic_time - start_time < timeout
begin
test.call
return true
rescue Exception => e
last_exception = e
Thread.pass
end
end
raise last_exception
end
def join_with(threads, timeout = 5)
threads = Array(threads)
threads.each do |t|
joined_thread = t.join(timeout * threads.size)
expect(joined_thread).not_to eq nil
end
end
end
end