Skip to content

Commit

Permalink
adds specs for ec2tag watch method, and fixes bug where if an exception
Browse files Browse the repository at this point in the history
was raised the normal sleep got skipped, so it could get stuck calling
the failing discover instances code in a tight loop every few ms
  • Loading branch information
dcosson committed Sep 11, 2015
1 parent d5d580a commit 9d9e5ad
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/synapse/service_watcher/ec2tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ def watch
if set_backends(discover_instances)
log.info "synapse: ec2tag watcher backends have changed."
end
sleep_until_next_check(start)
rescue Exception => e
log.warn "synapse: error in ec2tag watcher thread: #{e.inspect}"
log.warn e.backtrace
ensure
sleep_until_next_check(start)
end
end

Expand Down
27 changes: 24 additions & 3 deletions spec/lib/synapse/service_watcher_ec2tags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,17 @@ def munge_haproxy_arg(name, new_value)
context 'when missing arguments' do
it 'does not break if aws_region is missing' do
expect {
Synapse::ServiceWatcher::EC2Watcher.new(remove_discovery_arg('aws_region'), mock_synapse)
Synapse::ServiceWatcher::Ec2tagWatcher.new(remove_discovery_arg('aws_region'), mock_synapse)
}.not_to raise_error
end
it 'does not break if aws_access_key_id is missing' do
expect {
Synapse::ServiceWatcher::EC2Watcher.new(remove_discovery_arg('aws_access_key_id'), mock_synapse)
Synapse::ServiceWatcher::Ec2tagWatcher.new(remove_discovery_arg('aws_access_key_id'), mock_synapse)
}.not_to raise_error
end
it 'does not break if aws_secret_access_key is missing' do
expect {
Synapse::ServiceWatcher::EC2Watcher.new(remove_discovery_arg('aws_secret_access_key'), mock_synapse)
Synapse::ServiceWatcher::Ec2tagWatcher.new(remove_discovery_arg('aws_secret_access_key'), mock_synapse)
}.not_to raise_error
end
it 'complains if server_port_override is missing' do
Expand All @@ -122,6 +122,27 @@ def munge_haproxy_arg(name, new_value)
let(:instance1) { FakeAWSInstance.new }
let(:instance2) { FakeAWSInstance.new }

context 'watch' do

it 'discovers instances, configures backends, then sleeps' do
fake_backends = [1,2,3]
expect(subject).to receive(:discover_instances).and_return(fake_backends)
expect(subject).to receive(:set_backends).with(fake_backends) { subject.stop }
expect(subject).to receive(:sleep_until_next_check)
subject.send(:watch)
end

it 'sleeps until next check if discover_instances fails' do
expect(subject).to receive(:discover_instances) do
subject.stop
raise "discover failed"
end
expect(subject).to receive(:sleep_until_next_check)
subject.send(:watch)
end

end

context 'using the AWS API' do
let(:ec2_client) { double('AWS::EC2') }
let(:instance_collection) { double('AWS::EC2::InstanceCollection') }
Expand Down

0 comments on commit 9d9e5ad

Please sign in to comment.