Skip to content

Commit

Permalink
Fixes some failure and accelerating the Unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ngiger committed Sep 25, 2013
1 parent f452e2e commit 28666c7
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 81 deletions.
13 changes: 7 additions & 6 deletions ext/meddata/src/session.rb
Expand Up @@ -51,14 +51,15 @@ class Session < HttpSession
:product => 'www.medwin.ch',
:refdata => 'www.refdata.ch',
}
attr_accessor :http_path, :form_keys, :detail_key
attr_accessor :http_path, :form_keys, :detail_key, :sleeps_in_seconds # usually 1, but 0.01 while running Tests
def initialize(search_type=:partner, server=SERVERS[search_type])
@http_path = HTTP_PATHS[search_type]
@form_keys = FORM_KEYS[search_type]
@detail_key = DETAIL_KEYS[search_type]
@sleeps_in_seconds = Module.constants.index(:MiniTest) ? 0.01 : 1
super(server)
resp = get '/'
sleep(2) # for Timeout::Error
resp = get '/'
sleep(2*@sleeps_in_seconds) # for Timeout::Error
resp = get @http_path
handle_resp!(resp)
rescue Timeout::Error => err
Expand All @@ -73,7 +74,7 @@ def detail_html(ctl)
rescue Errno::ECONNRESET
if(tries > 0)
tries -= 1
sleep(3 - tries)
sleep(3*@sleeps_in_seconds - tries*@sleeps_in_seconds)
retry
else
raise
Expand Down Expand Up @@ -109,7 +110,7 @@ def get_result_list(criteria)
retries ||= 3
if retries > 0
retries -= 1
sleep 60 # wait a minute for the network to recover
sleep 60*@sleeps_in_seconds # wait a minute for the network to recover
retry
else
raise
Expand All @@ -122,7 +123,7 @@ def get_result_list(criteria)
retries ||= 3
if retries > 0
retries -= 1
sleep 600 # wait 10 minutes for the server to recover
sleep 600*@sleeps_in_seconds # wait 10 minutes for the server to recover
retry
else
raise
Expand Down
161 changes: 86 additions & 75 deletions ext/meddata/test/test_session.rb
Expand Up @@ -3,8 +3,7 @@

$: << File.expand_path('../..', File.dirname(__FILE__))

gem 'minitest'
require 'minitest/autorun'
require 'test/unit'
require 'flexmock'
require 'meddata/src/session'

Expand All @@ -20,7 +19,7 @@ def get(arg)

module ODDB
module MedData
class TestSession <Minitest::Test
class TestSession <Test::Unit::TestCase
include FlexMock::TestCase
def setup
@response = flexmock('response') do |r|
Expand Down Expand Up @@ -68,7 +67,7 @@ def test_post_hash
def test_post_hash__ctl
criteria = {}
expected = [
["__EVENTTARGET", "detail_key:ctl:ctl00"],
["__EVENTTARGET", "detail_key$ctl$ctl00"],
["__EVENTARGUMENT", ""],
["hiddenlang", "de"]
]
Expand All @@ -82,8 +81,7 @@ def test_post_hash__viewstate
["__EVENTTARGET", ""],
["__EVENTARGUMENT", ""],
["btnSearch", "Suche"],
["__VIEWSTATE", "viewstate"],
["hiddenlang", "de"]
["hiddenlang", "de"],
]
assert_equal(expected, @session.post_hash(criteria))
end
Expand All @@ -95,7 +93,6 @@ def test_post_hash__eventvalidation
["__EVENTTARGET", ""],
["__EVENTARGUMENT", ""],
["btnSearch", "Suche"],
["__EVENTVALIDATION", "eventvalidation"],
["hiddenlang", "de"]
]
assert_equal(expected, @session.post_hash(criteria))
Expand All @@ -115,15 +112,17 @@ def test_post_header
expected = [
["Host", @http_server],
["User-Agent",
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; de-de) AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.2 Safari/525.22"],
"Mozilla/5.0 (X11; Linux x86_64; rv:20.0) Gecko/20100101 Firefox/20.0"],
["Accept",
"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,*/*;q=0.1"],
["Accept-Encoding", "gzip, deflate"],
["Accept-Language", "de-ch,en-us;q=0.7,en;q=0.3"],
["Accept-Charset", "UTF-8"],
["Keep-Alive", "300"],
["Connection", "keep-alive"],
["Content-Type", "application/x-www-form-urlencoded"],
["Cookie", "cookie_header"]
["Referer", ""],
["Cookie", "cookie_header"],
]
assert_equal(expected, @session.post_headers)
end
Expand All @@ -147,115 +146,127 @@ def test_get_result_list
assert_equal('body', @session.get_result_list(criteria))
end
end
def test_get_result_list__errno_enetunreach
criteria = {}
def test_detail_html
response = Net::HTTPFound.new(1,2,3)
flexmock(@http,
:post => response,
:get => @response
)
uri = flexmock('uri', :request_uri => 'request_uri')
flexmock(URI, :parse => uri)
flexmock(@response) do |r|
r.should_receive(:body).and_raise(Errno::ENETUNREACH)
stderr_null do
assert_equal('body', @session.detail_html(nil))
end
end
end

