diff options
Diffstat (limited to 'spec/sidekiq_cluster/sidekiq_cluster_spec.rb')
-rw-r--r-- | spec/sidekiq_cluster/sidekiq_cluster_spec.rb | 135 |
1 files changed, 22 insertions, 113 deletions
diff --git a/spec/sidekiq_cluster/sidekiq_cluster_spec.rb b/spec/sidekiq_cluster/sidekiq_cluster_spec.rb index 1d2b47e78ce..c0a919a4aec 100644 --- a/spec/sidekiq_cluster/sidekiq_cluster_spec.rb +++ b/spec/sidekiq_cluster/sidekiq_cluster_spec.rb @@ -5,68 +5,30 @@ require 'rspec-parameterized' require_relative '../../sidekiq_cluster/sidekiq_cluster' RSpec.describe Gitlab::SidekiqCluster do # rubocop:disable RSpec/FilePath - describe '.trap_signals' do - it 'traps the given signals' do - expect(described_class).to receive(:trap).ordered.with(:INT) - expect(described_class).to receive(:trap).ordered.with(:HUP) - - described_class.trap_signals(%i(INT HUP)) - end - end - - describe '.trap_terminate' do - it 'traps the termination signals' do - expect(described_class).to receive(:trap_signals) - .with(described_class::TERMINATE_SIGNALS) - - described_class.trap_terminate { } - end - end - - describe '.trap_forward' do - it 'traps the signals to forward' do - expect(described_class).to receive(:trap_signals) - .with(described_class::FORWARD_SIGNALS) - - described_class.trap_forward { } - end - end - - describe '.signal' do - it 'sends a signal to the given process' do - allow(Process).to receive(:kill).with(:INT, 4) - expect(described_class.signal(4, :INT)).to eq(true) - end - - it 'returns false when the process does not exist' do - allow(Process).to receive(:kill).with(:INT, 4).and_raise(Errno::ESRCH) - expect(described_class.signal(4, :INT)).to eq(false) - end - end - - describe '.signal_processes' do - it 'sends a signal to every given process' do - expect(described_class).to receive(:signal).with(1, :INT) - - described_class.signal_processes([1], :INT) - end - end - describe '.start' do it 'starts Sidekiq with the given queues, environment and options' do - expected_options = { - env: :production, - directory: 'foo/bar', - max_concurrency: 20, - min_concurrency: 10, - timeout: 25, - dryrun: true + process_options = { + pgroup: true, + err: $stderr, + out: $stdout } - expect(described_class).to receive(:start_sidekiq).ordered.with(%w(foo), expected_options.merge(worker_id: 0)) - expect(described_class).to receive(:start_sidekiq).ordered.with(%w(bar baz), expected_options.merge(worker_id: 1)) + expect(Bundler).to receive(:with_original_env).and_call_original.twice + + expect(Process).to receive(:spawn).ordered.with({ + "ENABLE_SIDEKIQ_CLUSTER" => "1", + "SIDEKIQ_WORKER_ID" => "0" + }, + "bundle", "exec", "sidekiq", "-c10", "-eproduction", "-t25", "-gqueues:foo", "-rfoo/bar", "-qfoo,1", process_options + ) + expect(Process).to receive(:spawn).ordered.with({ + "ENABLE_SIDEKIQ_CLUSTER" => "1", + "SIDEKIQ_WORKER_ID" => "1" + }, + "bundle", "exec", "sidekiq", "-c10", "-eproduction", "-t25", "-gqueues:bar,baz", "-rfoo/bar", "-qbar,1", "-qbaz,1", process_options + ) - described_class.start([%w(foo), %w(bar baz)], env: :production, directory: 'foo/bar', max_concurrency: 20, min_concurrency: 10, dryrun: true) + described_class.start([%w(foo), %w(bar baz)], env: :production, directory: 'foo/bar', max_concurrency: 20, min_concurrency: 10) end it 'starts Sidekiq with the given queues and sensible default options' do @@ -99,7 +61,7 @@ RSpec.describe Gitlab::SidekiqCluster do # rubocop:disable RSpec/FilePath it 'starts a Sidekiq process' do allow(Process).to receive(:spawn).and_return(1) - expect(described_class).to receive(:wait_async).with(1) + expect(Gitlab::ProcessManagement).to receive(:wait_async).with(1) expect(described_class.start_sidekiq(%w(foo), **options)).to eq(1) end @@ -109,7 +71,7 @@ RSpec.describe Gitlab::SidekiqCluster do # rubocop:disable RSpec/FilePath .with(env, *args, anything) .and_return(1) - expect(described_class).to receive(:wait_async).with(1) + expect(Gitlab::ProcessManagement).to receive(:wait_async).with(1) expect(described_class.start_sidekiq(%w(foo foo bar baz), **options)).to eq(1) end @@ -119,7 +81,7 @@ RSpec.describe Gitlab::SidekiqCluster do # rubocop:disable RSpec/FilePath .with(anything, *args, a_hash_including(pgroup: true)) .and_return(1) - allow(described_class).to receive(:wait_async) + allow(Gitlab::ProcessManagement).to receive(:wait_async) expect(described_class.start_sidekiq(%w(foo bar baz), **options)).to eq(1) end end @@ -152,57 +114,4 @@ RSpec.describe Gitlab::SidekiqCluster do # rubocop:disable RSpec/FilePath it { expect(described_class.concurrency(queues, min, max)).to eq(expected) } end end - - describe '.wait_async' do - it 'waits for a process in a separate thread' do - thread = described_class.wait_async(Process.spawn('true')) - - # Upon success Process.wait just returns the PID. - expect(thread.value).to be_a_kind_of(Numeric) - end - end - - # In the X_alive? checks, we check negative PIDs sometimes as a simple way - # to be sure the pids are definitely for non-existent processes. - # Note that -1 is special, and sends the signal to every process we have permission - # for, so we use -2, -3 etc - describe '.all_alive?' do - it 'returns true if all processes are alive' do - processes = [Process.pid] - - expect(described_class.all_alive?(processes)).to eq(true) - end - - it 'returns false when a thread was not alive' do - processes = [-2] - - expect(described_class.all_alive?(processes)).to eq(false) - end - end - - describe '.any_alive?' do - it 'returns true if at least one process is alive' do - processes = [Process.pid, -2] - - expect(described_class.any_alive?(processes)).to eq(true) - end - - it 'returns false when all threads are dead' do - processes = [-2, -3] - - expect(described_class.any_alive?(processes)).to eq(false) - end - end - - describe '.write_pid' do - it 'writes the PID of the current process to the given file' do - handle = double(:handle) - - allow(File).to receive(:open).with('/dev/null', 'w').and_yield(handle) - - expect(handle).to receive(:write).with(Process.pid.to_s) - - described_class.write_pid('/dev/null') - end - end end |