Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/circleci/cli/command/watch_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def run(token)
@refresh_lock.synchronize { @refresh_pending = true }
end
client.connect
watch_builds
watch_builds(client)
ensure
release_subscription(client, channel) if client
end
Expand All @@ -44,8 +44,9 @@ def release_subscription(client, channel)
client.disconnect
end

def watch_builds
def watch_builds(client)
loop do
check_connection(client)
refresh_builds_if_needed
stop_existing_watcher_if_needed
start_watcher_if_needed
Expand All @@ -56,6 +57,11 @@ def watch_builds
@build_watcher = nil
end

def check_connection(client)
error = client.poll_connection_error
raise Thor::Error, "Pusher connection failed: #{error.message}" if error
end

def refresh_builds_if_needed
pending = @refresh_lock.synchronize do
pending_refresh = @refresh_pending
Expand Down
7 changes: 7 additions & 0 deletions lib/circleci/cli/networking/pusher_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Networking
class CircleCIPusherClient
def initialize(token:)
@token = token
@connection_errors = Queue.new
end

def connect
Expand All @@ -17,10 +18,15 @@ def connect
connection = socket
# The socket's async mode cannot disconnect before the handshake finishes.
Thread.handle_interrupt(Interrupt => :never) do
@connection_errors.clear
@connection_thread = Thread.new { connect_socket(connection) }
end
end

def poll_connection_error
@connection_errors.pop(timeout: 0)
end

def disconnect
Thread.handle_interrupt(Interrupt => :never) do
@connection_thread&.kill&.join
Expand Down Expand Up @@ -52,6 +58,7 @@ def unsubscribe(channel)
def connect_socket(connection)
connection.connect
rescue StandardError => e
@connection_errors.push(e)
connection.global_channel.dispatch_with_all('pusher:error', e)
end

Expand Down
40 changes: 39 additions & 1 deletion spec/circleci/cli/api_failure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,47 @@
end
end

describe 'watch Pusher connection failure' do
it 'reports the failure on stderr, exits unsuccessfully, and disconnects' do
allow(ENV).to receive(:fetch).and_call_original
allow(ENV).to receive(:fetch).with('CIRCLE_CI_TOKEN', nil).and_return('test-token')
expect(CircleCI::CLI::Response::Build).to receive(:branch).with('user', 'repo', 'main').once.and_return([])
account = circleci_response('pusher_id' => 'pusher-123')
allow(CircleCi::User).to receive(:new).and_return(double(me: account))

release_connection = Queue.new
connecting = Queue.new
global_channel = instance_double(PusherClient::Channel, dispatch_with_all: nil)
channel = instance_double(PusherClient::Channel, bind: nil)
socket = instance_double(PusherClient::Socket, global_channel:)
networking = CircleCI::CLI::Networking
allow(networking::VerifiedPusherSocket).to receive(:new).and_return(socket)
allow(socket).to receive(:connect) do
connecting << Thread.current
release_connection.pop
raise IOError, 'socket closed'
end
allow(socket).to receive(:subscribe).with('private-pusher-123')
allow(socket).to receive(:[]).with('private-pusher-123').and_return(channel)
expect(socket).to receive(:unsubscribe).with('private-pusher-123').ordered
expect(socket).to receive(:disconnect).ordered
expect_any_instance_of(CircleCI::CLI::Command::WatchCommand).to receive(:sleep).with(1).once do
worker = connecting.pop(timeout: 5)
expect(worker).to be_a(Thread)
release_connection << true
expect(worker.join(5)).to eq(worker)
end

expect do
expect { described_class.start(%w[watch -p user/repo -b main]) }
.to raise_error(SystemExit) { |error| expect(error.status).to eq(1) }
end.to output("Pusher connection failed: socket closed\n").to_stderr
end
end

describe 'watch account lookup' do
let(:arguments) { %w[watch -p user/repo -b main] }
let(:client) { double('Pusher client', connect: nil, unsubscribe: nil, disconnect: nil) }
let(:client) { double('Pusher client', connect: nil, poll_connection_error: nil, unsubscribe: nil, disconnect: nil) }
let(:builds_body) { [] }

before do
Expand Down
16 changes: 10 additions & 6 deletions spec/circleci/cli/command/watch_command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

describe '.run' do
let(:repository) { double('repository') }
let(:client) { double('client', connect: nil, bind: nil, unsubscribe: nil, disconnect: nil) }
let(:client) do
double('client', connect: nil, poll_connection_error: nil, bind: nil, unsubscribe: nil, disconnect: nil)
end
let(:account) { double('account', pusher_id: 'pusher-id') }
let(:config) { Struct.new(:token).new }

Expand Down Expand Up @@ -103,7 +105,9 @@
end

describe 'the first watch iteration' do
let(:client) { double('client', connect: nil, bind: nil, unsubscribe: nil, disconnect: nil) }
let(:client) do
double('client', connect: nil, poll_connection_error: nil, bind: nil, unsubscribe: nil, disconnect: nil)
end
let(:account) { double('account', pusher_id: 'pusher-id') }
let(:watcher) { double('watcher', close: nil) }
let(:build_response) { CircleCI::CLI::Response::Build }
Expand Down Expand Up @@ -177,7 +181,7 @@

describe 'account notifications' do
let(:repository) { CircleCI::CLI::Command::BuildRepository.new('user', 'repo', branch: 'main', user: 'octocat') }
let(:client) { double('client', connect: nil, unsubscribe: nil, disconnect: nil) }
let(:client) { double('client', connect: nil, poll_connection_error: nil, unsubscribe: nil, disconnect: nil) }
let(:account) { double('account', pusher_id: 'pusher-id') }
let(:running_build) do
double('running build', build_number: 42, branch: 'main', user: 'octocat', running?: true, finished?: false)
Expand Down Expand Up @@ -233,7 +237,7 @@
expect(builds).to receive(:branch).with('user', 'other', 'main').once.and_return([])
other_options = options.dup
other_options.project = 'user/other'
other_client = double('other client', connect: nil, unsubscribe: nil, disconnect: nil)
other_client = double('other client', connect: nil, poll_connection_error: nil, unsubscribe: nil, disconnect: nil)
allow(CircleCI::CLI::Networking::CircleCIPusherClient).to receive(:new).and_return(client, other_client)

previous_callback = nil
Expand Down Expand Up @@ -306,7 +310,7 @@
allow(watcher).to receive(:close)

notify = nil
expect(client).to receive(:bind).with('private-pusher-id', 'call') { |*_, &callback| notify = callback }
expect(client).to receive(:bind).with('private-pusher-id', 'call', subscription_success) { |*_, &callback| notify = callback }
expect(watcher).to receive(:start).ordered
expect(session).to receive(:sleep).with(1).ordered { notify.call }
expect(response).to receive(:branch).with('user', 'repo', 'main').ordered.and_raise(Net::ReadTimeout)
Expand All @@ -326,7 +330,7 @@
expect(response).to receive(:branch).with('user', 'repo', 'main').ordered.and_return([])
allow(CircleCI::CLI::Command::BuildRepository).to receive(:new).and_return(repository)
allow(described_class).to receive(:new).with(options, repository).and_return(session)
expect(client).to receive(:bind).with('private-pusher-id', 'call').and_yield
expect(client).to receive(:bind).with('private-pusher-id', 'call', subscription_success).and_yield
expect(response).to receive(:branch).with('user', 'repo', 'main').ordered
.and_raise(Thor::Error, 'CircleCI API error (HTTP 401): Unauthorized')

Expand Down