Welcome to mirror list, hosted at ThFree Co, Russian Federation.

export_user_spec.rb « workers « spec - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 657b801d852788f8c4bd5b772e6856858e60ff14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# frozen_string_literal: true

describe Workers::ExportUser do
  before do
    allow(User).to receive(:find).with(alice.id).and_return(alice)
  end

  it 'calls export! on user with given id' do
    expect(alice).to receive(:perform_export!)
    Workers::ExportUser.new.perform(alice.id)
  end

  it 'sends a success message when the export is successful' do
    allow(alice).to receive(:export).and_return(OpenStruct.new)
    expect(ExportMailer).to receive(:export_complete_for).with(alice).and_call_original
    Workers::ExportUser.new.perform(alice.id)
  end

  it 'sends a failure message when the export fails' do
    allow(alice).to receive(:export).and_return(nil)
    expect(alice).to receive(:perform_export!).and_return(false)
    expect(ExportMailer).to receive(:export_failure_for).with(alice).and_call_original
    Workers::ExportUser.new.perform(alice.id)
  end

  context "concurrency" do
    before do
      AppConfig.environment.single_process_mode = false
      AppConfig.settings.archive_jobs_concurrency = 1
    end

    after :all do
      AppConfig.environment.single_process_mode = true
    end

    let(:pid) { "#{Socket.gethostname}:#{Process.pid}:#{SecureRandom.hex(6)}" }

    it "schedules a job for later when already another parallel export job is running" do
      expect(Sidekiq::Workers).to receive(:new).and_return(
        [[pid, SecureRandom.hex(4), {"payload" => {"class" => "Workers::ExportUser"}}]]
      )

      expect(Workers::ExportUser).to receive(:perform_in).with(kind_of(Integer), alice.id)
      expect(alice).not_to receive(:perform_export!)

      Workers::ExportUser.new.perform(alice.id)
    end

    it "runs the export when the own running job" do
      expect(Sidekiq::Workers).to receive(:new).and_return(
        [[pid, Thread.current.object_id.to_s(36), {"payload" => {"class" => "Workers::ExportUser"}}]]
      )

      expect(Workers::ExportUser).not_to receive(:perform_in).with(kind_of(Integer), alice.id)
      expect(alice).to receive(:perform_export!)

      Workers::ExportUser.new.perform(alice.id)
    end

    it "runs the export when no other job is running" do
      expect(Sidekiq::Workers).to receive(:new).and_return([])

      expect(Workers::ExportUser).not_to receive(:perform_in).with(kind_of(Integer), alice.id)
      expect(alice).to receive(:perform_export!)

      Workers::ExportUser.new.perform(alice.id)
    end

    it "runs the export when some other job is running" do
      expect(Sidekiq::Workers).to receive(:new).and_return(
        [[pid, SecureRandom.hex(4), {"payload" => {"class" => "Workers::OtherJob"}}]]
      )

      expect(Workers::ExportUser).not_to receive(:perform_in).with(kind_of(Integer), alice.id)
      expect(alice).to receive(:perform_export!)

      Workers::ExportUser.new.perform(alice.id)
    end

    it "runs the export when diaspora is in single process mode" do
      AppConfig.environment.single_process_mode = true
      expect(Sidekiq::Workers).not_to receive(:new)
      expect(Workers::ExportUser).not_to receive(:perform_in).with(kind_of(Integer), alice.id)
      expect(alice).to receive(:perform_export!)

      Workers::ExportUser.new.perform(alice.id)
    end
  end
end