From 353e0f7b5a8d228436f834bbad4e85086bd4af8c Mon Sep 17 00:00:00 2001 From: Devin Gaffney Date: Wed, 31 Jan 2024 07:22:17 -0800 Subject: [PATCH] CV2-4233 add retry looper for alegre requests that intermittently fail (#1786) * CV2-4233 add retry looper for alegre requests that intermittently fail * CV2-4233 fix fixture * update test with right params * change blah to proper type * update function based on it being a hash that's actually being passed around * refactors from PR * add stub * fix typo --- app/models/concerns/alegre_v2.rb | 12 +++++++++++- test/models/bot/alegre_v2_test.rb | 8 ++++++++ test/models/fact_check_test.rb | 2 ++ test/models/project_media_2_test.rb | 2 ++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/models/concerns/alegre_v2.rb b/app/models/concerns/alegre_v2.rb index 112ffad6ed..3d9f84ca62 100644 --- a/app/models/concerns/alegre_v2.rb +++ b/app/models/concerns/alegre_v2.rb @@ -231,6 +231,16 @@ def parse_similarity_results(project_media, field, results, relationship_type) }.reject{ |k,_| k == project_media.id }] end + def safe_get_sync(project_media, field, params={}) + response = get_sync(project_media, field, params) + retries = 0 + while (response.nil? || response["result"].nil?) && retries < 3 + response = get_sync(project_media, field, params) + retries += 1 + end + response + end + def get_items(project_media, field, confirmed=false) relationship_type = confirmed ? Relationship.confirmed_type : Relationship.suggested_type type = get_type(project_media) @@ -238,7 +248,7 @@ def get_items(project_media, field, confirmed=false) parse_similarity_results( project_media, field, - get_sync(project_media, field, threshold)["result"], + safe_get_sync(project_media, field, threshold)["result"], relationship_type ) end diff --git a/test/models/bot/alegre_v2_test.rb b/test/models/bot/alegre_v2_test.rb index 89c8d57bc3..a79d740f2f 100644 --- a/test/models/bot/alegre_v2_test.rb +++ b/test/models/bot/alegre_v2_test.rb @@ -283,6 +283,14 @@ def teardown assert_equal JSON.parse(Bot::Alegre.get_sync(pm1).to_json), JSON.parse(response.to_json) end + test "should safe_get_sync" do + pm1 = create_project_media team: @team, media: create_uploaded_audio + WebMock.stub_request(:post, "#{CheckConfig.get('alegre_host')}/similarity/sync/audio").to_return(body: '{}') + expected = {} + actual = Bot::Alegre.safe_get_sync(pm1, "audio", {}) + assert_equal expected, actual + end + test "should run delete request" do pm1 = create_project_media team: @team, media: create_uploaded_audio response = {"requested"=> diff --git a/test/models/fact_check_test.rb b/test/models/fact_check_test.rb index fde1e8da65..4102c5d69e 100644 --- a/test/models/fact_check_test.rb +++ b/test/models/fact_check_test.rb @@ -30,9 +30,11 @@ def setup end test "should create fact check without optional fields" do + Bot::Alegre.stubs(:send_field_to_similarity_index).returns({"success": true}) assert_difference 'FactCheck.count' do create_fact_check url: nil, title: random_string, summary: nil end + Bot::Alegre.unstub(:send_field_to_similarity_index) end test "should not create fact check without user" do diff --git a/test/models/project_media_2_test.rb b/test/models/project_media_2_test.rb index 66bf6d5c39..9359059f30 100644 --- a/test/models/project_media_2_test.rb +++ b/test/models/project_media_2_test.rb @@ -75,6 +75,7 @@ def setup Sidekiq::Testing.inline! do pm = create_project_media quote: 'Title 0' assert_equal 'Title 0', pm.title + Bot::Alegre.stubs(:send_field_to_similarity_index).returns({"success": true}) cd = create_claim_description project_media: pm, description: 'Title 1' assert_queries 0, '=' do assert_equal 'Title 1', pm.title @@ -86,6 +87,7 @@ def setup assert_queries(0, '>') do assert_equal 'Title 1', pm.reload.title(true) end + Bot::Alegre.unstub(:send_field_to_similarity_index) end end