Skip to content

Commit

Permalink
Merge pull request #254 from QueueClassic/mini_test
Browse files Browse the repository at this point in the history
Give some <3 to our unit tests
  • Loading branch information
senny committed Feb 19, 2015
2 parents 6cb9734 + 32f0ec7 commit 7d44daa
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 31 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Expand Up @@ -2,7 +2,10 @@ language: ruby
before_script:
- psql -c 'create database queue_classic_test;' -U postgres
env:
- QC_DATABASE_URL="postgres://postgres@localhost/queue_classic_test"
global:
- QC_DATABASE_URL="postgres://postgres@localhost/queue_classic_test"
- QC_BENCHMARK=true
- QC_BENCHMARK_MAX_TIME_DEQUEUE=60
rvm:
- 2.2
- 2.1
Expand Down
3 changes: 1 addition & 2 deletions Gemfile
Expand Up @@ -5,6 +5,5 @@ gem "rake"
gemspec

group :test do
gem "turn"
gem 'minitest', '~> 5.3.1'
gem 'minitest', '~> 5.5.1'
end
1 change: 0 additions & 1 deletion Rakefile
Expand Up @@ -11,5 +11,4 @@ Rake::TestTask.new do |t|
t.test_files = FileList['test/**/*_test.rb']
t.verbose = true
t.warning = true
t.ruby_opts << "-rubygems" if RUBY_VERSION < "1.9"
end
23 changes: 12 additions & 11 deletions test/benchmark_test.rb
Expand Up @@ -2,36 +2,37 @@

if ENV["QC_BENCHMARK"]
class BenchmarkTest < QCTest
BENCHMARK_SIZE = Integer(ENV.fetch("QC_BENCHMARK_SIZE", 10_000))
BENCHMARK_MAX_TIME_DEQUEUE = Integer(ENV.fetch("QC_BENCHMARK_MAX_TIME_DEQUEUE", 30))
BENCHMARK_MAX_TIME_ENQUEUE = Integer(ENV.fetch("QC_BENCHMARK_MAX_TIME_ENQUEUE", 5))

def test_enqueue
n = 10_000
start = Time.now
n.times do
QC.enqueue("1.odd?", [])
BENCHMARK_SIZE.times do
QC.enqueue("1.odd?")
end
assert_equal(n, QC.count)
assert_equal(BENCHMARK_SIZE, QC.count)

elapsed = Time.now - start
assert_in_delta(4, elapsed, 1)
assert_operator(elapsed, :<, BENCHMARK_MAX_TIME_ENQUEUE)
end

def test_dequeue
worker = QC::Worker.new
worker.running = true
n = 10_000
n.times do
QC.enqueue("1.odd?", [])
BENCHMARK_SIZE.times do
QC.enqueue("1.odd?")
end
assert_equal(n, QC.count)
assert_equal(BENCHMARK_SIZE, QC.count)

start = Time.now
n.times do
BENCHMARK_SIZE.times do
worker.work
end
elapsed = Time.now - start

assert_equal(0, QC.count)
assert_in_delta(10, elapsed, 3)
assert_operator(elapsed, :<, BENCHMARK_MAX_TIME_DEQUEUE)
end

end
Expand Down
12 changes: 0 additions & 12 deletions test/config_test.rb
Expand Up @@ -118,16 +118,4 @@ def test_assign_default_worker_class
ensure
QC.default_worker_class = original_worker
end

private
def with_env(temporary_environment)
original_environment = {}
temporary_environment.each do |name, value|
original_environment[name] = ENV[name]
ENV[name] = value
end
yield
ensure
original_environment.each { |name, value| ENV[name] = value }
end
end
10 changes: 10 additions & 0 deletions test/helper.rb
Expand Up @@ -51,4 +51,14 @@ def capture_debug_output
$stdout = original_stdout
end

def with_env(temporary_environment)
original_environment = {}
temporary_environment.each do |name, value|
original_environment[name] = ENV[name]
ENV[name] = value
end
yield
ensure
original_environment.each { |name, value| ENV[name] = value }
end
end
8 changes: 4 additions & 4 deletions test/lib/queue_classic_rails_connection_test.rb
Expand Up @@ -22,10 +22,10 @@ def test_uses_active_record_connection_if_exists
end

def test_does_not_use_active_record_connection_if_env_var_set
ENV['QC_RAILS_DATABASE'] = 'false'
connection = get_connection
assert_raises(MockExpectationError) { connection.verify }
ENV['QC_RAILS_DATABASE'] = 'true'
with_env 'QC_RAILS_DATABASE' => 'false' do
connection = get_connection
assert_raises(MockExpectationError) { connection.verify }
end
end

private
Expand Down

0 comments on commit 7d44daa

Please sign in to comment.