diff --git a/lib/zold/hungry_wallets.rb b/lib/zold/hungry_wallets.rb index 982a2b0d..8bebd628 100644 --- a/lib/zold/hungry_wallets.rb +++ b/lib/zold/hungry_wallets.rb @@ -52,13 +52,15 @@ def initialize(wallets, remotes, copies, pool, def acq(id, exclusive: false) @wallets.acq(id, exclusive: exclusive) do |wallet| - if @queue.size > 256 - @log.error("Hungry queue is full with #{@queue.size} wallets, can't add #{id}") - else - @mutex.synchronize do - unless @queue.include?(id) - @queue << id - @log.debug("Hungry queue got #{id}, at the pos no.#{@queue.size - 1}") + unless wallet.exists? + if @queue.size > 256 + @log.error("Hungry queue is full with #{@queue.size} wallets, can't add #{id}") + else + @mutex.synchronize do + unless @queue.include?(id) + @queue << id + @log.debug("Hungry queue got #{id}, at the pos no.#{@queue.size - 1}") + end end end end diff --git a/test/test_hungry_wallets.rb b/test/test_hungry_wallets.rb index 878de6fe..aaf8f04a 100644 --- a/test/test_hungry_wallets.rb +++ b/test/test_hungry_wallets.rb @@ -50,4 +50,21 @@ def test_pulls_wallet assert_requested(get, times: 1) end end + + def test_doesnt_pull_wallet_if_exists + FakeHome.new(log: test_log).run do |home| + pool = Zold::ThreadPool.new('test', log: test_log) + remotes = home.remotes + remotes.add('localhost', 4096) + wallet = home.create_wallet + get = stub_request(:get, "http://localhost:4096/wallet/#{wallet.id}").to_return(status: 200) + wallets = Zold::HungryWallets.new( + home.wallets, remotes, File.join(home.dir, 'copies'), + pool, log: test_log + ) + wallets.acq(wallet.id) { |w| assert(w.exists?) } + pool.join(2) + assert_requested(get, times: 0) + end + end end