Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@fix/mock flaky test #6081

6 changes: 3 additions & 3 deletions lib/lift_wing_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class LiftWingApi
# All the wikis with an articlequality model as of 2023-06-28
# https://wikitech.wikimedia.org/wiki/Machine_Learning/LiftWingq
AVAILABLE_WIKIPEDIAS = %w[en eu fa fr gl nl pt ru sv tr uk].freeze
# config/initializers/retry_config.rb
RETRY_COUNT = 5

def self.valid_wiki?(wiki)
return true if wiki.project == 'wikidata'
Expand Down Expand Up @@ -51,7 +53,6 @@ def get_revision_data(rev_ids)
end

log_error_batch(rev_ids)

return results
end

Expand All @@ -60,7 +61,7 @@ def get_revision_data(rev_ids)
# Returns a hash with wp10, features, deleted, and prediction, or empty hash if
# there is an error.
def get_single_revision_parsed_data(rev_id)
tries ||= 5
tries ||= RETRY_COUNT
body = { rev_id:, extended_output: true }.to_json
response = lift_wing_server.post(quality_query_url, body)
parsed_response = Oj.load(response.body)
Expand All @@ -69,7 +70,6 @@ def get_single_revision_parsed_data(rev_id)
return { 'wp10' => nil, 'features' => nil, 'deleted' => deleted?(parsed_response),
'prediction' => nil }
end

build_successful_response(rev_id, parsed_response)
rescue StandardError => e
tries -= 1
Expand Down
15 changes: 10 additions & 5 deletions spec/lib/lift_wing_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
# Get revision data for valid rev ids for English Wikipedia
let(:subject0) { lift_wing_api_class_en_wiki.get_revision_data(rev_ids) }

# Get revision data for valid rev ids for Wikidata
let(:subject1) { described_class.new(wiki).get_revision_data(rev_ids) }

# Get revision data for deleted rev ids for English Wikipedia
let(:subject2) { lift_wing_api_class_en_wiki.get_revision_data([deleted_rev_id]) }

Expand All @@ -47,8 +44,16 @@
end
end

it 'fetches json from api.wikimedia.org for wikidata' do
VCR.use_cassette 'liftwing_api/wikidata' do
context 'fetch json data from api.wikimedia.org' do
before do
stub_wiki_validation
stub_lift_wing_response
end

# Get revision data for valid rev ids for Wikidata
let(:subject1) { described_class.new(wiki).get_revision_data([829840084, 829840085]) }

it 'fetches data for wikidata' do
expect(subject1).to be_a(Hash)
expect(subject1.dig('829840084')).to have_key('wp10')
expect(subject1.dig('829840084', 'wp10')).to eq(nil)
Expand Down
21 changes: 15 additions & 6 deletions spec/lib/reference_counter_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,28 @@
let(:wikidata) { Wiki.get_or_create(language: nil, project: 'wikidata') }
let(:deleted_rev_ids) { [708326238] }
let(:rev_ids) { [5006940, 5006942, 5006946] }
let(:response) { stub_reference_counter_response }

it 'raises InvalidProjectError if using wikidata project' do
expect do
described_class.new(wikidata)
end.to raise_error(described_class::InvalidProjectError)
end

it 'returns the number of references if response is 200 OK', vcr: true do
ref_counter_api = described_class.new(es_wiktionary)
response = ref_counter_api.get_number_of_references_from_revision_ids rev_ids
expect(response.dig('5006940', 'num_ref')).to eq(10)
expect(response.dig('5006942', 'num_ref')).to eq(4)
expect(response.dig('5006946', 'num_ref')).to eq(2)
context 'returns the number of references' do
before do
stub_wiki_validation
stub_reference_counter_response
end

# Get revision data for valid rev ids for Wikidata
it 'if response is 200 OK', vcr: true do
ref_counter_api = described_class.new(es_wiktionary)
response = ref_counter_api.get_number_of_references_from_revision_ids rev_ids
expect(response.dig('5006940', 'num_ref')).to eq(10)
expect(response.dig('5006942', 'num_ref')).to eq(4)
expect(response.dig('5006946', 'num_ref')).to eq(2)
end
end

# it 'logs the message if response is not 200 OK', vcr: true do
Expand Down
98 changes: 56 additions & 42 deletions spec/lib/revision_score_api_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
let(:subject) { handler.get_revision_data [829840090, 829840091] }