class TestSessionException <Test::Unit::TestCase
include FlexMock::TestCase

def stdout_null
require 'tempfile'
$stdout = Tempfile.open('stdout')
yield
$stdout.close
$stdout = STDOUT
end

def stderr_null
require 'tempfile'
$stderr = Tempfile.open('stderr')
yield
$stderr.close
$stderr = STDERR
end

def setup_to_raise_error
response = Net::HTTPFound.new(1,2,3)
@response = flexmock('response') do |r|
r.should_receive(:[]).with('set-cookie').and_return('cookie_header')
r.should_receive(:body).once.and_return('body')
end
@http = flexmock('http',
:post => response,
:get => @response)
flexmock(Net::HTTP, :new => @http)
@http_server = flexmock('http_server')
ODDB::MedData::Session::HTTP_PATHS.store :search_type_test, 'http_path'
ODDB::MedData::Session::FORM_KEYS.store :search_type_test, [ [:name, 'txtSearchName'] ]
ODDB::MedData::Session::DETAIL_KEYS.store :search_type_test, 'detail_key'
uri = flexmock('uri', :request_uri => 'request_uri')
flexmock(URI, :parse => uri)
flexmock(@session,
:sleep => nil,
:flexmock_original_behavior_for_should_receive => nil
)
end

def test_get_result_list__errno_enetunreach
setup_to_raise_error
criteria = {}
stderr_null do
assert_raises(Errno::ENETUNREACH) do
@session = ODDB::MedData::Session.new(:search_type_test, @http_server)
@response.should_receive(:body).and_raise(Errno::ENETUNREACH)
@session.get_result_list(criteria)
end
end
end
end

def test_get_result_list__runtime_error
setup_to_raise_error
criteria = {}
response = Net::HTTPFound.new(1,2,3)
flexmock(@http,
:post => response,
:get => @response
)
uri = flexmock('uri', :request_uri => 'request_uri')
flexmock(URI, :parse => uri)
flexmock(@response) do |r|
r.should_receive(:body).and_raise(RuntimeError)
end
flexmock(@session,
:sleep => nil,
:flexmock_original_behavior_for_should_receive => nil
)
stderr_null do
assert_raises(RuntimeError) do
@session = ODDB::MedData::Session.new(:search_type_test, @http_server)
@response.should_receive(:body).and_raise(RuntimeError)
@session.get_result_list(criteria)
end
end
end
def stdout_null
require 'tempfile'
$stdout = Tempfile.open('stdout')
yield
$stdout.close
$stdout = STDOUT

def test_detail_html__errno__econnreset
setup_to_raise_error
stderr_null do
assert_raises(Errno::ECONNRESET) do
@session = ODDB::MedData::Session.new(:search_type_test, @http_server)
@response.should_receive(:body).and_raise(Errno::ECONNRESET)
@session.detail_html('detail')
end
end
end

def test_get_result_list__internal_server_error
criteria = {}
response = Net::HTTPFound.new(1,2,3)
flexmock(@http,
@response = flexmock('response') do |r|
r.should_receive(:[]).with('set-cookie').and_return('cookie_header')
r.should_receive(:body).times(1).and_return('body')
end
response = Net::HTTPFound.new(1,2,3)
@http = flexmock(@http,
:post => response,
:get => @response
)
flexmock(Net::HTTP, :new => @http)
@http_server = flexmock('http_server')
ODDB::MedData::Session::HTTP_PATHS.store :search_type_test, 'http_path'
ODDB::MedData::Session::FORM_KEYS.store :search_type_test, [ [:name, 'txtSearchName'] ]
ODDB::MedData::Session::DETAIL_KEYS.store :search_type_test, 'detail_key'
uri = flexmock('uri', :request_uri => 'request_uri')
flexmock(URI, :parse => uri)
flexmock(@response) do |r|
r.should_receive(:body).and_raise(RuntimeError, 'InternalServerError')
end
flexmock(@session,
:sleep => nil,
:flexmock_original_behavior_for_should_receive => nil
)
@session = ODDB::MedData::Session.new(:search_type_test, @http_server)
stderr_null{stdout_null{
assert_raises(RuntimeError) do
@response.should_receive(:body).and_raise(RuntimeError)
@session.get_result_list(criteria)
end
}}
}}

end
def test_detail_html
response = Net::HTTPFound.new(1,2,3)
flexmock(@http,
:post => response,
:get => @response
)
uri = flexmock('uri', :request_uri => 'request_uri')
flexmock(URI, :parse => uri)
stderr_null do
assert_equal('body', @session.detail_html(nil))
end
end
def test_detail_html__errno__econnreset
response = Net::HTTPFound.new(1,2,3)
flexmock(@http,
:post => response,
:get => @response
)
uri = flexmock('uri', :request_uri => 'request_uri')
flexmock(URI, :parse => uri)
flexmock(@response) do |r|
r.should_receive(:body).and_raise(Errno::ECONNRESET)
end
flexmock(@session,
:sleep => nil,
:flexmock_original_behavior_for_should_receive => nil
)

stderr_null do
assert_raises(Errno::ECONNRESET) do
@session.detail_html(nil)
end
end
end

end
end # MedData
end # ODDB

0 comments on commit 28666c7

Please sign in to comment.