-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathrepeating_task_spec.rb
More file actions
86 lines (78 loc) · 2.06 KB
/
repeating_task_spec.rb
File metadata and controls
86 lines (78 loc) · 2.06 KB
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
require "ldclient-rb/impl/repeating_task"
require "concurrent/atomics"
require "spec_helper"
module LaunchDarkly
module Impl
describe RepeatingTask do
def null_logger
double.as_null_object
end
it "can name the task" do
signal = Concurrent::Event.new
task = RepeatingTask.new(0.01, 0, -> { signal.set }, null_logger, "Junie B.")
expect(task.name).to eq("Junie B.")
task.stop
end
it "does not start when created" do
signal = Concurrent::Event.new
task = RepeatingTask.new(0.01, 0, -> { signal.set }, null_logger, "test")
begin
expect(signal.wait(0.1)).to be false
ensure
task.stop
end
end
it "executes until stopped" do
queue = Queue.new
task = RepeatingTask.new(0.1, 0, -> { queue << Time.now }, null_logger, "test")
begin
last = nil
task.start
3.times do
time = queue.pop
unless last.nil?
expect(time.to_f - last.to_f).to be >= 0.05
end
last = time
end
ensure
task.stop
stopped_time = Time.now
end
no_more_items = false
2.times do
begin
time = queue.pop(true)
expect(time.to_f).to be <= stopped_time.to_f
rescue ThreadError
no_more_items = true
break
end
end
expect(no_more_items).to be true
end
it "can be stopped from within the task" do
counter = 0
stopped = Concurrent::Event.new
task = RepeatingTask.new(0.01, 0,
-> {
counter += 1
if counter >= 2
task.stop
stopped.set
end
},
null_logger, "test")
begin
task.start
expect(stopped.wait(0.1)).to be true
expect(counter).to be 2
sleep(0.1)
expect(counter).to be 2
ensure
task.stop
end
end
end
end
end