describe '#get_revision_data' do
it 'returns completed scores if retrieves data without errors' do
it 'returns completed scores if data is retrieved without errors' do
VCR.use_cassette 'revision_score_api_handler/en_wikipedia' do
expect(subject).to be_a(Hash)
expect(subject.dig('829840090', 'wp10').to_f).to be_within(0.01).of(62.81)
Expand All @@ -28,15 +28,26 @@
end
end

it 'returns completed scores if there is an error hitting LiftWingApi' do
VCR.use_cassette 'revision_score_api_handler/en_wikipedia_liftwing_error' do
stub_request(:any, /.*api.wikimedia.org.*/)
.to_raise(Errno::ETIMEDOUT)
expect(subject).to be_a(Hash)
expect(subject.dig('829840090')).to eq({ 'wp10' => nil,
'features' => { 'num_ref' => 132 }, 'deleted' => false, 'prediction' => nil })
expect(subject.dig('829840091')).to eq({ 'wp10' => nil,
'features' => { 'num_ref' => 1 }, 'deleted' => false, 'prediction' => nil })
describe 'error hitting LiftWingApi' do
before do
stub_wiki_validation
stub_revision_score_reference_counter_reponse
end

let(:wiki) { create(:wiki, project: 'wikipedia', language: 'es') }
let(:handler) { described_class.new(wiki:) }
let(:subject) { handler.get_revision_data [829840090, 829840091] }

it 'returns completed scores if there is an error hitting LiftWingApi' do
VCR.use_cassette 'revision_score_api_handler/en_wikipedia_liftwing_error' do
stub_request(:any, /.*api.wikimedia.org.*/)
.to_raise(Errno::ETIMEDOUT)
expect(subject).to be_a(Hash)
expect(subject.dig('829840090')).to eq({ 'wp10' => nil,
'features' => { 'num_ref' => 132 }, 'deleted' => false, 'prediction' => nil })
expect(subject.dig('829840091')).to eq({ 'wp10' => nil,
'features' => { 'num_ref' => 1 }, 'deleted' => false, 'prediction' => nil })
end
end
end

Expand Down Expand Up @@ -76,34 +87,36 @@
end

context 'when the wiki is available only for LiftWing API' do
before { stub_wiki_validation }

let(:wiki) { create(:wiki, project: 'wikidata', language: nil) }
let(:handler) { described_class.new(wiki:) }
let(:subject) { handler.get_revision_data [144495297, 144495298] }

before do
stub_wiki_validation
stub_revision_score_lift_wing_reponse
end

describe '#get_revision_data' do
it 'returns completed scores if retrieves data without errors' do
VCR.use_cassette 'revision_score_api_handler/wikidata' do
expect(subject).to be_a(Hash)
expect(subject.dig('144495297', 'wp10').to_f).to eq(0)
expect(subject.dig('144495297', 'features')).to be_a(Hash)
expect(subject.dig('144495297', 'features',
'feature.len(<datasource.wikidatawiki.revision.references>)')).to eq(2)
# 'num_ref' key doesn't exist for wikidata features
expect(subject.dig('144495297', 'features').key?('num_ref')).to eq(false)
expect(subject.dig('144495297', 'deleted')).to eq(false)
expect(subject.dig('144495297', 'prediction')).to eq('D')

expect(subject.dig('144495298', 'wp10').to_f).to eq(0)
expect(subject.dig('144495298', 'features')).to be_a(Hash)
expect(subject.dig('144495298', 'features',
'feature.len(<datasource.wikidatawiki.revision.references>)')).to eq(0)
# 'num_ref' key doesn't exist for wikidata features
expect(subject.dig('144495298', 'features').key?('num_ref')).to eq(false)
expect(subject.dig('144495298', 'deleted')).to eq(false)
expect(subject.dig('144495298', 'prediction')).to eq('E')
end
let(:subject) { handler.get_revision_data [144495297, 144495298] }

it 'returns completed scores if data is retrieved without errors' do
expect(subject).to be_a(Hash)
expect(subject.dig('144495297', 'wp10').to_f).to eq(0)
expect(subject.dig('144495297', 'features')).to be_a(Hash)
expect(subject.dig('144495297', 'features',
'feature.len(<datasource.wikidatawiki.revision.references>)')).to eq(2)
# 'num_ref' key doesn't exist for wikidata features
expect(subject.dig('144495297', 'features').key?('num_ref')).to eq(false)
expect(subject.dig('144495297', 'deleted')).to eq(false)
expect(subject.dig('144495297', 'prediction')).to eq('D')

