Permalink
Browse files

if all engines error, we're out of luck

  • Loading branch information...
1 parent 237a071 commit 249d4719ba5cc66b594e0385d38ba2a5f731acd7 @jrochkind committed Aug 21, 2012
Showing with 44 additions and 16 deletions.
  1. +19 −2 app/controllers/battle_controller.rb
  2. +25 −14 test/functional/battle_controller_test.rb
@@ -28,8 +28,13 @@ def index
if @results[engine].failed?
handle_failed_results(choice)
end
- end
- end
+ end
+ end
+ rescue OutOfEngines
+ @results = nil
+ @one = nil
+ @two = nil
+ render :status => 500, :text => "Sorry, a searching error occured and we could not get results for you."
end
@@ -101,6 +106,13 @@ def handle_failed_results(choice)
@results.delete orig_engine
new_engine = choices.pop
+
+ # If we're out of choices, bail! render failure and we're done.
+ if new_engine.nil?
+ raise OutOfEngines
+ end
+
+
instance_variable_set("@#{choice}", new_engine)
new_results = BentoSearch.get_engine(new_engine).search(params[:q])
@@ -113,4 +125,9 @@ def handle_failed_results(choice)
end
end
+ # raised when all the engines failed in searching and
+ # there's no more to try.
+ class OutOfEngines < Exception
+ end
+
end
@@ -11,6 +11,14 @@ def setup
BentoSearch.register_engine("CC") do |conf|
conf.engine = "BentoSearch::MockEngine"
end
+ BentoSearch.register_engine("error") do |conf|
+ conf.engine = "BentoSearch::MockEngine"
+ conf.error = {:message => "Fake error"}
+ end
+ BentoSearch.register_engine("error2") do |conf|
+ conf.engine = "BentoSearch::MockEngine"
+ conf.error = {:message => "Fake error"}
+ end
BattleController.contenders = ["AA", "BB", "CC"]
end
@@ -49,11 +57,7 @@ def teardown
end
end
- test "should recover from errors when searching" do
- BentoSearch.register_engine("error") do |conf|
- conf.engine = "BentoSearch::MockEngine"
- conf.error = {:message => "Fake error"}
- end
+ test "should recover from errors when searching" do
BattleController.contenders = ["AA", "BB", "CC", "error"]
# Fake it into 'randomly' picking error in it's first two please
@@ -89,14 +93,6 @@ def choices
end
test "should recover from multiple errors when searching" do
- BentoSearch.register_engine("error") do |conf|
- conf.engine = "BentoSearch::MockEngine"
- conf.error = {:message => "Fake error"}
- end
- BentoSearch.register_engine("error2") do |conf|
- conf.engine = "BentoSearch::MockEngine"
- conf.error = {:message => "Fake error"}
- end
BattleController.contenders = ["AA", "BB", "error2", "error", "CC"]
# Fake it into 'randomly' picking error in it's first two please
@@ -117,10 +113,25 @@ def choices
assert_equal "BB", assigns["two"]
# and neither are errors
- assert ! assigns["results"].values.find {|r| r.failed?}
+ assert ! assigns["results"].values.find {|r| r.failed?}
+ end
+
+ test "too many errors to display results" do
+ BattleController.contenders = ["error2", "error", "CC"]
+ assert_difference("Error.count", 2) do
+ get :index, :q => "Cancer"
+ end
+
+ assert_response :error
+
+ assert ! assigns["one"]
+ assert ! assigns["two"]
+ assert ! assigns["results"]
end
+
+
test "choice" do
option_a = ["a", "b", "c"].sample
option_b = ["1", "2", "3"].sample

0 comments on commit 249d471

Please sign in to comment.