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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1e72cd2a275c0cc94bcac05463b285062788c4bd
4
- data.tar.gz: 37d0d9082bca7164f370fca2f1e8c7ec057db9f8
3
+ metadata.gz: 864b05c24ba407f0763f65f7a07e1af3d9678efd
4
+ data.tar.gz: c83f33b9c2ea06fa1783ba51b51b07948920e359
5
5
  SHA512:
6
- metadata.gz: 4f4f468799a33bea2755f2bf99ff971940197296c3ace39a4a55737c8d2f06c5250f1a27c8d1e1d33ea5ba72eba143f0f1901a4d7fdc844ff0ab4c3b582c1064
7
- data.tar.gz: 46f254797c31d50e690c36d6c388b79053997e5e0e0f59af72f1c59938965c4369a975c4b33f552e14e6c70407a2e595dcaa97ec64d6cec3c23e25d57fe01c0c
6
+ metadata.gz: cb206f8de354b6192c0bb5153cedda328b9d928abedf86423bcdc1e601c720bdb312c87b9bde65353d73a03f8f1a0ccc7384ffe80c60e837c7be53c443751a48
7
+ data.tar.gz: ca0f60b71576b34b325cf7bc2a3c94a793f1c52ede65fe9ef79914414417303c59a54d5a7c04828c333fdda89c5be4d17e37a4b85f0cdaa17b4c3d576fd5ad72
@@ -1,3 +1,3 @@
1
1
  module Threasy
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -46,6 +46,10 @@ module Threasy
46
46
  worker.work
47
47
  end
48
48
 
49
+ def clear
50
+ queue.clear
51
+ end
52
+
49
53
  def log(msg)
50
54
  Threasy.logger.debug msg
51
55
  end
@@ -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
@@ -19,4 +19,12 @@ describe Threasy::Schedule::Entry do
19
19
  end
20
20
  end
21
21
 
22
+ describe "#at" do
23
+ it "should default to now + every interval" do
24
+ Timecop.freeze do
25
+ expect(subject.new(nil, every: 0.1).at).to eq(Time.now + 0.1)
26
+ end
27
+ end
28
+ end
29
+
22
30
  end
@@ -1,76 +1,50 @@
1
1
  describe "Threasy::Schedule" do
2
2
  let(:job){ double("job") }
3
- let(:work){ double("work") }
4
- subject{ Threasy::Schedule.new(work) }
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
- expect(work).to receive(:enqueue).with(job)
9
- subject.add(job, in: 0.1)
10
- sleep 0.3
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
- expect(work).to receive(:enqueue).with(job)
15
- subject.add(job, at: Time.now + 0.1)
16
- sleep 0.3
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
- expect(work).to receive(:enqueue).with(job).at_least(:twice)
21
- subject.add(job, every: 0.1, in: 0.1)
22
- sleep 0.5
23
- end
24
-
25
- it "should allow blocks to be processed on schedule" do
26
- block_job = ->{ 1 + 1 }
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
- expect(work).to receive(:enqueue).with(job).at_least(:once)
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
- subject{ Threasy::Schedule.new }
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
- Threasy.config.max_sleep = 0.1
65
- job_ran = false
66
- job = -> { job_ran = true }
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
@@ -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
- before :each do
6
- @work = Threasy::Work.new
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(@work.respond_to?(:enqueue)).to eq(true)
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
- job = double("job")
16
- expect(job).to receive(:perform).once
17
- @work.enqueue job
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
- i = 0
23
- @work.enqueue do
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
- expect(TestJob).to receive(:perform).once
34
- @work.enqueue "TestJob"
35
- sleep 0.1
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: threasy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carl Zulauf