inst-jobs-statsd 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/lib/inst-jobs-statsd.rb +26 -0
  3. data/lib/inst_jobs_statsd/default_tracking.rb +15 -0
  4. data/lib/inst_jobs_statsd/jobs_tracker.rb +25 -0
  5. data/lib/inst_jobs_statsd/naming.rb +49 -0
  6. data/lib/inst_jobs_statsd/stats/counters/failed.rb +28 -0
  7. data/lib/inst_jobs_statsd/stats/counters/orphaned.rb +34 -0
  8. data/lib/inst_jobs_statsd/stats/counters/run.rb +21 -0
  9. data/lib/inst_jobs_statsd/stats/counters.rb +10 -0
  10. data/lib/inst_jobs_statsd/stats/periodic/failed.rb +21 -0
  11. data/lib/inst_jobs_statsd/stats/periodic/queue.rb +49 -0
  12. data/lib/inst_jobs_statsd/stats/periodic/run.rb +38 -0
  13. data/lib/inst_jobs_statsd/stats/periodic.rb +91 -0
  14. data/lib/inst_jobs_statsd/stats/timing/failed.rb +17 -0
  15. data/lib/inst_jobs_statsd/stats/timing/perform.rb +29 -0
  16. data/lib/inst_jobs_statsd/stats/timing/pop.rb +28 -0
  17. data/lib/inst_jobs_statsd/stats/timing.rb +27 -0
  18. data/lib/inst_jobs_statsd/version.rb +3 -0
  19. data/spec/factories/jobs.rb +9 -0
  20. data/spec/factories/workers.rb +9 -0
  21. data/spec/gemfiles/42.gemfile +7 -0
  22. data/spec/gemfiles/42.gemfile.lock +201 -0
  23. data/spec/gemfiles/50.gemfile +7 -0
  24. data/spec/gemfiles/50.gemfile.lock +224 -0
  25. data/spec/gemfiles/51.gemfile +7 -0
  26. data/spec/gemfiles/51.gemfile.lock +224 -0
  27. data/spec/inst_jobs_statsd/jobs_tracker_spec.rb +30 -0
  28. data/spec/inst_jobs_statsd/naming_spec.rb +26 -0
  29. data/spec/inst_jobs_statsd/stats/counters/failed_spec.rb +20 -0
  30. data/spec/inst_jobs_statsd/stats/counters/orphaned_spec.rb +27 -0
  31. data/spec/inst_jobs_statsd/stats/counters/run_spec.rb +27 -0
  32. data/spec/inst_jobs_statsd/stats/periodic/failed_spec.rb +31 -0
  33. data/spec/inst_jobs_statsd/stats/periodic/queue_spec.rb +95 -0
  34. data/spec/inst_jobs_statsd/stats/periodic/run_spec.rb +53 -0
  35. data/spec/inst_jobs_statsd/stats/periodic_spec.rb +63 -0
  36. data/spec/inst_jobs_statsd/stats/timing/failed_spec.rb +25 -0
  37. data/spec/inst_jobs_statsd/stats/timing/perform_spec.rb +35 -0
  38. data/spec/inst_jobs_statsd/stats/timing/pop_spec.rb +34 -0
  39. data/spec/inst_jobs_statsd/stats/timing_spec.rb +35 -0
  40. data/spec/inst_statsd/default_tracking_spec.rb +16 -0
  41. data/spec/matchers.rb +3 -0
  42. data/spec/setup_test_db.rb +41 -0
  43. data/spec/spec_helper.rb +63 -0
  44. metadata +327 -0
