|
1 | 1 | require 'spec_helper' |
2 | 2 | require 'jobs/v3/services/fetch_last_operation_job' |
3 | 3 | require_relative '../../services/shared/when_broker_returns_retry_after_header' |
| 4 | +require_relative 'async_operation_end_timestamp' |
4 | 5 |
|
5 | 6 | module VCAP::CloudController |
6 | 7 | module V3 |
7 | 8 | RSpec.describe FetchLastOperationJob, job_context: :worker do |
8 | 9 | let(:proposed_service_plan) { ServicePlan.make } |
9 | 10 | let(:proposed_maintenance_info) { { 'version' => '2.0' } } |
10 | | - let(:maximum_polling_duration_for_plan) {} |
11 | | - let(:service_plan) { ServicePlan.make(maximum_polling_duration: maximum_polling_duration_for_plan) } |
| 11 | + let(:service_plan) { ServicePlan.make } |
12 | 12 | let(:service_instance) do |
13 | 13 | operation = ServiceInstanceOperation.make(proposed_changes: { |
14 | 14 | name: 'new-fake-name', |
@@ -39,7 +39,6 @@ module V3 |
39 | 39 | description: description |
40 | 40 | } |
41 | 41 | end |
42 | | - let(:max_duration) { 10080 } |
43 | 42 | let(:request_attrs) do |
44 | 43 | { |
45 | 44 | dummy_data: 'dummy_data' |
@@ -77,61 +76,21 @@ def run_job(job, successes: 1, failures: 0) |
77 | 76 | end |
78 | 77 |
|
79 | 78 | describe '#initialize' do |
80 | | - let(:default_polling_interval) { 120 } |
81 | | - let(:max_duration) { 10080 } |
82 | | - |
83 | | - before do |
84 | | - config_override = { |
85 | | - broker_client_default_async_poll_interval_seconds: default_polling_interval, |
86 | | - broker_client_max_async_poll_duration_minutes: max_duration, |
87 | | - } |
88 | | - TestConfig.override(config_override) |
89 | | - end |
90 | | - |
91 | | - context 'when the caller does not provide the maximum number of attempts' do |
92 | | - it 'should the default configuration value' do |
93 | | - Timecop.freeze(Time.now) |
94 | | - expect(job.end_timestamp).to eq(Time.now + max_duration.minutes) |
95 | | - end |
96 | | - end |
97 | | - |
98 | 79 | context 'when the default poll interval is greater than the max value (24 hours)' do |
99 | 80 | let(:default_polling_interval) { 24.hours + 1.minute } |
| 81 | + before do |
| 82 | + config_override = { |
| 83 | + broker_client_default_async_poll_interval_seconds: default_polling_interval, |
| 84 | + } |
| 85 | + TestConfig.override(config_override) |
| 86 | + end |
100 | 87 |
|
101 | 88 | it 'enqueues the job using the maximum polling interval' do |
102 | 89 | expect(job.poll_interval).to eq 24.hours |
103 | 90 | end |
104 | 91 | end |
105 | 92 |
|
106 | | - context 'when the service plan has maximum_polling_duration' do |
107 | | - let(:maximum_polling_duration_for_plan) { 36000000 } # in seconds |
108 | | - |
109 | | - context "when the config value is smaller than plan's maximum_polling_duration" do |
110 | | - let(:max_duration) { 10 } # in minutes |
111 | | - it 'should set end_timestamp to config value' do |
112 | | - Timecop.freeze(Time.now) |
113 | | - expect(job.end_timestamp).to eq(Time.now + max_duration.minutes) |
114 | | - end |
115 | | - end |
116 | | - |
117 | | - context "when the config value is greater than plan's maximum_polling_duration" do |
118 | | - let(:max_duration) { 1068367346 } # in minutes |
119 | | - it "should set end_timestamp to the plan's maximum_polling_duration value" do |
120 | | - Timecop.freeze(Time.now) |
121 | | - expect(job.end_timestamp).to eq(Time.now + maximum_polling_duration_for_plan.seconds) |
122 | | - end |
123 | | - end |
124 | | - end |
125 | | - |
126 | | - context 'when there is a database error in fetching the plan' do |
127 | | - it 'should set end_timestamp to config value' do |
128 | | - allow(ManagedServiceInstance).to receive(:first) do |e| |
129 | | - raise Sequel::Error.new(e) |
130 | | - end |
131 | | - Timecop.freeze(Time.now) |
132 | | - expect(job.end_timestamp).to eq(Time.now + max_duration.minutes) |
133 | | - end |
134 | | - end |
| 93 | + include_context 'end_timestamp' |
135 | 94 | end |
136 | 95 |
|
137 | 96 | describe '#perform' do |
@@ -320,6 +279,7 @@ def run_job(job, successes: 1, failures: 0) |
320 | 279 |
|
321 | 280 | context 'when the job has fetched for more than the max poll duration' do |
322 | 281 | let(:state) { 'in progress' } |
| 282 | + let(:max_duration) { 10080 } |
323 | 283 |
|
324 | 284 | before do |
325 | 285 | run_job(job) |
@@ -473,38 +433,6 @@ def run_job(job, successes: 1, failures: 0) |
473 | 433 | end |
474 | 434 | end |
475 | 435 |
|
476 | | - describe '#end_timestamp' do |
477 | | - let(:max_poll_duration) { VCAP::CloudController::Config.config.get(:broker_client_max_async_poll_duration_minutes) } |
478 | | - |
479 | | - context 'when the job is new' do |
480 | | - it 'adds the broker_client_max_async_poll_duration_minutes to the current time' do |
481 | | - now = Time.now |
482 | | - expected_end_timestamp = now + max_poll_duration.minutes |
483 | | - Timecop.freeze now do |
484 | | - expect(job.end_timestamp).to be_within(0.01).of(expected_end_timestamp) |
485 | | - end |
486 | | - end |
487 | | - end |
488 | | - |
489 | | - context 'when the job is fetched from the database' do |
490 | | - it 'returns the previously computed and persisted end_timestamp' do |
491 | | - now = Time.now |
492 | | - expected_end_timestamp = now + max_poll_duration.minutes |
493 | | - |
494 | | - job_id = nil |
495 | | - Timecop.freeze now do |
496 | | - enqueued_job = Jobs::Enqueuer.new(job, queue: Jobs::Queues.generic, run_at: Time.now).enqueue |
497 | | - job_id = enqueued_job.id |
498 | | - end |
499 | | - |
500 | | - Timecop.freeze(now + 1.day) do |
501 | | - rehydrated_job = Delayed::Job.first(id: job_id).payload_object.handler.handler |
502 | | - expect(rehydrated_job.end_timestamp).to be_within(0.01).of(expected_end_timestamp) |
503 | | - end |
504 | | - end |
505 | | - end |
506 | | - end |
507 | | - |
508 | 436 | pending('not yet ready') do |
509 | 437 | context 'when all operations succeed and the state is `succeeded`' do |
510 | 438 | let(:state) { 'succeeded' } |
|
0 commit comments