|  | 
|  | 1 | +require 'spec_helper' | 
|  | 2 | +require 'locket/lock_runner' | 
|  | 3 | + | 
|  | 4 | +RSpec.describe Locket::LockRunner do | 
|  | 5 | +  let(:locket_service) { instance_double(Models::Locket::Stub) } | 
|  | 6 | +  let(:key) { 'lock-key' } | 
|  | 7 | +  let(:owner) { 'lock-owner' } | 
|  | 8 | +  let(:host) { 'locket.capi.land' } | 
|  | 9 | +  let(:port) { '1234' } | 
|  | 10 | +  let(:client_ca_path) { File.join(Paths::FIXTURES, 'certs/bbs_ca.crt') } | 
|  | 11 | +  let(:client_cert_path) { File.join(Paths::FIXTURES, 'certs/bbs_client.crt') } | 
|  | 12 | +  let(:client_key_path) { File.join(Paths::FIXTURES, 'certs/bbs_client.key') } | 
|  | 13 | +  let(:credentials) { instance_double(GRPC::Core::ChannelCredentials) } | 
|  | 14 | +  let(:lock_request) do | 
|  | 15 | +    Models::LockRequest.new( | 
|  | 16 | +      { | 
|  | 17 | +        resource: { key: key, owner: owner, type_code: Models::TypeCode::LOCK }, | 
|  | 18 | +        ttl_in_seconds: 15 | 
|  | 19 | +      } | 
|  | 20 | +    ) | 
|  | 21 | +  end | 
|  | 22 | + | 
|  | 23 | +  let(:client) do | 
|  | 24 | +    Locket::LockRunner.new( | 
|  | 25 | +      host: host, | 
|  | 26 | +      port: port, | 
|  | 27 | +      client_ca_path: client_ca_path, | 
|  | 28 | +      client_key_path: client_key_path, | 
|  | 29 | +      client_cert_path: client_cert_path, | 
|  | 30 | +    ) | 
|  | 31 | +  end | 
|  | 32 | + | 
|  | 33 | +  before do | 
|  | 34 | +    client_ca = File.open(client_ca_path).read | 
|  | 35 | +    client_key = File.open(client_key_path).read | 
|  | 36 | +    client_cert = File.open(client_cert_path).read | 
|  | 37 | + | 
|  | 38 | +    allow(GRPC::Core::ChannelCredentials).to receive(:new). | 
|  | 39 | +      with(client_ca, client_key, client_cert). | 
|  | 40 | +      and_return(credentials) | 
|  | 41 | + | 
|  | 42 | +    allow(Models::Locket::Stub).to receive(:new). | 
|  | 43 | +      with("#{host}:#{port}", credentials). | 
|  | 44 | +      and_return(locket_service) | 
|  | 45 | +  end | 
|  | 46 | + | 
|  | 47 | +  after do | 
|  | 48 | +    client.stop | 
|  | 49 | +  end | 
|  | 50 | + | 
|  | 51 | +  describe '#start' do | 
|  | 52 | +    it 'continuously attempts to re-acquire the lock' do | 
|  | 53 | +      allow(locket_service).to receive(:lock) | 
|  | 54 | +      allow(client).to receive(:sleep) | 
|  | 55 | + | 
|  | 56 | +      client.start(key, owner) | 
|  | 57 | +      sleep 0.1 | 
|  | 58 | + | 
|  | 59 | +      expect(locket_service).to have_received(:lock).with(lock_request).at_least(3).times | 
|  | 60 | +    end | 
|  | 61 | + | 
|  | 62 | +    it 'raises an error when restarted after it has already been started' do | 
|  | 63 | +      client.start(key, owner) | 
|  | 64 | + | 
|  | 65 | +      expect { client.start(key, owner) }.to raise_error(Locket::LockRunner::Error, 'Cannot start more than once') | 
|  | 66 | +    end | 
|  | 67 | +  end | 
|  | 68 | + | 
|  | 69 | +  describe '#lock_acquired?' do | 
|  | 70 | +    context 'initialization' do | 
|  | 71 | +      it 'does not report that it has a lock before start is called' do | 
|  | 72 | +        expect(client.lock_acquired?).to be(false) | 
|  | 73 | +      end | 
|  | 74 | +    end | 
|  | 75 | + | 
|  | 76 | +    context 'when attempting to acquire a lock' do | 
|  | 77 | +      let(:fake_logger) { instance_double(Steno::Logger, debug: nil) } | 
|  | 78 | + | 
|  | 79 | +      before do | 
|  | 80 | +        allow(Steno).to receive(:logger).and_return(fake_logger) | 
|  | 81 | +        allow(client).to receive(:sleep) | 
|  | 82 | +      end | 
|  | 83 | + | 
|  | 84 | +      context 'when it does not acquire a lock' do | 
|  | 85 | +        it 'does not report that it has a lock' do | 
|  | 86 | +          error = GRPC::BadStatus.new(GRPC::AlreadyExists) | 
|  | 87 | +          client.instance_variable_set(:@lock_acquired, true) | 
|  | 88 | +          allow(locket_service).to receive(:lock). | 
|  | 89 | +            and_raise(error) | 
|  | 90 | + | 
|  | 91 | +          client.start(key, owner) | 
|  | 92 | +          sleep 0.1 | 
|  | 93 | + | 
|  | 94 | +          expect(client.lock_acquired?).to be(false) | 
|  | 95 | +          expect(fake_logger).to have_received(:debug).with("Failed to acquire lock '#{key}' for owner '#{owner}': #{error.message}").at_least(:once) | 
|  | 96 | +        end | 
|  | 97 | +      end | 
|  | 98 | + | 
|  | 99 | +      context 'when it does acquire a lock' do | 
|  | 100 | +        it 'reports that it has a lock' do | 
|  | 101 | +          allow(locket_service).to receive(:lock). | 
|  | 102 | +            and_return(Models::LockResponse) | 
|  | 103 | + | 
|  | 104 | +          client.start(key, owner) | 
|  | 105 | +          sleep 0.1 | 
|  | 106 | + | 
|  | 107 | +          expect(client.lock_acquired?).to be(true) | 
|  | 108 | +          expect(fake_logger).to have_received(:debug).with("Acquired lock '#{key}' for owner '#{owner}'").at_least(:once) | 
|  | 109 | +        end | 
|  | 110 | +      end | 
|  | 111 | +    end | 
|  | 112 | +  end | 
|  | 113 | +end | 
0 commit comments