|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "aws-sdk-translate" |
| 4 | + |
| 5 | +describe Jobs::DetectPostsTranslation do |
| 6 | + fab!(:posts) { Fabricate.times(5, :post) } |
| 7 | + |
| 8 | + before do |
| 9 | + SiteSetting.translator_enabled = true |
| 10 | + SiteSetting.translator = "Amazon" |
| 11 | + client = Aws::Translate::Client.new(stub_responses: true) |
| 12 | + client.stub_responses( |
| 13 | + :translate_text, |
| 14 | + { translated_text: "大丈夫", source_language_code: "en", target_language_code: "jp" }, |
| 15 | + ) |
| 16 | + Aws::Translate::Client.stubs(:new).returns(client) |
| 17 | + posts.each do |post| |
| 18 | + Discourse.redis.sadd?(DiscourseTranslator::LANG_DETECT_NEEDED, post.id) |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + it "processes posts in batches and updates their translations" do |
| 23 | + described_class.new.execute({}) |
| 24 | + |
| 25 | + posts.each do |post| |
| 26 | + post.reload |
| 27 | + expect(post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD]).not_to be_nil |
| 28 | + end |
| 29 | + |
| 30 | + expect(Discourse.redis.smembers(DiscourseTranslator::LANG_DETECT_NEEDED)).to be_empty |
| 31 | + end |
| 32 | + |
| 33 | + it "does not process posts if the translator is disabled" do |
| 34 | + SiteSetting.translator_enabled = false |
| 35 | + described_class.new.execute({}) |
| 36 | + |
| 37 | + posts.each do |post| |
| 38 | + post.reload |
| 39 | + expect(post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD]).to be_nil |
| 40 | + end |
| 41 | + |
| 42 | + expect(Discourse.redis.smembers(DiscourseTranslator::LANG_DETECT_NEEDED)).to match_array(posts.map(&:id).map(&:to_s)) |
| 43 | + end |
| 44 | + |
| 45 | + it "processes a maximum of MAX_QUEUE_SIZE posts per run" do |
| 46 | + large_number = 2000 |
| 47 | + large_number.times { |i| Discourse.redis.sadd?(DiscourseTranslator::LANG_DETECT_NEEDED, i + 1) } |
| 48 | + described_class.new.execute({}) |
| 49 | + |
| 50 | + remaining = Discourse.redis.scard(DiscourseTranslator::LANG_DETECT_NEEDED) |
| 51 | + expect(remaining).to eq(large_number - Jobs::DetectPostsTranslation::MAX_QUEUE_SIZE) |
| 52 | + end |
| 53 | + |
| 54 | + it "handles an empty Redis queue gracefully" do |
| 55 | + Discourse.redis.del(DiscourseTranslator::LANG_DETECT_NEEDED) |
| 56 | + expect { described_class.new.execute({}) }.not_to raise_error |
| 57 | + end |
| 58 | + |
| 59 | + it "removes successfully processed posts from Redis" do |
| 60 | + described_class.new.execute({}) |
| 61 | + |
| 62 | + posts.each do |post| |
| 63 | + expect(Discourse.redis.sismember(DiscourseTranslator::LANG_DETECT_NEEDED, post.id)).to be_falsey |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + it "skips posts that no longer exist" do |
| 68 | + non_existent_post_id = -1 |
| 69 | + Discourse.redis.sadd?(DiscourseTranslator::LANG_DETECT_NEEDED, non_existent_post_id) |
| 70 | + |
| 71 | + expect { described_class.new.execute({}) }.not_to raise_error |
| 72 | + |
| 73 | + expect(Discourse.redis.sismember(DiscourseTranslator::LANG_DETECT_NEEDED, non_existent_post_id)).to be_falsey |
| 74 | + end |
| 75 | + |
| 76 | + it "ensures posts are processed within a distributed mutex" do |
| 77 | + mutex_spy = instance_spy(DistributedMutex) |
| 78 | + allow(DistributedMutex).to receive(:synchronize).and_yield |
| 79 | + |
| 80 | + described_class.new.execute({}) |
| 81 | + |
| 82 | + posts.each do |post| |
| 83 | + expect(DistributedMutex).to have_received(:synchronize).with("detect_translation_#{post.id}") |
| 84 | + end |
| 85 | + end |
| 86 | +end |
0 commit comments