Skip to content

Commit

Permalink
don't get stuck in a loop
Browse files Browse the repository at this point in the history
set a max number of attempts to create the random sample of subjects
  • Loading branch information
camallen committed Aug 13, 2015
1 parent f2357fc commit 8693854
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/postgresql_selection.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class PostgresqlSelection
attr_reader :workflow, :user, :opts

MAX_RANDOM_SAMPLE_ATTEMPTS = 100

def initialize(workflow, user=nil)
@workflow, @user = workflow, user
end
Expand Down Expand Up @@ -65,8 +67,11 @@ def select_results_in_order

def construct_random_sample_to_limit
results = []
attempt = 0
until results.length >= limit do
results = results | sample.limit(limit).pluck(:id)
attempt += 1
break if attempt >= MAX_RANDOM_SAMPLE_ATTEMPTS
end
results
end
Expand Down
9 changes: 8 additions & 1 deletion spec/lib/postgresql_selection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def update_sms_priorities
sms_scope.count - _seen_count
end


context "when a user has only seen a few subjects" do
let(:seen_count) { 5 }
let!(:uss) do
Expand Down Expand Up @@ -87,6 +86,14 @@ def update_sms_priorities
subject { PostgresqlSelection.new(workflow, user) }

it_behaves_like "select for incomplete_project"

it "should give up trying to construct a random list after set number of attempts" do
unreachable_limit = SetMemberSubject.count + 1
allow_any_instance_of(PostgresqlSelection).to receive(:available_count).and_return(unreachable_limit + 1)
allow_any_instance_of(PostgresqlSelection).to receive(:limit).and_return(unreachable_limit)
results = subject.select
expect(results).to eq(results)
end
end

context "grouped selection" do
Expand Down

0 comments on commit 8693854

Please sign in to comment.