Skip to content

Commit

Permalink
zold-io#683 hungry wallets fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jan 30, 2019
1 parent a2bf546 commit fc83743
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/zold/hungry_wallets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions test/test_hungry_wallets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fc83743

Please sign in to comment.