@@ -0,0 +1,20 @@
1
+ RSpec.describe InstJobsStatsd::Stats::Counters::Failed do
2
+ describe '.enable' do
3
+ it 'enables all the things' do
4
+ expect(InstJobsStatsd::Stats::Counters::Failed).to receive(:enable_failed_count)
5
+ InstJobsStatsd::Stats::Counters::Failed.enable
6
+ end
7
+ end
8
+
9
+ describe '.report_failed_count' do
10
+ let(:x) { Struct.new(:perform).new(true) }
11
+ it do
12
+ expect(InstStatsd::Statsd).to receive(:count)
13
+ .with(array_including(/\.failed$/), 1, 1)
14
+
15
+ InstJobsStatsd::Stats::Counters::Failed.enable_failed_count
16
+ x.send_later(:perform)
17
+ Delayed::Job.first.fail!
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,27 @@
1
+ RSpec.describe InstJobsStatsd::Stats::Counters::Orphaned do
2
+ describe '.enable' do
3
+ it 'enables all the things' do
4
+ expect(InstJobsStatsd::Stats::Counters::Orphaned).to receive(:enable_orphaned_count)
5
+ InstJobsStatsd::Stats::Counters::Orphaned.enable
6
+ end
7
+ end
8
+
9
+ describe '.report_orphaned_count' do
10
+ let(:x) { Struct.new(:perform).new(true) }
11
+
12
+ before do
13
+ Delayed::Worker.lifecycle.reset!
14
+ InstJobsStatsd::Stats::Counters::Orphaned.enable
15
+
16
+ 4.times { x.send_later(:perform) }
17
+ Delayed::Job.order(:id).limit(3)
18
+ .update_all(locked_by: 'test', locked_at: Delayed::Job.db_time_now)
19
+ end
20
+
21
+ it do
22
+ expect(InstStatsd::Statsd).to receive(:count)
23
+ .with(array_including(/\.orphaned$/), 2, 1)
24
+ Delayed::Worker.lifecycle.run_callbacks(:perform, nil, Delayed::Job.first) {}
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ RSpec.describe InstJobsStatsd::Stats::Counters::Run do
2
+ describe '.enable' do
3
+ it 'enables all the things' do
4
+ expect(InstJobsStatsd::Stats::Counters::Run).to receive(:enable_run_count)
5
+ InstJobsStatsd::Stats::Counters::Run.enable
6
+ end
7
+ end
8
+
9
+ describe '.report_run_count' do
10
+ let(:x) { Struct.new(:perform).new(true) }
11
+
12
+ before do
13
+ Delayed::Worker.lifecycle.reset!
14
+ InstJobsStatsd::Stats::Counters::Run.enable
15
+
16
+ 2.times { x.send_later(:perform) }
17
+ end
18
+
19
+ it do
20
+ expect(InstStatsd::Statsd).to receive(:count)
21
+ .twice.with(array_including(/\.run$/), 1, 1)
22
+ Delayed::Job.all.each do |job|
23
+ Delayed::Worker.lifecycle.run_callbacks(:perform, {}, job) {}
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,31 @@
1
+ RSpec.describe InstJobsStatsd::Stats::Periodic::Failed do
2
+ before do
3
+ InstJobsStatsd::Stats::Periodic.instance_variable_set(:@instance, nil)
4
+ end
5
+
6
+ describe '.enable' do
7
+ it 'enables all the things' do
8
+ expect(InstJobsStatsd::Stats::Periodic::Failed).to receive(:enable_failed_depth)
9
+ InstJobsStatsd::Stats::Periodic::Failed.enable
10
+ end
11
+ end
12
+
13
+ describe '.report_failed_depth' do
14
+ let(:x) { Struct.new(:perform).new(true) }
15
+ let(:now) { Delayed::Job.db_time_now }
16
+
17
+ before do
18
+ InstJobsStatsd::Stats::Periodic.enable_callbacks
19
+ InstJobsStatsd::Stats::Periodic::Failed.enable_failed_depth
20
+
21
+ x.send_later(:perform)
22
+ Delayed::Job.first.fail!
23
+ end
24
+
25
+ it do
26
+ expect(InstStatsd::Statsd).to receive(:gauge)
27
+ .with(array_including(/\.failed_depth$/), 1, 1)
28
+ InstJobsStatsd::Stats::Periodic::Failed.report_failed_depth
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,95 @@
1
+ RSpec.describe InstJobsStatsd::Stats::Periodic::Queue do
2
+ before do
3
+ InstJobsStatsd::Stats::Periodic.instance_variable_set(:@instance, nil)
4
+ end
5
+
6
+ describe '.enable' do
7
+ it 'enables all the things' do
8
+ expect(InstJobsStatsd::Stats::Periodic::Queue).to receive(:enable_queue_depth)
9
+ expect(InstJobsStatsd::Stats::Periodic::Queue).to receive(:enable_queue_age)
10
+ InstJobsStatsd::Stats::Periodic::Queue.enable
11
+ end
12
+ end
13
+
14
+ describe '.report_queue_depth' do
15
+ let(:x) { Struct.new(:perform).new(true) }
16
+ let(:now) { Delayed::Job.db_time_now }
17
+
18
+ before do
19
+ InstJobsStatsd::Stats::Periodic::Queue.enable_queue_depth
20
+
21
+ x.send_later(:perform)
22
+ Delayed::Job.first.update(locked_at: Delayed::Job.db_time_now, locked_by: 'test')
23
+
24
+ x.send_later(:perform)
25
+ x.send_later_enqueue_args(:perform, run_at: now + 1.minute)
26
+ x.send_later_enqueue_args(:perform, run_at: now + 10.minutes)
27
+ end
28
+
29
+ it do
30
+ expect(InstStatsd::Statsd).to receive(:gauge)
31
+ .with(array_including(/\.queue_depth$/), 1, 1)
32
+ InstJobsStatsd::Stats::Periodic::Queue.report_queue_depth
33
+ end
34
+
35
+ it do
36
+ expect(InstStatsd::Statsd).to receive(:gauge)
37
+ .with(array_including(/\.queue_depth$/), 2, 1)
38
+ Timecop.freeze(2.minutes.from_now) do
39
+ InstJobsStatsd::Stats::Periodic::Queue.report_queue_depth
40
+ end
41
+ end
42
+
43
+ it do
44
+ expect(InstStatsd::Statsd).to receive(:gauge)
45
+ .with(array_including(/\.queue_depth$/), 3, 1)
46
+ Timecop.freeze(20.minutes.from_now) do
47
+ InstJobsStatsd::Stats::Periodic::Queue.report_queue_depth
48
+ end
49
+ end
50
+ end
51
+
52
+ describe '.report_queue_age' do
53
+ let(:x) { Struct.new(:perform).new(true) }
54
+ let(:now) { Delayed::Job.db_time_now }
55
+
56
+ before do
57
+ InstJobsStatsd::Stats::Periodic::Queue.enable_queue_age
58
+
59
+ x.send_later(:perform)
60
+ Delayed::Job.first.update(locked_at: Delayed::Job.db_time_now, locked_by: 'test')
61
+
62
+ x.send_later(:perform)
63
+ x.send_later_enqueue_args(:perform, run_at: now + 1.minute)
64
+ x.send_later_enqueue_args(:perform, run_at: now + 10.minutes)
65
+ end
66
+
67
+ it do
68
+ expect(InstStatsd::Statsd).to receive(:gauge)
69
+ .ordered.with(array_including(/\.queue_age_total$/), number_near(0), 1)
70
+ expect(InstStatsd::Statsd).to receive(:gauge)
71
+ .ordered.with(array_including(/\.queue_age_max$/), number_near(0), 1)
72
+ InstJobsStatsd::Stats::Periodic::Queue.report_queue_age
73
+ end
74
+
75
+ it do
76
+ expect(InstStatsd::Statsd).to receive(:gauge)
77
+ .ordered.with(array_including(/\.queue_age_total$/), number_near(180), 1)
78
+ expect(InstStatsd::Statsd).to receive(:gauge)
79
+ .ordered.with(array_including(/\.queue_age_max$/), number_near(120), 1)
80
+ Timecop.freeze(2.minutes.from_now) do
81
+ InstJobsStatsd::Stats::Periodic::Queue.report_queue_age
82
+ end
83
+ end
84
+
85
+ it do
86
+ expect(InstStatsd::Statsd).to receive(:gauge)
87
+ .ordered.with(array_including(/\.queue_age_total$/), number_near(2940), 1)
88
+ expect(InstStatsd::Statsd).to receive(:gauge)
89
+ .ordered.with(array_including(/\.queue_age_max$/), number_near(1200), 1)
90
+ Timecop.freeze(20.minutes.from_now) do
91
+ InstJobsStatsd::Stats::Periodic::Queue.report_queue_age
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,53 @@
1
+ RSpec.describe InstJobsStatsd::Stats::Periodic::Run do
2
+ before do
3
+ InstJobsStatsd::Stats::Periodic.instance_variable_set(:@instance, nil)
4
+ end
5
+
6
+ describe '.enable' do
7
+ it 'enables all the things' do
8
+ expect(InstJobsStatsd::Stats::Periodic::Run).to receive(:enable_run_depth)
9
+ expect(InstJobsStatsd::Stats::Periodic::Run).to receive(:enable_run_age)
10
+ InstJobsStatsd::Stats::Periodic::Run.enable
11
+ end
12
+ end
13
+
14
+ describe '.report_run_depth' do
15
+ let(:x) { Struct.new(:perform).new(true) }
16
+ let(:now) { Delayed::Job.db_time_now }
17
+
18
+ before do
19
+ InstJobsStatsd::Stats::Periodic::Run.enable_run_depth
20
+
21
+ x.send_later(:perform)
22
+ x.send_later(:perform)
23
+ Delayed::Job.first.update(locked_at: Delayed::Job.db_time_now, locked_by: 'test')
24
+ end
25
+
26
+ it do
27
+ expect(InstStatsd::Statsd).to receive(:gauge)
28
+ .with(array_including(/\.run_depth$/), 1, 1)
29
+ InstJobsStatsd::Stats::Periodic::Run.report_run_depth
30
+ end
31
+ end
32
+
33
+ describe '.report_run_age' do
34
+ let(:x) { Struct.new(:perform).new(true) }
35
+ let(:now) { Delayed::Job.db_time_now }
36
+
37
+ before do
38
+ InstJobsStatsd::Stats::Periodic::Run.enable_run_age
39
+
40
+ x.send_later(:perform)
41
+ x.send_later(:perform)
42
+ Delayed::Job.first.update(locked_at: now, locked_by: 'test')
43
+ end
44
+
45
+ it do
46
+ expect(InstStatsd::Statsd).to receive(:gauge)
47
+ .ordered.with(array_including(/\.run_age_total$/), number_near(0), 1)
48
+ expect(InstStatsd::Statsd).to receive(:gauge)
49
+ .ordered.with(array_including(/\.run_age_max$/), number_near(0), 1)
50
+ InstJobsStatsd::Stats::Periodic::Run.report_run_age
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,63 @@
1
+ RSpec.describe InstJobsStatsd::Stats::Periodic do
2
+ before(:each) do
3
+ Delayed::Worker.lifecycle.reset!
4
+ InstJobsStatsd::Stats::Periodic.instance_variable_set(:@instance, nil)
5
+ end
6
+
7
+ describe '.enable_callbacks' do
8
+ it 'only happens once' do
9
+ expect(InstJobsStatsd::Stats::Periodic::Callbacks).to receive(:new).once.and_call_original
10
+ 2.times { InstJobsStatsd::Stats::Periodic.enable_callbacks }
11
+ end
12
+ end
13
+
14
+ describe '.add' do
15
+ it 'does nothing if not enabled' do
16
+ expect_any_instance_of(InstJobsStatsd::Stats::Periodic::Callbacks).not_to receive(:add)
17
+ InstJobsStatsd::Stats::Periodic.add(-> {})
18
+ end
19
+
20
+ it 'does something if enabled' do
21
+ expect_any_instance_of(InstJobsStatsd::Stats::Periodic::Callbacks)
22
+ .to receive(:add).and_call_original
23
+ InstJobsStatsd::Stats::Periodic.enable_callbacks
24
+ InstJobsStatsd::Stats::Periodic.add(-> {})
25
+ end
26
+ end
27
+
28
+ describe 'Callbacks' do
29
+ before(:each) do
30
+ InstJobsStatsd::Stats::Periodic.instance_variable_set(:@instance, nil)
31
+ end
32
+
33
+ it 'calls the procs at the interval' do
34
+ InstJobsStatsd::Stats::Periodic.enable_callbacks
35
+
36
+ @count = 0
37
+ InstJobsStatsd::Stats::Periodic.add(-> { @count += 1 })
38
+ InstJobsStatsd::Stats::Periodic.add(-> { @count += 2 })
39
+
40
+ Timecop.freeze(Delayed::Job.db_time_now + 60.seconds) do
41
+ Delayed::Worker.lifecycle.run_callbacks(:work_queue_pop, nil, nil) {}
42
+ end
43
+
44
+ expect(@count).to eq 3
45
+ end
46
+ end
47
+
48
+ describe 'Timer' do
49
+ before(:each) { @did_block = false }
50
+
51
+ it 'does not call the block too soon' do
52
+ t = InstJobsStatsd::Stats::Periodic::Timer.new(60)
53
+ expect { t.tick { @did_block = true } }.not_to(change { @did_block })
54
+ end
55
+
56
+ it 'calls the block after the interval elapses' do
57
+ t = InstJobsStatsd::Stats::Periodic::Timer.new(60)
58
+ Timecop.freeze(Delayed::Job.db_time_now + 60.seconds) do
59
+ expect { t.tick { @did_block = true } }.to(change { @did_block })
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,25 @@
1
+ RSpec.describe InstJobsStatsd::Stats::Timing::Failed do
2
+ let(:worker) { build :worker }
3
+ let(:job) { build :job, run_at: 1.minute.ago }
4
+
5
+ before(:each) { Delayed::Worker.lifecycle.reset! }
6
+
7
+ describe '.enable' do
8
+ it 'enables all the things' do
9
+ expect(InstJobsStatsd::Stats::Timing::Failed).to receive(:enable_failure_timing)
10
+ InstJobsStatsd::Stats::Timing::Failed.enable
11
+ end
12
+ end
13
+
14
+ describe '.enable_failure_timing' do
15
+ it 'reports failure time' do
16
+ expect(InstStatsd::Statsd).to receive(:timing)
17
+ .once.with(array_including(/\.failed_after$/), any_args)
18
+ InstJobsStatsd::Stats::Timing::Failed.enable_failure_timing
19
+ Delayed::Worker.lifecycle.run_callbacks(
20
+ :error, worker, job, Exception.new('test')
21
+ ) { @in_block = true }
22
+ expect(@in_block).to be_truthy
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,35 @@
1
+ RSpec.describe InstJobsStatsd::Stats::Timing::Perform do
2
+ let(:worker) { build :worker }
3
+ let(:job) { build :job, run_at: 1.minute.ago }
4
+
5
+ before(:each) { Delayed::Worker.lifecycle.reset! }
6
+
7
+ describe '.enable' do
8
+ it 'enables all the things' do
9
+ expect(InstJobsStatsd::Stats::Timing::Perform).to receive(:enable_batching)
10
+ expect(InstJobsStatsd::Stats::Timing::Perform).to receive(:enable_perform_timing)
11
+ InstJobsStatsd::Stats::Timing::Perform.enable
12
+ end
13
+ end
14
+
15
+ describe '.enable_batching' do
16
+ it 'does batching' do
17
+ expect(InstStatsd::Statsd).to receive(:batch).and_call_original
18
+ InstJobsStatsd::Stats::Timing::Perform.enable_batching
19
+ Delayed::Worker.lifecycle.run_callbacks(:perform, worker, job) { @in_block = true }
20
+ expect(@in_block).to be_truthy
21
+ end
22
+ end
23
+
24
+ describe '.enable_perform_timing' do
25
+ it 'reports queu and perform time' do
26
+ expect(InstStatsd::Statsd).to receive(:timing)
27
+ .once.ordered.with(array_including(/\.queue$/), any_args)
28
+ expect(InstStatsd::Statsd).to receive(:timing)
29
+ .once.ordered.with(array_including(/\.perform$/), any_args)
30
+ InstJobsStatsd::Stats::Timing::Perform.enable_perform_timing
31
+ Delayed::Worker.lifecycle.run_callbacks(:perform, worker, job) { @in_block = true }
32
+ expect(@in_block).to be_truthy
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,34 @@
1
+ RSpec.describe InstJobsStatsd::Stats::Timing::Pop do
2
+ let(:worker) { build :worker }
3
+ let(:job) { build :job, run_at: 1.minute.ago }
4
+
5
+ before(:each) { Delayed::Worker.lifecycle.reset! }
6
+
7
+ describe '.enable' do
8
+ it 'enables all the things' do
9
+ expect(InstJobsStatsd::Stats::Timing::Pop).to receive(:enable_pop_timing)
10
+ expect(InstJobsStatsd::Stats::Timing::Pop).to receive(:enable_workqueue_pop_timing)
11
+ InstJobsStatsd::Stats::Timing::Pop.enable
12
+ end
13
+ end
14
+
15
+ describe '.enable_pop_timing' do
16
+ it 'reports pop time' do
17
+ expect(InstStatsd::Statsd).to receive(:timing)
18
+ .once.ordered.with(array_including(/\.pop$/), any_args)
19
+ InstJobsStatsd::Stats::Timing::Pop.enable_pop_timing
20
+ Delayed::Worker.lifecycle.run_callbacks(:pop, worker) { @in_block = true }
21
+ expect(@in_block).to be_truthy
22
+ end
23
+ end
24
+
25
+ describe '.enable_workqueue_pop_timing' do
26
+ it 'reports pop time' do
27
+ expect(InstStatsd::Statsd).to receive(:timing)
28
+ .once.ordered.with(array_including(/\.workqueuepop$/), any_args)
29
+ InstJobsStatsd::Stats::Timing::Pop.enable_workqueue_pop_timing
30
+ Delayed::Worker.lifecycle.run_callbacks(:work_queue_pop, worker, {}) { @in_block = true }
31
+ expect(@in_block).to be_truthy
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,35 @@
1
+ RSpec.describe InstJobsStatsd::Stats::Timing do
2
+ let(:job) { build :job, run_at: 1.minute.ago }
3
+
4
+ describe 'report_tinming' do
5
+ context 'with a block' do
6
+ it 'reports the elapsed time' do
7
+ expect(InstStatsd::Statsd).to receive(:time)
8
+ InstJobsStatsd::Stats::Timing.report_timing('test', job: job) { sleep 0.001 }
9
+ end
10
+ end
11
+
12
+ context 'without a block' do
13
+ it 'reports the given time' do
14
+ expect(InstStatsd::Statsd).to receive(:timing)
15
+ InstJobsStatsd::Stats::Timing.report_timing('test', job: job, timing: 123)
16
+ end
17
+ end
18
+ end
19
+
20
+ describe 'report_job_timing_queued' do
21
+ it 'reports the stat' do
22
+ expect(InstStatsd::Statsd).to receive(:timing)
23
+ .with(array_including(/\.queue$/), any_args)
24
+ InstJobsStatsd::Stats::Timing.report_job_timing_queued(job)
25
+ end
26
+ end
27
+
28
+ describe 'report_job_timing_failed' do
29
+ it 'reports the stat' do
30
+ expect(InstStatsd::Statsd).to receive(:timing)
31
+ .with(array_including(/\.failed_after$/), any_args)
32
+ InstJobsStatsd::Stats::Timing.report_job_timing_failed(job)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,16 @@
1
+ RSpec.describe InstStatsd::DefaultTracking do
2
+ it 'includes methods defined in the base gem' do
3
+ expect(InstStatsd::DefaultTracking.respond_to?(:track_sql)).to be_truthy
4
+ end
5
+
6
+ describe 'track_job_stats' do
7
+ it 'defines :track_jobs' do
8
+ expect(InstStatsd::DefaultTracking.respond_to?(:track_jobs)).to be_truthy
9
+ end
10
+
11
+ it 'only does it once' do
12
+ expect(InstJobsStatsd::JobsTracker).to receive(:new).once.and_call_original
13
+ 2.times { InstStatsd::DefaultTracking.track_jobs }
14
+ end
15
+ end
16
+ end
data/spec/matchers.rb ADDED
@@ -0,0 +1,3 @@
1
+ RSpec::Matchers.define :number_near do |target, max_delta = 0.5|
2
+ match { |actual| (actual - target).abs <= max_delta }
3
+ end
@@ -0,0 +1,41 @@
1
+ # All the below is essentially copied from inst-jobs spec_helper.rb,
2
+ # to create the test db, run migrations, etc
3
+ ENV['TEST_ENV_NUMBER'] ||= '1'
4
+ ENV['TEST_DB_HOST'] ||= 'localhost'
5
+ ENV['TEST_DB_DATABASE'] ||= "inst-jobs-test-#{ENV['TEST_ENV_NUMBER']}"
6
+ ENV['TEST_REDIS_CONNECTION'] ||= 'redis://localhost:6379/'
7
+
8
+ Delayed::Backend::Redis::Job.redis = Redis.new(url: ENV['TEST_REDIS_CONNECTION'])
9
+ Delayed::Backend::Redis::Job.redis.select ENV['TEST_ENV_NUMBER']
10
+
11
+ connection_config = {
12
+ adapter: :postgresql,
13
+ host: ENV['TEST_DB_HOST'].presence,
14
+ encoding: 'utf8',
15
+ username: ENV['TEST_DB_USERNAME'],
16
+ database: ENV['TEST_DB_DATABASE']
17
+ }
18
+
19
+ if ::Rails.version < '5'
20
+ class ActiveRecord::Migration
21
+ class << self
22
+ def [](_version); self; end
23
+ end
24
+ end
25
+ end
26
+
27
+ # create the test db if it does not exist, to help out wwtd
28
+ ActiveRecord::Base.establish_connection(connection_config.merge(database: 'postgres'))
29
+ begin
30
+ ActiveRecord::Base.connection.create_database(connection_config[:database])
31
+ rescue ActiveRecord::StatementInvalid
32
+ end
33
+ ActiveRecord::Base.establish_connection(connection_config)
34
+ # TODO reset db and migrate again, to test migrations
35
+
36
+ # Apply the migrations from the inst-jobs gem
37
+ inst_jobs_spec = Gem::Specification.find_by_name('inst-jobs')
38
+ ActiveRecord::Migrator.migrate(inst_jobs_spec.gem_dir + '/db/migrate')
39
+ ActiveRecord::Migrator.migrate(inst_jobs_spec.gem_dir + '/spec/migrate')
40
+ Delayed::Backend::ActiveRecord::Job.reset_column_information
41
+ Delayed::Backend::ActiveRecord::Job::Failed.reset_column_information
@@ -0,0 +1,63 @@
1
+ if /^2\.4/ =~ RUBY_VERSION && /50\./ =~ ENV['BUNDLE_GEMFILE'] # Limit coverage to one build
2
+ require 'simplecov'
3
+
4
+ SimpleCov.start do
5
+ add_filter 'lib/inst_jobs_statsd/version.rb'
6
+ add_filter 'spec'
7
+ track_files 'lib/**/*.rb'
8
+ end
9
+
10
+ SimpleCov.minimum_coverage(100)
11
+ end
12
+
13
+ require 'inst-jobs-statsd'
14
+ require 'delayed/testing'
15
+
16
+ require 'byebug'
17
+ require 'database_cleaner'
18
+ require 'factory_girl'
19
+ require 'pry'
20
+ require 'timecop'
21
+
22
+ # No reason to add default sleep time to specs:
23
+ Delayed::Settings.sleep_delay = 0
24
+ Delayed::Settings.sleep_delay_stagger = 0
25
+
26
+ RSpec.configure do |config|
27
+ config.expect_with(:rspec) do |c|
28
+ c.syntax = %i[should expect]
29
+ end
30
+
31
+ config.include FactoryGirl::Syntax::Methods
32
+ config.before(:suite) do
33
+ FactoryGirl.find_definitions
34
+ end
35
+
36
+ config.before(:suite) do
37
+ DatabaseCleaner.strategy = :transaction
38
+ DatabaseCleaner.clean_with(:truncation)
39
+ end
40
+
41
+ config.before(:each) do |example|
42
+ DatabaseCleaner.strategy = (example.metadata[:sinatra] ? :truncation : :transaction)
43
+ DatabaseCleaner.start
44
+ end
45
+
46
+ config.after(:each) do
47
+ DatabaseCleaner.clean
48
+ end
49
+
50
+ # Seed global randomization in this process using the `--seed` CLI option.
51
+ # Setting this allows you to use `--seed` to deterministically reproduce
52
+ # test failures related to randomization by passing the same `--seed` value
53
+ # as the one that triggered the failure.
54
+ config.order = :random
55
+ Kernel.srand config.seed
56
+ end
57
+
58
+ require_relative 'setup_test_db'
59
+
60
+ Time.zone = 'UTC'
61
+ Rails.logger = Logger.new(nil)
62
+
63
+ require_relative 'matchers'