expect(subject.dig('144495298', 'wp10').to_f).to eq(0)
expect(subject.dig('144495298', 'features')).to be_a(Hash)
expect(subject.dig('144495298', 'features',
'feature.len(<datasource.wikidatawiki.revision.references>)')).to eq(0)
# 'num_ref' key doesn't exist for wikidata features
expect(subject.dig('144495298', 'features').key?('num_ref')).to eq(false)
expect(subject.dig('144495298', 'deleted')).to eq(false)
expect(subject.dig('144495298', 'prediction')).to eq('E')
end

it 'returns completed scores if there is an error hitting LiftWingApi' do
Expand All @@ -119,21 +132,22 @@
end

context 'when the wiki is available only for reference-counter API' do
before { stub_wiki_validation }
before do
stub_wiki_validation
stub_revision_score_reference_counter_reponse
end

let(:wiki) { create(:wiki, project: 'wikipedia', language: 'es') }
let(:handler) { described_class.new(wiki:) }
let(:subject) { handler.get_revision_data [157412237, 157417768] }

describe '#get_revision_data' do
it 'returns completed scores if retrieves data without errors' do
VCR.use_cassette 'revision_score_api_handler/es_wikipedia' do
expect(subject).to be_a(Hash)
expect(subject.dig('157412237')).to eq({ 'wp10' => nil,
'features' => { 'num_ref' => 111 }, 'deleted' => false, 'prediction' => nil })
expect(subject.dig('157417768')).to eq({ 'wp10' => nil,
'features' => { 'num_ref' => 42 }, 'deleted' => false, 'prediction' => nil })
end
expect(subject).to be_a(Hash)
expect(subject.dig('157412237')).to eq({ 'wp10' => nil,
'features' => { 'num_ref' => 111 }, 'deleted' => false, 'prediction' => nil })
expect(subject.dig('157417768')).to eq({ 'wp10' => nil,
'features' => { 'num_ref' => 42 }, 'deleted' => false, 'prediction' => nil })
end

it 'returns completed scores if there is an error hitting reference-counter api' do
Expand Down
12 changes: 9 additions & 3 deletions spec/services/update_course_stats_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,17 @@
it 'tracks update errors properly in Replica' do
allow(Sentry).to receive(:capture_exception)

# Stub the constant RETRY_COUNT for this test to ensure retries are controlled
stub_const('LiftWingApi::RETRY_COUNT', 1)
# Raising errors only in Replica
stub_request(:any, %r{https://replica-revision-tools.wmcloud.org/.*}).to_raise(Errno::ECONNREFUSED)
VCR.use_cassette 'course_update/replica' do
subject
end
sentry_tag_uuid = subject.sentry_tag_uuid
expect(course.flags['update_logs'][1]['error_count']).to eq 1
expected_error_count = subject.error_count

expect(course.flags['update_logs'][1]['error_count']).to eq expected_error_count
expect(course.flags['update_logs'][1]['sentry_tag_uuid']).to eq sentry_tag_uuid

# Checking whether Sentry receives correct error and tags as arguments
Expand All @@ -100,18 +104,20 @@
it 'tracks update errors properly in LiftWing' do
allow(Sentry).to receive(:capture_exception)

stub_const('LiftWingApi::RETRY_COUNT', 1)
# Raising errors only in LiftWing
stub_request(:any, %r{https://api.wikimedia.org/service/lw.*}).to_raise(Faraday::ConnectionFailed)
VCR.use_cassette 'course_update/lift_wing_api' do
subject
end
sentry_tag_uuid = subject.sentry_tag_uuid
expect(course.flags['update_logs'][1]['error_count']).to eq 8
expected_error_count = subject.error_count
expect(course.flags['update_logs'][1]['error_count']).to eq expected_error_count
expect(course.flags['update_logs'][1]['sentry_tag_uuid']).to eq sentry_tag_uuid

# Checking whether Sentry receives correct error and tags as arguments
expect(Sentry).to have_received(:capture_exception)
.exactly(8).times.with(Faraday::ConnectionFailed, anything)
.exactly(expected_error_count).times.with(Faraday::ConnectionFailed, anything)
expect(Sentry).to have_received(:capture_exception)
.exactly(8).times.with anything, hash_including(tags: { update_service_id: sentry_tag_uuid,
course: course.slug })
Expand Down
Loading
Loading