threasy 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/threasy/version.rb +1 -1
- data/lib/threasy/work.rb +4 -0
- data/spec/spec_helper.rb +10 -0
- data/spec/threasy/schedule/entry_spec.rb +8 -0
- data/spec/threasy/schedule_spec.rb +23 -49
- data/spec/threasy/work_spec.rb +20 -18
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 864b05c24ba407f0763f65f7a07e1af3d9678efd
|
4
|
+
data.tar.gz: c83f33b9c2ea06fa1783ba51b51b07948920e359
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb206f8de354b6192c0bb5153cedda328b9d928abedf86423bcdc1e601c720bdb312c87b9bde65353d73a03f8f1a0ccc7384ffe80c60e837c7be53c443751a48
|
7
|
+
data.tar.gz: ca0f60b71576b34b325cf7bc2a3c94a793f1c52ede65fe9ef79914414417303c59a54d5a7c04828c333fdda89c5be4d17e37a4b85f0cdaa17b4c3d576fd5ad72
|
data/lib/threasy/version.rb
CHANGED
data/lib/threasy/work.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -3,4 +3,14 @@ require 'timecop'
|
|
3
3
|
|
4
4
|
require File.join(File.dirname(__FILE__), "..", "lib", "threasy")
|
5
5
|
|
6
|
+
def async(timeout = 10)
|
7
|
+
t = Thread.new do
|
8
|
+
Thread.stop
|
9
|
+
end
|
10
|
+
yield -> { t.wakeup }
|
11
|
+
raise "Example's time limit exceeded" unless t.join(20)
|
12
|
+
end
|
13
|
+
|
14
|
+
Threasy.config.max_sleep = 0.1
|
15
|
+
|
6
16
|
# Threasy.config.logger.level = Logger::DEBUG
|
@@ -1,76 +1,50 @@
|
|
1
1
|
describe "Threasy::Schedule" do
|
2
2
|
let(:job){ double("job") }
|
3
|
-
|
4
|
-
|
3
|
+
subject { Threasy.schedules }
|
4
|
+
|
5
|
+
after(:each) { subject.clear }
|
5
6
|
|
6
7
|
describe "#add" do
|
7
8
|
it "should allow a job to be processed after specified delay" do
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
async do |done|
|
10
|
+
subject.add(-> { done.() }, in: 0.01)
|
11
|
+
end
|
11
12
|
end
|
12
13
|
|
13
14
|
it "should allow a job to be processed at the specified time" do
|
14
|
-
|
15
|
-
|
16
|
-
|
15
|
+
async do |done|
|
16
|
+
subject.add(-> { done.() }, at: Time.now + 0.01)
|
17
|
+
end
|
17
18
|
end
|
18
19
|
|
19
20
|
it "should allow a job to be repeated at the specified interval" do
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
expect(work).to receive(:enqueue).with(block_job)
|
28
|
-
subject.add(in: 0.1, &block_job)
|
29
|
-
sleep 0.3
|
30
|
-
end
|
31
|
-
|
32
|
-
it "should allow string expressions to be processed on schedule" do
|
33
|
-
expect(work).to receive(:enqueue).with("TestScheduledJob")
|
34
|
-
subject.add("TestScheduledJob", in: 0.1)
|
35
|
-
sleep 0.3
|
36
|
-
end
|
37
|
-
|
38
|
-
it "should default first run to now + every_interval" do
|
39
|
-
expect(work).to receive(:enqueue).with(job).at_least(:twice)
|
40
|
-
subject.add(job, every: 0.1)
|
41
|
-
sleep 0.5
|
21
|
+
async do |done|
|
22
|
+
repeats = 0
|
23
|
+
subject.add(every: 0.01, in: 0.01) do
|
24
|
+
repeats += 1
|
25
|
+
done.() if repeats == 2
|
26
|
+
end
|
27
|
+
end
|
42
28
|
end
|
43
29
|
end
|
44
30
|
|
45
31
|
describe "#remove" do
|
46
32
|
it "should be possible to remove a job from the schedule" do
|
47
|
-
|
48
|
-
|
49
|
-
entry = subject.add(job, every: 0.1)
|
50
|
-
sleep 0.3
|
51
|
-
|
33
|
+
entry = subject.add(job, every: 0.01)
|
34
|
+
expect(subject).to receive(:remove_entry).with(entry)
|
52
35
|
entry.remove
|
53
|
-
|
54
|
-
expect(work).not_to receive(:enqueue)
|
55
|
-
sleep 0.3
|
56
36
|
end
|
57
37
|
end
|
58
38
|
|
59
39
|
context "when laptop suspends" do
|
60
|
-
|
61
|
-
let(:hour) { 60*60 }
|
40
|
+
let(:hour) { 60 * 60 }
|
62
41
|
|
63
42
|
it "should recover in a few seconds when time suddenly jumps forward" do
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
subject.add(in: hour + 1, &job)
|
68
|
-
|
69
|
-
Timecop.travel(Time.now + hour) do
|
70
|
-
Timeout.timeout(6) do
|
71
|
-
loop { job_ran ? break : sleep(0.1) }
|
72
|
-
end
|
43
|
+
async do |done|
|
44
|
+
subject.add(-> { done.() }, in: hour + 0.01)
|
45
|
+
Timecop.travel(Time.now + hour)
|
73
46
|
end
|
47
|
+
Timecop.return
|
74
48
|
end
|
75
49
|
|
76
50
|
end
|
data/spec/threasy/work_spec.rb
CHANGED
@@ -1,38 +1,40 @@
|
|
1
1
|
class TestJob
|
2
|
+
def initialize(resolver)
|
3
|
+
@resolver = resolver
|
4
|
+
end
|
5
|
+
|
6
|
+
def perform
|
7
|
+
@resolver.()
|
8
|
+
end
|
2
9
|
end
|
3
10
|
|
4
11
|
describe "Threasy::Work" do
|
5
|
-
|
6
|
-
|
7
|
-
end
|
12
|
+
subject { Threasy::Work.new }
|
13
|
+
after(:each) { subject.clear }
|
8
14
|
|
9
15
|
describe "#enqueue" do
|
10
16
|
it "should have a method for enqueing" do
|
11
|
-
expect(
|
17
|
+
expect(subject.respond_to?(:enqueue)).to eq(true)
|
12
18
|
end
|
13
19
|
|
14
20
|
it "should allow a job object to be enqueued and worked" do
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
sleep 0.1
|
21
|
+
async do |done|
|
22
|
+
subject.enqueue TestJob.new(done)
|
23
|
+
end
|
19
24
|
end
|
20
25
|
|
21
26
|
it "should allow a job block to be enqueued and worked" do
|
22
|
-
|
23
|
-
|
24
|
-
sleep 0.1
|
25
|
-
i += 1
|
27
|
+
async do |done|
|
28
|
+
subject.enqueue { done.() }
|
26
29
|
end
|
27
|
-
expect(i).to eq(0)
|
28
|
-
sleep 0.2
|
29
|
-
expect(i).to eq(1)
|
30
30
|
end
|
31
31
|
|
32
32
|
it "should allow a string expression to be enqueued and worked" do
|
33
|
-
|
34
|
-
|
35
|
-
|
33
|
+
async do |done|
|
34
|
+
expect(TestJob).to receive(:perform).once
|
35
|
+
subject.enqueue "TestJob"
|
36
|
+
subject.enqueue { done.() }
|
37
|
+
end
|
36
38
|
end
|
37
39
|
end
|
38
40
|
end
|