summaryrefslogtreecommitdiff
path: root/spec/workers/concerns/gitlab/github_import/rescheduling_methods_spec.rb
blob: 8de4059c4ae4499214d2aba189919fae4a2eabdf (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
require 'spec_helper'

describe Gitlab::GithubImport::ReschedulingMethods do
  let(:worker) do
    Class.new { include(Gitlab::GithubImport::ReschedulingMethods) }.new
  end

  describe '#perform' do
    context 'with a non-existing project' do
      it 'does not perform any work' do
        expect(worker)
          .not_to receive(:try_import)

        worker.perform(-1, {})
      end

      it 'notifies any waiters so they do not wait forever' do
        expect(worker)
          .to receive(:notify_waiter)
          .with('123')

        worker.perform(-1, {}, '123')
      end
    end

    context 'with an existing project' do
      let(:project) { create(:project) }

      it 'notifies any waiters upon successfully importing the data' do
        expect(worker)
          .to receive(:try_import)
          .with(
            an_instance_of(Project),
            an_instance_of(Gitlab::GithubImport::Client),
            { 'number' => 2 }
          )
          .and_return(true)

        expect(worker)
          .to receive(:notify_waiter).with('123')

        worker.perform(project.id, { 'number' => 2 }, '123')
      end

      it 'reschedules itself if the data could not be imported' do
        expect(worker)
          .to receive(:try_import)
          .with(
            an_instance_of(Project),
            an_instance_of(Gitlab::GithubImport::Client),
            { 'number' => 2 }
          )
          .and_return(false)

        expect(worker)
          .not_to receive(:notify_waiter)

        expect_any_instance_of(Gitlab::GithubImport::Client)
          .to receive(:rate_limit_resets_in)
          .and_return(14)

        expect(worker.class)
          .to receive(:perform_in)
          .with(14, project.id, { 'number' => 2 }, '123')

        worker.perform(project.id, { 'number' => 2 }, '123')
      end
    end
  end

  describe '#try_import' do
    it 'returns true when the import succeeds' do
      expect(worker)
        .to receive(:import)
        .with(10, 20)

      expect(worker.try_import(10, 20)).to eq(true)
    end

    it 'returns false when the import fails due to hitting the GitHub API rate limit' do
      expect(worker)
        .to receive(:import)
        .with(10, 20)
        .and_raise(Gitlab::GithubImport::RateLimitError)

      expect(worker.try_import(10, 20)).to eq(false)
    end
  end

  describe '#notify_waiter' do
    it 'notifies the waiter if a waiter key is specified' do
      expect(worker)
        .to receive(:jid)
        .and_return('abc123')

      expect(Gitlab::JobWaiter)
        .to receive(:notify)
        .with('123', 'abc123')

      worker.notify_waiter('123')
    end

    it 'does not notify any waiters if no waiter key is specified' do
      expect(Gitlab::JobWaiter)
        .not_to receive(:notify)

      worker.notify_waiter(nil)
    end
  end
end