Skip to content

Commit

Permalink
Cleanup in tests.
Browse files Browse the repository at this point in the history
- introduced separate task for acceptance tests
- split acceptance scenario into two separate
- moved units to test/unit/ directory
- declare default driver in test user
- sleep in tests if for the weak
  • Loading branch information
mostlyobvious committed Jul 31, 2012
1 parent 8dce8ea commit af0fa50
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 23 deletions.
10 changes: 8 additions & 2 deletions Rakefile
Expand Up @@ -3,9 +3,15 @@
require 'bundler/gem_tasks'
require 'rake/testtask'

Rake::TestTask.new(:test) do |test|
test.pattern = 'test/**/*_test.rb'
Rake::TestTask.new('test:unit') do |test|
test.pattern = 'test/unit/*_test.rb'
test.libs << 'lib' << 'test'
end

Rake::TestTask.new('test:acceptance') do |test|
test.pattern = 'test/acceptance/*_test.rb'
test.libs << 'lib' << 'test'
end

task :test => %w(test:unit test:acceptance)
task :default => :test
1 change: 0 additions & 1 deletion lib/m2r.rb
@@ -1,4 +1,3 @@
require 'rubygems'
require 'ffi-rzmq'
require 'multi_json'
require 'tnetstring'
Expand Down
17 changes: 7 additions & 10 deletions test/acceptance/acceptance_test.rb
@@ -1,24 +1,21 @@
require 'acceptance_helper'
require 'test_helper'

module M2R
class AcceptanceTest < MiniTest::Unit::TestCase
include ProcessHelper

def test_requests
pid = Process.spawn("bundle exec foreman start --procfile=example/Procfile", pgroup: true, out: "/dev/null", err: "/dev/null")
sleep(1)
user = TestUser.new(:driver => :mechanize)
def test_rack_example
user = TestUser.new
user.visit("/handler")
user.see!("SENDER", "PATH", "HEADERS", "x-forwarded-for", "x-forwarded-for", "BODY")
end

def test_handler_example
user = TestUser.new
user.visit("/rack")
assert user.find("pre").text.include?(" {{{{{{ { ( ( ( ( (-----:=")

user.click_on("flip!")
assert user.find("pre").text.include?("=:-----( ( ( ( ( { {{{{{{")
ensure
Process.kill("SIGTERM", pid) if pid
sleep(1)
end
end

end
4 changes: 0 additions & 4 deletions test/acceptance_helper.rb → test/support/capybara.rb
@@ -1,9 +1,5 @@
require 'test_helper'
require 'bbq/test'
require 'support/test_user'
require 'capybara'
require 'capybara/mechanize'

Capybara.app_host = "http://localhost:6767/"
Capybara.current_driver = :mechanize

32 changes: 32 additions & 0 deletions test/support/process_helper.rb
@@ -0,0 +1,32 @@
require 'timeout'

module ProcessHelper
attr_accessor :pid

def setup
Process.spawn("bundle exec foreman start --procfile=example/Procfile", pgroup: true, out: "/dev/null", err: "/dev/null")
self.pid = read_pid_from_file('example/tmp/mongrel2.pid')

This comment has been minimized.

Copy link
@paneq

paneq Aug 1, 2012

Collaborator

If we read pid of mongrel2 instead of taking foreman pid and send SIGTERM to it then what stops handler processes started by foreman ?

mongrel2:     m2sh start -name main
rack_handler: bundle exec rackup -I../lib -s mongrel2 lobster.ru
http_handler: bundle exec ruby -I../lib http_0mq.rb

I can't figure it out.

This comment has been minimized.

Copy link
@paneq

paneq Aug 1, 2012

Collaborator

https://gist.github.com/8ed64408863e95d19c06

Here is output of running the tests without it being nullified. As you can see the handlers are not killed after first test.

This comment has been minimized.

Copy link
@paneq

paneq Aug 1, 2012

Collaborator

https://gist.github.com/d4340f4f59bee76e1ff7

Here is proper output with this changes:

   def setup
     check_mongrel
-    Process.spawn("bundle exec foreman start --procfile=example/Procfile", pgroup: true, out: "/dev/null", err: "/dev/null")
-    self.pid = read_pid_from_file('example/tmp/mongrel2.pid')
+    self.pid = Process.spawn("bundle exec foreman start --procfile=example/Procfile", pgroup: true)
+    sleep 1
   end

   def teardown
     Process.kill("SIGTERM", pid) if pid
+    sleep 1
   end

This comment has been minimized.

Copy link
@paneq

paneq Aug 1, 2012

Collaborator

I know that sleep is bad but on the other hand waiting for mongrel pid might not be the best solution either as we need to ensure that all processes are running and connected to mongrel2 (which is hard to check with zmq as we all know). Also we could just spawn one example handler per one test instead of spawning both and testing only one of them.

This comment has been minimized.

Copy link
@mostlyobvious

mostlyobvious Aug 1, 2012

Author Collaborator

Duh, obviously that's wrong pid for SIGTERM. Besides that i'ts ok to wait for mongrel since it spawns a tcp socket a client is waiting for. That dumbass sleep is working for you but not for me nor CI.

This comment has been minimized.

Copy link
@paneq

paneq Aug 1, 2012

Collaborator

"Besides that i'ts ok to wait for mongrel since it spawns a tcp socket a client is waiting for" - but if we connect to mongrel as client and handler is not connected to mongrel also then I think our request will be lost (that was my point).

This comment has been minimized.

Copy link
@paneq

paneq Aug 1, 2012

Collaborator

But maybe mongrel resends the request that could not be handled after some time but I doubt it.

end

def teardown
Process.kill("SIGTERM", pid) if pid
end

def read_pid_from_file(pidfile)
pid = nil
Timeout.timeout(2) do
loop do
begin
pid = File.read(pidfile)
break unless pid.empty?
rescue Errno::ENOENT
sleep(0.1)
next
end
end
pid.to_i
end
rescue Timeout::Error
raise "Unable to read PID from file #{pidfile}."
end
end
12 changes: 7 additions & 5 deletions test/support/test_user.rb
@@ -1,13 +1,15 @@
require 'bbq/test'
require 'bbq/test_user'

class TestUser < Bbq::TestUser
include MiniTest::Assertions

def initialize
super(:driver => :mechanize)
end

def see!(*args)
args.each do |arg|
assert has_content?(arg), %Q/Expected to see "#{arg}" but not found./
end
msg = "Expected to see %s but not found"
args.each { |arg| assert has_content?(arg), msg % arg }
end
end


4 changes: 3 additions & 1 deletion test/test_helper.rb
@@ -1,5 +1,7 @@
require 'minitest/autorun'
require 'mocha'
require 'm2r'

require 'support/test_handler'
require 'support/test_user'
require 'support/capybara'
require 'support/process_helper'
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit af0fa50

Please sign in to comment.